-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathenhanced_lexer.go
More file actions
221 lines (199 loc) · 4.78 KB
/
Copy pathenhanced_lexer.go
File metadata and controls
221 lines (199 loc) · 4.78 KB
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
package parser
import "strings"
// An enhanced lexer that supports negative numbers
type EnhancedLexer struct {
input string
position int
readPosition int
ch byte
}
// NewEnhancedLexer creates a new enhanced lexer that supports negative numbers
func NewEnhancedLexer(input string) *EnhancedLexer {
l := &EnhancedLexer{input: input}
l.readChar()
return l
}
func (l *EnhancedLexer) readChar() {
if l.readPosition >= len(l.input) {
l.ch = 0
} else {
l.ch = l.input[l.readPosition]
}
l.position = l.readPosition
l.readPosition++
}
func (l *EnhancedLexer) peekChar() byte {
if l.readPosition >= len(l.input) {
return 0
}
return l.input[l.readPosition]
}
// peekTwoChars returns the character two positions ahead
func peekTwoChars(l *EnhancedLexer) byte {
if l.readPosition+1 >= len(l.input) {
return 0
}
return l.input[l.readPosition+1]
}
func (l *EnhancedLexer) NextToken() Token {
var tok Token
l.skipWhitespace()
switch l.ch {
case '=':
tok = newToken(EQ, l.ch)
case '!':
if l.peekChar() == '=' {
ch := l.ch
l.readChar()
literal := string(ch) + string(l.ch)
tok = Token{Type: NE, Literal: literal}
} else {
tok = newToken(ILLEGAL, l.ch)
}
case '<':
if l.peekChar() == '=' {
ch := l.ch
l.readChar()
literal := string(ch) + string(l.ch)
tok = Token{Type: LE, Literal: literal}
} else {
tok = newToken(LT, l.ch)
}
case '>':
if l.peekChar() == '=' {
ch := l.ch
l.readChar()
literal := string(ch) + string(l.ch)
tok = Token{Type: GE, Literal: literal}
} else {
tok = newToken(GT, l.ch)
}
case '(':
tok = newToken(LPAREN, l.ch)
case ')':
tok = newToken(RPAREN, l.ch)
case ',':
// Check if this comma is between digits, which would make it part of a number
if isDigit(l.peekChar()) && l.position > 0 && isDigit(l.input[l.position-1]) {
tok.Type = NUMBER
tok.Literal = l.readNumber()
// Include the comma in the literal
tok.Literal = "," + tok.Literal
return tok
} else {
tok = newToken(COMMA, l.ch)
}
case '\'':
tok.Type = STRING
tok.Literal = l.readString()
// Check for unclosed string marker
if strings.HasPrefix(tok.Literal, "_UNCLOSED_STRING_") {
tok.Type = ILLEGAL
tok.Literal = "unclosed string: " + strings.TrimPrefix(tok.Literal, "_UNCLOSED_STRING_")
}
return tok
case '-':
// Check if it's a negative number
if isDigit(l.peekChar()) {
tok.Type = NUMBER
tok.Literal = l.readNumber()
return tok
} else {
tok = newToken(ILLEGAL, l.ch)
}
case 0: // EOF
tok.Literal = ""
tok.Type = EOF
default:
if isLetter(l.ch) {
tok.Literal = l.readIdentifier()
typeFromIdent := LookupIdentifier(tok.Literal)
tok.Type = typeFromIdent
return tok
} else if isDigit(l.ch) {
tok.Type = NUMBER
tok.Literal = l.readNumber()
return tok
} else {
tok = newToken(ILLEGAL, l.ch)
}
}
l.readChar()
return tok
}
func (l *EnhancedLexer) skipWhitespace() {
for l.ch == ' ' || l.ch == '\t' || l.ch == '\n' || l.ch == '\r' {
l.readChar()
}
}
func (l *EnhancedLexer) readIdentifier() string {
position := l.position
for isLetter(l.ch) || isDigit(l.ch) || l.ch == '.' || l.ch == '_' || l.ch == '-' {
l.readChar()
}
return l.input[position:l.position]
}
func (l *EnhancedLexer) readNumber() string {
position := l.position
// Handle negative numbers
if l.ch == '-' {
l.readChar()
}
// Read integer part, allowing commas for readability (e.g., 1,000,000)
hasDigits := false
for isDigit(l.ch) || l.ch == ',' {
if isDigit(l.ch) {
hasDigits = true
}
l.readChar()
}
if !hasDigits {
// Ensure we have at least one digit
return l.input[position:l.position]
}
// Read decimal part if present
if l.ch == '.' && isDigit(l.peekChar()) {
l.readChar() // Read '.'
for isDigit(l.ch) {
l.readChar()
}
}
// Read scientific notation if present (e.g., 1.23e45, 1e10)
if (l.ch == 'e' || l.ch == 'E') && (isDigit(l.peekChar()) ||
((l.peekChar() == '+' || l.peekChar() == '-') && isDigit(peekTwoChars(l)))) {
l.readChar() // Read 'e' or 'E'
// Read sign if present
if l.ch == '+' || l.ch == '-' {
l.readChar()
}
// Read exponent
for isDigit(l.ch) {
l.readChar()
}
}
return l.input[position:l.position]
}
func (l *EnhancedLexer) readString() string {
position := l.position + 1
for {
l.readChar()
if l.ch == '\'' || l.ch == 0 {
break
}
// Handle escape sequences
if l.ch == '\\' && l.peekChar() == '\'' {
l.readChar() // Skip the backslash and include the quote
}
}
literal := l.input[position:l.position]
// Check if the string was unclosed (ended with EOF)
isUnclosed := l.ch == 0
if l.ch != 0 { // Only advance if we're not at EOF
l.readChar()
}
if isUnclosed {
// Return a special marker that can be checked by the token consumer
return "_UNCLOSED_STRING_" + literal
}
return literal
}