-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.go
182 lines (164 loc) · 3.4 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
package main
type TokenType string
type Token struct {
Type TokenType
Literal string
}
const (
Illegal TokenType = "ILLEGAL"
EOF TokenType = "EOF"
// Structural Characters
BeginArray TokenType = "["
BeginObject TokenType = "{"
EndArray TokenType = "]"
EndObject TokenType = "}"
NameSeparator TokenType = ":"
ValueSeparator TokenType = ","
// Values
False TokenType = "false"
Null TokenType = "null"
True TokenType = "true"
String TokenType = "string"
Number TokenType = "number"
Object TokenType = "object"
Array TokenType = "array"
// Numbers
Minus TokenType = "-"
DecimalPoint TokenType = "."
)
type Lexer struct {
input string
position int
readPosition int
ch byte
}
func NewLexer(input string) *Lexer {
l := &Lexer{input: input}
l.readChar()
return l
}
func (l *Lexer) readChar() {
if l.readPosition >= len(l.input) {
l.ch = 0
} else {
l.ch = l.input[l.readPosition]
}
l.position = l.readPosition
l.readPosition += 1
}
func (l *Lexer) peekChar() byte {
if l.readPosition >= len(l.input) {
return 0
}
return l.input[l.readPosition]
}
func (l *Lexer) NextToken() Token {
var tok Token
l.skipWhitespace()
switch l.ch {
case '[':
tok = Token{BeginArray, string(l.ch)}
case ']':
tok = Token{EndArray, string(l.ch)}
case '{':
tok = Token{BeginObject, string(l.ch)}
case '}':
tok = Token{EndObject, string(l.ch)}
case ':':
tok = Token{NameSeparator, string(l.ch)}
case ',':
tok = Token{ValueSeparator, string(l.ch)}
case '"':
tok = l.readString()
case 0:
tok = Token{EOF, ""}
default:
if l.isDigit(l.ch) || l.ch == '-' {
tok = l.readNumber()
} else if l.isLiteralName(l.ch) {
tok = l.readLiteral()
} else {
tok = Token{Illegal, string(l.ch)}
}
}
l.readChar()
return tok
}
func (l *Lexer) readString() Token {
var tok Token
position := l.position + 1
for {
l.readChar()
if l.ch == '"' {
tok = Token{String, l.input[position:l.position]}
break
}
if l.ch == 0 {
if l.input[l.position-1] != '"' {
tok = Token{Illegal, l.input[position:l.position]}
}
break
}
}
return tok
}
func (l *Lexer) readNumber() Token {
start := l.position
for l.isDigit(l.peekChar()) || l.peekChar() == '.' || l.peekChar() == '-' {
l.readChar()
}
return Token{Number, l.input[start:l.readPosition]}
}
func (l *Lexer) isLiteralName(ch byte) bool {
return ch == 't' || ch == 'f' || ch == 'n'
}
func (l *Lexer) readLiteral() Token {
start := l.position
var tok Token
switch l.ch {
case 't':
for i, c := range True[1:] {
if c != rune(l.peekChar()) {
return Token{Illegal, l.input[start : start+i]}
}
l.readChar()
}
tok = Token{True, "true"}
case 'f':
for i, c := range False[1:] {
if c != rune(l.peekChar()) {
return Token{Illegal, l.input[start : start+i]}
}
l.readChar()
}
tok = Token{False, "false"}
case 'n':
for i, c := range Null[1:] {
if c != rune(l.peekChar()) {
return Token{Illegal, l.input[start : start+i]}
}
l.readChar()
}
tok = Token{Null, "null"}
default:
return Token{Illegal, string(l.ch)}
}
return tok
}
func (l *Lexer) isDigit(ch byte) bool {
return '0' <= ch && ch <= '9'
}
func (l *Lexer) skipWhitespace() {
for {
if l.ch == ' ' {
l.readChar()
continue
}
if l.ch == '\\' && (l.peekChar() == 't' || l.peekChar() == 'n' || l.peekChar() == 'r') {
l.readChar()
l.readChar()
continue
}
break
}
}