-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.go
252 lines (216 loc) · 5.33 KB
/
lexer.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright 2013 Benjamin Gentil. All rights reserved.
// license can be found in the LICENSE file (MIT License)
package zlang
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
const (
LeftBlockDelim = '{'
RightBlockDelim = '}'
LeftParentDelim = '('
RightParentDelim = ')'
ParamDelim = ','
StringDelim = '"'
EscapeCar = '\\'
Comment = "//"
LeftBlockComment = "/*"
RightBlockComment = "*/"
)
// For error reporting
type Position struct {
name string
line int
char int
}
type LexItem struct {
Token Token
Val string
}
func (i LexItem) String() string {
switch {
case i.Token == TOK_EOF:
return "EOF"
case i.Token == TOK_ERROR:
return i.Val
case len(i.Val) > 10:
return fmt.Sprintf("%.10q...", i.Val)
}
return fmt.Sprintf("%q", i.Val)
}
const eof = -1
// lexer holds the state of the scanner.
type Lexer struct {
name string // the position to be reported in case of error
input string // the string being scanned.
pos int // current position in the input.
start int // start position of this item.
width int // width of last rune read from input.
}
// Initialize the lexer
func NewLexer(name, input string) *Lexer {
return &Lexer{
name: name,
input: input,
pos: 0,
start: 0,
width: 1,
}
}
// get the line number for error reporting
func (l *Lexer) lineNumber() int {
return 1 + strings.Count(l.input[:l.pos], "\n")
}
// Report an error
func (l *Lexer) emit_error(format string, args ...interface{}) LexItem {
return LexItem{TOK_ERROR, fmt.Sprintf(format, args...)}
}
func (l *Lexer) emit(t Token) LexItem {
value := l.getVal()
l.start = l.pos
return LexItem{t, value}
}
func (l *Lexer) getVal() string {
return l.input[l.start:l.pos]
}
// get next rune of the input
func (l *Lexer) getRune() (r rune) {
if l.pos >= len(l.input) {
l.width = 0
return eof
}
r, l.width = utf8.DecodeRuneInString(l.input[l.pos:])
l.pos += l.width
return r
}
// seek next rune but doesn't consume it
func (l *Lexer) peekRune() rune {
r := l.getRune()
l.pos -= l.width
return r
}
// ignore the current string, used for space and tabs
func (l *Lexer) ignore() {
l.start = l.pos
}
// Parser will call NextItem until item's token is TOK_EOF or TOK_ERROR
func (l *Lexer) NextItem() LexItem {
lastRune := ' '
for isSpace(lastRune) {
l.ignore() // don't emit space token
lastRune = l.getRune()
}
// identifier starts with a letter
if isLetter(lastRune) || lastRune == POINTER_CHAR {
for isAlphaNumeric(l.peekRune()) {
lastRune = l.getRune()
}
return l.emit(resolveIdentifier(l.getVal()))
}
if isDigit(lastRune) {
isFloat := false
// parse hexa
if lastRune == '0' && l.peekRune() == 'x' {
l.getRune() // get the x
for isHexa(l.peekRune()) {
lastRune = l.getRune()
}
return l.emit(TOK_BYTE)
}
for isDigitOrDot(l.peekRune()) {
lastRune = l.getRune()
if lastRune == '.' {
if !isFloat {
isFloat = true
} else { // more than 1 dot
return l.emit_error("misformated float %s", l.getVal())
}
}
}
if !isFloat {
return l.emit(TOK_INT)
}
return l.emit(TOK_FLOAT)
}
if isEndOfLine(lastRune) {
return l.emit(TOK_ENDL)
}
// parse a string
if lastRune == StringDelim {
l.ignore() // skip opening "
for {
nextRune := l.peekRune()
if nextRune == StringDelim && lastRune != EscapeCar {
s := l.emit(TOK_STRING)
lastRune = l.getRune() // get the closing "
return s
}
if nextRune == eof {
return l.emit_error("unclosed string %s", l.getVal())
}
lastRune = l.getRune()
}
}
// parse a multiline comment
if lastRune == '/' && l.peekRune() == '*' {
lastRune = l.getRune() // position on *
for {
lastRune = l.getRune()
nextRune := l.peekRune()
if lastRune == '*' && nextRune == '/' {
lastRune = l.getRune() // get the closing /
return l.emit(TOK_COMMENT)
}
if nextRune == eof {
return l.emit_error("unclosed comment %s", l.getVal())
}
}
}
// parse a single line comment
if lastRune == '/' && l.peekRune() == '/' {
for !isEndOfLine(l.peekRune()) && lastRune != eof {
lastRune = l.getRune()
}
return l.emit(TOK_COMMENT)
}
if tok_op := resolveOperator(string(lastRune)); tok_op != TOK_ERROR {
return l.emit(tok_op)
}
if tok_del := resolveDelimiter(string(lastRune)); tok_del != TOK_ERROR {
return l.emit(tok_del)
}
if lastRune == eof {
return l.emit(TOK_EOF)
}
return l.emit_error("unknown token with value '%s' last rune = 0x%x", l.getVal(), lastRune)
}
// isSpace reports whether r is a space character.
func isSpace(r rune) bool {
return r == ' ' || r == '\t'
}
// isEndOfLine reports whether r is an end-of-line character.
func isEndOfLine(r rune) bool {
return r == '\r' || r == '\n'
}
// isAlphaNumeric reports whether r is an alphabetic, digit, or underscore.
func isAlphaNumeric(r rune) bool {
return r == '_' || unicode.IsLetter(r) || unicode.IsDigit(r)
}
// isAlphaNumeric reports whether r is an alphabetic, digit, or underscore.
func isLetter(r rune) bool {
return unicode.IsLetter(r)
}
// isDigit reports whether r is a digit
func isDigit(r rune) bool {
return unicode.IsDigit(r)
}
// isDigit reports whether r is a digit or a dot (float)
func isDigitOrDot(r rune) bool {
return unicode.IsDigit(r) || r == '.'
}
// isHexa reports whether r is hexa
func isHexa(r rune) bool {
return strings.ContainsRune("0123456789ABCDEFabcdef", r)
}