Skip to content

Commit

Permalink
asm/lexer: Fix handling of EOF and add test case to verify the implem…
Browse files Browse the repository at this point in the history
…entation. Updates #8.
  • Loading branch information
mewmew committed Jan 17, 2015
1 parent edc14fa commit 12ba68e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion asm/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ func (l *lexer) lex() {
// errorf appends an error token at the current position.
func (l *lexer) errorf(format string, args ...interface{}) {
err := fmt.Sprintf(format, args...)
_, width := utf8.DecodeLastRuneInString(l.input[:l.cur])
// Backup one rune if not at EOF.
width := 0
if !(l.cur == len(l.input) && l.width == 0) {
_, width = utf8.DecodeLastRuneInString(l.input[:l.cur])
}
tok := token.Token{
Kind: token.Error,
Val: err,
Expand Down
9 changes: 9 additions & 0 deletions asm/lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ func TestParse(t *testing.T) {
{Kind: token.EOF, Pos: 12},
},
},
// i=16
{
input: `"fooλbar" "baz`,
want: []token.Token{
{Kind: token.String, Val: "fooλbar", Pos: 0},
{Kind: token.Error, Val: "unexpected eof in quoted string", Pos: 15},
{Kind: token.EOF, Pos: 15},
},
},
}
for i, g := range golden {
got := Parse(g.input)
Expand Down
4 changes: 4 additions & 0 deletions asm/lexer/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func lexAt(l *lexer) stateFn {
case l.accept(`"`):
s, ok := readString(l)
if !ok {
l.emitEOF()
// Terminate the lexer with a nil state function.
return nil
}
Expand Down Expand Up @@ -207,6 +208,7 @@ func lexPercent(l *lexer) stateFn {
case l.accept(`"`):
s, ok := readString(l)
if !ok {
l.emitEOF()
// Terminate the lexer with a nil state function.
return nil
}
Expand Down Expand Up @@ -269,6 +271,7 @@ func lexDollar(l *lexer) stateFn {
case l.accept(`"`):
s, ok := readString(l)
if !ok {
l.emitEOF()
// Terminate the lexer with a nil state function.
return nil
}
Expand Down Expand Up @@ -362,6 +365,7 @@ func lexQuote(l *lexer) stateFn {
// Consume a string constant ("foo", "fo\6F").
s, ok := readString(l)
if !ok {
l.emitEOF()
// Terminate the lexer with a nil state function.
return nil
}
Expand Down

0 comments on commit 12ba68e

Please sign in to comment.