-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.go
515 lines (454 loc) · 11.4 KB
/
parser.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package gmars
import (
"fmt"
"strings"
)
type lineType uint8
const (
lineEmpty lineType = iota
lineInstruction
linePseudoOp
lineComment
)
type sourceLine struct {
line int
codeLine int
typ lineType
// string values of input to parse tokens from lexer into
labels []string
op string
amode string
a []token
bmode string
b []token
comment string
newlines int
}
type parser struct {
lex tokenReader
// state for the running parser
nextToken token
line int
codeLine int
atEOF bool
err error
currentLine sourceLine
metadata WarriorData
endSeen bool
// collected lines
lines []sourceLine
// maps of symbol definitions and references used to verify that each
// symbol is defined exactly once and each reference is defined.
symbols map[string]int
references map[string]int
}
func newParser(lex tokenReader) *parser {
p := &parser{
lex: lex,
symbols: make(map[string]int),
references: make(map[string]int),
line: 1,
}
p.next()
p.loadPredefinedSymbols()
return p
}
type parseStateFn func(p *parser) parseStateFn
func (p *parser) loadPredefinedSymbols() {
p.symbols["CORESIZE"] = -1
p.symbols["MAXLENGTH"] = -1
p.symbols["MAXPROCESSES"] = -1
p.symbols["MINDISTANCE"] = -1
// p.symbols["CURLINE"] = -1
}
// parse runs the state machine. the main flows are:
//
// code lines:
//
// line -> labels -> op -> aMode -> aExpr -> bMode -> bExpr -> line
// line -> labels -> op -> aMode -> aExpr -> line
// line -> labels -> psuedoOp -> expr -> line
//
// empty line:
//
// line -> emptyLines -> line
//
// comment line:
//
// line -> line
func (p *parser) parse() ([]sourceLine, WarriorData, error) {
for state := parseLine; state != nil; {
state = state(p)
}
if p.err != nil {
return nil, WarriorData{}, p.err
}
err := p.validateSymbols()
if err != nil {
return nil, WarriorData{}, err
}
return p.lines, p.metadata, nil
}
func (p *parser) validateSymbols() error {
for symbol, i := range p.references {
_, ok := p.symbols[symbol]
if !ok {
return fmt.Errorf("line %d: symbol '%s' undefined", i, symbol)
}
}
return nil
}
func (p *parser) next() token {
if p.atEOF {
return token{typ: tokEOF}
}
nextToken, err := p.lex.NextToken()
if err != nil {
p.atEOF = true
return p.nextToken
}
lastToken := p.nextToken
p.nextToken = nextToken
if lastToken.typ == tokNewline {
p.line += 1
}
return lastToken
}
// helper function to emit the current working line and consume
// the current token. return nextState or nil on EOF
func (p *parser) consumeEmitLine(nextState parseStateFn) parseStateFn {
// consume current character
nextToken := p.next()
if p.nextToken.typ != tokNewline {
p.err = fmt.Errorf("expected newline, got: '%s'", p.nextToken)
return nil
}
p.currentLine.newlines += 1
p.lines = append(p.lines, p.currentLine)
nextToken = p.next()
if nextToken.typ == tokEOF {
return nil
}
return nextState
}
// initial state, dispatches to new states based on the first token:
// newline: parseEmptyLines
// comment: parseComment
// text: parseLabels
// eof: nil
// anything else: error
func parseLine(p *parser) parseStateFn {
if p.endSeen {
return nil
}
p.currentLine = sourceLine{line: p.line}
switch p.nextToken.typ {
case tokNewline:
p.currentLine.typ = lineEmpty
return parseEmptyLines
case tokComment:
if strings.HasPrefix(p.nextToken.val, ";name") {
p.metadata.Name = strings.TrimSpace(p.nextToken.val[5:])
} else if strings.HasPrefix(p.nextToken.val, ";author") {
p.metadata.Author = strings.TrimSpace(p.nextToken.val[7:])
} else if strings.HasPrefix(p.nextToken.val, ";strategy") {
if len(p.nextToken.val) > 10 {
p.metadata.Strategy += p.nextToken.val[10:] + "\n"
}
}
p.currentLine.typ = lineComment
return parseComment
case tokText:
return parseLabels
case tokEOF:
return nil
default:
p.err = fmt.Errorf("line %d: unexpected token: '%s' type %d", p.line, p.nextToken, p.nextToken.typ)
return nil
}
}
// parseNewlines consumes newlines and then returns:
// eof: nil
// anything else: parseLine
func parseEmptyLines(p *parser) parseStateFn {
for p.nextToken.typ == tokNewline {
p.currentLine.newlines += 1
p.next()
}
p.lines = append(p.lines, p.currentLine)
return parseLine
}
// parseComment emits a comment and deals with newlines
// newline: parseLine
func parseComment(p *parser) parseStateFn {
p.currentLine.comment = p.nextToken.val
return p.consumeEmitLine(parseLine)
}
// parseLabels consumes text tokens until an op is read
// label text token: parseLabels
// op text token: parseOp
// colon: parseColon
// newline / comments: consume
// anyting else: nil
func parseLabels(p *parser) parseStateFn {
// just consume newlines and comments for now
if p.nextToken.typ == tokNewline || p.nextToken.typ == tokComment {
p.next()
return parseLabels
}
if p.nextToken.IsOp() {
if p.nextToken.IsPseudoOp() {
return parsePseudoOp
}
return parseOp
}
if p.nextToken.typ == tokColon {
return parseColon
}
_, ok := p.symbols[p.nextToken.val]
if ok {
p.err = fmt.Errorf("line %d: symbol '%s' redefined", p.line, p.nextToken.val)
}
p.symbols[p.nextToken.val] = p.line
p.currentLine.labels = append(p.currentLine.labels, p.nextToken.val)
nextToken := p.next()
if nextToken.typ != tokText {
p.err = fmt.Errorf("line %d: label or op expected, got '%s'", p.line, nextToken)
return nil
}
return parseLabels
}
// from: parseLabels
// newline or comments: parseColon
// op text: parseOp
// psudo op text: parsePseudoOp
// text: parseLabels
// anything else: nil
func parseColon(p *parser) parseStateFn {
for p.nextToken.typ == tokColon {
p.next()
}
// just consume newlines and comments for now
if p.nextToken.typ == tokNewline || p.nextToken.typ == tokComment {
p.next()
return parseColon
}
if p.nextToken.IsOp() {
if p.nextToken.IsPseudoOp() {
return parsePseudoOp
}
return parseOp
}
if p.nextToken.typ == tokText {
return parseLabels
}
p.err = fmt.Errorf("line %d: op expected after colon, got '%s'", p.line, p.nextToken)
return nil
}
// from: parseLabels
// comment: parseComment
// newline: parseLine
// exprssionterm: parsePseudoExpr
// anything else: error
func parsePseudoOp(p *parser) parseStateFn {
p.currentLine.op = p.nextToken.val
p.currentLine.typ = linePseudoOp
if strings.ToLower(p.nextToken.val) == "end" {
p.endSeen = true
}
lastToken := p.nextToken
p.next()
if p.nextToken.IsExpressionTerm() {
return parsePseudoExpr
} else if p.nextToken.typ == tokComment {
return parseComment
} else if p.nextToken.typ == tokEOF {
if lastToken.NoOperandsOk() {
p.next()
p.currentLine.newlines += 1
p.lines = append(p.lines, p.currentLine)
return nil
}
} else if p.nextToken.typ == tokNewline {
if lastToken.NoOperandsOk() {
p.next()
p.currentLine.newlines += 1
p.lines = append(p.lines, p.currentLine)
return parseLine
}
p.err = fmt.Errorf("line %d: expected operand expression after pseudo-op '%s', got newline", p.line, lastToken.val)
return nil
}
p.err = fmt.Errorf("line %d: expected operand expression, comment, or newline after pseudo-op, got: '%s'", p.line, p.nextToken)
return nil
}
// from: parsePseudoOp
// on comment: parseComment
// expressionterm: parsePseudoExpr
// anything else: error
func parsePseudoExpr(p *parser) parseStateFn {
if p.currentLine.a == nil {
p.currentLine.a = make([]token, 0)
}
for p.nextToken.IsExpressionTerm() {
if p.nextToken.typ == tokText {
_, ok := p.references[p.nextToken.val]
if !ok {
p.references[p.nextToken.val] = p.line
}
}
p.currentLine.a = append(p.currentLine.a, p.nextToken)
p.next()
}
switch p.nextToken.typ {
case tokComment:
return parseComment
case tokNewline:
p.next()
p.currentLine.newlines += 1
p.lines = append(p.lines, p.currentLine)
return parseLine
case tokEOF:
p.lines = append(p.lines, p.currentLine)
return parseLine
default:
p.err = fmt.Errorf("line %d: expected comment or newline after expression, got '%s'", p.line, p.nextToken)
return nil
}
}
// from: parseLabels
// addressmode: parseModeA
// expressionterm: parseExprA
// anything else: error
func parseOp(p *parser) parseStateFn {
p.currentLine.op = p.nextToken.val
p.currentLine.typ = lineInstruction
p.currentLine.codeLine = p.codeLine
p.codeLine += 1
p.next()
if p.nextToken.IsAddressMode() {
return parseModeA
}
if p.nextToken.IsExpressionTerm() && p.nextToken.val != "*" {
return parseExprA
}
switch p.nextToken.typ {
case tokSymbol:
if p.nextToken.val == "*" {
return parseModeA
}
return parseExprA
default:
p.err = fmt.Errorf("line %d: expected operand expression after op, got '%s'", p.line, p.nextToken)
return nil
}
}
// from: parseOp
// experssionterm: parseExprA
// anything else: error
func parseModeA(p *parser) parseStateFn {
p.currentLine.amode = p.nextToken.val
p.next()
if p.nextToken.IsExpressionTerm() {
return parseExprA
}
p.err = fmt.Errorf("line %d: expected address mode or operand expression, got '%s'", p.line, p.nextToken)
return nil
}
// from: parseOp, parseModeA
// expression term: recursively consume tokens to exprA
// comma: parseComma
// comment: parseComment
// newline: emit and parseLine
// anything else: error
func parseExprA(p *parser) parseStateFn {
if p.currentLine.a == nil {
p.currentLine.a = make([]token, 0)
}
for p.nextToken.IsExpressionTerm() {
if p.nextToken.typ == tokText {
_, ok := p.references[p.nextToken.val]
if !ok {
p.references[p.nextToken.val] = p.line
}
}
p.currentLine.a = append(p.currentLine.a, p.nextToken)
p.next()
}
switch p.nextToken.typ {
case tokComment:
return parseComment
case tokComma:
return parseComma
case tokNewline:
fallthrough
case tokEOF:
p.lines = append(p.lines, p.currentLine)
return parseLine
default:
p.err = fmt.Errorf("line %d: expected comma or newline after op, got '%s'", p.line, p.nextToken)
return nil
}
}
// from: parseExprA
// addressmode: parseModeB
// expression term: parseExprB
// anything else: error
func parseComma(p *parser) parseStateFn {
p.next()
if p.nextToken.IsAddressMode() {
return parseModeB
} else if p.nextToken.IsExpressionTerm() {
return parseExprB
} else {
p.err = fmt.Errorf("expected address mode or expression after comma, got '%s'", p.nextToken)
return nil
}
}
// from: parseComma
// expressionterm: parseExprB
// anything else: error
func parseModeB(p *parser) parseStateFn {
p.currentLine.bmode = p.nextToken.val
p.next()
if p.nextToken.IsExpressionTerm() {
return parseExprB
}
p.err = fmt.Errorf("line %d: expected address mode or operand expression, got '%s'", p.line, p.nextToken)
return nil
}
// from parseComma, parseModeB
// expressionTerm: recursively consume tokens to exprB
// comment: parseComment
// newline: parseLine
// anything else: error
func parseExprB(p *parser) parseStateFn {
if p.currentLine.b == nil {
p.currentLine.b = make([]token, 0)
}
for p.nextToken.IsExpressionTerm() {
if p.nextToken.typ == tokText {
_, ok := p.references[p.nextToken.val]
if !ok {
p.references[p.nextToken.val] = p.line
}
}
p.currentLine.b = append(p.currentLine.b, p.nextToken)
p.next()
}
switch p.nextToken.typ {
case tokComment:
return parseComment
case tokNewline:
p.currentLine.newlines += 1
p.lines = append(p.lines, p.currentLine)
p.next()
return parseLine
case tokEOF:
p.lines = append(p.lines, p.currentLine)
return parseLine
default:
p.err = fmt.Errorf("line %d: expected comma or newline after op, got '%s'", p.line, p.nextToken)
return nil
}
}