Skip to content

Commit

Permalink
Stop lexing when hitting EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
Xuewei Zhang authored and xueweiz committed Apr 9, 2021
1 parent 38179f3 commit 227e04e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ func stateIdent(l *lexer) stateFn {
Loop:
for {
switch r := l.peek(); {
case r == eof:
return l.errorf("unclosed tag")
case !whitespace(r) && !strings.HasPrefix(l.input[l.pos:], l.rightDelim):
// absorb
l.next()
Expand Down
19 changes: 19 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package mustache

import (
"reflect"
"strings"
"testing"
)

Expand Down Expand Up @@ -99,3 +100,21 @@ func TestParser(t *testing.T) {
}
}
}

func TestParserNegative(t *testing.T) {
for _, test := range []struct {
template string
expErr string
}{
{
"{{foo}",
`1:6 syntax error: unreachable code t_error:"unclosed tag"`,
},
} {
parser := newParser(newLexer(test.template, "{{", "}}"))
_, err := parser.parse()
if err == nil || !strings.Contains(err.Error(), test.expErr) {
t.Errorf("expect error: %q, got %q", test.expErr, err)
}
}
}

0 comments on commit 227e04e

Please sign in to comment.