forked from brettlangdon/forge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
259 lines (231 loc) · 5.48 KB
/
scanner.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package forge
import (
"bufio"
"io"
"strings"
"github.com/go-aah/forge/token"
)
var eof = rune(0)
func isLetter(ch rune) bool {
return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z')
}
func isDigit(ch rune) bool {
return ('0' <= ch && ch <= '9')
}
func isNonNewlineWhitespace(ch rune) bool {
return (ch == ' ' || ch == '\t' || ch == '\r')
}
func isBoolean(str string) bool {
lower := strings.ToLower(str)
return lower == "true" || lower == "false"
}
func isNull(str string) bool {
return strings.ToLower(str) == "null"
}
func isInclude(str string) bool {
return strings.ToLower(str) == "include"
}
// Scanner struct used to hold data necessary for parsing tokens
// from the input reader
type Scanner struct {
curLine int
curCol int
curTok token.Token
curCh rune
newline bool
reader *bufio.Reader
freader io.Reader
}
// NewScanner creates and initializes a new *Scanner from an io.Readerx
func NewScanner(reader io.Reader) *Scanner {
scanner := &Scanner{
reader: bufio.NewReader(reader),
freader: reader,
curLine: 1,
curCol: 1,
newline: false,
}
scanner.readRune()
return scanner
}
func (scanner *Scanner) readRune() {
if scanner.newline {
scanner.curLine++
scanner.curCol = 0
scanner.newline = false
} else {
scanner.curCol++
}
nextCh, _, err := scanner.reader.ReadRune()
if err == io.EOF || err != nil {
scanner.curCh = eof
return
}
scanner.curCh = nextCh
if scanner.curCh == '\n' {
scanner.newline = true
}
}
func (scanner *Scanner) parseIdentifier() {
scanner.curTok.ID = token.IDENTIFIER
scanner.curTok.Literal = string(scanner.curCh)
for {
scanner.readRune()
if !isLetter(scanner.curCh) && !isDigit(scanner.curCh) && scanner.curCh != '_' {
break
}
scanner.curTok.Literal += string(scanner.curCh)
}
if isBoolean(scanner.curTok.Literal) {
scanner.curTok.ID = token.BOOLEAN
} else if isNull(scanner.curTok.Literal) {
scanner.curTok.ID = token.NULL
} else if isInclude(scanner.curTok.Literal) {
scanner.curTok.ID = token.INCLUDE
}
}
func (scanner *Scanner) parseEnvironment() {
scanner.curTok.ID = token.ENVIRONMENT
scanner.curTok.Literal = ""
for {
scanner.readRune()
if !isLetter(scanner.curCh) && scanner.curCh != '_' {
break
}
scanner.curTok.Literal += string(scanner.curCh)
}
}
func (scanner *Scanner) parseNumber(negative bool) {
scanner.curTok.ID = token.INTEGER
scanner.curTok.Literal = string(scanner.curCh)
if negative {
scanner.curTok.Literal = "-" + scanner.curTok.Literal
}
digit := false
for {
scanner.readRune()
if scanner.curCh == '.' && !digit {
scanner.curTok.ID = token.FLOAT
digit = true
} else if !isDigit(scanner.curCh) {
break
}
scanner.curTok.Literal += string(scanner.curCh)
}
}
func (scanner *Scanner) parseString(delimiter rune) {
scanner.curTok.ID = token.STRING
scanner.curTok.Literal = ""
if scanner.curCh == delimiter {
scanner.readRune()
return
}
// Whether or not we are trying to escape a character
escape := false
// If the first character is an escape
if scanner.curCh == '\\' {
escape = true
} else {
scanner.curTok.Literal = string(scanner.curCh)
}
for {
scanner.readRune()
if !escape {
// We haven't seen a backslash yet
if scanner.curCh == '\\' {
// A new backslash
escape = true
continue
} else if scanner.curCh == delimiter {
// An unescaped quote, time to bail out
break
}
// Continue as normal, no escaping necessary
} else if scanner.curCh == '\\' || scanner.curCh == delimiter {
// A valid escape character, continue as normal
escape = false
} else {
// We had a backslash, but an invalid escape character
// TODO: "Unsupported escape character found"
break
}
scanner.curTok.Literal += string(scanner.curCh)
}
scanner.readRune()
}
func (scanner *Scanner) parseComment() {
scanner.curTok.ID = token.COMMENT
scanner.curTok.Literal = ""
for {
scanner.readRune()
if scanner.curCh == '\n' || scanner.curCh == eof {
break
}
scanner.curTok.Literal += string(scanner.curCh)
}
scanner.readRune()
}
func (scanner *Scanner) skipNonNewlineWhitespace() {
for {
scanner.readRune()
if !isNonNewlineWhitespace(scanner.curCh) {
break
}
}
}
// NextToken will read in the next valid token from the Scanner
func (scanner *Scanner) NextToken() token.Token {
if isNonNewlineWhitespace(scanner.curCh) {
scanner.skipNonNewlineWhitespace()
}
scanner.curTok = token.Token{
ID: token.ILLEGAL,
Literal: string(scanner.curCh),
Line: scanner.curLine,
Column: scanner.curCol,
}
switch ch := scanner.curCh; {
case isLetter(ch) || ch == '_':
scanner.parseIdentifier()
case isDigit(ch):
scanner.parseNumber(false)
case ch == '#':
scanner.parseComment()
case ch == '$':
scanner.parseEnvironment()
case ch == eof:
scanner.curTok.ID = token.EOF
scanner.curTok.Literal = "EOF"
_ = close(scanner.freader)
default:
scanner.readRune()
scanner.curTok.Literal = string(ch)
switch ch {
case ',':
scanner.curTok.ID = token.COMMA
case '=':
scanner.curTok.ID = token.EQUAL
case '"', '\'':
scanner.parseString(ch)
case '[':
scanner.curTok.ID = token.LBRACKET
case ']':
scanner.curTok.ID = token.RBRACKET
case '{':
scanner.curTok.ID = token.LBRACE
case '}':
scanner.curTok.ID = token.RBRACE
case ';':
scanner.curTok.ID = token.SEMICOLON
case '\n':
scanner.curTok.ID = token.NEWLINE
case '.':
scanner.curTok.ID = token.PERIOD
case '-':
if isDigit(scanner.curCh) {
scanner.parseNumber(true)
}
}
}
return scanner.curTok
}