-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.go
145 lines (119 loc) · 2.5 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
package pgn
type lexer struct {
input string
position int // Current position
readPosition int // Position to read (after current position)
ch byte // Current character under examination
}
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) NextToken() token {
var tok token
l.skipWhitespace()
switch l.ch {
case '.':
tok = newToken(PERIOD, l.ch)
case '*':
tok = newToken(ASTERIX, l.ch)
case '[':
tok = newToken(LBRACKET, l.ch)
case ']':
tok = newToken(RBRACKET, l.ch)
case '(':
tok = newToken(LPAREN, l.ch)
case ')':
tok = newToken(RPAREN, l.ch)
case '<':
tok = newToken(LANGLE, l.ch)
case '%':
tok = newToken(PERCENTAGE, l.ch)
case '>':
tok = newToken(RANGLE, l.ch)
case '"':
tok.Type = STRING
tok.Literal = l.readString()
case '$':
tok.Type = NAG
tok.Literal = l.readNAG()
case 0:
tok.Type = EOF
tok.Literal = ""
default:
if isLetter(l.ch) || isDigit(l.ch) {
tok.Literal, tok.Type = l.readSymbolOrInteger()
} else {
tok = newToken(ILLEGAL, l.ch)
}
}
l.readChar()
return tok
}
func newToken(tokenType tokenType, ch byte) token {
return token{Type: tokenType, Literal: string(ch)}
}
func (l *lexer) skipWhitespace() {
for l.ch == ' ' || l.ch == '\t' || l.ch == '\n' || l.ch == '\r' {
l.readChar()
}
}
func (l *lexer) readString() string {
position := l.position + 1
for {
l.readChar()
if l.ch == '"' || l.ch == 0 {
break
}
}
return l.input[position:l.position]
}
func (l *lexer) readNAG() string {
position := l.position + 1
l.readChar()
for isDigit(l.ch) {
l.readChar()
}
return l.input[position:l.position]
}
func (l *lexer) readSymbolOrInteger() (string, tokenType) {
flag := false
position := l.position
for isDigit(l.ch) || isLetter(l.ch) || isSpecialChar(l.ch) {
if l.peekChar() == '.' || l.peekChar() == '*' || l.peekChar() == '$' {
flag = true
break
}
l.readChar()
}
var tokenLiteral string
if flag {
tokenLiteral = l.input[position:l.readPosition]
} else {
tokenLiteral = l.input[position:l.position]
}
isInteger := isDigitsOnly(tokenLiteral)
if isInteger {
return tokenLiteral, INTEGER
}
return tokenLiteral, SYMBOL
}
func (l *lexer) peekChar() byte {
if l.readPosition >= len(l.input) {
return 0
} else {
return l.input[l.readPosition]
}
}