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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
// Test GoAWK Lexer
package lexer_test
import (
"fmt"
"strconv"
"strings"
"testing"
. "github.com/benhoyt/goawk/lexer"
)
func TestLexer(t *testing.T) {
tests := []struct {
input string
output string
}{
// Comments, whitespace, line continuations
{"+# foo \n- #foo", `1:1 + "", 1:8 <newline> "", 2:1 - ""`},
{"+\\\n-", `1:1 + "", 2:1 - ""`},
{"+\\\r\n-", `1:1 + "", 2:1 - ""`},
{"+\\-", `1:1 + "", 1:3 <illegal> "expected \\n after \\ line continuation"`},
// Names and keywords
{"x", `1:1 name "x"`},
{"x y0", `1:1 name "x", 1:3 name "y0"`},
{"x 0y", `1:1 name "x", 1:3 number "0", 1:4 name "y"`},
{"sub SUB", `1:1 sub "", 1:5 name "SUB"`},
// String tokens
{`"foo"`, `1:1 string "foo"`},
{`"a\t\r\n\z\'\"\a\b\f\vb"`, `1:1 string "a\t\r\nz'\"\a\b\f\vb"`},
{`"x`, `1:3 <illegal> "didn't find end quote in string"`},
{`"foo\"`, `1:7 <illegal> "didn't find end quote in string"`},
{"\"x\n\"", `1:3 <illegal> "can't have newline in string"`},
{`'foo'`, `1:1 string "foo"`},
{`'a\t\r\n\z\'\"b'`, `1:1 string "a\t\r\nz'\"b"`},
{`'x`, `1:3 <illegal> "didn't find end quote in string"`},
{"'x\n'", `1:3 <illegal> "can't have newline in string"`},
{`"\x0.\x00.\x0A\x10\xff\xFF\x41"`, `1:1 string "\x00.\x00.\n\x10\xff\xffA"`},
{`"\xg"`, `1:4 <illegal> "1 or 2 hex digits expected"`},
{`"\0\78\7\77\777\0 \141 "`, `1:1 string "\x00\a8\a?\xff\x00 a "`},
{`"\ufg\uffg\uabcg\u201C\u1F602"`, `1:1 string "\x0fgÿg઼g“😂"`},
{`"\u201C"`, `1:1 string "“"`},
{`"\u00000041Z"`, `1:1 string "AZ"`},
{`"\ug"`, `1:4 <illegal> "1-8 hex digits expected"`},
{`"\uffffffffg"`, `1:12 <illegal> "invalid Unicode character"`},
// Number tokens
{"0", `1:1 number "0"`},
{"9", `1:1 number "9"`},
{" 0 ", `1:2 number "0"`},
{"\n 1", `1:1 <newline> "", 2:3 number "1"`},
{"1234", `1:1 number "1234"`},
{".5", `1:1 number ".5"`},
{".5e1", `1:1 number ".5e1"`},
{"5e+1", `1:1 number "5e+1"`},
{"5e-1", `1:1 number "5e-1"`},
{"0.", `1:1 number "0."`},
{"42e", `1:1 number "42", 1:3 name "e"`},
{"4.2e", `1:1 number "4.2", 1:4 name "e"`},
{"1.e3", `1:1 number "1.e3"`},
{"1.e3", `1:1 number "1.e3"`},
{"1e3foo", `1:1 number "1e3", 1:4 name "foo"`},
{"1e3+", `1:1 number "1e3", 1:4 + ""`},
{"1e3.4", `1:1 number "1e3", 1:4 number ".4"`},
{"1e-", `1:1 number "1", 1:2 name "e", 1:3 - ""`},
{"1e+", `1:1 number "1", 1:2 name "e", 1:3 + ""`},
{"42`", `1:1 number "42", 1:3 <illegal> "unexpected char"`},
{"0..", `1:1 number "0.", 1:4 <illegal> "expected digits"`},
{".", `1:2 <illegal> "expected digits"`},
// Misc errors
{"&=", `1:2 <illegal> "unexpected char after '&'"`},
}
for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
l := NewLexer([]byte(test.input))
strs := []string{}
for {
pos, tok, val := l.Scan()
if tok == EOF {
break
}
if tok == NUMBER {
// Ensure ParseFloat() works, as that's what our
// parser uses to convert
trimmed := strings.TrimRight(val, "eE")
_, err := strconv.ParseFloat(trimmed, 64)
if err != nil {
t.Fatalf("couldn't parse float: %q", val)
}
}
strs = append(strs, fmt.Sprintf("%d:%d %s %q", pos.Line, pos.Column, tok, val))
if tok == ILLEGAL {
break
}
}
output := strings.Join(strs, ", ")
if output != test.output {
t.Errorf("expected %q, got %q", test.output, output)
}
})
}
}
func TestRegex(t *testing.T) {
tests := []struct {
input string
output string
}{
{`/foo/`, `1:1 regex "foo"`},
{`/=foo/`, `1:1 regex "=foo"`},
{`/a\/b/`, `1:1 regex "a/b"`},
{`/a\/\zb/`, `1:1 regex "a/\\zb"`},
{`/a`, `1:3 <illegal> "didn't find end slash in regex"`},
{"/a\n", `1:3 <illegal> "can't have newline in regex"`},
}
for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
l := NewLexer([]byte(test.input))
l.Scan() // Scan first token (probably DIV)
pos, tok, val := l.ScanRegex()
output := fmt.Sprintf("%d:%d %s %q", pos.Line, pos.Column, tok, val)
if output != test.output {
t.Errorf("expected %q, got %q", test.output, output)
}
})
}
}
func TestScanRegexInvalid(t *testing.T) {
defer func() {
r := recover()
if message, ok := r.(string); ok {
expected := "ScanRegex should only be called after DIV or DIV_ASSIGN token"
if message != expected {
t.Fatalf("expected %q, got %q", expected, message)
}
} else {
t.Fatalf("expected panic of string type")
}
}()
l := NewLexer([]byte("foo/"))
l.Scan() // Scan first token (NAME foo)
l.ScanRegex()
}
func TestHadSpace(t *testing.T) {
tests := []struct {
input string
tokens []Token
spaces []bool
}{
{`foo(x)`, []Token{NAME, LPAREN, NAME, RPAREN}, []bool{false, false, false, false}},
{`foo (x) `, []Token{NAME, LPAREN, NAME, RPAREN}, []bool{false, true, false, false}},
{` foo ( x ) `, []Token{NAME, LPAREN, NAME, RPAREN}, []bool{true, true, true, true}},
}
for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
l := NewLexer([]byte(test.input))
for i := 0; ; i++ {
_, tok, _ := l.Scan()
if tok == EOF {
break
}
if tok != test.tokens[i] {
t.Errorf("expected %s for token %d, got %s", test.tokens[i], i, tok)
}
if l.HadSpace() != test.spaces[i] {
t.Errorf("expected %v for space %d, got %v", test.spaces[i], i, l.HadSpace())
}
}
})
}
}
func TestPeekByte(t *testing.T) {
l := NewLexer([]byte("foo()"))
b := l.PeekByte()
if b != 'f' {
t.Errorf("expected 'f', got %q", b)
}
_, tok, _ := l.Scan()
if tok != NAME {
t.Errorf("expected name, got %s", tok)
}
b = l.PeekByte()
if b != '(' {
t.Errorf("expected '(', got %q", b)
}
_, tok, _ = l.Scan()
if tok != LPAREN {
t.Errorf("expected (, got %s", tok)
}
_, tok, _ = l.Scan()
if tok != RPAREN {
t.Errorf("expected ), got %s", tok)
}
b = l.PeekByte()
if b != 0 {
t.Errorf("expected 0, got %q", b)
}
}
func TestKeywordToken(t *testing.T) {
tests := []struct {
name string
tok Token
}{
{"print", PRINT},
{"split", F_SPLIT},
{"BEGIN", BEGIN},
{"foo", ILLEGAL},
{"GoAWK", ILLEGAL},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tok := KeywordToken(test.name)
if tok != test.tok {
t.Errorf("expected %v, got %v", test.tok, tok)
}
})
}
}
func TestAllTokens(t *testing.T) {
input := "# comment line\n" +
"+ += && = : , -- /\n/= $ @ == >= > >> ++ { [ < ( #\n" +
"<= ~ % %= * *= !~ ! != | || ^ ^= ** **= ? } ] ) ; - -= " +
"BEGIN break continue delete do else END exit " +
"for function getline if in next nextfile print printf return while " +
"atan2 close cos exp fflush gsub index int length log match rand " +
"sin split sprintf sqrt srand sub substr system tolower toupper " +
"x \"str\\n\" 1234\n" +
"` ."
strs := make([]string, 0, LAST+1)
seen := make([]bool, LAST+1)
l := NewLexer([]byte(input))
for {
_, tok, _ := l.Scan()
strs = append(strs, tok.String())
seen[int(tok)] = true
if tok == EOF {
break
}
}
output := strings.Join(strs, " ")
expected := "<newline> " +
"+ += && = : , -- / <newline> /= $ @ == >= > >> ++ { [ < ( <newline> " +
"<= ~ % %= * *= !~ ! != | || ^ ^= ^ ^= ? } ] ) ; - -= " +
"BEGIN break continue delete do else END exit " +
"for function getline if in next nextfile print printf return while " +
"atan2 close cos exp fflush gsub index int length log match rand " +
"sin split sprintf sqrt srand sub substr system tolower toupper " +
"name string number <newline> " +
"<illegal> <illegal> EOF"
if output != expected {
t.Errorf("expected %q, got %q", expected, output)
}
for i, s := range seen {
if !s && Token(i) != CONCAT && Token(i) != REGEX {
t.Errorf("token %s (%d) not seen", Token(i), i)
}
}
l = NewLexer([]byte(`/foo/`))
_, tok1, _ := l.Scan()
_, tok2, val := l.ScanRegex()
if tok1 != DIV || tok2 != REGEX || val != "foo" {
t.Errorf(`expected / regex "foo", got %s %s %q`, tok1, tok2, val)
}
l = NewLexer([]byte(`/=foo/`))
_, tok1, _ = l.Scan()
_, tok2, val = l.ScanRegex()
if tok1 != DIV_ASSIGN || tok2 != REGEX || val != "=foo" {
t.Errorf(`expected /= regex "=foo", got %s %s %q`, tok1, tok2, val)
}
}
func TestUnescape(t *testing.T) {
tests := []struct {
input string
output string
error string
}{
{``, "", ""},
{`foo bar`, "foo bar", ""},
{`foo\tbar`, "foo\tbar", ""},
{"foo\nbar", "", "can't have newline in string"},
{`foo"`, "foo\"", ""},
{`O'Connor`, "O'Connor", ""},
{`foo\`, "foo\\", ""},
// Other cases tested in TestLexer string handling.
}
for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
got, err := Unescape(test.input)
if err != nil {
if err.Error() != test.error {
t.Fatalf("expected error %q, got %q", test.error, err)
}
} else {
if test.error != "" {
t.Fatalf("expected error %q, got %q", test.error, "")
}
if got != test.output {
t.Fatalf("expected %q, got %q", test.output, got)
}
}
})
}
}
func benchmarkLexer(b *testing.B, repeat int, source string) {
fullSource := []byte(strings.Repeat(source+"\n", repeat))
b.ResetTimer()
for i := 0; i < b.N; i++ {
l := NewLexer(fullSource)
for {
_, tok, _ := l.Scan()
if tok == EOF || tok == ILLEGAL {
break
}
}
}
}
func BenchmarkProgram(b *testing.B) {
benchmarkLexer(b, 5, `{ print $1, ($3+$4)*$5 }`)
}
func BenchmarkNames(b *testing.B) {
benchmarkLexer(b, 5, `x y i foobar abcdefghij0123456789 _`)
}
func BenchmarkKeywords(b *testing.B) {
benchmarkLexer(b, 5, `BEGIN END print sub if length`)
}
func BenchmarkSimpleTokens(b *testing.B) {
benchmarkLexer(b, 5, "\n : , { [ ( } ] ) ~ ? ; $")
}
func BenchmarkChoiceTokens(b *testing.B) {
benchmarkLexer(b, 5, `/ /= % %= + ++ += * ** **= *= = == ^ ^= ! != !~ < <= > >= >> && | ||`)
}
func BenchmarkNumbers(b *testing.B) {
benchmarkLexer(b, 5, `0 1 .5 1234 1234567890 1234.56789e-50`)
}
func BenchmarkStrings(b *testing.B) {
benchmarkLexer(b, 5, `"x" "y" "xyz" "foo" "foo bar baz" "foo\tbar\rbaz\n"`)
}
func BenchmarkRegex(b *testing.B) {
source := `/x/ /./ /foo/ /bar/ /=equals=/ /\/\/\/\//`
fullSource := []byte(strings.Repeat(source+" ", 5))
b.ResetTimer()
for i := 0; i < b.N; i++ {
l := NewLexer(fullSource)
for {
_, tok, _ := l.Scan()
if tok == EOF {
break
}
if tok != DIV && tok != DIV_ASSIGN {
b.Fatalf("expected / or /=, got %s", tok)
}
_, tok, _ = l.ScanRegex()
if tok != REGEX {
b.Fatalf("expected regex, got %s", tok)
}
}
}
}
func Example() {
lexer := NewLexer([]byte(`$0 { print $1 }`))
for {
pos, tok, val := lexer.Scan()
if tok == EOF {
break
}
fmt.Printf("%d:%d %s %q\n", pos.Line, pos.Column, tok, val)
}
// Output:
// 1:1 $ ""
// 1:2 number "0"
// 1:4 { ""
// 1:6 print ""
// 1:12 $ ""
// 1:13 number "1"
// 1:15 } ""
}
|