-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathlexer.go
220 lines (197 loc) · 4.85 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
package main
// Lexer is a lexer that tokenizes the input.
type Lexer struct {
ch byte
input string
pos int
nextPos int
line int
column int
}
// NewLexer returns a new lexer for tokenizing the input string.
func NewLexer(input string) *Lexer {
l := &Lexer{input: input, line: 1, column: 0}
l.readChar()
return l
}
// readChar advances the lexer to the next character.
func (l *Lexer) readChar() {
l.column++
l.ch = l.peekChar()
l.pos = l.nextPos
l.nextPos++
}
// NextToken returns the next token in the input.
func (l *Lexer) NextToken() Token {
l.skipWhitespace()
var tok = Token{Line: l.line, Column: l.column}
switch l.ch {
case 0:
tok = l.newToken(EOF, l.ch)
case '@':
tok = l.newToken(AT, l.ch)
l.readChar()
case '=':
tok = l.newToken(EQUAL, l.ch)
l.readChar()
case '%':
tok = l.newToken(PERCENT, l.ch)
l.readChar()
case '#':
tok.Type = COMMENT
tok.Literal = l.readComment()
case '+':
tok = l.newToken(PLUS, l.ch)
l.readChar()
case '{':
tok.Type = JSON
tok.Literal = "{" + l.readJSON() + "}"
l.readChar()
case '\'':
tok.Type = STRING
tok.Literal = l.readString('\'')
l.readChar()
case '"':
tok.Type = STRING
tok.Literal = l.readString('"')
l.readChar()
default:
if isDigit(l.ch) || (isDot(l.ch) && isDigit(l.peekChar())) {
tok.Literal = l.readNumber()
tok.Type = NUMBER
} else if isLetter(l.ch) || isDot(l.ch) {
tok.Literal = l.readIdentifier()
tok.Type = LookupIdentifier(tok.Literal)
} else {
tok = l.newToken(ILLEGAL, l.ch)
l.readChar()
}
}
return tok
}
// newToken creates a new token with the given type and literal.
func (l *Lexer) newToken(tokenType TokenType, ch byte) Token {
literal := string(ch)
return Token{
Type: tokenType,
Literal: literal,
Line: l.line,
Column: l.column,
}
}
// readComment reads a comment.
// // Foo => Token(Foo).
func (l *Lexer) readComment() string {
pos := l.pos + 1
for {
l.readChar()
if isNewLine(l.ch) || l.ch == 0 {
break
}
}
// The current character is a newline.
// skipWhitespace() will handle this for us and increment the line counter.
return l.input[pos:l.pos]
}
// readString reads a string from the input.
// "Foo" => Token(Foo).
func (l *Lexer) readString(endChar byte) string {
pos := l.pos + 1
for {
l.readChar()
if l.ch == endChar || l.ch == 0 || isNewLine(l.ch) {
break
}
}
return l.input[pos:l.pos]
}
// readJSON reads a JSON object from the input.
// {"foo": "bar"} => Token({"foo": "bar"}).
func (l *Lexer) readJSON() string {
pos := l.pos + 1
for {
l.readChar()
if l.ch == '}' || l.ch == 0 {
break
}
}
return l.input[pos:l.pos]
}
// readNumber reads a number from the input.
// 123 => Token(123).
func (l *Lexer) readNumber() string {
pos := l.pos
for isDigit(l.ch) || isDot(l.ch) {
l.readChar()
}
return l.input[pos:l.pos]
}
// readIdentifier reads an identifier from the input.
// Foo => Token(Foo).
func (l *Lexer) readIdentifier() string {
pos := l.pos
for isLetter(l.ch) || isDot(l.ch) || isDash(l.ch) || isUnderscore(l.ch) || isSlash(l.ch) || isPercent(l.ch) || isDigit(l.ch) {
l.readChar()
}
return l.input[pos:l.pos]
}
// skipWhitespace skips whitespace characters.
// If it encounters a newline, it increments the line counter to keep track
// of the token's line number.
func (l *Lexer) skipWhitespace() {
for isWhitespace(l.ch) {
// Note: we don't use isNewline since we don't want to double count \r\n on
// windows and increment the l.line.
if l.ch == '\n' {
l.line++
l.column = 0
}
l.readChar()
}
}
// isDot returns whether a character is a dot.
func isDot(ch byte) bool {
return ch == '.'
}
// isDash returns whether a character is a dash.
func isDash(ch byte) bool {
return ch == '-'
}
// isUnderscore returns whether a character is an underscore.
func isUnderscore(ch byte) bool {
return ch == '_'
}
// isPercent returns whether a character is a percent.
func isPercent(ch byte) bool {
return ch == '%'
}
// isSlash returns whether a character is a slash.
func isSlash(ch byte) bool {
return ch == '/'
}
// isLetter returns whether a character is a letter.
func isLetter(ch byte) bool {
return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z'
}
// isDigit returns whether a character is a digit.
func isDigit(ch byte) bool {
return '0' <= ch && ch <= '9'
}
// isWhitespace returns whether a character is a whitespace.
func isWhitespace(ch byte) bool {
return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'
}
// isNewLine returns whether a character is a newline.
//
// Note: in windows a single new line is \r\n so using isNewline is not
// recommended for counting the number of new lines.
func isNewLine(ch byte) bool {
return ch == '\n' || ch == '\r'
}
// peekChar returns the next character in the input without advancing the lexer.
func (l *Lexer) peekChar() byte {
if l.nextPos >= len(l.input) {
return 0
}
return l.input[l.nextPos]
}