github.com/influxdata/influxql@v1.1.0/parse_tree.go (about) 1 package influxql 2 3 import ( 4 "fmt" 5 ) 6 7 var Language = &ParseTree{} 8 9 type ParseTree struct { 10 Handlers map[Token]func(*Parser) (Statement, error) 11 Tokens map[Token]*ParseTree 12 Keys []string 13 } 14 15 // With passes the current parse tree to a function to allow nested functions. 16 func (t *ParseTree) With(fn func(*ParseTree)) { 17 fn(t) 18 } 19 20 // Group groups together a set of related handlers with a common token prefix. 21 func (t *ParseTree) Group(tokens ...Token) *ParseTree { 22 for _, tok := range tokens { 23 // Look for the parse tree for this token. 24 if subtree := t.Tokens[tok]; subtree != nil { 25 t = subtree 26 continue 27 } 28 29 // No subtree exists yet. Verify that we don't have a conflicting 30 // statement. 31 if _, conflict := t.Handlers[tok]; conflict { 32 panic(fmt.Sprintf("conflict for token %s", tok)) 33 } 34 35 // Create the new parse tree and register it inside of this one for 36 // later reference. 37 newT := &ParseTree{} 38 if t.Tokens == nil { 39 t.Tokens = make(map[Token]*ParseTree) 40 } 41 t.Tokens[tok] = newT 42 t.Keys = append(t.Keys, tok.String()) 43 t = newT 44 } 45 return t 46 } 47 48 // Handle registers a handler to be invoked when seeing the given token. 49 func (t *ParseTree) Handle(tok Token, fn func(*Parser) (Statement, error)) { 50 // Verify that there is no conflict for this token in this parse tree. 51 if _, conflict := t.Tokens[tok]; conflict { 52 panic(fmt.Sprintf("conflict for token %s", tok)) 53 } 54 55 if _, conflict := t.Handlers[tok]; conflict { 56 panic(fmt.Sprintf("conflict for token %s", tok)) 57 } 58 59 if t.Handlers == nil { 60 t.Handlers = make(map[Token]func(*Parser) (Statement, error)) 61 } 62 t.Handlers[tok] = fn 63 t.Keys = append(t.Keys, tok.String()) 64 } 65 66 // Parse parses a statement using the language defined in the parse tree. 67 func (t *ParseTree) Parse(p *Parser) (Statement, error) { 68 for { 69 tok, pos, lit := p.ScanIgnoreWhitespace() 70 if subtree := t.Tokens[tok]; subtree != nil { 71 t = subtree 72 continue 73 } 74 75 if stmt := t.Handlers[tok]; stmt != nil { 76 return stmt(p) 77 } 78 79 // There were no registered handlers. Return the valid tokens in the order they were added. 80 return nil, newParseError(tokstr(tok, lit), t.Keys, pos) 81 } 82 } 83 84 func (t *ParseTree) Clone() *ParseTree { 85 newT := &ParseTree{} 86 if t.Handlers != nil { 87 newT.Handlers = make(map[Token]func(*Parser) (Statement, error), len(t.Handlers)) 88 for tok, handler := range t.Handlers { 89 newT.Handlers[tok] = handler 90 } 91 } 92 93 if t.Tokens != nil { 94 newT.Tokens = make(map[Token]*ParseTree, len(t.Tokens)) 95 for tok, subtree := range t.Tokens { 96 newT.Tokens[tok] = subtree.Clone() 97 } 98 } 99 return newT 100 } 101 102 func init() { 103 Language.Handle(SELECT, func(p *Parser) (Statement, error) { 104 return p.parseSelectStatement(targetNotRequired) 105 }) 106 Language.Handle(DELETE, func(p *Parser) (Statement, error) { 107 return p.parseDeleteStatement() 108 }) 109 Language.Group(SHOW).With(func(show *ParseTree) { 110 show.Group(CONTINUOUS).Handle(QUERIES, func(p *Parser) (Statement, error) { 111 return p.parseShowContinuousQueriesStatement() 112 }) 113 show.Handle(DATABASES, func(p *Parser) (Statement, error) { 114 return p.parseShowDatabasesStatement() 115 }) 116 show.Handle(DIAGNOSTICS, func(p *Parser) (Statement, error) { 117 return p.parseShowDiagnosticsStatement() 118 }) 119 show.Group(FIELD).With(func(field *ParseTree) { 120 field.Handle(KEY, func(p *Parser) (Statement, error) { 121 return p.parseShowFieldKeyCardinalityStatement() 122 }) 123 field.Handle(KEYS, func(p *Parser) (Statement, error) { 124 return p.parseShowFieldKeysStatement() 125 }) 126 }) 127 show.Group(GRANTS).Handle(FOR, func(p *Parser) (Statement, error) { 128 return p.parseGrantsForUserStatement() 129 }) 130 show.Group(MEASUREMENT).Handle(EXACT, func(p *Parser) (Statement, error) { 131 return p.parseShowMeasurementCardinalityStatement(true) 132 }) 133 show.Group(MEASUREMENT).Handle(CARDINALITY, func(p *Parser) (Statement, error) { 134 return p.parseShowMeasurementCardinalityStatement(false) 135 }) 136 show.Handle(MEASUREMENTS, func(p *Parser) (Statement, error) { 137 return p.parseShowMeasurementsStatement() 138 }) 139 show.Handle(QUERIES, func(p *Parser) (Statement, error) { 140 return p.parseShowQueriesStatement() 141 }) 142 show.Group(RETENTION).Handle(POLICIES, func(p *Parser) (Statement, error) { 143 return p.parseShowRetentionPoliciesStatement() 144 }) 145 show.Handle(SERIES, func(p *Parser) (Statement, error) { 146 return p.parseShowSeriesStatement() 147 }) 148 show.Group(SHARD).Handle(GROUPS, func(p *Parser) (Statement, error) { 149 return p.parseShowShardGroupsStatement() 150 }) 151 show.Handle(SHARDS, func(p *Parser) (Statement, error) { 152 return p.parseShowShardsStatement() 153 }) 154 show.Handle(STATS, func(p *Parser) (Statement, error) { 155 return p.parseShowStatsStatement() 156 }) 157 show.Handle(SUBSCRIPTIONS, func(p *Parser) (Statement, error) { 158 return p.parseShowSubscriptionsStatement() 159 }) 160 show.Group(TAG).With(func(tag *ParseTree) { 161 tag.Handle(KEY, func(p *Parser) (Statement, error) { 162 return p.parseShowTagKeyCardinalityStatement() 163 }) 164 tag.Handle(KEYS, func(p *Parser) (Statement, error) { 165 return p.parseShowTagKeysStatement() 166 }) 167 tag.Handle(VALUES, func(p *Parser) (Statement, error) { 168 return p.parseShowTagValuesStatement() 169 }) 170 }) 171 show.Handle(USERS, func(p *Parser) (Statement, error) { 172 return p.parseShowUsersStatement() 173 }) 174 }) 175 Language.Group(CREATE).With(func(create *ParseTree) { 176 create.Group(CONTINUOUS).Handle(QUERY, func(p *Parser) (Statement, error) { 177 return p.parseCreateContinuousQueryStatement() 178 }) 179 create.Handle(DATABASE, func(p *Parser) (Statement, error) { 180 return p.parseCreateDatabaseStatement() 181 }) 182 create.Handle(USER, func(p *Parser) (Statement, error) { 183 return p.parseCreateUserStatement() 184 }) 185 create.Group(RETENTION).Handle(POLICY, func(p *Parser) (Statement, error) { 186 return p.parseCreateRetentionPolicyStatement() 187 }) 188 create.Handle(SUBSCRIPTION, func(p *Parser) (Statement, error) { 189 return p.parseCreateSubscriptionStatement() 190 }) 191 }) 192 Language.Group(DROP).With(func(drop *ParseTree) { 193 drop.Group(CONTINUOUS).Handle(QUERY, func(p *Parser) (Statement, error) { 194 return p.parseDropContinuousQueryStatement() 195 }) 196 drop.Handle(DATABASE, func(p *Parser) (Statement, error) { 197 return p.parseDropDatabaseStatement() 198 }) 199 drop.Handle(MEASUREMENT, func(p *Parser) (Statement, error) { 200 return p.parseDropMeasurementStatement() 201 }) 202 drop.Group(RETENTION).Handle(POLICY, func(p *Parser) (Statement, error) { 203 return p.parseDropRetentionPolicyStatement() 204 }) 205 drop.Handle(SERIES, func(p *Parser) (Statement, error) { 206 return p.parseDropSeriesStatement() 207 }) 208 drop.Handle(SHARD, func(p *Parser) (Statement, error) { 209 return p.parseDropShardStatement() 210 }) 211 drop.Handle(SUBSCRIPTION, func(p *Parser) (Statement, error) { 212 return p.parseDropSubscriptionStatement() 213 }) 214 drop.Handle(USER, func(p *Parser) (Statement, error) { 215 return p.parseDropUserStatement() 216 }) 217 }) 218 Language.Handle(EXPLAIN, func(p *Parser) (Statement, error) { 219 return p.parseExplainStatement() 220 }) 221 Language.Handle(GRANT, func(p *Parser) (Statement, error) { 222 return p.parseGrantStatement() 223 }) 224 Language.Handle(REVOKE, func(p *Parser) (Statement, error) { 225 return p.parseRevokeStatement() 226 }) 227 Language.Group(ALTER, RETENTION).Handle(POLICY, func(p *Parser) (Statement, error) { 228 return p.parseAlterRetentionPolicyStatement() 229 }) 230 Language.Group(SET, PASSWORD).Handle(FOR, func(p *Parser) (Statement, error) { 231 return p.parseSetPasswordUserStatement() 232 }) 233 Language.Group(KILL).Handle(QUERY, func(p *Parser) (Statement, error) { 234 return p.parseKillQueryStatement() 235 }) 236 }