-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.go
159 lines (122 loc) · 2.63 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
package main
import (
"regexp"
)
type TokenType byte
const (
TokenTypeNumber TokenType = iota
TokenTypeBoolean
TokenTypeString
TokenTypeOpenSquareBracket
TokenTypeCloseSquareBracket
TokenTypeOpenCurlyBrace
TokenTypeCloseCurlyBrace
TokenTypeComma
TokenTypeColon
)
type Token struct {
Type TokenType
Value string
}
// The individual lex functions return a nil pointer if they failed to read a
// token.
var digitRegex = regexp.MustCompile("[0-9]")
func lexNumber(input string, i int) *Token {
tokenValue := ""
hasConsumedDecimalPoint := false
for j := i; j < len(input); j++ {
charString := input[j : j+1]
if digitRegex.MatchString(charString) {
tokenValue += charString
} else if charString == "." && !hasConsumedDecimalPoint {
hasConsumedDecimalPoint = true
tokenValue += charString
} else {
break
}
}
if len(tokenValue) == 0 {
return nil
}
return &Token{TokenTypeNumber, tokenValue}
}
func lexBoolean(input string, i int) *Token {
if i+5 <= len(input) && input[i:i+5] == "false" {
return &Token{TokenTypeBoolean, "false"}
}
if i+4 <= len(input) && input[i:i+4] == "true" {
return &Token{TokenTypeBoolean, "true"}
}
return nil
}
// Does not currently handle escaped quotes
// Does not currently handle newlines
func lexString(input string, i int) *Token {
j := i
if input[j] != '"' {
return nil
}
j++
for ; j < len(input); j++ {
if input[j] == '"' {
return &Token{TokenTypeString, input[i : j+1]}
}
}
// We got to the end without finding a closing quote
return nil
}
func lexDelimiter(input string, i int) *Token {
if input[i] == '[' {
return &Token{TokenTypeOpenSquareBracket, "["}
}
if input[i] == ']' {
return &Token{TokenTypeCloseSquareBracket, "]"}
}
if input[i] == '{' {
return &Token{TokenTypeOpenCurlyBrace, "{"}
}
if input[i] == '}' {
return &Token{TokenTypeCloseCurlyBrace, "}"}
}
if input[i] == ',' {
return &Token{TokenTypeComma, ","}
}
if input[i] == ':' {
return &Token{TokenTypeColon, ":"}
}
return nil
}
func Lex(input string) (tokens []Token, ok bool) {
tokens = []Token{}
i := 0
check := func(token *Token) bool {
if token == nil {
return false
}
tokens = append(tokens, *token)
i += len(token.Value)
return true
}
for i < len(input) {
nextChar := input[i]
// Spaces and tabs are not considered tokens
if nextChar == ' ' || nextChar == '\t' {
i++
continue
}
if check(lexNumber(input, i)) {
continue
}
if check(lexBoolean(input, i)) {
continue
}
if check(lexString(input, i)) {
continue
}
if check(lexDelimiter(input, i)) {
continue
}
return []Token{}, false
}
return tokens, true
}