Skip to content

Commit

Permalink
lex invalid input gracefully in case it is after 'end' op
Browse files Browse the repository at this point in the history
  • Loading branch information
bobertlo committed Nov 23, 2024
1 parent 1bb4387 commit fa3bb24
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
7 changes: 6 additions & 1 deletion lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,13 @@ func lexInput(l *lexer) lexStateFn {
case '\x1a':
return l.consume(lexInput)
default:
// we will put this in the stream. if a file is formatted
// properly, and invalid input should be after an 'end'
// pseudo-op which will cause the parser to stop before
// processing this token, otherwise it is an error
l.tokens <- token{tokInvalid, string(l.nextRune)}
return l.consume(lexInput)
l.tokens <- token{typ: tokEOF}
return nil
}

return nil
Expand Down
11 changes: 2 additions & 9 deletions symbol_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type symbolScanner struct {

nextToken token
atEOF bool
endSeen bool
valBuf []token
labelBuf []string
forLevel int
Expand Down Expand Up @@ -109,7 +108,6 @@ func scanLabels(p *symbolScanner) scanStateFn {
}
return scanConsumeLine
case "end":
p.endSeen = true
if p.forLevel > 1 {
return scanConsumeLine
} else {
Expand All @@ -120,13 +118,8 @@ func scanLabels(p *symbolScanner) scanStateFn {
}
} else if p.nextToken.IsOp() {
return scanConsumeLine
} else if p.nextToken.typ == tokError {
if p.endSeen {
return nil
} else {

return nil
}
} else if p.nextToken.typ == tokInvalid {
return nil
}
p.labelBuf = append(p.labelBuf, p.nextToken.val)
return p.consume(scanLabels)
Expand Down
4 changes: 4 additions & 0 deletions symbol_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func TestSymbolScanner(t *testing.T) {
"test": {{tokNumber, "2"}},
},
},
{
input: "for 1\nend\nrof\n ~",
output: map[string][]token{},
},
}
runSymbolScannerTests(t, tests)
}

0 comments on commit fa3bb24

Please sign in to comment.