-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathtoken.go
105 lines (90 loc) · 1.79 KB
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package conditions
// Token represents a lexical token.
type Token int
const (
// ILLEGAL token represent illegal token found in the statement
ILLEGAL Token = iota
// EOF token represents end of statement
EOF
// Literals
literalBegin
IDENT // Variable references $0, $5, etc
NUMBER // 12345.67
STRING // "abc"
ARRAY // array of values (string or number) ["a","b","c"] [342,4325,6,4]
TRUE // true
FALSE // false
literalEnd
operatorBegin
AND // AND
OR // OR
EQ // =
NEQ // !=
LT // <
LTE // <=
GT // >
GTE // >=
NAND // NAND
XOR // XOR
EREG // =~
NEREG // !~
IN // IN
NOTIN // NOT IN
operatorEnd
LPAREN // (
RPAREN // )
)
var tokens = [...]string{
ILLEGAL: "ILLEGAL",
EOF: "EOF",
IDENT: "IDENT",
NUMBER: "NUMBER",
STRING: "STRING",
ARRAY: "ARRAY",
TRUE: "TRUE",
FALSE: "FALSE",
AND: "AND",
OR: "OR",
EQ: "==",
NEQ: "!=",
LT: "<",
LTE: "<=",
GT: ">",
GTE: ">=",
NAND: "NAND",
XOR: "XOR",
EREG: "=~",
NEREG: "!~",
IN: "IN",
NOTIN: "NOT IN",
LPAREN: "(",
RPAREN: ")",
}
// 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, XOR:
return 1
case AND, NAND:
return 2
case EQ, NEQ, LT, LTE, GT, GTE, IN, NOTIN, EREG, NEREG:
return 3
}
return 0
}
// isOperator returns true for operator tokens.
func (tok Token) isOperator() bool { return tok > operatorBegin && tok < operatorEnd }
// 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()
}