influxdb/influxql/token.go

189 lines
3.1 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 // 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 // ,
keyword_beg
// Keywords
AS
ASC
BY
CONTINUOUS
DELETE
DESC
DROP
EXPLAIN
FROM
INNER
JOIN
LIMIT
LIST
MERGE
ORDER
QUERIES
SELECT
SERIES
WHERE
keyword_end
)
var tokens = [...]string{
ILLEGAL: "ILLEGAL",
EOF: "EOF",
WS: "WS",
2014-11-15 19:04:30 +00:00
IDENT: "IDENT",
NUMBER: "NUMBER",
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: ",",
AS: "AS",
ASC: "ASC",
BY: "BY",
CONTINUOUS: "CONTINUOUS",
DELETE: "DELETE",
DESC: "DESC",
DROP: "DROP",
EXPLAIN: "EXPLAIN",
FROM: "FROM",
INNER: "INNER",
JOIN: "JOIN",
LIMIT: "LIMIT",
LIST: "LIST",
MERGE: "MERGE",
ORDER: "ORDER",
QUERIES: "QUERIES",
SELECT: "SELECT",
SERIES: "SERIES",
WHERE: "WHERE",
}
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
}
// IsLiteral returns true for literal tokens.
func (tok Token) IsLiteral() bool { return tok > literal_beg && tok < literal_end }
// IsOperator returns true for operator tokens.
func (tok Token) IsOperator() bool { return tok > operator_beg && tok < operator_end }
// IsKeyword returns true for keyword tokens.
func (tok Token) IsKeyword() bool { return tok > keyword_beg && tok < keyword_end }
// 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
}