Skip to content

Commit

Permalink
Merge branch 'fix-regression-15'
Browse files Browse the repository at this point in the history
Fixes #15

Signed-off-by: Tim Henderson <tadh@google.com>
  • Loading branch information
timtadh committed Feb 5, 2018
2 parents 0804877 + e3d2062 commit 869336e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,43 @@ func TestPartialLexer(x *testing.T) {
t.AssertNil(lexer.CompileDFA())
scan(lexer)
}

func TestRegression(t *testing.T) {
skip := func(*Scanner, *machines.Match) (interface{}, error) {
return nil, nil
}
token := func(id int, name string) Action {
return func(s *Scanner, m *machines.Match) (interface{}, error) {
return string(m.Bytes), nil
}
}

data := "true" // This input fails.
// data := "true " // this with a trailing space does not.

lexer := NewLexer()
lexer.Add([]byte("true"), token(0, "TRUE"))
lexer.Add([]byte("( |\t|\n|\r)+"), skip)

if err := lexer.CompileDFA(); err != nil {
t.Fatal(err)
}

var scanner *Scanner

scanner, err := lexer.Scanner([]byte(data))
if err != nil {
t.Fatal(err)
}

found := 0
tok, err, eos := scanner.Next()
for ; !eos; tok, err, eos = scanner.Next() {
fmt.Printf("Token: %v\n", tok)
found++
}
if found != 1 {
t.Errorf("Expected exactly 1 tokens got %v, ===\nErr: %v\nEOS: %v\nTC: %d\n", found, err, eos, scanner.TC)

}
}
13 changes: 13 additions & 0 deletions machines/dfa_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ func DFALexerEngine(startState, errorState int, trans DFATrans, accepting DFAAcc
if match, has := accepting[state]; has {
matchID = match
matchTC = tc
startLC := lineCols[startTC]
endLC := lineCols[matchTC-1]
match := &Match{
PC: matchID,
TC: startTC,
StartLine: startLC.line,
StartColumn: startLC.col,
EndLine: endLC.line,
EndColumn: endLC.col,
Bytes: text[startTC:matchTC],
}
matchID = -1
return tc, match, nil, scan
}
if matchTC != len(text) && startTC >= len(text) {
// the user has moved us farther than the text. Assume that was
Expand Down

0 comments on commit 869336e

Please sign in to comment.