-
Notifications
You must be signed in to change notification settings - Fork 1
/
token.go
115 lines (96 loc) · 1.63 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
106
107
108
109
110
111
112
113
114
115
package metadata
import "strings"
type Token int
var tokens = [...]string{
ILLEGAL: "ILLEGAL",
EOF: "EOF",
WS: "WS",
ADD: "+",
SUB: "-",
MUL: "*",
DIV: "/",
EQ: "=",
LT: "<",
LTE: "<=",
GT: ">",
GTE: ">=",
PGT: "~>",
IDENT: "IDENT",
STRING: "STRING",
BADSTRING: "BADSTRING",
BADESCAPE: "BADESCAPE",
TRUE: "TRUE",
FALSE: "FALSE",
COMMENT: "#",
COMMA: ",",
VERSION: "version",
NAME: "name",
DEPENDS: "depends",
}
const (
// Special tokens
ILLEGAL Token = iota
EOF
WS
operator_beg
// operators
ADD // +
SUB // -
MUL // *
DIV // /
// Verion Specifiers
EQ // =
LT // <
LTE // <=
GT // >
GTE // >=
PGT // ~> the pessimistic gt
operator_end
literal_beg
// Literals
IDENT
STRING
BADSTRING
BADESCAPE
TRUE
FALSE
literal_end
// Misc Characters
COMMENT
COMMA // ,
keyword_beg
//Keywords
VERSION
NAME
DEPENDS
keyword_end
)
var keywords map[string]Token
func init() {
keywords = make(map[string]Token)
for tok := keyword_beg + 1; tok < keyword_end; tok++ {
keywords[strings.ToLower(tokens[tok])] = tok
}
keywords["true"] = TRUE
keywords["false"] = FALSE
}
// 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
}
// String returns the string representation of the token.
func (tok Token) String() string {
if tok >= 0 && tok < Token(len(tokens)) {
return tokens[tok]
}
return ""
}
// Lookup returns the token associated with a given string.
func Lookup(ident string) Token {
if tok, ok := keywords[strings.ToLower(ident)]; ok {
return tok
}
return IDENT
}