summaryrefslogtreecommitdiff
path: root/scanner/scan_test.go
blob: bf3602de4306e879c7ad62412d4a1350dabed410 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package scanner_test

import (
	"log"
	"strings"
	"testing"

	"github.com/mvertes/parscan/lang/golang"
	"github.com/mvertes/parscan/scanner"
)

var GoScanner *scanner.Scanner

func init() {
	log.SetFlags(log.Lshortfile)
	GoScanner = scanner.NewScanner(golang.GoSpec)
}

func TestScan(t *testing.T) {
	for _, test := range tests {
		test := test
		t.Run("", func(t *testing.T) {
			errStr := ""
			tokens, err := GoScanner.Scan(test.src, true)
			if err != nil {
				errStr = err.Error()
			}
			if errStr != test.err {
				t.Errorf("got error %v, want error %#v", errStr, test.err)
			}
			if result := tokStr(tokens); result != test.tok {
				t.Errorf("got %v, want %v", result, test.tok)
			}
		})
	}
}

func tokStr(tokens []scanner.Token) (s string) {
	var sb strings.Builder
	for _, t := range tokens {
		sb.WriteString(t.String() + " ")
	}
	s += sb.String()
	return s
}

var tests = []struct {
	src, tok, err string
}{{ // #00
	src: "",
}, { // #01
	src: "   abc + 5",
	tok: `Ident"abc" Add Int"5" Semicolon `,
}, { // #02
	src: "abc0+5 ",
	tok: `Ident"abc0" Add Int"5" Semicolon `,
}, { // #03
	src: "a+5\na=x-4",
	tok: `Ident"a" Add Int"5" Semicolon Ident"a" Assign Ident"x" Sub Int"4" Semicolon `,
}, { // #04
	src: `return "hello world" + 4`,
	tok: `Return String"\"hello world\"" Add Int"4" Semicolon `,
}, { // #05
	src: `print(4 * (3+7))`,
	tok: `Ident"print" ParenBlock"(4 * (3+7))" Semicolon `,
}, { // #06
	src: `"foo`,
	err: "1:1: block not terminated",
}, { // #07
	src: `abc
def "foo truc`,
	err: "2:6: block not terminated",
}, { // #08
	src: `"ab\"`,
	err: "1:1: block not terminated",
}, { // #09
	src: `"ab\\"`,
	tok: `String"\"ab\\\\\"" Semicolon `,
}, { // #10
	src: `"ab\\\"`,
	err: "1:1: block not terminated",
}, { // #11
	src: `"ab\\\\"`,
	tok: `String"\"ab\\\\\\\\\"" Semicolon `,
}, { // #12
	src: `"abc
def"`,
	err: "1:1: block not terminated",
}, { // #13
	src: "`hello\nworld`",
	tok: "String\"`hello\\nworld`\" Semicolon ",
}, { // #14
	src: "2* (3+4",
	err: "1:4: block not terminated",
}, { // #15
	src: `("fo)o")+1`,
	tok: `ParenBlock"(\"fo)o\")" Add Int"1" Semicolon `,
}, { // #16
	src: `"foo""bar"`,
	tok: `String"\"foo\"" String"\"bar\"" Semicolon `,
}, { // #17
	src: "/* a comment */ a = 2",
	tok: `Comment"/* a comment */" Ident"a" Assign Int"2" Semicolon `,
}, { // #18
	src: "return // quit\nbegin",
	tok: `Return Comment"// quit" Semicolon Ident"begin" Semicolon `,
}, { // #19
	src: "return // quit",
	tok: `Return Comment"// quit" Semicolon `,
}, { // #20
	src: "println(3 /* argh ) */)",
	tok: `Ident"println" ParenBlock"(3 /* argh ) */)" Semicolon `,
}, { // #21
	src: `println("in f")`,
	tok: `Ident"println" ParenBlock"(\"in f\")" Semicolon `,
}, { // #22
	src: "a, b = 1, 2",
	tok: `Ident"a" Comma Ident"b" Assign Int"1" Comma Int"2" Semicolon `,
}, { // #23
	src: "1 + \n2 + 3",
	tok: `Int"1" Add Int"2" Add Int"3" Semicolon `,
}, { // #24
	src: "i++\n2 + 3",
	tok: `Ident"i" Inc Semicolon Int"2" Add Int"3" Semicolon `,
}, { // #25
	src: "return\na = 1",
	tok: `Return Semicolon Ident"a" Assign Int"1" Semicolon `,
}, { // #26
	src: "if\na == 2 { return }",
	tok: `If Ident"a" Equal Int"2" BraceBlock"{ return }" Semicolon `,
}, { // #27
	src: "f(4)\nreturn",
	tok: `Ident"f" ParenBlock"(4)" Semicolon Return Semicolon `,
}, { // #28
	src: "f(3).\nfield",
	tok: `Ident"f" ParenBlock"(3)" Period"." Ident"field" Semicolon `,
}, { // #29
	src: "\n\n\tif i < 1 {return 0}",
	tok: `If Ident"i" Less Int"1" BraceBlock"{return 0}" Semicolon `,
}}