-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
178 lines (165 loc) · 3.38 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import "fmt"
type token struct {
typ tktype
lit string
loc *loc
}
func (t *token) String() string {
switch t.typ {
case tkIdent, tkStr, tkNum:
return fmt.Sprintf("{%s (%s %d)}", t.lit, t.typ, t.loc.line)
default:
return fmt.Sprintf("{%s (%d)}", t.typ, t.loc.line)
}
}
type tktype int
func (t tktype) String() string {
switch t {
case tkIdent:
return "ident"
case tkStr:
return "str"
case tkNum:
return "num"
case tkEof:
return "eof"
default:
for _, kw := range keywords {
if kw.t == t {
return kw.s
}
}
for _, punct := range punctuators {
if punct.t == t {
if t == tkNewLine {
return "newline"
}
return punct.s
}
}
}
return "?"
}
const (
// punctuators
tkDot tktype = iota // .
tkNewLine // \n
tkColon // :
tkColonEq // :=
tkEq // =
tkHash // #
tkComma // ,
tkLParen // (
tkRParen // )
tkLBracket // [
tkRBracket // ]
tkLBrace // {
tkRBrace // }
tk2VBar // ||
tk2Amp // &&
tk2Eq // ==
tkBangEq // !=
tkLess // <
tkLessEq // <=
tkGreater // >
tkGreaterEq // >=
tkPlus // +
tkHyphen // -
tkVBar // |
tkCaret // ^
tkStar // *
tkSlash // /
tkPercent // %
tk2Less // <<
tk2Greater // >>
tkAmp // &
tkBang // !
tkPlusEq // +=
tkHyphenEq // -=
tkStarEq // *=
tkSlashEq // /=
tkPercentEq // %=
tkAmpEq // &=
tkVBarEq // |=
tkCaretEq // ^=
// keywords
tkTrue // true
tkFalse // false
tkIf // if
tkElif // elif
tkElse // else
tkFor // for
tkIn // in
tkDef // def
tkContinue // continue
tkBreak // break
tkReturn // return
tkImport // import
tkStruct // struct
tkIdent
tkStr
tkNum
tkEof
)
type strToTktype struct {
s string
t tktype
}
var keywords = []*strToTktype{
{"true", tkTrue},
{"false", tkFalse},
{"if", tkIf},
{"elif", tkElif},
{"else", tkElse},
{"for", tkFor},
{"in", tkIn},
{"def", tkDef},
{"continue", tkContinue},
{"break", tkBreak},
{"return", tkReturn},
{"import", tkImport},
{"struct", tkStruct},
}
var punctuators = []*strToTktype{
{"&&", tk2Amp},
{"||", tk2VBar},
{"==", tk2Eq},
{"!=", tkBangEq},
{"<=", tkLessEq},
{">=", tkGreaterEq},
{"+=", tkPlusEq},
{"-=", tkHyphenEq},
{"*=", tkStarEq},
{"/=", tkSlashEq},
{"%=", tkPercentEq},
{"&=", tkAmpEq},
{"|=", tkVBarEq},
{"^=", tkCaretEq},
{":=", tkColonEq},
{"<<", tk2Less},
{">>", tk2Greater},
{"<", tkLess},
{">", tkGreater},
{".", tkDot},
{":", tkColon},
{"=", tkEq},
{"+", tkPlus},
{"-", tkHyphen},
{"*", tkStar},
{"/", tkSlash},
{"%", tkPercent},
{"#", tkHash},
{",", tkComma},
{"(", tkLParen},
{")", tkRParen},
{"[", tkLBracket},
{"]", tkRBracket},
{"{", tkLBrace},
{"}", tkRBrace},
{"&", tkAmp},
{"|", tkVBar},
{"^", tkCaret},
{"!", tkBang},
{"\n", tkNewLine},
}