-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtoken.go
196 lines (167 loc) · 3.29 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"github.com/DQNEO/minigo/stdlib/fmt"
"github.com/DQNEO/minigo/stdlib/strconv"
"os"
)
type identifier string
// https://golang.org/ref/spec#Keywords
var keywords = []string{
"break",
"default",
"func",
"interface",
"select",
"case",
"defer",
"go",
"map",
"struct",
"chan",
"else",
"goto",
"package",
"switch",
"const",
"fallthrough",
"if",
"range",
"type",
"continue",
"for",
"import",
"return",
"var",
}
type TokenType int
const (
T_EOF TokenType = iota
T_INT
T_STRING
T_CHAR
T_IDENT
T_PUNCT
T_KEYWORWD
)
func typeToGostring(typ TokenType) string {
switch typ {
case T_EOF:
return "EOF"
case T_INT:
return "int"
case T_STRING:
return "string"
case T_CHAR:
return "char"
case T_IDENT:
return "ident"
case T_PUNCT:
return "punct"
case T_KEYWORWD:
return "keyword"
}
return ""
}
type Token struct {
typ TokenType
sval string
filename string
line int
column int
}
type TokenStream struct {
tokens []*Token
index int
}
func NewTokenStream(bs *ByteStream) *TokenStream {
tokens := Tokenize(bs)
assert(len(tokens) > 0, nil, "tokens should have length")
return &TokenStream{
tokens: tokens,
index: 0,
}
}
func (ts *TokenStream) isEnd() bool {
return ts.index > len(ts.tokens)-1
}
func (tok *Token) getSval() string {
if len(tok.sval) > 0 {
return tok.sval
}
return ""
}
func (tok *Token) String() string {
sval := tok.getSval()
gs := Sprintf("(\"%s\" at %s:%d:%d)",
sval, tok.filename, tok.line, tok.column)
return gs
}
func (tok *Token) isEOF() bool {
return tok != nil && tok.typ == T_EOF
}
func (tok *Token) isPunct(s string) bool {
gs := s
return tok != nil && tok.typ == T_PUNCT && tok.sval == gs
}
func (tok *Token) isKeyword(s string) bool {
gs := s
return tok != nil && tok.typ == T_KEYWORWD && tok.sval == gs
}
func (tok *Token) isIdent(s string) bool {
gs := s
return tok != nil && tok.typ == T_IDENT && tok.sval == gs
}
func (tok *Token) getIdent() identifier {
if !tok.isTypeIdent() {
errorft(tok, "ident expeced, but got %v", tok)
}
return identifier(tok.sval)
}
func (tok *Token) getIntval() int {
val, _ := strconv.Atoi(string(tok.sval))
return val
}
func (tok *Token) isTypePunct() bool {
return tok != nil && tok.typ == T_PUNCT
}
func (tok *Token) isTypeKeyword() bool {
return tok != nil && tok.typ == T_KEYWORWD
}
func (tok *Token) isTypeInt() bool {
return tok != nil && tok.typ == T_INT
}
func (tok *Token) isTypeChar() bool {
return tok != nil && tok.typ == T_CHAR
}
func (tok *Token) isTypeString() bool {
return tok != nil && tok.typ == T_STRING
}
func (tok *Token) isTypeIdent() bool {
return tok != nil && tok.typ == T_IDENT
}
func (tok *Token) isSemicolon() bool {
return tok.isPunct(";")
}
func (tok *Token) dump() {
sval := tok.getSval()
s := Sprintf("tok: line=%d, type=%s, sval=\"%s\"\n",
tok.line, typeToGostring(tok.typ), sval)
os.Stderr.Write([]byte(s))
}
func Sprintf(format string, a ...interface{}) string {
var args []interface{}
for _, x := range a {
var y interface{}
switch x.(type) {
case identifier: // This case is not reached by 2nd gen compiler
var ident identifier = x.(identifier)
tmpbytes2 := []byte(ident)
y = tmpbytes2
default:
y = x
}
args = append(args, y)
}
a = nil // unset
return fmt.Sprintf(format, args...)
}