diff options
Diffstat (limited to 'pkg/lang/scanner/scanner_test.go')
| -rw-r--r-- | pkg/lang/scanner/scanner_test.go | 187 |
1 files changed, 94 insertions, 93 deletions
diff --git a/pkg/lang/scanner/scanner_test.go b/pkg/lang/scanner/scanner_test.go index 99e77fd..4df4b23 100644 --- a/pkg/lang/scanner/scanner_test.go +++ b/pkg/lang/scanner/scanner_test.go @@ -3,6 +3,7 @@ package scanner_test import ( "jinx/pkg/lang/scanner" "jinx/pkg/lang/scanner/token" + "jinx/pkg/libs/source" "strings" "testing" @@ -10,7 +11,7 @@ import ( ) func TestBasic(t *testing.T) { - source := ` + src := ` fn basic() { var x = 1 var y = x + 1 @@ -22,158 +23,158 @@ func TestBasic(t *testing.T) { return true }` - s := scanner.New(strings.NewReader(source)) + s := scanner.New(strings.NewReader(src)) tokens, err := s.Scan() require.NoError(t, err) expected := []token.Token{ - token.Simple(token.EOL, token.NewLoc(0, 0)), - - token.Simple(token.KwFn, token.NewLoc(1, 1)), - token.New(token.Ident, token.NewLoc(1, 4), "basic"), - token.Simple(token.LParen, token.NewLoc(1, 9)), - token.Simple(token.RParen, token.NewLoc(1, 10)), - token.Simple(token.LBrace, token.NewLoc(1, 12)), - token.Simple(token.EOL, token.NewLoc(1, 13)), - - token.Simple(token.KwVar, token.NewLoc(2, 2)), - token.New(token.Ident, token.NewLoc(2, 6), "x"), - token.Simple(token.Assign, token.NewLoc(2, 8)), - token.New(token.Int, token.NewLoc(2, 10), uint64(1)), - token.Simple(token.EOL, token.NewLoc(2, 11)), - - token.Simple(token.KwVar, token.NewLoc(3, 2)), - token.New(token.Ident, token.NewLoc(3, 6), "y"), - token.Simple(token.Assign, token.NewLoc(3, 8)), - token.New(token.Ident, token.NewLoc(3, 10), "x"), - token.Simple(token.Plus, token.NewLoc(3, 12)), - token.New(token.Int, token.NewLoc(3, 14), uint64(1)), - token.Simple(token.EOL, token.NewLoc(3, 15)), - - token.Simple(token.KwIf, token.NewLoc(4, 2)), - token.New(token.Ident, token.NewLoc(4, 5), "x"), - token.Simple(token.Lt, token.NewLoc(4, 7)), - token.New(token.Ident, token.NewLoc(4, 9), "y"), - token.Simple(token.LBrace, token.NewLoc(4, 11)), - token.Simple(token.EOL, token.NewLoc(4, 12)), - - token.New(token.Ident, token.NewLoc(5, 3), "say"), - token.Simple(token.LParen, token.NewLoc(5, 6)), - token.New(token.String, token.NewLoc(5, 7), "x is less than y"), - token.Simple(token.RParen, token.NewLoc(5, 25)), - token.Simple(token.EOL, token.NewLoc(5, 26)), - - token.Simple(token.RBrace, token.NewLoc(6, 2)), - token.Simple(token.KwElse, token.NewLoc(6, 4)), - token.Simple(token.LBrace, token.NewLoc(6, 9)), - token.Simple(token.EOL, token.NewLoc(6, 10)), - - token.New(token.Ident, token.NewLoc(7, 3), "say"), - token.Simple(token.LParen, token.NewLoc(7, 6)), - token.New(token.String, token.NewLoc(7, 7), "x is greater than or equal to y"), - token.Simple(token.RParen, token.NewLoc(7, 40)), - token.Simple(token.EOL, token.NewLoc(7, 41)), - - token.Simple(token.RBrace, token.NewLoc(8, 2)), - token.Simple(token.EOL, token.NewLoc(8, 3)), - - token.Simple(token.KwReturn, token.NewLoc(9, 2)), - token.Simple(token.KwTrue, token.NewLoc(9, 9)), - token.Simple(token.EOL, token.NewLoc(9, 13)), - - token.Simple(token.RBrace, token.NewLoc(10, 1)), - - token.Simple(token.EOF, token.NewLoc(10, 2)), + token.Simple(token.EOL, source.NewLoc(0, 0)), + + token.Simple(token.KwFn, source.NewLoc(1, 1)), + token.New(token.Ident, source.NewLoc(1, 4), "basic"), + token.Simple(token.LParen, source.NewLoc(1, 9)), + token.Simple(token.RParen, source.NewLoc(1, 10)), + token.Simple(token.LBrace, source.NewLoc(1, 12)), + token.Simple(token.EOL, source.NewLoc(1, 13)), + + token.Simple(token.KwVar, source.NewLoc(2, 2)), + token.New(token.Ident, source.NewLoc(2, 6), "x"), + token.Simple(token.Assign, source.NewLoc(2, 8)), + token.New(token.Int, source.NewLoc(2, 10), uint64(1)), + token.Simple(token.EOL, source.NewLoc(2, 11)), + + token.Simple(token.KwVar, source.NewLoc(3, 2)), + token.New(token.Ident, source.NewLoc(3, 6), "y"), + token.Simple(token.Assign, source.NewLoc(3, 8)), + token.New(token.Ident, source.NewLoc(3, 10), "x"), + token.Simple(token.Plus, source.NewLoc(3, 12)), + token.New(token.Int, source.NewLoc(3, 14), uint64(1)), + token.Simple(token.EOL, source.NewLoc(3, 15)), + + token.Simple(token.KwIf, source.NewLoc(4, 2)), + token.New(token.Ident, source.NewLoc(4, 5), "x"), + token.Simple(token.Lt, source.NewLoc(4, 7)), + token.New(token.Ident, source.NewLoc(4, 9), "y"), + token.Simple(token.LBrace, source.NewLoc(4, 11)), + token.Simple(token.EOL, source.NewLoc(4, 12)), + + token.New(token.Ident, source.NewLoc(5, 3), "say"), + token.Simple(token.LParen, source.NewLoc(5, 6)), + token.New(token.String, source.NewLoc(5, 7), "x is less than y"), + token.Simple(token.RParen, source.NewLoc(5, 25)), + token.Simple(token.EOL, source.NewLoc(5, 26)), + + token.Simple(token.RBrace, source.NewLoc(6, 2)), + token.Simple(token.KwElse, source.NewLoc(6, 4)), + token.Simple(token.LBrace, source.NewLoc(6, 9)), + token.Simple(token.EOL, source.NewLoc(6, 10)), + + token.New(token.Ident, source.NewLoc(7, 3), "say"), + token.Simple(token.LParen, source.NewLoc(7, 6)), + token.New(token.String, source.NewLoc(7, 7), "x is greater than or equal to y"), + token.Simple(token.RParen, source.NewLoc(7, 40)), + token.Simple(token.EOL, source.NewLoc(7, 41)), + + token.Simple(token.RBrace, source.NewLoc(8, 2)), + token.Simple(token.EOL, source.NewLoc(8, 3)), + + token.Simple(token.KwReturn, source.NewLoc(9, 2)), + token.Simple(token.KwTrue, source.NewLoc(9, 9)), + token.Simple(token.EOL, source.NewLoc(9, 13)), + + token.Simple(token.RBrace, source.NewLoc(10, 1)), + + token.Simple(token.EOF, source.NewLoc(10, 2)), } require.Equal(t, expected, tokens) } func TestTightIdent(t *testing.T) { - source := `say(message)` + src := `say(message)` - s := scanner.New(strings.NewReader(source)) + s := scanner.New(strings.NewReader(src)) tokens, err := s.Scan() require.NoError(t, err) expected := []token.Token{ - token.New(token.Ident, token.NewLoc(0, 0), "say"), - token.Simple(token.LParen, token.NewLoc(0, 3)), - token.New(token.Ident, token.NewLoc(0, 4), "message"), - token.Simple(token.RParen, token.NewLoc(0, 11)), - token.Simple(token.EOF, token.NewLoc(0, 12)), + token.New(token.Ident, source.NewLoc(0, 0), "say"), + token.Simple(token.LParen, source.NewLoc(0, 3)), + token.New(token.Ident, source.NewLoc(0, 4), "message"), + token.Simple(token.RParen, source.NewLoc(0, 11)), + token.Simple(token.EOF, source.NewLoc(0, 12)), } require.Equal(t, expected, tokens) } func TestTightNumber(t *testing.T) { - source := `1+2+3` + src := `1+2+3` - s := scanner.New(strings.NewReader(source)) + s := scanner.New(strings.NewReader(src)) tokens, err := s.Scan() require.NoError(t, err) expected := []token.Token{ - token.New(token.Int, token.NewLoc(0, 0), uint64(1)), - token.Simple(token.Plus, token.NewLoc(0, 1)), - token.New(token.Int, token.NewLoc(0, 2), uint64(2)), - token.Simple(token.Plus, token.NewLoc(0, 3)), - token.New(token.Int, token.NewLoc(0, 4), uint64(3)), - token.Simple(token.EOF, token.NewLoc(0, 5)), + token.New(token.Int, source.NewLoc(0, 0), uint64(1)), + token.Simple(token.Plus, source.NewLoc(0, 1)), + token.New(token.Int, source.NewLoc(0, 2), uint64(2)), + token.Simple(token.Plus, source.NewLoc(0, 3)), + token.New(token.Int, source.NewLoc(0, 4), uint64(3)), + token.Simple(token.EOF, source.NewLoc(0, 5)), } require.Equal(t, expected, tokens) } func TestNewlineStacking(t *testing.T) { - source := ` + src := ` x y ` - s := scanner.New(strings.NewReader(source)) + s := scanner.New(strings.NewReader(src)) tokens, err := s.Scan() require.NoError(t, err) expected := []token.Token{ - token.Simple(token.EOL, token.NewLoc(0, 0)), - token.New(token.Ident, token.NewLoc(1, 1), "x"), - token.Simple(token.EOL, token.NewLoc(1, 2)), - token.New(token.Ident, token.NewLoc(4, 1), "y"), - token.Simple(token.EOL, token.NewLoc(4, 2)), - token.Simple(token.EOF, token.NewLoc(5, 1)), + token.Simple(token.EOL, source.NewLoc(0, 0)), + token.New(token.Ident, source.NewLoc(1, 1), "x"), + token.Simple(token.EOL, source.NewLoc(1, 2)), + token.New(token.Ident, source.NewLoc(4, 1), "y"), + token.Simple(token.EOL, source.NewLoc(4, 2)), + token.Simple(token.EOF, source.NewLoc(5, 1)), } require.Equal(t, expected, tokens) } func TestEmojiInStrings(t *testing.T) { - source := ` + src := ` say("🇺🇦" + "❤️!") ` - s := scanner.New(strings.NewReader(source)) + s := scanner.New(strings.NewReader(src)) tokens, err := s.Scan() require.NoError(t, err) expected := []token.Token{ - token.Simple(token.EOL, token.NewLoc(0, 0)), - token.New(token.Ident, token.NewLoc(1, 1), "say"), - token.Simple(token.LParen, token.NewLoc(1, 4)), - token.New(token.String, token.NewLoc(1, 5), "🇺🇦"), - token.Simple(token.Plus, token.NewLoc(1, 10)), - token.New(token.String, token.NewLoc(1, 12), "❤️!"), - token.Simple(token.RParen, token.NewLoc(1, 17)), - token.Simple(token.EOL, token.NewLoc(1, 18)), - token.Simple(token.EOF, token.NewLoc(2, 1)), + token.Simple(token.EOL, source.NewLoc(0, 0)), + token.New(token.Ident, source.NewLoc(1, 1), "say"), + token.Simple(token.LParen, source.NewLoc(1, 4)), + token.New(token.String, source.NewLoc(1, 5), "🇺🇦"), + token.Simple(token.Plus, source.NewLoc(1, 10)), + token.New(token.String, source.NewLoc(1, 12), "❤️!"), + token.Simple(token.RParen, source.NewLoc(1, 17)), + token.Simple(token.EOL, source.NewLoc(1, 18)), + token.Simple(token.EOF, source.NewLoc(2, 1)), } require.Equal(t, expected, tokens) |
