influxdb/influxql/token.go

259 lines
4.2 KiB
Go
Raw Normal View History

2014-11-15 19:04:30 +00:00
package influxql
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
WS
2014-11-15 19:04:30 +00:00
literal_beg
// Literals
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
EQ // =
NEQ // !=
LT // <
LTE // <=
GT // >
GTE // >=
2014-11-15 19:04:30 +00:00
operator_end
LPAREN // (
RPAREN // )
COMMA // ,
SEMICOLON // ;
COLON // :
//DOT // .
2014-11-15 19:04:30 +00:00
keyword_beg
// Keywords
ALL
ALTER
2014-11-15 19:04:30 +00:00
AS
ASC
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
DATABASE
2014-12-31 13:47:21 +00:00
DEFAULT
2014-11-15 19:04:30 +00:00
DELETE
DESC
DROP
DURATION
END
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
IF
2014-11-15 19:04:30 +00:00
INNER
INSERT
INTO
2014-12-10 05:07:29 +00:00
KEYS
2014-11-15 19:04:30 +00:00
LIMIT
LIST
2014-12-10 05:07:29 +00:00
MEASUREMENT
MEASUREMENTS
ON
2014-11-15 19:04:30 +00:00
ORDER
PASSWORD
POLICY
PRIVILEGES
2014-11-15 19:04:30 +00:00
QUERIES
QUERY
READ
REPLICATION
RETENTION
REVOKE
2014-11-15 19:04:30 +00:00
SELECT
SERIES
2014-12-10 05:07:29 +00:00
TAG
TO
USER
2014-12-10 05:07:29 +00:00
VALUES
2014-11-15 19:04:30 +00:00
WHERE
WITH
WRITE
2014-11-15 19:04:30 +00:00
keyword_end
)
var tokens = [...]string{
ILLEGAL: "ILLEGAL",
EOF: "EOF",
WS: "WS",
2014-11-15 19:04:30 +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",
EQ: "=",
NEQ: "!=",
LT: "<",
LTE: "<=",
GT: ">",
GTE: ">=",
2014-11-15 19:04:30 +00:00
LPAREN: "(",
RPAREN: ")",
COMMA: ",",
SEMICOLON: ";",
COLON: ":",
2014-11-15 19:04:30 +00:00
ALL: "ALL",
ALTER: "ALTER",
2014-12-10 05:07:29 +00:00
AS: "AS",
ASC: "ASC",
BEGIN: "BEGIN",
2014-12-10 05:07:29 +00:00
BY: "BY",
CREATE: "CREATE",
CONTINUOUS: "CONTINUOUS",
DATABASE: "DATABASE",
2014-12-31 16:22:07 +00:00
DEFAULT: "DEFAULT",
2014-12-10 05:07:29 +00:00
DELETE: "DELETE",
DESC: "DESC",
DROP: "DROP",
DURATION: "DURATION",
END: "END",
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",
IF: "IF",
2014-12-10 05:07:29 +00:00
INNER: "INNER",
INSERT: "INSERT",
INTO: "INTO",
KEYS: "KEYS",
LIMIT: "LIMIT",
LIST: "LIST",
MEASUREMENT: "MEASUREMENT",
MEASUREMENTS: "MEASUREMENTS",
ON: "ON",
2014-12-10 05:07:29 +00:00
ORDER: "ORDER",
PASSWORD: "PASSWORD",
POLICY: "POLICY",
PRIVILEGES: "PRIVILEGES",
2014-12-10 05:07:29 +00:00
QUERIES: "QUERIES",
QUERY: "QUERY",
READ: "READ",
REPLICATION: "REPLICATION",
RETENTION: "RETENTION",
REVOKE: "REVOKE",
2014-12-10 05:07:29 +00:00
SELECT: "SELECT",
SERIES: "SERIES",
TAG: "TAG",
TO: "TO",
USER: "USER",
2014-12-10 05:07:29 +00:00
VALUES: "VALUES",
WHERE: "WHERE",
WITH: "WITH",
WRITE: "WRITE",
2014-11-15 19:04:30 +00:00
}
var keywords map[string]Token
func init() {
keywords = make(map[string]Token)
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
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
}
// 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
}