1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- package flexer
- import "testing"
- import . "src.eruta.nl/beoran/ll1/common"
- const (
- tWord = Kind(-1 - iota)
- tArrow
- tSpace
- tString
- tPlus = Kind('+')
- tEos = Kind('.')
- )
- func TestFlexer(t *testing.T) {
- pos := Location{}
- expected := []Token{
- MakeToken(pos, tSpace, "\t "),
- MakeToken(pos, tWord, "PROGRAM"),
- MakeToken(pos, tSpace, " "),
- MakeToken(pos, tArrow, "->"),
- MakeToken(pos, tSpace, " "),
- MakeToken(pos, tWord, "STATEMENT"),
- MakeToken(pos, tPlus, "+"),
- MakeToken(pos, tSpace, " "),
- MakeToken(pos, tEos, ".\n"),
- MakeToken(pos, tWord, "say"),
- MakeToken(pos, tSpace, " "),
- MakeToken(pos, tString, "hello\nworld"),
- MakeToken(pos, tEos, "."),
- }
- f := NewFlexer(`test`, "\t PROGRAM -> STATEMENT+ .\nsay \"hello\\nworld\".")
- f.Lexeme(tSpace, `[ \t]+`, "", nil)
- f.Lexeme(tWord, `[A-Za-z_]+`, "", nil)
- f.Lexeme(tArrow, `\->`, "", nil)
- f.Lexeme(tPlus, `\+`, "", nil)
- f.Lexeme(tEos, `\.[\n\r]*`, "", nil)
- f.Lexeme(SkipKind, `"`, "", ContextAction("string"))
- f.Lexeme(tString, `"`, "string", PopAction(tString))
- f.Lexeme(SkipKind, `\\[etnru][0-9a-f]*`, "string", EscapeAction('"'))
- f.Lexeme(SkipKind, `.`, "string", StoreAction())
- toks := LexAll(f)
- for i, e := range expected {
- tok := toks[i]
- t.Logf("toks: %d, %v", i, tok)
- ko := tok.Kind()
- ke := e.Kind()
- if ko != ke {
- t.Errorf("error: kind:%d|%d|", ko, ke)
- }
- to := tok.Text()
- te := e.Text()
- if to != te {
- t.Errorf("error: text:%s|%s|", to, te)
- }
- }
- if !f.EOF() {
- t.Errorf("error: should be EOF")
- }
- }
|