2014-11-15 19:04:30 +00:00
|
|
|
package influxql
|
|
|
|
|
2014-11-19 23:47:46 +00:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-11-15 19:04:30 +00:00
|
|
|
// Token is a lexical token of the InfluxQL language.
|
|
|
|
type Token int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Special tokens
|
|
|
|
ILLEGAL Token = iota
|
|
|
|
EOF
|
2014-11-19 23:47:46 +00:00
|
|
|
WS
|
2014-11-15 19:04:30 +00:00
|
|
|
|
|
|
|
literal_beg
|
|
|
|
// Literals
|
2015-01-05 02:50:56 +00:00
|
|
|
IDENT // main
|
|
|
|
NUMBER // 12345.67
|
|
|
|
DURATION_VAL // 13h
|
|
|
|
STRING // "abc"
|
|
|
|
BADSTRING // "abc
|
|
|
|
BADESCAPE // \q
|
|
|
|
TRUE // true
|
|
|
|
FALSE // false
|
2014-11-15 19:04:30 +00:00
|
|
|
literal_end
|
|
|
|
|
|
|
|
operator_beg
|
|
|
|
// Operators
|
|
|
|
ADD // +
|
|
|
|
SUB // -
|
|
|
|
MUL // *
|
|
|
|
DIV // /
|
|
|
|
|
|
|
|
AND // AND
|
|
|
|
OR // OR
|
|
|
|
|
2014-12-06 18:17:58 +00:00
|
|
|
EQ // =
|
2014-11-19 23:47:46 +00:00
|
|
|
NEQ // !=
|
|
|
|
LT // <
|
|
|
|
LTE // <=
|
|
|
|
GT // >
|
|
|
|
GTE // >=
|
2014-11-15 19:04:30 +00:00
|
|
|
operator_end
|
|
|
|
|
2014-11-20 15:02:47 +00:00
|
|
|
LPAREN // (
|
|
|
|
RPAREN // )
|
|
|
|
COMMA // ,
|
|
|
|
SEMICOLON // ;
|
2015-01-06 22:34:01 +00:00
|
|
|
DOT // .
|
2014-11-15 19:04:30 +00:00
|
|
|
|
|
|
|
keyword_beg
|
|
|
|
// Keywords
|
2014-12-29 18:14:33 +00:00
|
|
|
ALL
|
2015-01-05 18:24:50 +00:00
|
|
|
ALTER
|
2014-11-15 19:04:30 +00:00
|
|
|
AS
|
|
|
|
ASC
|
2015-01-06 21:30:27 +00:00
|
|
|
BEGIN
|
2014-11-15 19:04:30 +00:00
|
|
|
BY
|
2014-11-25 04:49:09 +00:00
|
|
|
CREATE
|
2014-11-15 19:04:30 +00:00
|
|
|
CONTINUOUS
|
2014-12-29 18:14:33 +00:00
|
|
|
DATABASE
|
2015-01-09 15:47:57 +00:00
|
|
|
DATABASES
|
2014-12-31 13:47:21 +00:00
|
|
|
DEFAULT
|
2014-11-15 19:04:30 +00:00
|
|
|
DELETE
|
|
|
|
DESC
|
|
|
|
DROP
|
2015-01-05 02:50:56 +00:00
|
|
|
DURATION
|
2015-01-06 21:30:27 +00:00
|
|
|
END
|
2014-12-29 18:14:33 +00:00
|
|
|
EXISTS
|
2014-11-15 19:04:30 +00:00
|
|
|
EXPLAIN
|
2014-12-10 05:07:29 +00:00
|
|
|
FIELD
|
2014-11-15 19:04:30 +00:00
|
|
|
FROM
|
2015-01-03 03:56:26 +00:00
|
|
|
GRANT
|
2014-11-22 04:12:48 +00:00
|
|
|
GROUP
|
2014-12-29 18:14:33 +00:00
|
|
|
IF
|
2014-11-15 19:04:30 +00:00
|
|
|
INNER
|
2014-11-20 15:02:47 +00:00
|
|
|
INSERT
|
|
|
|
INTO
|
2015-01-28 10:02:36 +00:00
|
|
|
KEY
|
2014-12-10 05:07:29 +00:00
|
|
|
KEYS
|
2014-11-15 19:04:30 +00:00
|
|
|
LIMIT
|
2015-01-26 03:40:50 +00:00
|
|
|
SHOW
|
2014-12-10 05:07:29 +00:00
|
|
|
MEASUREMENT
|
|
|
|
MEASUREMENTS
|
2015-01-25 20:34:49 +00:00
|
|
|
OFFSET
|
2014-12-29 18:14:33 +00:00
|
|
|
ON
|
2014-11-15 19:04:30 +00:00
|
|
|
ORDER
|
2014-12-29 18:14:33 +00:00
|
|
|
PASSWORD
|
|
|
|
POLICY
|
2015-01-13 20:21:06 +00:00
|
|
|
POLICIES
|
2014-12-29 18:14:33 +00:00
|
|
|
PRIVILEGES
|
2014-11-15 19:04:30 +00:00
|
|
|
QUERIES
|
2014-11-22 23:33:21 +00:00
|
|
|
QUERY
|
2014-12-29 18:14:33 +00:00
|
|
|
READ
|
2015-01-05 02:50:56 +00:00
|
|
|
REPLICATION
|
2014-12-29 18:14:33 +00:00
|
|
|
RETENTION
|
|
|
|
REVOKE
|
2014-11-15 19:04:30 +00:00
|
|
|
SELECT
|
|
|
|
SERIES
|
2014-12-10 05:07:29 +00:00
|
|
|
TAG
|
2014-12-29 18:14:33 +00:00
|
|
|
TO
|
|
|
|
USER
|
2015-01-14 16:53:17 +00:00
|
|
|
USERS
|
2014-12-10 05:07:29 +00:00
|
|
|
VALUES
|
2014-11-15 19:04:30 +00:00
|
|
|
WHERE
|
2014-12-29 18:14:33 +00:00
|
|
|
WITH
|
|
|
|
WRITE
|
2014-11-15 19:04:30 +00:00
|
|
|
keyword_end
|
|
|
|
)
|
|
|
|
|
|
|
|
var tokens = [...]string{
|
|
|
|
ILLEGAL: "ILLEGAL",
|
2014-11-19 23:47:46 +00:00
|
|
|
EOF: "EOF",
|
|
|
|
WS: "WS",
|
2014-11-15 19:04:30 +00:00
|
|
|
|
2015-01-05 02:50:56 +00:00
|
|
|
IDENT: "IDENT",
|
|
|
|
NUMBER: "NUMBER",
|
|
|
|
DURATION_VAL: "DURATION_VAL",
|
|
|
|
STRING: "STRING",
|
|
|
|
TRUE: "TRUE",
|
|
|
|
FALSE: "FALSE",
|
2014-11-15 19:04:30 +00:00
|
|
|
|
|
|
|
ADD: "+",
|
|
|
|
SUB: "-",
|
|
|
|
MUL: "*",
|
|
|
|
DIV: "/",
|
|
|
|
|
|
|
|
AND: "AND",
|
|
|
|
OR: "OR",
|
|
|
|
|
2014-12-06 18:17:58 +00:00
|
|
|
EQ: "=",
|
2014-11-19 23:47:46 +00:00
|
|
|
NEQ: "!=",
|
|
|
|
LT: "<",
|
|
|
|
LTE: "<=",
|
|
|
|
GT: ">",
|
|
|
|
GTE: ">=",
|
2014-11-15 19:04:30 +00:00
|
|
|
|
2014-11-20 15:02:47 +00:00
|
|
|
LPAREN: "(",
|
|
|
|
RPAREN: ")",
|
|
|
|
COMMA: ",",
|
|
|
|
SEMICOLON: ";",
|
2015-01-06 22:34:01 +00:00
|
|
|
DOT: ".",
|
2014-11-15 19:04:30 +00:00
|
|
|
|
2014-12-29 18:14:33 +00:00
|
|
|
ALL: "ALL",
|
2015-01-05 18:24:50 +00:00
|
|
|
ALTER: "ALTER",
|
2014-12-10 05:07:29 +00:00
|
|
|
AS: "AS",
|
|
|
|
ASC: "ASC",
|
2015-01-06 21:30:27 +00:00
|
|
|
BEGIN: "BEGIN",
|
2014-12-10 05:07:29 +00:00
|
|
|
BY: "BY",
|
|
|
|
CREATE: "CREATE",
|
|
|
|
CONTINUOUS: "CONTINUOUS",
|
2014-12-29 18:14:33 +00:00
|
|
|
DATABASE: "DATABASE",
|
2015-01-09 15:47:57 +00:00
|
|
|
DATABASES: "DATABASES",
|
2014-12-31 16:22:07 +00:00
|
|
|
DEFAULT: "DEFAULT",
|
2014-12-10 05:07:29 +00:00
|
|
|
DELETE: "DELETE",
|
|
|
|
DESC: "DESC",
|
|
|
|
DROP: "DROP",
|
2015-01-05 02:50:56 +00:00
|
|
|
DURATION: "DURATION",
|
2015-01-06 21:30:27 +00:00
|
|
|
END: "END",
|
2014-12-29 18:14:33 +00:00
|
|
|
EXISTS: "EXISTS",
|
2014-12-10 05:07:29 +00:00
|
|
|
EXPLAIN: "EXPLAIN",
|
|
|
|
FIELD: "FIELD",
|
|
|
|
FROM: "FROM",
|
2015-01-03 03:56:26 +00:00
|
|
|
GRANT: "GRANT",
|
2014-12-10 05:07:29 +00:00
|
|
|
GROUP: "GROUP",
|
2014-12-29 18:14:33 +00:00
|
|
|
IF: "IF",
|
2014-12-10 05:07:29 +00:00
|
|
|
INNER: "INNER",
|
|
|
|
INSERT: "INSERT",
|
|
|
|
INTO: "INTO",
|
2015-01-28 10:02:36 +00:00
|
|
|
KEY: "KEY",
|
2014-12-10 05:07:29 +00:00
|
|
|
KEYS: "KEYS",
|
|
|
|
LIMIT: "LIMIT",
|
2015-01-26 03:40:50 +00:00
|
|
|
SHOW: "SHOW",
|
2014-12-10 05:07:29 +00:00
|
|
|
MEASUREMENT: "MEASUREMENT",
|
|
|
|
MEASUREMENTS: "MEASUREMENTS",
|
2015-01-25 20:34:49 +00:00
|
|
|
OFFSET: "OFFSET",
|
2014-12-29 18:14:33 +00:00
|
|
|
ON: "ON",
|
2014-12-10 05:07:29 +00:00
|
|
|
ORDER: "ORDER",
|
2014-12-29 18:14:33 +00:00
|
|
|
PASSWORD: "PASSWORD",
|
|
|
|
POLICY: "POLICY",
|
2015-01-13 20:21:06 +00:00
|
|
|
POLICIES: "POLICIES",
|
2014-12-29 18:14:33 +00:00
|
|
|
PRIVILEGES: "PRIVILEGES",
|
2014-12-10 05:07:29 +00:00
|
|
|
QUERIES: "QUERIES",
|
|
|
|
QUERY: "QUERY",
|
2014-12-29 18:14:33 +00:00
|
|
|
READ: "READ",
|
2015-01-05 02:50:56 +00:00
|
|
|
REPLICATION: "REPLICATION",
|
2014-12-29 18:14:33 +00:00
|
|
|
RETENTION: "RETENTION",
|
|
|
|
REVOKE: "REVOKE",
|
2014-12-10 05:07:29 +00:00
|
|
|
SELECT: "SELECT",
|
|
|
|
SERIES: "SERIES",
|
|
|
|
TAG: "TAG",
|
2014-12-29 18:14:33 +00:00
|
|
|
TO: "TO",
|
|
|
|
USER: "USER",
|
2015-01-14 16:53:17 +00:00
|
|
|
USERS: "USERS",
|
2014-12-10 05:07:29 +00:00
|
|
|
VALUES: "VALUES",
|
|
|
|
WHERE: "WHERE",
|
2014-12-29 18:14:33 +00:00
|
|
|
WITH: "WITH",
|
|
|
|
WRITE: "WRITE",
|
2014-11-15 19:04:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var keywords map[string]Token
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
keywords = make(map[string]Token)
|
2014-11-19 23:47:46 +00:00
|
|
|
for tok := keyword_beg + 1; tok < keyword_end; tok++ {
|
|
|
|
keywords[strings.ToUpper(tokens[tok])] = tok
|
|
|
|
keywords[strings.ToLower(tokens[tok])] = tok
|
|
|
|
}
|
|
|
|
for _, tok := range []Token{AND, OR} {
|
|
|
|
keywords[strings.ToUpper(tokens[tok])] = tok
|
|
|
|
keywords[strings.ToLower(tokens[tok])] = tok
|
2014-11-15 19:04:30 +00:00
|
|
|
}
|
|
|
|
keywords["true"] = TRUE
|
|
|
|
keywords["false"] = FALSE
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the string representation of the token.
|
|
|
|
func (tok Token) String() string {
|
|
|
|
if tok >= 0 && tok < Token(len(tokens)) {
|
|
|
|
return tokens[tok]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Precedence returns the operator precedence of the binary operator token.
|
|
|
|
func (tok Token) Precedence() int {
|
|
|
|
switch tok {
|
|
|
|
case OR:
|
|
|
|
return 1
|
|
|
|
case AND:
|
|
|
|
return 2
|
2014-11-19 23:47:46 +00:00
|
|
|
case EQ, NEQ, LT, LTE, GT, GTE:
|
2014-11-15 19:04:30 +00:00
|
|
|
return 3
|
|
|
|
case ADD, SUB:
|
|
|
|
return 4
|
|
|
|
case MUL, DIV:
|
|
|
|
return 5
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2014-11-22 23:33:21 +00:00
|
|
|
// isOperator returns true for operator tokens.
|
|
|
|
func (tok Token) isOperator() bool { return tok > operator_beg && tok < operator_end }
|
2014-11-22 04:12:48 +00:00
|
|
|
|
|
|
|
// tokstr returns a literal if provided, otherwise returns the token string.
|
|
|
|
func tokstr(tok Token, lit string) string {
|
|
|
|
if lit != "" {
|
|
|
|
return lit
|
|
|
|
}
|
|
|
|
return tok.String()
|
|
|
|
}
|
|
|
|
|
2014-11-15 19:04:30 +00:00
|
|
|
// Lookup returns the token associated with a given string.
|
|
|
|
func Lookup(ident string) Token {
|
|
|
|
if tok, ok := keywords[ident]; ok {
|
|
|
|
return tok
|
|
|
|
}
|
|
|
|
return IDENT
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pos specifies the line and character position of a token.
|
|
|
|
// The Char and Line are both zero-based indexes.
|
|
|
|
type Pos struct {
|
|
|
|
Line int
|
|
|
|
Char int
|
|
|
|
}
|