| 1 |
// Copyright (c) 2016, Daniel Martà <[email protected]> |
| 2 |
// See LICENSE for licensing information |
| 3 |
|
| 4 |
package syntax |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"os" |
| 11 |
"os/exec" |
| 12 |
"reflect" |
| 13 |
"regexp" |
| 14 |
"runtime" |
| 15 |
"slices" |
| 16 |
"strings" |
| 17 |
"sync" |
| 18 |
"testing" |
| 19 |
"time" |
| 20 |
|
| 21 |
"github.com/go-quicktest/qt" |
| 22 |
"github.com/google/go-cmp/cmp" |
| 23 |
"mvdan.cc/sh/v3/internal" |
| 24 |
) |
| 25 |
|
| 26 |
func TestParseFiles(t *testing.T) { |
| 27 |
t.Parallel() |
| 28 |
for lang := range langResolvedVariants.bits() { |
| 29 |
t.Run(lang.String(), func(t *testing.T) { |
| 30 |
p := NewParser(Variant(lang)) |
| 31 |
for i, c := range append(fileTests, fileTestsNoPrint...) { |
| 32 |
want := c.byLangIndex[lang.index()] |
| 33 |
switch want := want.(type) { |
| 34 |
case nil: |
| 35 |
continue |
| 36 |
case *File: |
| 37 |
for j, in := range c.inputs { |
| 38 |
t.Run(fmt.Sprintf("OK/%03d-%d", i, j), singleParse(p, in, want)) |
| 39 |
} |
| 40 |
case string: |
| 41 |
want = strings.Replace(want, "LANG", p.lang.String(), 1) |
| 42 |
for j, in := range c.inputs { |
| 43 |
t.Run(fmt.Sprintf("Err/%03d-%d", i, j), func(t *testing.T) { |
| 44 |
t.Logf("input: %s", in) |
| 45 |
_, err := p.Parse(newStrictReader(in), "") |
| 46 |
if err == nil { |
| 47 |
t.Fatalf("Expected error: %v", want) |
| 48 |
} |
| 49 |
if got := err.Error(); got != want { |
| 50 |
t.Fatalf("Error mismatch\nwant: %s\ngot: %s", |
| 51 |
want, got) |
| 52 |
} |
| 53 |
}) |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
}) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
func TestParseErr(t *testing.T) { |
| 62 |
t.Parallel() |
| 63 |
for lang := range langResolvedVariants.bits() { |
| 64 |
t.Run(lang.String(), func(t *testing.T) { |
| 65 |
p := NewParser(Variant(lang), KeepComments(true)) |
| 66 |
for _, c := range errorCases { |
| 67 |
want := c.byLangIndex[lang.index()] |
| 68 |
if want == "" { |
| 69 |
continue |
| 70 |
} |
| 71 |
t.Run("", func(t *testing.T) { // number them #001, #002, ... |
| 72 |
want = strings.Replace(want, "LANG", p.lang.String(), 1) |
| 73 |
t.Logf("input: %s", c.in) |
| 74 |
_, err := p.Parse(newStrictReader(c.in), "") |
| 75 |
if err == nil { |
| 76 |
t.Fatalf("Expected error: %v", want) |
| 77 |
} |
| 78 |
if got := err.Error(); got != want { |
| 79 |
t.Fatalf("Error mismatch\nwant: %s\ngot: %s", |
| 80 |
want, got) |
| 81 |
} |
| 82 |
}) |
| 83 |
} |
| 84 |
}) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
func TestParseConfirm(t *testing.T) { |
| 89 |
if testing.Short() { |
| 90 |
t.Skip("calling external shells is slow") |
| 91 |
} |
| 92 |
for lang := range langResolvedVariants.bits() { |
| 93 |
t.Run(lang.String(), func(t *testing.T) { |
| 94 |
external, ok := externalShells[lang] |
| 95 |
if !ok { |
| 96 |
t.Skip("no external shell to check against") |
| 97 |
} |
| 98 |
if external.require != nil { |
| 99 |
external.require(t) |
| 100 |
} |
| 101 |
for i, c := range append(fileTests, fileTestsNoPrint...) { |
| 102 |
want := c.byLangIndex[lang.index()] |
| 103 |
switch want.(type) { |
| 104 |
case nil: |
| 105 |
continue |
| 106 |
case *File: |
| 107 |
for j, in := range c.inputs { |
| 108 |
wantErr := lang.in(c.flipConfirmSet) |
| 109 |
t.Run(fmt.Sprintf("OK/%03d-%d", i, j), confirmParse(in, external.cmd, wantErr)) |
| 110 |
} |
| 111 |
case string: |
| 112 |
for j, in := range c.inputs { |
| 113 |
wantErr := !lang.in(c.flipConfirmSet) |
| 114 |
t.Run(fmt.Sprintf("Err/%03d-%d", i, j), confirmParse(in, external.cmd, wantErr)) |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
if lang == LangZsh { |
| 119 |
return // TODO: we don't confirm errors with zsh yet |
| 120 |
} |
| 121 |
for i, c := range errorCases { |
| 122 |
want := c.byLangIndex[lang.index()] |
| 123 |
if want == "" { |
| 124 |
continue |
| 125 |
} |
| 126 |
wantErr := !lang.in(c.flipConfirmSet) |
| 127 |
t.Run(fmt.Sprintf("ErrOld/%03d", i), confirmParse(c.in, external.cmd, wantErr)) |
| 128 |
} |
| 129 |
}) |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
func TestParseBashKeepComments(t *testing.T) { |
| 134 |
t.Parallel() |
| 135 |
p := NewParser(KeepComments(true)) |
| 136 |
for i, c := range fileTestsKeepComments { |
| 137 |
want, _ := c.byLangIndex[LangBash.index()].(*File) |
| 138 |
if want == nil { |
| 139 |
continue |
| 140 |
} |
| 141 |
for j, in := range c.inputs { |
| 142 |
t.Run(fmt.Sprintf("%03d-%d", i, j), singleParse(p, in, want)) |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func TestParsePosOverflow(t *testing.T) { |
| 148 |
t.Parallel() |
| 149 |
|
| 150 |
// Consider using a custom reader to save memory. |
| 151 |
tests := []struct { |
| 152 |
name, in, want string |
| 153 |
}{ |
| 154 |
{ |
| 155 |
"LineOverflowIsValid", |
| 156 |
strings.Repeat("\n", lineMax) + "foo; bar", |
| 157 |
"<nil>", |
| 158 |
}, |
| 159 |
{ |
| 160 |
"LineOverflowPosString", |
| 161 |
strings.Repeat("\n", lineMax) + ")", |
| 162 |
"?:1: `)` can only be used to close a subshell", |
| 163 |
}, |
| 164 |
{ |
| 165 |
"LineOverflowExtraPosString", |
| 166 |
strings.Repeat("\n", lineMax+5) + ")", |
| 167 |
"?:1: `)` can only be used to close a subshell", |
| 168 |
}, |
| 169 |
{ |
| 170 |
"ColOverflowPosString", |
| 171 |
strings.Repeat(" ", colMax) + ")", |
| 172 |
"1:?: `)` can only be used to close a subshell", |
| 173 |
}, |
| 174 |
{ |
| 175 |
"ColOverflowExtraPosString", |
| 176 |
strings.Repeat(" ", colMax) + ")", |
| 177 |
"1:?: `)` can only be used to close a subshell", |
| 178 |
}, |
| 179 |
{ |
| 180 |
"ColOverflowSkippedPosString", |
| 181 |
strings.Repeat(" ", colMax+5) + "\n)", |
| 182 |
"2:1: `)` can only be used to close a subshell", |
| 183 |
}, |
| 184 |
{ |
| 185 |
"LargestLineNumber", |
| 186 |
strings.Repeat("\n", lineMax-1) + ")", |
| 187 |
"262143:1: `)` can only be used to close a subshell", |
| 188 |
}, |
| 189 |
{ |
| 190 |
"LargestColNumber", |
| 191 |
strings.Repeat(" ", colMax-1) + ")", |
| 192 |
"1:16383: `)` can only be used to close a subshell", |
| 193 |
}, |
| 194 |
} |
| 195 |
for _, test := range tests { |
| 196 |
t.Run(test.name, func(t *testing.T) { |
| 197 |
t.Parallel() |
| 198 |
|
| 199 |
p := NewParser() |
| 200 |
_, err := p.Parse(strings.NewReader(test.in), "") |
| 201 |
got := fmt.Sprint(err) |
| 202 |
if got != test.want { |
| 203 |
t.Fatalf("want error %q, got %q", test.want, got) |
| 204 |
} |
| 205 |
}) |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
func TestMain(m *testing.M) { |
| 210 |
internal.TestMainSetup() |
| 211 |
m.Run() |
| 212 |
} |
| 213 |
|
| 214 |
var ( |
| 215 |
onceHasBash53 = sync.OnceValue(func() bool { |
| 216 |
return cmdContains("version 5.3", "bash", "--version") |
| 217 |
}) |
| 218 |
|
| 219 |
onceHasDash059 = sync.OnceValue(func() bool { |
| 220 |
// dash provides no way to check its version, so we have to |
| 221 |
// check if it's new enough as to not have the bug that breaks |
| 222 |
// our integration tests. |
| 223 |
// This also means our check does not require a specific version. |
| 224 |
// |
| 225 |
// We get odd failures on Windows on CI, and it's hard to debug |
| 226 |
// or even understand what version of dash it's using; skip on those. |
| 227 |
return cmdContains("Bad subst", "dash", "-c", "echo ${#<}") && |
| 228 |
runtime.GOOS != "windows" |
| 229 |
}) |
| 230 |
|
| 231 |
onceHasMksh59 = sync.OnceValue(func() bool { |
| 232 |
return cmdContains(" R59 ", "mksh", "-c", "echo $KSH_VERSION") |
| 233 |
}) |
| 234 |
|
| 235 |
onceHasZsh59 = sync.OnceValue(func() bool { |
| 236 |
return cmdContains("zsh 5.9", "zsh", "--version") |
| 237 |
}) |
| 238 |
) |
| 239 |
|
| 240 |
type externalShell struct { |
| 241 |
cmd string |
| 242 |
require func(testing.TB) |
| 243 |
} |
| 244 |
|
| 245 |
// requireShells can be set to make sure that no external shell tests |
| 246 |
// are being skipped due to a misalignment in installed versions. |
| 247 |
var requireShells = os.Getenv("REQUIRE_SHELLS") == "1" |
| 248 |
|
| 249 |
func skipExternal(tb testing.TB, message string) { |
| 250 |
if requireShells { |
| 251 |
tb.Fatal(message) |
| 252 |
} else { |
| 253 |
tb.Skip(message) |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
// Note that externalShells is a map, and not an array, |
| 258 |
// because [LangVariant.index] is not a constant expression. |
| 259 |
// This seems fine; this table is only for the sake of testing. |
| 260 |
var externalShells = map[LangVariant]externalShell{ |
| 261 |
LangBash: {"bash", func(tb testing.TB) { |
| 262 |
if !onceHasBash53() { |
| 263 |
skipExternal(tb, "bash 5.2 required to run") |
| 264 |
} |
| 265 |
}}, |
| 266 |
LangPOSIX: {"dash", func(tb testing.TB) { |
| 267 |
if !onceHasDash059() { |
| 268 |
skipExternal(tb, "dash 0.5.9+ required to run") |
| 269 |
} |
| 270 |
}}, |
| 271 |
LangMirBSDKorn: {"mksh", func(tb testing.TB) { |
| 272 |
if !onceHasMksh59() { |
| 273 |
skipExternal(tb, "mksh 59 required to run") |
| 274 |
} |
| 275 |
}}, |
| 276 |
LangZsh: {"zsh", func(tb testing.TB) { |
| 277 |
if !onceHasZsh59() { |
| 278 |
skipExternal(tb, "zsh 5.9 required to run") |
| 279 |
} |
| 280 |
}}, |
| 281 |
} |
| 282 |
|
| 283 |
func cmdContains(substr, cmd string, args ...string) bool { |
| 284 |
out, err := exec.Command(cmd, args...).CombinedOutput() |
| 285 |
got := string(out) |
| 286 |
if err != nil { |
| 287 |
got += "\n" + err.Error() |
| 288 |
} |
| 289 |
return strings.Contains(got, substr) |
| 290 |
} |
| 291 |
|
| 292 |
var extGlobRe = regexp.MustCompile(`[@?*+!]\(`) |
| 293 |
|
| 294 |
func confirmParse(in, cmd string, wantErr bool) func(*testing.T) { |
| 295 |
return func(t *testing.T) { |
| 296 |
t.Helper() |
| 297 |
t.Parallel() |
| 298 |
t.Logf("input: %s", in) |
| 299 |
var opts []string |
| 300 |
if strings.Contains(in, "\\\r\n") { |
| 301 |
t.Skip("shells do not generally support CRLF line endings") |
| 302 |
} |
| 303 |
if cmd == "bash" && extGlobRe.MatchString(in) { |
| 304 |
// otherwise bash refuses to parse these |
| 305 |
// properly. Also avoid -n since that too makes |
| 306 |
// bash bail. |
| 307 |
in = "shopt -s extglob\n" + in |
| 308 |
} else if !wantErr { |
| 309 |
// -n makes bash accept invalid inputs like |
| 310 |
// "let" or "`{`", so only use it in |
| 311 |
// non-erroring tests. Should be safe to not use |
| 312 |
// -n anyway since these are supposed to just fail. |
| 313 |
// also, -n will break if we are using extglob |
| 314 |
// as extglob is not actually applied. |
| 315 |
opts = append(opts, "-n") |
| 316 |
} |
| 317 |
|
| 318 |
// All the bits of shell we test should either finish or fail very quickly, |
| 319 |
// given that they are very small. If we make a mistake with an endless loop, |
| 320 |
// or we somehow trigger a bug that makes a shell hang, kill it. |
| 321 |
ctx, cancel := context.WithTimeout(t.Context(), 2*time.Second) |
| 322 |
defer cancel() |
| 323 |
|
| 324 |
cmd := exec.CommandContext(ctx, cmd, opts...) |
| 325 |
killCommandOnTestExit(cmd) |
| 326 |
cmd.Dir = t.TempDir() // to be safe |
| 327 |
cmd.Stdin = strings.NewReader(in) |
| 328 |
var stderrBuf strings.Builder |
| 329 |
cmd.Stderr = &stderrBuf |
| 330 |
err := cmd.Run() |
| 331 |
|
| 332 |
if cmd.ProcessState.ExitCode() == -1 { |
| 333 |
t.Fatalf("shell terminated by signal: %v", err) |
| 334 |
} |
| 335 |
|
| 336 |
// bash sometimes likes to error on an input via stderr |
| 337 |
// while forgetting to set the exit code to non-zero. Fun. |
| 338 |
// Note that we do not treat warnings as errors. |
| 339 |
stderrLines := strings.Split(stderrBuf.String(), "\n") |
| 340 |
for i, line := range stderrLines { |
| 341 |
stderrLines[i] = strings.TrimSpace(line) |
| 342 |
} |
| 343 |
stderrLines = slices.DeleteFunc(stderrLines, func(line string) bool { |
| 344 |
return line == "" || strings.Contains(line, "warning:") |
| 345 |
}) |
| 346 |
if stderr := strings.Join(stderrLines, "\n"); stderr != "" { |
| 347 |
if err == nil { |
| 348 |
err = fmt.Errorf("non-fatal error: %s", stderr) |
| 349 |
} else { |
| 350 |
err = fmt.Errorf("%v: %s", err, stderr) |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
if wantErr && err == nil { |
| 355 |
t.Fatalf("Expected error in %q", strings.Join(cmd.Args, " ")) |
| 356 |
} else if !wantErr && err != nil { |
| 357 |
t.Fatalf("Unexpected error in %q: %v", strings.Join(cmd.Args, " "), err) |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
var cmpOpt = cmp.FilterValues(func(p1, p2 Pos) bool { return true }, cmp.Ignore()) |
| 363 |
|
| 364 |
func singleParse(p *Parser, in string, want *File) func(t *testing.T) { |
| 365 |
return func(t *testing.T) { |
| 366 |
t.Helper() |
| 367 |
t.Logf("input: %s", in) |
| 368 |
got, err := p.Parse(newStrictReader(in), "") |
| 369 |
if err != nil { |
| 370 |
t.Fatalf("Unexpected error: %v", err) |
| 371 |
} |
| 372 |
Walk(got, sanityChecker{tb: t, src: in}.visit) |
| 373 |
qt.Assert(t, qt.CmpEquals(got, want, cmpOpt)) |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
type errorCase struct { |
| 378 |
in string |
| 379 |
|
| 380 |
byLangIndex [langResolvedVariantsCount]string |
| 381 |
|
| 382 |
// The real shells where testing the input succeeds rather than failing as expected. |
| 383 |
flipConfirmSet LangVariant |
| 384 |
} |
| 385 |
|
| 386 |
func errCase(in string, opts ...func(*errorCase)) errorCase { |
| 387 |
c := errorCase{in: in} |
| 388 |
for _, o := range opts { |
| 389 |
o(&c) |
| 390 |
} |
| 391 |
return c |
| 392 |
} |
| 393 |
|
| 394 |
func langErr(want string, langSets ...LangVariant) func(*errorCase) { |
| 395 |
return func(c *errorCase) { |
| 396 |
// The parameter is a slice to allow omitting the argument. |
| 397 |
switch len(langSets) { |
| 398 |
case 0: |
| 399 |
for i := range c.byLangIndex { |
| 400 |
c.byLangIndex[i] = want |
| 401 |
} |
| 402 |
return |
| 403 |
case 1: |
| 404 |
// continue below |
| 405 |
default: |
| 406 |
panic("use a LangVariant bitset") |
| 407 |
} |
| 408 |
for lang := range langSets[0].bits() { |
| 409 |
c.byLangIndex[lang.index()] = want |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
func flipConfirm(langSet LangVariant) func(*errorCase) { |
| 415 |
return func(c *errorCase) { c.flipConfirmSet = langSet } |
| 416 |
} |
| 417 |
|
| 418 |
var flipConfirmAll = flipConfirm(langResolvedVariants) |
| 419 |
|
| 420 |
// The real shells which allow unclosed heredocs. |
| 421 |
// TODO: allow ending a heredoc at EOF in these language variant modes. |
| 422 |
var flipConfirmUnclosedHeredoc = flipConfirm(LangBash | LangPOSIX | LangBats | LangZsh) |
| 423 |
|
| 424 |
func init() { |
| 425 |
seenInputs := make(map[string]bool) |
| 426 |
for i, c := range errorCases { |
| 427 |
if seenInputs[c.in] { |
| 428 |
panic(fmt.Sprintf("duplicate at %d: %q", i, c.in)) |
| 429 |
} |
| 430 |
seenInputs[c.in] = true |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
var errorCases = []errorCase{ |
| 435 |
errCase( |
| 436 |
"echo \x80", |
| 437 |
langErr("1:6: invalid UTF-8 encoding"), |
| 438 |
flipConfirmAll, // common shells use bytes |
| 439 |
), |
| 440 |
errCase( |
| 441 |
"\necho \x80", |
| 442 |
langErr("2:6: invalid UTF-8 encoding"), |
| 443 |
flipConfirmAll, // common shells use bytes |
| 444 |
), |
| 445 |
errCase( |
| 446 |
"echo foo\x80bar", |
| 447 |
langErr("1:9: invalid UTF-8 encoding"), |
| 448 |
flipConfirmAll, // common shells use bytes |
| 449 |
), |
| 450 |
errCase( |
| 451 |
"echo foo\xc3", |
| 452 |
langErr("1:9: invalid UTF-8 encoding"), |
| 453 |
flipConfirmAll, // common shells use bytes |
| 454 |
), |
| 455 |
errCase( |
| 456 |
"#foo\xc3", |
| 457 |
langErr("1:5: invalid UTF-8 encoding"), |
| 458 |
flipConfirmAll, // common shells use bytes |
| 459 |
), |
| 460 |
errCase( |
| 461 |
"echo a\x80", |
| 462 |
langErr("1:7: invalid UTF-8 encoding"), |
| 463 |
flipConfirmAll, // common shells use bytes |
| 464 |
), |
| 465 |
errCase( |
| 466 |
"<<$\xc8\n$\xc8", |
| 467 |
langErr("1:4: invalid UTF-8 encoding"), |
| 468 |
flipConfirmAll, // common shells use bytes |
| 469 |
), |
| 470 |
errCase( |
| 471 |
"echo $((foo\x80bar", |
| 472 |
langErr("1:12: invalid UTF-8 encoding"), |
| 473 |
), |
| 474 |
errCase( |
| 475 |
"z=($\\\n#\\\n\\\n$#\x91\\\n", |
| 476 |
langErr("4:3: invalid UTF-8 encoding", LangBash), |
| 477 |
), |
| 478 |
errCase( |
| 479 |
`${ `, |
| 480 |
langErr("1:1: reached EOF without matching `${` with `}`", LangMirBSDKorn), |
| 481 |
), |
| 482 |
errCase( |
| 483 |
`${ foo;`, |
| 484 |
langErr("1:1: reached EOF without matching `${` with `}`", LangMirBSDKorn), |
| 485 |
), |
| 486 |
errCase( |
| 487 |
`${ foo }`, |
| 488 |
langErr("1:1: reached EOF without matching `${` with `}`", LangMirBSDKorn), |
| 489 |
), |
| 490 |
errCase( |
| 491 |
`${|`, |
| 492 |
langErr("1:1: reached EOF without matching `${` with `}`", LangMirBSDKorn), |
| 493 |
), |
| 494 |
errCase( |
| 495 |
`${|foo;`, |
| 496 |
langErr("1:1: reached EOF without matching `${` with `}`", LangMirBSDKorn), |
| 497 |
), |
| 498 |
errCase( |
| 499 |
`${|foo }`, |
| 500 |
langErr("1:1: reached EOF without matching `${` with `}`", LangMirBSDKorn), |
| 501 |
), |
| 502 |
errCase( |
| 503 |
`${ { foo; }bar; }`, |
| 504 |
langErr("1:12: statements must be separated by &, ; or a newline", LangBash|LangMirBSDKorn), |
| 505 |
), |
| 506 |
errCase( |
| 507 |
"((foo\x80bar", |
| 508 |
langErr("1:6: invalid UTF-8 encoding"), |
| 509 |
), |
| 510 |
errCase( |
| 511 |
";\x80", |
| 512 |
langErr("1:2: invalid UTF-8 encoding"), |
| 513 |
), |
| 514 |
errCase( |
| 515 |
"${a\x80", |
| 516 |
langErr("1:4: invalid UTF-8 encoding"), |
| 517 |
), |
| 518 |
errCase( |
| 519 |
"${a#\x80", |
| 520 |
langErr("1:5: invalid UTF-8 encoding"), |
| 521 |
), |
| 522 |
errCase( |
| 523 |
"${a-'\x80", |
| 524 |
langErr("1:6: invalid UTF-8 encoding"), |
| 525 |
), |
| 526 |
errCase( |
| 527 |
"echo $((a |\x80", |
| 528 |
langErr("1:12: invalid UTF-8 encoding"), |
| 529 |
), |
| 530 |
errCase( |
| 531 |
"!", |
| 532 |
langErr("1:1: `!` cannot form a statement alone"), |
| 533 |
), |
| 534 |
errCase( |
| 535 |
"! !", |
| 536 |
langErr("1:1: cannot negate a command multiple times"), |
| 537 |
flipConfirm(LangBash), // bash allows lone `!`, unlike dash, mksh, and us. |
| 538 |
), |
| 539 |
errCase( |
| 540 |
"! ! foo", |
| 541 |
langErr("1:1: cannot negate a command multiple times"), |
| 542 |
flipConfirm(LangBash|LangMirBSDKorn), // bash allows lone `!`, unlike dash, mksh, and us. |
| 543 |
), |
| 544 |
errCase( |
| 545 |
"}", |
| 546 |
langErr("1:1: `}` can only be used to close a block"), |
| 547 |
), |
| 548 |
errCase( |
| 549 |
"foo | }", |
| 550 |
langErr("1:7: `}` can only be used to close a block"), |
| 551 |
), |
| 552 |
errCase( |
| 553 |
"foo }", |
| 554 |
langErr("1:5: `}` can only be used to close a block", LangZsh), |
| 555 |
), |
| 556 |
errCase( |
| 557 |
"then", |
| 558 |
langErr("1:1: `then` can only be used in an `if`"), |
| 559 |
), |
| 560 |
errCase( |
| 561 |
"elif", |
| 562 |
langErr("1:1: `elif` can only be used in an `if`"), |
| 563 |
), |
| 564 |
errCase( |
| 565 |
"fi", |
| 566 |
langErr("1:1: `fi` can only be used to end an `if`"), |
| 567 |
), |
| 568 |
errCase( |
| 569 |
"do", |
| 570 |
langErr("1:1: `do` can only be used in a loop"), |
| 571 |
), |
| 572 |
errCase( |
| 573 |
"done", |
| 574 |
langErr("1:1: `done` can only be used to end a loop"), |
| 575 |
), |
| 576 |
errCase( |
| 577 |
"esac", |
| 578 |
langErr("1:1: `esac` can only be used to end a `case`"), |
| 579 |
), |
| 580 |
errCase( |
| 581 |
"a=b { foo; }", |
| 582 |
langErr("1:12: `}` can only be used to close a block"), |
| 583 |
), |
| 584 |
errCase( |
| 585 |
"a=b foo() { bar; }", |
| 586 |
langErr("1:8: a command can only contain words and redirects; encountered `(`"), |
| 587 |
), |
| 588 |
errCase( |
| 589 |
"a=b if foo; then bar; fi", |
| 590 |
langErr("1:13: `then` can only be used in an `if`"), |
| 591 |
), |
| 592 |
errCase( |
| 593 |
">f { foo; }", |
| 594 |
langErr("1:1: redirects before compound commands are a zsh feature; tried parsing as LANG"), |
| 595 |
langErr("", LangZsh), |
| 596 |
), |
| 597 |
errCase( |
| 598 |
">f foo() { bar; }", |
| 599 |
langErr("1:1: redirects before compound commands are a zsh feature; tried parsing as LANG"), |
| 600 |
langErr("", LangZsh), |
| 601 |
), |
| 602 |
errCase( |
| 603 |
">f if foo; then bar; fi", |
| 604 |
langErr("1:1: redirects before compound commands are a zsh feature; tried parsing as LANG"), |
| 605 |
langErr("", LangZsh), |
| 606 |
), |
| 607 |
errCase( |
| 608 |
"if done; then b; fi", |
| 609 |
langErr("1:4: `done` can only be used to end a loop"), |
| 610 |
), |
| 611 |
errCase( |
| 612 |
"'", |
| 613 |
langErr("1:1: reached EOF without closing quote `'`"), |
| 614 |
), |
| 615 |
errCase( |
| 616 |
`"`, |
| 617 |
langErr("1:1: reached EOF without closing quote `\"`"), |
| 618 |
), |
| 619 |
errCase( |
| 620 |
`'\''`, |
| 621 |
langErr("1:4: reached EOF without closing quote `'`"), |
| 622 |
), |
| 623 |
errCase( |
| 624 |
";", |
| 625 |
langErr("1:1: `;` can only immediately follow a statement"), |
| 626 |
), |
| 627 |
errCase( |
| 628 |
"{ ; }", |
| 629 |
langErr("1:1: `{` must be followed by a statement list"), |
| 630 |
langErr("", LangZsh|LangMirBSDKorn), |
| 631 |
), |
| 632 |
errCase( |
| 633 |
`"foo"(){ :; }`, |
| 634 |
langErr("1:1: invalid func name"), |
| 635 |
flipConfirm(LangMirBSDKorn), // TODO: support non-literal func names |
| 636 |
), |
| 637 |
errCase( |
| 638 |
`foo$bar(){ :; }`, |
| 639 |
langErr("1:1: invalid func name"), |
| 640 |
), |
| 641 |
errCase( |
| 642 |
"{", |
| 643 |
langErr("1:1: `{` must be followed by a statement list"), |
| 644 |
langErr("1:1: reached EOF without matching `{` with `}`", LangZsh|LangMirBSDKorn), |
| 645 |
), |
| 646 |
errCase( |
| 647 |
"{ foo;", |
| 648 |
langErr("1:1: reached EOF without matching `{` with `}`"), |
| 649 |
), |
| 650 |
errCase( |
| 651 |
"{ foo; #}", |
| 652 |
langErr("1:1: reached EOF without matching `{` with `}`"), |
| 653 |
), |
| 654 |
errCase( |
| 655 |
"(x", |
| 656 |
langErr("1:1: reached EOF without matching `(` with `)`"), |
| 657 |
), |
| 658 |
errCase( |
| 659 |
")", |
| 660 |
langErr("1:1: `)` can only be used to close a subshell"), |
| 661 |
), |
| 662 |
errCase( |
| 663 |
"`", |
| 664 |
langErr("1:1: reached EOF without closing quote \"`\""), |
| 665 |
), |
| 666 |
errCase( |
| 667 |
";;", |
| 668 |
langErr("1:1: `;;` can only be used in a case clause"), |
| 669 |
), |
| 670 |
errCase( |
| 671 |
"( foo;", |
| 672 |
langErr("1:1: reached EOF without matching `(` with `)`"), |
| 673 |
), |
| 674 |
errCase( |
| 675 |
"&", |
| 676 |
langErr("1:1: `&` can only immediately follow a statement"), |
| 677 |
), |
| 678 |
errCase( |
| 679 |
"|", |
| 680 |
langErr("1:1: `|` can only immediately follow a statement"), |
| 681 |
), |
| 682 |
errCase( |
| 683 |
"&&", |
| 684 |
langErr("1:1: `&&` can only immediately follow a statement"), |
| 685 |
), |
| 686 |
errCase( |
| 687 |
"||", |
| 688 |
langErr("1:1: `||` can only immediately follow a statement"), |
| 689 |
), |
| 690 |
errCase( |
| 691 |
"foo; || bar", |
| 692 |
langErr("1:6: `||` can only immediately follow a statement"), |
| 693 |
), |
| 694 |
errCase( |
| 695 |
"echo & || bar", |
| 696 |
langErr("1:8: `||` can only immediately follow a statement"), |
| 697 |
), |
| 698 |
errCase( |
| 699 |
"echo & ; bar", |
| 700 |
langErr("1:8: `;` can only immediately follow a statement"), |
| 701 |
), |
| 702 |
errCase( |
| 703 |
"foo;;", |
| 704 |
langErr("1:4: `;;` can only be used in a case clause"), |
| 705 |
), |
| 706 |
errCase( |
| 707 |
"foo(", |
| 708 |
langErr("1:1: `foo(` must be followed by `)`", LangPOSIX|LangBash|LangMirBSDKorn|LangBats), |
| 709 |
langErr("1:4: reached EOF without matching `(` with `)`", LangZsh), |
| 710 |
), |
| 711 |
errCase( |
| 712 |
"foo(bar", |
| 713 |
langErr("1:1: `foo(` must be followed by `)`", LangPOSIX|LangBash|LangMirBSDKorn|LangBats), |
| 714 |
langErr("1:4: reached EOF without matching `(` with `)`", LangZsh), |
| 715 |
), |
| 716 |
errCase( |
| 717 |
"Ã (", |
| 718 |
langErr("1:1: `foo(` must be followed by `)`", LangPOSIX|LangBash|LangMirBSDKorn|LangBats), |
| 719 |
langErr("1:3: reached EOF without matching `(` with `)`", LangZsh), |
| 720 |
), |
| 721 |
errCase( |
| 722 |
"foo'", |
| 723 |
langErr("1:4: reached EOF without closing quote `'`"), |
| 724 |
), |
| 725 |
errCase( |
| 726 |
`foo"`, |
| 727 |
langErr("1:4: reached EOF without closing quote `\"`"), |
| 728 |
), |
| 729 |
errCase( |
| 730 |
`"foo`, |
| 731 |
langErr("1:1: reached EOF without closing quote `\"`"), |
| 732 |
), |
| 733 |
errCase( |
| 734 |
`"foobar\`, |
| 735 |
langErr("1:1: reached EOF without closing quote `\"`"), |
| 736 |
), |
| 737 |
errCase( |
| 738 |
`"foo\a`, |
| 739 |
langErr("1:1: reached EOF without closing quote `\"`"), |
| 740 |
), |
| 741 |
errCase( |
| 742 |
"foo()", |
| 743 |
langErr("1:1: `foo()` must be followed by a statement"), |
| 744 |
flipConfirm(LangMirBSDKorn), // TODO: some variants allow a missing body |
| 745 |
), |
| 746 |
errCase( |
| 747 |
"foo() {", |
| 748 |
langErr("1:7: `{` must be followed by a statement list"), |
| 749 |
langErr("1:7: reached EOF without matching `{` with `}`", LangZsh|LangMirBSDKorn), |
| 750 |
), |
| 751 |
errCase( |
| 752 |
"foo() { bar;", |
| 753 |
langErr("1:7: reached EOF without matching `{` with `}`"), |
| 754 |
), |
| 755 |
errCase( |
| 756 |
"foo-bar() { x; }", |
| 757 |
langErr("1:1: invalid func name", LangPOSIX), |
| 758 |
), |
| 759 |
errCase( |
| 760 |
"foò() { x; }", |
| 761 |
langErr("1:1: invalid func name", LangPOSIX), |
| 762 |
), |
| 763 |
errCase( |
| 764 |
"echo foo(", |
| 765 |
langErr("1:9: a command can only contain words and redirects; encountered `(`", LangPOSIX|LangBash|LangMirBSDKorn|LangBats), |
| 766 |
langErr("1:9: reached EOF without matching `(` with `)`", LangZsh), |
| 767 |
), |
| 768 |
errCase( |
| 769 |
"echo &&", |
| 770 |
langErr("1:6: `&&` must be followed by a statement"), |
| 771 |
), |
| 772 |
errCase( |
| 773 |
"echo |", |
| 774 |
langErr("1:6: `|` must be followed by a statement"), |
| 775 |
), |
| 776 |
errCase( |
| 777 |
"echo ||", |
| 778 |
langErr("1:6: `||` must be followed by a statement"), |
| 779 |
), |
| 780 |
errCase( |
| 781 |
"echo | #bar", |
| 782 |
langErr("1:6: `|` must be followed by a statement"), |
| 783 |
), |
| 784 |
errCase( |
| 785 |
"echo && #bar", |
| 786 |
langErr("1:6: `&&` must be followed by a statement"), |
| 787 |
), |
| 788 |
errCase( |
| 789 |
"`echo &&`", |
| 790 |
langErr("1:7: `&&` must be followed by a statement"), |
| 791 |
), |
| 792 |
errCase( |
| 793 |
"`echo |`", |
| 794 |
langErr("1:7: `|` must be followed by a statement"), |
| 795 |
), |
| 796 |
errCase( |
| 797 |
"echo | ! true", |
| 798 |
langErr("1:8: `!` can only be used in full statements"), |
| 799 |
), |
| 800 |
errCase( |
| 801 |
"echo >", |
| 802 |
langErr("1:6: `>` must be followed by a word"), |
| 803 |
), |
| 804 |
errCase( |
| 805 |
"echo >>", |
| 806 |
langErr("1:6: `>>` must be followed by a word"), |
| 807 |
), |
| 808 |
errCase( |
| 809 |
"echo <", |
| 810 |
langErr("1:6: `<` must be followed by a word"), |
| 811 |
), |
| 812 |
errCase( |
| 813 |
"echo 2>", |
| 814 |
langErr("1:7: `>` must be followed by a word"), |
| 815 |
), |
| 816 |
errCase( |
| 817 |
"echo <\nbar", |
| 818 |
langErr("1:6: `<` must be followed by a word"), |
| 819 |
), |
| 820 |
errCase( |
| 821 |
"echo | < #bar", |
| 822 |
langErr("1:8: `<` must be followed by a word"), |
| 823 |
), |
| 824 |
errCase( |
| 825 |
"echo && > #", |
| 826 |
langErr("1:9: `>` must be followed by a word"), |
| 827 |
), |
| 828 |
errCase( |
| 829 |
"<<", |
| 830 |
langErr("1:1: `<<` must be followed by a word"), |
| 831 |
), |
| 832 |
errCase( |
| 833 |
"<<EOF", |
| 834 |
langErr("1:1: unclosed here-document `EOF`"), |
| 835 |
flipConfirmUnclosedHeredoc, |
| 836 |
), |
| 837 |
errCase( |
| 838 |
"<<EOF\n\\", |
| 839 |
langErr("1:1: unclosed here-document `EOF`"), |
| 840 |
flipConfirmUnclosedHeredoc, |
| 841 |
), |
| 842 |
errCase( |
| 843 |
"<<EOF\n\\\n", |
| 844 |
langErr("1:1: unclosed here-document `EOF`"), |
| 845 |
flipConfirmUnclosedHeredoc, |
| 846 |
), |
| 847 |
errCase( |
| 848 |
"<<EOF\n\\\nEOF", |
| 849 |
langErr("1:1: unclosed here-document `EOF`"), |
| 850 |
flipConfirmAll, // why does mksh allow this? |
| 851 |
), |
| 852 |
errCase( |
| 853 |
"<<EOF\nfoo\\\nEOF", |
| 854 |
langErr("1:1: unclosed here-document `EOF`"), |
| 855 |
flipConfirmUnclosedHeredoc, |
| 856 |
), |
| 857 |
errCase( |
| 858 |
"<<'EOF'\n\\\n", |
| 859 |
langErr("1:1: unclosed here-document `EOF`"), |
| 860 |
flipConfirmUnclosedHeredoc, |
| 861 |
), |
| 862 |
errCase( |
| 863 |
"<<EOF <`\n#\n`\n``", |
| 864 |
langErr("1:1: unclosed here-document `EOF`"), |
| 865 |
), |
| 866 |
errCase( |
| 867 |
"<<'EOF'", |
| 868 |
langErr("1:1: unclosed here-document `EOF`"), |
| 869 |
flipConfirmUnclosedHeredoc, |
| 870 |
), |
| 871 |
errCase( |
| 872 |
"<<\\EOF", |
| 873 |
langErr("1:1: unclosed here-document `EOF`"), |
| 874 |
flipConfirmUnclosedHeredoc, |
| 875 |
), |
| 876 |
errCase( |
| 877 |
"<<\\\\EOF", |
| 878 |
langErr("1:1: unclosed here-document `\\EOF`"), |
| 879 |
flipConfirmUnclosedHeredoc, |
| 880 |
), |
| 881 |
errCase( |
| 882 |
"<<-EOF", |
| 883 |
langErr("1:1: unclosed here-document `EOF`"), |
| 884 |
flipConfirmUnclosedHeredoc, |
| 885 |
), |
| 886 |
errCase( |
| 887 |
"<<-EOF\n\t", |
| 888 |
langErr("1:1: unclosed here-document `EOF`"), |
| 889 |
flipConfirmUnclosedHeredoc, |
| 890 |
), |
| 891 |
errCase( |
| 892 |
"<<-'EOF'\n\t", |
| 893 |
langErr("1:1: unclosed here-document `EOF`"), |
| 894 |
flipConfirmUnclosedHeredoc, |
| 895 |
), |
| 896 |
errCase( |
| 897 |
"<<\nEOF\nbar\nEOF", |
| 898 |
langErr("1:1: `<<` must be followed by a word"), |
| 899 |
), |
| 900 |
errCase( |
| 901 |
"$(<<EOF\nNOTEOF)", |
| 902 |
langErr("1:3: unclosed here-document `EOF`", LangBash|LangMirBSDKorn), |
| 903 |
// Note that this fails on external shells as they treat ")" as part of the heredoc. |
| 904 |
), |
| 905 |
errCase( |
| 906 |
"`<<EOF\nNOTEOF`", |
| 907 |
langErr("1:2: unclosed here-document `EOF`", LangBash|LangMirBSDKorn), |
| 908 |
flipConfirmAll, |
| 909 |
// Note that this works on external shells as they treat "`" as outside the heredoc. |
| 910 |
), |
| 911 |
errCase( |
| 912 |
"if", |
| 913 |
langErr("1:1: `if` must be followed by a statement list"), |
| 914 |
langErr("1:1: `if <cond>` must be followed by `then`", LangZsh|LangMirBSDKorn), |
| 915 |
), |
| 916 |
errCase( |
| 917 |
"if true;", |
| 918 |
langErr("1:1: `if <cond>` must be followed by `then`"), |
| 919 |
), |
| 920 |
errCase( |
| 921 |
"if true then", |
| 922 |
langErr("1:1: `if <cond>` must be followed by `then`"), |
| 923 |
), |
| 924 |
errCase( |
| 925 |
"if true; then bar;", |
| 926 |
langErr("1:1: `if` statement must end with `fi`"), |
| 927 |
), |
| 928 |
errCase( |
| 929 |
"if true; then bar; fi#etc", |
| 930 |
langErr("1:1: `if` statement must end with `fi`"), |
| 931 |
), |
| 932 |
errCase( |
| 933 |
"if a; then b; elif c;", |
| 934 |
langErr("1:15: `elif <cond>` must be followed by `then`"), |
| 935 |
), |
| 936 |
errCase( |
| 937 |
"'foo' '", |
| 938 |
langErr("1:7: reached EOF without closing quote `'`"), |
| 939 |
), |
| 940 |
errCase( |
| 941 |
"'foo\n' '", |
| 942 |
langErr("2:3: reached EOF without closing quote `'`"), |
| 943 |
), |
| 944 |
errCase( |
| 945 |
"while", |
| 946 |
langErr("1:1: `while` must be followed by a statement list"), |
| 947 |
langErr("1:1: `while <cond>` must be followed by `do`", LangZsh|LangMirBSDKorn), |
| 948 |
), |
| 949 |
errCase( |
| 950 |
"while true;", |
| 951 |
langErr("1:1: `while <cond>` must be followed by `do`"), |
| 952 |
), |
| 953 |
errCase( |
| 954 |
"while true; do bar", |
| 955 |
langErr("1:1: `while` statement must end with `done`"), |
| 956 |
), |
| 957 |
errCase( |
| 958 |
"while true; do bar;", |
| 959 |
langErr("1:1: `while` statement must end with `done`"), |
| 960 |
), |
| 961 |
errCase( |
| 962 |
"until", |
| 963 |
langErr("1:1: `until` must be followed by a statement list"), |
| 964 |
langErr("1:1: `until <cond>` must be followed by `do`", LangZsh|LangMirBSDKorn), |
| 965 |
), |
| 966 |
errCase( |
| 967 |
"until true;", |
| 968 |
langErr("1:1: `until <cond>` must be followed by `do`"), |
| 969 |
), |
| 970 |
errCase( |
| 971 |
"until true; do bar", |
| 972 |
langErr("1:1: `until` statement must end with `done`"), |
| 973 |
), |
| 974 |
errCase( |
| 975 |
"until true; do bar;", |
| 976 |
langErr("1:1: `until` statement must end with `done`"), |
| 977 |
), |
| 978 |
errCase( |
| 979 |
"for", |
| 980 |
langErr("1:1: `for` must be followed by a literal"), |
| 981 |
), |
| 982 |
errCase( |
| 983 |
"for i", |
| 984 |
langErr("1:1: `for foo` must be followed by `in`, `do`, `;`, or a newline"), |
| 985 |
), |
| 986 |
errCase( |
| 987 |
"for i in;", |
| 988 |
langErr("1:1: `for foo [in words]` must be followed by `do`"), |
| 989 |
), |
| 990 |
errCase( |
| 991 |
"for i in 1 2 3;", |
| 992 |
langErr("1:1: `for foo [in words]` must be followed by `do`"), |
| 993 |
), |
| 994 |
errCase( |
| 995 |
"for i in 1 2 &", |
| 996 |
langErr("1:1: `for foo [in words]` must be followed by `do`"), |
| 997 |
), |
| 998 |
errCase( |
| 999 |
"for i in 1 2 (", |
| 1000 |
langErr("1:14: word list can only contain words"), |
| 1001 |
langErr("1:14: reached EOF without matching `(` with `)`", LangZsh), |
| 1002 |
), |
| 1003 |
errCase( |
| 1004 |
"for i in 1 2 3; do echo $i;", |
| 1005 |
langErr("1:1: `for` statement must end with `done`"), |
| 1006 |
), |
| 1007 |
errCase( |
| 1008 |
"for i in 1 2 3; echo $i;", |
| 1009 |
langErr("1:1: `for foo [in words]` must be followed by `do`"), |
| 1010 |
), |
| 1011 |
errCase( |
| 1012 |
"for 'i' in 1 2 3; do echo $i; done", |
| 1013 |
langErr("1:1: `for` must be followed by a literal"), |
| 1014 |
), |
| 1015 |
errCase( |
| 1016 |
"for in 1 2 3; do echo $i; done", |
| 1017 |
langErr("1:1: `for foo` must be followed by `in`, `do`, `;`, or a newline"), |
| 1018 |
), |
| 1019 |
errCase( |
| 1020 |
"select", |
| 1021 |
langErr("1:1: `select` must be followed by a literal", LangBash|LangMirBSDKorn|LangZsh), |
| 1022 |
), |
| 1023 |
errCase( |
| 1024 |
"select i", |
| 1025 |
langErr("1:1: `select foo` must be followed by `in`, `do`, `;`, or a newline", LangBash|LangMirBSDKorn|LangZsh), |
| 1026 |
), |
| 1027 |
errCase( |
| 1028 |
"select i in;", |
| 1029 |
langErr("1:1: `select foo [in words]` must be followed by `do`", LangBash|LangMirBSDKorn|LangZsh), |
| 1030 |
), |
| 1031 |
errCase( |
| 1032 |
"select i in 1 2 3;", |
| 1033 |
langErr("1:1: `select foo [in words]` must be followed by `do`", LangBash|LangMirBSDKorn|LangZsh), |
| 1034 |
), |
| 1035 |
errCase( |
| 1036 |
"select i in 1 2 3; do echo $i;", |
| 1037 |
langErr("1:1: `select` statement must end with `done`", LangBash|LangMirBSDKorn|LangZsh), |
| 1038 |
), |
| 1039 |
errCase( |
| 1040 |
"select i in 1 2 3; echo $i;", |
| 1041 |
langErr("1:1: `select foo [in words]` must be followed by `do`", LangBash|LangMirBSDKorn|LangZsh), |
| 1042 |
), |
| 1043 |
errCase( |
| 1044 |
"select 'i' in 1 2 3; do echo $i; done", |
| 1045 |
langErr("1:1: `select` must be followed by a literal", LangBash|LangMirBSDKorn|LangZsh), |
| 1046 |
), |
| 1047 |
errCase( |
| 1048 |
"select in 1 2 3; do echo $i; done", |
| 1049 |
langErr("1:1: `select foo` must be followed by `in`, `do`, `;`, or a newline", LangBash|LangMirBSDKorn|LangZsh), |
| 1050 |
), |
| 1051 |
errCase( |
| 1052 |
"echo foo &\n;", |
| 1053 |
langErr("2:1: `;` can only immediately follow a statement"), |
| 1054 |
), |
| 1055 |
errCase( |
| 1056 |
"echo $(foo", |
| 1057 |
langErr("1:6: reached EOF without matching `$(` with `)`"), |
| 1058 |
), |
| 1059 |
errCase( |
| 1060 |
"echo $((foo", |
| 1061 |
langErr("1:6: reached EOF without matching `$((` with `))`"), |
| 1062 |
), |
| 1063 |
errCase( |
| 1064 |
`echo $((\`, |
| 1065 |
langErr("1:6: reached EOF without matching `$((` with `))`"), |
| 1066 |
), |
| 1067 |
errCase( |
| 1068 |
`echo $((o\`, |
| 1069 |
langErr("1:6: reached EOF without matching `$((` with `))`"), |
| 1070 |
), |
| 1071 |
errCase( |
| 1072 |
`echo $((foo\a`, |
| 1073 |
langErr("1:6: reached EOF without matching `$((` with `))`"), |
| 1074 |
), |
| 1075 |
errCase( |
| 1076 |
`echo $(($(a"`, |
| 1077 |
langErr("1:12: reached EOF without closing quote `\"`"), |
| 1078 |
), |
| 1079 |
errCase( |
| 1080 |
"echo $((`echo 0`", |
| 1081 |
langErr("1:6: reached EOF without matching `$((` with `))`"), |
| 1082 |
), |
| 1083 |
errCase( |
| 1084 |
`echo $((& $(`, |
| 1085 |
langErr("1:9: `&` must follow an expression"), |
| 1086 |
), |
| 1087 |
errCase( |
| 1088 |
`echo $((a'`, |
| 1089 |
langErr("1:10: reached EOF without closing quote `'`"), |
| 1090 |
), |
| 1091 |
errCase( |
| 1092 |
`echo $((a b"`, |
| 1093 |
langErr("1:11: not a valid arithmetic operator: `b`"), |
| 1094 |
), |
| 1095 |
errCase( |
| 1096 |
"echo $(())", |
| 1097 |
langErr("1:6: `$((` must be followed by an expression"), |
| 1098 |
flipConfirmAll, // TODO: empty arithmetic expressions seem to be OK? |
| 1099 |
), |
| 1100 |
errCase( |
| 1101 |
"echo $((()))", |
| 1102 |
langErr("1:9: `(` must be followed by an expression"), |
| 1103 |
), |
| 1104 |
errCase( |
| 1105 |
"echo $(((3))", |
| 1106 |
langErr("1:6: reached `)` without matching `$((` with `))`"), |
| 1107 |
), |
| 1108 |
errCase( |
| 1109 |
"echo $((+))", |
| 1110 |
langErr("1:9: `+` must be followed by an expression"), |
| 1111 |
), |
| 1112 |
errCase( |
| 1113 |
"echo $((a b c))", |
| 1114 |
langErr("1:11: not a valid arithmetic operator: `b`"), |
| 1115 |
), |
| 1116 |
errCase( |
| 1117 |
"echo $((a ; c))", |
| 1118 |
langErr("1:11: not a valid arithmetic operator: `;`"), |
| 1119 |
), |
| 1120 |
errCase( |
| 1121 |
"echo $((foo) )", |
| 1122 |
langErr("1:6: reached `)` without matching `$((` with `))`", LangBash|LangMirBSDKorn|LangZsh), |
| 1123 |
flipConfirmAll, // note that we don't backtrack |
| 1124 |
), |
| 1125 |
errCase( |
| 1126 |
"echo $((a *))", |
| 1127 |
langErr("1:11: `*` must be followed by an expression"), |
| 1128 |
), |
| 1129 |
errCase( |
| 1130 |
"echo $((++))", |
| 1131 |
langErr("1:9: `++` must be followed by a literal"), |
| 1132 |
), |
| 1133 |
errCase( |
| 1134 |
"echo $((a ? b))", |
| 1135 |
langErr("1:11: ternary operator missing `:` after `?`"), |
| 1136 |
), |
| 1137 |
errCase( |
| 1138 |
"echo $((a : b))", |
| 1139 |
langErr("1:11: ternary operator missing `?` before `:`"), |
| 1140 |
), |
| 1141 |
errCase( |
| 1142 |
"echo $((/", |
| 1143 |
langErr("1:9: `/` must follow an expression"), |
| 1144 |
), |
| 1145 |
errCase( |
| 1146 |
"echo $((:", |
| 1147 |
langErr("1:9: ternary operator missing `?` before `:`"), |
| 1148 |
), |
| 1149 |
errCase( |
| 1150 |
"echo $(((a)+=b))", |
| 1151 |
langErr("1:12: `+=` must follow a name"), |
| 1152 |
flipConfirm(LangMirBSDKorn), |
| 1153 |
), |
| 1154 |
errCase( |
| 1155 |
"echo $((1=2))", |
| 1156 |
langErr("1:10: `=` must follow a name"), |
| 1157 |
), |
| 1158 |
errCase( |
| 1159 |
"echo $(($0=2))", |
| 1160 |
langErr("1:11: `=` must follow a name"), |
| 1161 |
flipConfirmAll, |
| 1162 |
), |
| 1163 |
errCase( |
| 1164 |
"echo $(($(a)=2))", |
| 1165 |
langErr("1:13: `=` must follow a name"), |
| 1166 |
flipConfirmAll, |
| 1167 |
), |
| 1168 |
// errCase( |
| 1169 |
// "echo $((1'2`))", |
| 1170 |
// // TODO: Take a look at this again, since this no longer fails |
| 1171 |
// // after fixing https://github.com/mvdan/sh/issues/587. |
| 1172 |
// // Note that Bash seems to treat code inside $(()) as if it were |
| 1173 |
// // within double quotes, yet still requires single quotes to be |
| 1174 |
// // matched. |
| 1175 |
// // `1:10: not a valid arithmetic operator: ``, |
| 1176 |
// ), |
| 1177 |
errCase( |
| 1178 |
"<<EOF\n$(()a", |
| 1179 |
langErr("2:1: `$((` must be followed by an expression"), |
| 1180 |
), |
| 1181 |
errCase( |
| 1182 |
"<<EOF\n`))", |
| 1183 |
langErr("2:2: `)` can only be used to close a subshell"), |
| 1184 |
), |
| 1185 |
errCase( |
| 1186 |
"echo ${foo", |
| 1187 |
langErr("1:6: reached EOF without matching `${` with `}`"), |
| 1188 |
), |
| 1189 |
errCase( |
| 1190 |
"echo ${Ã }", |
| 1191 |
langErr("1:8: invalid parameter name"), |
| 1192 |
langErr("1:8: not a valid parameter expansion operator: `Ã `", LangZsh), // not great, but seems like an edge case |
| 1193 |
), |
| 1194 |
errCase( |
| 1195 |
"echo ${1a}", |
| 1196 |
langErr("1:8: invalid parameter name"), |
| 1197 |
), |
| 1198 |
errCase( |
| 1199 |
"echo ${foo-bar", |
| 1200 |
langErr("1:6: reached EOF without matching `${` with `}`"), |
| 1201 |
), |
| 1202 |
errCase( |
| 1203 |
"#foo\n{ bar;", |
| 1204 |
langErr("2:1: reached EOF without matching `{` with `}`"), |
| 1205 |
), |
| 1206 |
errCase( |
| 1207 |
`echo "foo${bar"`, |
| 1208 |
langErr("1:15: not a valid parameter expansion operator: `\"`"), |
| 1209 |
), |
| 1210 |
errCase( |
| 1211 |
"echo ${%", |
| 1212 |
langErr("1:6: `${%foo}` is a mksh feature; tried parsing as LANG"), |
| 1213 |
langErr("1:9: invalid parameter name", LangMirBSDKorn), |
| 1214 |
), |
| 1215 |
errCase( |
| 1216 |
"echo ${+", |
| 1217 |
langErr("1:6: `${+foo}` is a zsh feature; tried parsing as LANG"), |
| 1218 |
langErr("1:6: reached EOF without matching `${` with `}`", LangZsh), |
| 1219 |
), |
| 1220 |
errCase( |
| 1221 |
"echo ${#${", |
| 1222 |
langErr("1:9: nested parameter expansions are a zsh feature; tried parsing as LANG"), |
| 1223 |
langErr("1:9: reached EOF without matching `${` with `}`", LangZsh), |
| 1224 |
), |
| 1225 |
errCase( |
| 1226 |
"echo ${#$(", |
| 1227 |
langErr("1:9: nested parameter expansions are a zsh feature; tried parsing as LANG"), |
| 1228 |
langErr("1:9: reached EOF without matching `$(` with `)`", LangZsh), |
| 1229 |
), |
| 1230 |
errCase( |
| 1231 |
"echo ${(", |
| 1232 |
langErr("1:6: parameter expansion flags are a zsh feature; tried parsing as LANG"), |
| 1233 |
langErr("1:8: reached EOF without matching `(` with `)`", LangZsh), |
| 1234 |
), |
| 1235 |
errCase( |
| 1236 |
"echo $$(foo)", |
| 1237 |
langErr("1:8: a command can only contain words and redirects; encountered `(`", LangPOSIX|LangBash|LangMirBSDKorn|LangBats), |
| 1238 |
), |
| 1239 |
errCase( |
| 1240 |
"echo ${##", |
| 1241 |
langErr("1:6: reached EOF without matching `${` with `}`"), |
| 1242 |
), |
| 1243 |
errCase( |
| 1244 |
"echo ${#<}", |
| 1245 |
langErr("1:9: not a valid parameter expansion operator: `<`"), |
| 1246 |
), |
| 1247 |
errCase( |
| 1248 |
"echo ${%<}", |
| 1249 |
langErr("1:8: invalid parameter name", LangMirBSDKorn), |
| 1250 |
), |
| 1251 |
errCase( |
| 1252 |
"echo ${!<}", |
| 1253 |
langErr("1:9: not a valid parameter expansion operator: `<`", LangBash|LangMirBSDKorn), |
| 1254 |
), |
| 1255 |
errCase( |
| 1256 |
"echo ${@foo}", |
| 1257 |
langErr("1:9: `@` cannot be followed by a word"), |
| 1258 |
), |
| 1259 |
errCase( |
| 1260 |
"echo ${$needbraces}", |
| 1261 |
langErr("1:9: `$` cannot be followed by a word"), |
| 1262 |
), |
| 1263 |
errCase( |
| 1264 |
"echo ${?foo}", |
| 1265 |
langErr("1:9: `?` cannot be followed by a word"), |
| 1266 |
), |
| 1267 |
errCase( |
| 1268 |
"echo ${-foo}", |
| 1269 |
langErr("1:9: `-` cannot be followed by a word"), |
| 1270 |
), |
| 1271 |
errCase( |
| 1272 |
`echo ${"bad"}`, |
| 1273 |
langErr("1:6: invalid nested parameter expansion", LangZsh), |
| 1274 |
), |
| 1275 |
errCase( |
| 1276 |
`echo ${"$needbraces"}`, |
| 1277 |
langErr("1:10: `$` cannot be followed by a word", LangZsh), |
| 1278 |
), |
| 1279 |
errCase( |
| 1280 |
`echo ${"${foo}}`, |
| 1281 |
langErr("1:8: reached `}` without closing quote `\"`", LangZsh), |
| 1282 |
), |
| 1283 |
errCase( |
| 1284 |
`echo ${"${foo}bad"}`, |
| 1285 |
langErr("1:6: invalid nested parameter expansion", LangZsh), |
| 1286 |
), |
| 1287 |
errCase( |
| 1288 |
"echo ${${nested}foo}", |
| 1289 |
langErr("1:17: nested parameter expansion cannot be followed by a word", LangZsh), |
| 1290 |
), |
| 1291 |
errCase( |
| 1292 |
"echo ${@[@]} ${@[*]}", |
| 1293 |
langErr("1:9: cannot index a special parameter name", LangBash|LangMirBSDKorn), |
| 1294 |
), |
| 1295 |
errCase( |
| 1296 |
"echo ${${nested}[@]", |
| 1297 |
langErr("1:6: reached EOF without matching `${` with `}`", LangZsh), |
| 1298 |
), |
| 1299 |
errCase( |
| 1300 |
"echo ${*[@]} ${*[*]}", |
| 1301 |
langErr("1:9: cannot index a special parameter name", LangBash|LangMirBSDKorn), |
| 1302 |
), |
| 1303 |
errCase( |
| 1304 |
"echo ${#[x]}", |
| 1305 |
langErr("1:9: cannot index a special parameter name", LangBash|LangMirBSDKorn), |
| 1306 |
), |
| 1307 |
errCase( |
| 1308 |
"echo ${$[0]}", |
| 1309 |
langErr("1:9: cannot index a special parameter name", LangBash|LangMirBSDKorn), |
| 1310 |
), |
| 1311 |
errCase( |
| 1312 |
"echo ${?[@]}", |
| 1313 |
langErr("1:9: cannot index a special parameter name", LangBash|LangMirBSDKorn), |
| 1314 |
), |
| 1315 |
errCase( |
| 1316 |
"echo ${2[@]}", |
| 1317 |
langErr("1:9: cannot index a special parameter name", LangBash|LangMirBSDKorn), |
| 1318 |
), |
| 1319 |
errCase( |
| 1320 |
"echo ${foo*}", |
| 1321 |
langErr("1:11: not a valid parameter expansion operator: `*`", LangBash|LangMirBSDKorn), |
| 1322 |
), |
| 1323 |
errCase( |
| 1324 |
"echo ${foo;}", |
| 1325 |
langErr("1:11: not a valid parameter expansion operator: `;`", LangBash|LangMirBSDKorn), |
| 1326 |
), |
| 1327 |
errCase( |
| 1328 |
"echo ${foo!}", |
| 1329 |
langErr("1:11: not a valid parameter expansion operator: `!`", LangBash|LangMirBSDKorn), |
| 1330 |
), |
| 1331 |
errCase( |
| 1332 |
"echo ${#foo:-bar}", |
| 1333 |
langErr("1:12: cannot combine multiple parameter expansion operators", LangBash|LangMirBSDKorn), |
| 1334 |
), |
| 1335 |
errCase( |
| 1336 |
"echo ${%foo:1:3}", |
| 1337 |
langErr("1:12: cannot combine multiple parameter expansion operators", LangMirBSDKorn), |
| 1338 |
), |
| 1339 |
errCase( |
| 1340 |
"echo ${#foo%x}", |
| 1341 |
langErr("1:12: cannot combine multiple parameter expansion operators", LangMirBSDKorn), |
| 1342 |
), |
| 1343 |
errCase( |
| 1344 |
"echo foo\n;", |
| 1345 |
langErr("2:1: `;` can only immediately follow a statement"), |
| 1346 |
), |
| 1347 |
errCase( |
| 1348 |
"<<$ <<0\n$(<<$<<", |
| 1349 |
langErr("2:6: `<<` must be followed by a word", LangBash|LangMirBSDKorn), |
| 1350 |
), |
| 1351 |
errCase( |
| 1352 |
"(foo) bar", |
| 1353 |
langErr("1:7: statements must be separated by &, ; or a newline"), |
| 1354 |
), |
| 1355 |
errCase( |
| 1356 |
"{ foo; } bar", |
| 1357 |
langErr("1:10: statements must be separated by &, ; or a newline"), |
| 1358 |
), |
| 1359 |
errCase( |
| 1360 |
"if foo; then bar; fi bar", |
| 1361 |
langErr("1:22: statements must be separated by &, ; or a newline"), |
| 1362 |
), |
| 1363 |
errCase( |
| 1364 |
"case", |
| 1365 |
langErr("1:1: `case` must be followed by a word"), |
| 1366 |
), |
| 1367 |
errCase( |
| 1368 |
"case i", |
| 1369 |
langErr("1:1: `case x` must be followed by `in`"), |
| 1370 |
), |
| 1371 |
errCase( |
| 1372 |
"case i in 3) foo;", |
| 1373 |
langErr("1:1: `case` statement must end with `esac`"), |
| 1374 |
), |
| 1375 |
errCase( |
| 1376 |
"case i in 3) foo; 4) bar; esac", |
| 1377 |
langErr("1:20: a command can only contain words and redirects; encountered `)`"), |
| 1378 |
), |
| 1379 |
errCase( |
| 1380 |
"case i in 3&) foo;", |
| 1381 |
langErr("1:12: case patterns must be separated with `|`"), |
| 1382 |
), |
| 1383 |
errCase( |
| 1384 |
"case $i in &) foo;", |
| 1385 |
langErr("1:12: case patterns must consist of words"), |
| 1386 |
), |
| 1387 |
errCase( |
| 1388 |
"case i {", |
| 1389 |
langErr("1:1: `case i {` is a mksh feature; tried parsing as LANG"), |
| 1390 |
langErr("1:1: `case` statement must end with `}`", LangMirBSDKorn), |
| 1391 |
), |
| 1392 |
errCase( |
| 1393 |
"case i { x) y ;;", |
| 1394 |
langErr("1:1: `case` statement must end with `}`", LangMirBSDKorn), |
| 1395 |
), |
| 1396 |
errCase( |
| 1397 |
"\"`\"", |
| 1398 |
langErr("1:3: reached EOF without closing quote `\"`"), |
| 1399 |
), |
| 1400 |
errCase( |
| 1401 |
"`\"`", |
| 1402 |
langErr("1:2: reached \"`\" without closing quote `\"`"), |
| 1403 |
), |
| 1404 |
errCase( |
| 1405 |
"`\\```", |
| 1406 |
langErr("1:3: reached EOF without closing quote \"`\""), |
| 1407 |
), |
| 1408 |
errCase( |
| 1409 |
"`{\nfoo`", |
| 1410 |
langErr("1:2: reached \"`\" without matching `{` with `}`"), |
| 1411 |
), |
| 1412 |
errCase( |
| 1413 |
"echo \"`)`\"", |
| 1414 |
langErr("1:8: `)` can only be used to close a subshell"), |
| 1415 |
flipConfirm(LangPOSIX), // dash bug? |
| 1416 |
), |
| 1417 |
errCase( |
| 1418 |
"<<$bar\n$bar", |
| 1419 |
langErr("1:3: expansions not allowed in heredoc words"), |
| 1420 |
flipConfirmAll, // we are stricter |
| 1421 |
), |
| 1422 |
errCase( |
| 1423 |
"<<${bar}\n${bar}", |
| 1424 |
langErr("1:3: expansions not allowed in heredoc words"), |
| 1425 |
flipConfirmAll, // we are stricter |
| 1426 |
), |
| 1427 |
errCase( |
| 1428 |
"<<$(bar)\n$(bar)", |
| 1429 |
langErr("1:3: expansions not allowed in heredoc words", LangBash), |
| 1430 |
flipConfirmAll, // we are stricter |
| 1431 |
), |
| 1432 |
|
| 1433 |
errCase( |
| 1434 |
"<<$-\n$-", |
| 1435 |
langErr("1:3: expansions not allowed in heredoc words"), |
| 1436 |
flipConfirmAll, // we are stricter |
| 1437 |
), |
| 1438 |
errCase( |
| 1439 |
"<<`bar`\n`bar`", |
| 1440 |
langErr("1:3: expansions not allowed in heredoc words"), |
| 1441 |
flipConfirmAll, // we are stricter |
| 1442 |
), |
| 1443 |
errCase( |
| 1444 |
"<<\"$bar\"\n$bar", |
| 1445 |
langErr("1:4: expansions not allowed in heredoc words"), |
| 1446 |
flipConfirmAll, // we are stricter |
| 1447 |
), |
| 1448 |
errCase( |
| 1449 |
"<<a <<0\n$(<<$<<", |
| 1450 |
langErr("2:6: `<<` must be followed by a word"), |
| 1451 |
), |
| 1452 |
errCase( |
| 1453 |
`""()`, |
| 1454 |
langErr("1:1: invalid func name"), |
| 1455 |
flipConfirm(LangMirBSDKorn), // TODO: support non-literal func names, even empty ones? |
| 1456 |
), |
| 1457 |
errCase( |
| 1458 |
"]] )", |
| 1459 |
langErr("1:1: `]]` can only be used to close a test"), |
| 1460 |
langErr("1:4: a command can only contain words and redirects; encountered `)`", LangPOSIX), |
| 1461 |
), |
| 1462 |
errCase( |
| 1463 |
"((foo", |
| 1464 |
langErr("1:1: reached EOF without matching `((` with `))`", LangBash|LangMirBSDKorn|LangZsh), |
| 1465 |
langErr("1:2: reached EOF without matching `(` with `)`", LangPOSIX), |
| 1466 |
), |
| 1467 |
errCase( |
| 1468 |
"(())", |
| 1469 |
langErr("1:1: `((` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1470 |
), |
| 1471 |
errCase( |
| 1472 |
"echo ((foo", |
| 1473 |
langErr("1:6: `((` can only be used to open an arithmetic cmd", LangBash|LangMirBSDKorn|LangZsh), |
| 1474 |
langErr("1:1: `foo(` must be followed by `)`", LangPOSIX), |
| 1475 |
), |
| 1476 |
errCase( |
| 1477 |
"echo |&", |
| 1478 |
langErr("1:6: `|&` must be followed by a statement", LangBash|LangZsh), |
| 1479 |
langErr("1:6: `|` must be followed by a statement", LangPOSIX), |
| 1480 |
), |
| 1481 |
errCase( |
| 1482 |
"|& a", |
| 1483 |
langErr("1:1: `|&` is not a valid start for a statement", LangBash|LangMirBSDKorn|LangZsh), |
| 1484 |
), |
| 1485 |
errCase( |
| 1486 |
"foo |& bar", |
| 1487 |
langErr("1:5: `|` must be followed by a statement", LangPOSIX), |
| 1488 |
), |
| 1489 |
errCase( |
| 1490 |
"let", |
| 1491 |
langErr("1:1: `let` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1492 |
), |
| 1493 |
errCase( |
| 1494 |
"let a+ b", |
| 1495 |
langErr("1:6: `+` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1496 |
), |
| 1497 |
errCase( |
| 1498 |
"let + a", |
| 1499 |
langErr("1:5: `+` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1500 |
), |
| 1501 |
errCase( |
| 1502 |
"let a ++", |
| 1503 |
langErr("1:7: `++` must be followed by a literal", LangBash|LangMirBSDKorn|LangZsh), |
| 1504 |
), |
| 1505 |
errCase( |
| 1506 |
"let (a)++", |
| 1507 |
langErr("1:8: `++` must follow a name", LangBash|LangMirBSDKorn|LangZsh), |
| 1508 |
), |
| 1509 |
errCase( |
| 1510 |
"let 1++", |
| 1511 |
langErr("1:6: `++` must follow a name", LangBash|LangMirBSDKorn|LangZsh), |
| 1512 |
), |
| 1513 |
errCase( |
| 1514 |
"let $0++", |
| 1515 |
langErr("1:7: `++` must follow a name", LangBash|LangMirBSDKorn|LangZsh), |
| 1516 |
), |
| 1517 |
errCase( |
| 1518 |
"let --(a)", |
| 1519 |
langErr("1:5: `--` must be followed by a literal", LangBash|LangMirBSDKorn|LangZsh), |
| 1520 |
), |
| 1521 |
errCase( |
| 1522 |
"let --$a", |
| 1523 |
langErr("1:5: `--` must be followed by a literal", LangBash|LangMirBSDKorn|LangZsh), |
| 1524 |
), |
| 1525 |
errCase( |
| 1526 |
"let a+\n", |
| 1527 |
langErr("1:6: `+` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1528 |
), |
| 1529 |
errCase( |
| 1530 |
"let ))", |
| 1531 |
langErr("1:1: `let` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1532 |
), |
| 1533 |
errCase( |
| 1534 |
"`let !`", |
| 1535 |
langErr("1:6: `!` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1536 |
), |
| 1537 |
errCase( |
| 1538 |
"let a:b", |
| 1539 |
langErr("1:6: ternary operator missing `?` before `:`", LangBash|LangMirBSDKorn|LangZsh), |
| 1540 |
), |
| 1541 |
errCase( |
| 1542 |
"let a+b=c", |
| 1543 |
langErr("1:8: `=` must follow a name", LangBash|LangMirBSDKorn|LangZsh), |
| 1544 |
), |
| 1545 |
errCase( |
| 1546 |
"`let` { foo; }", |
| 1547 |
langErr("1:2: `let` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1548 |
), |
| 1549 |
errCase( |
| 1550 |
"$(let)", |
| 1551 |
langErr("1:3: `let` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1552 |
), |
| 1553 |
errCase( |
| 1554 |
"[[", |
| 1555 |
langErr("1:1: `[[` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1556 |
), |
| 1557 |
errCase( |
| 1558 |
"[[ ]]", |
| 1559 |
langErr("1:1: `[[` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1560 |
), |
| 1561 |
errCase( |
| 1562 |
"[[ a", |
| 1563 |
langErr("1:1: reached EOF without matching `[[` with `]]`", LangBash|LangMirBSDKorn|LangZsh), |
| 1564 |
), |
| 1565 |
errCase( |
| 1566 |
"[[ a ||", |
| 1567 |
langErr("1:6: `||` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1568 |
), |
| 1569 |
errCase( |
| 1570 |
"[[ a && &&", |
| 1571 |
langErr("1:6: `&&` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1572 |
), |
| 1573 |
errCase( |
| 1574 |
"[[ a && ]]", |
| 1575 |
langErr("1:6: `&&` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1576 |
), |
| 1577 |
errCase( |
| 1578 |
"[[ a ==", |
| 1579 |
langErr("1:6: `==` must be followed by a word", LangBash|LangMirBSDKorn|LangZsh), |
| 1580 |
), |
| 1581 |
errCase( |
| 1582 |
"[[ a =~", |
| 1583 |
langErr("1:6: `=~` must be followed by a word", LangBash|LangZsh), |
| 1584 |
langErr("1:6: regex tests are a bash/zsh feature; tried parsing as LANG", LangMirBSDKorn), |
| 1585 |
), |
| 1586 |
errCase( |
| 1587 |
"[[ -f a", |
| 1588 |
langErr("1:1: reached EOF without matching `[[` with `]]`", LangBash|LangMirBSDKorn|LangZsh), |
| 1589 |
), |
| 1590 |
errCase( |
| 1591 |
"[[ -n\na ]]", |
| 1592 |
langErr("1:4: `-n` must be followed by a word", LangBash|LangMirBSDKorn|LangZsh), |
| 1593 |
), |
| 1594 |
errCase( |
| 1595 |
"[[ a -ef\nb ]]", |
| 1596 |
langErr("1:6: `-ef` must be followed by a word", LangBash|LangMirBSDKorn|LangZsh), |
| 1597 |
), |
| 1598 |
errCase( |
| 1599 |
"[[ a ==\nb ]]", |
| 1600 |
langErr("1:6: `==` must be followed by a word", LangBash|LangMirBSDKorn|LangZsh), |
| 1601 |
), |
| 1602 |
errCase( |
| 1603 |
"[[ a -nt b", |
| 1604 |
langErr("1:1: reached EOF without matching `[[` with `]]`", LangBash|LangMirBSDKorn|LangZsh), |
| 1605 |
), |
| 1606 |
errCase( |
| 1607 |
"[[ a =~ b", |
| 1608 |
langErr("1:1: reached EOF without matching `[[` with `]]`", LangBash|LangZsh), |
| 1609 |
), |
| 1610 |
errCase( |
| 1611 |
"[[ a b c ]]", |
| 1612 |
langErr("1:6: not a valid test operator: `b`", LangBash|LangMirBSDKorn|LangZsh), |
| 1613 |
), |
| 1614 |
errCase( |
| 1615 |
"[[ a b$x c ]]", |
| 1616 |
langErr("1:6: test operator words must consist of a single literal", LangBash|LangMirBSDKorn|LangZsh), |
| 1617 |
), |
| 1618 |
errCase( |
| 1619 |
"[[ a & b ]]", |
| 1620 |
langErr("1:6: not a valid test operator: `&`", LangBash|LangMirBSDKorn|LangZsh), |
| 1621 |
), |
| 1622 |
errCase( |
| 1623 |
"[[ true && () ]]", |
| 1624 |
langErr("1:12: `(` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1625 |
), |
| 1626 |
errCase( |
| 1627 |
"[[ true && (&& ]]", |
| 1628 |
langErr("1:12: `(` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1629 |
), |
| 1630 |
errCase( |
| 1631 |
"[[ a == ! b ]]", |
| 1632 |
langErr("1:11: not a valid test operator: `b`", LangBash|LangMirBSDKorn|LangZsh), |
| 1633 |
), |
| 1634 |
errCase( |
| 1635 |
"[[ (! ) ]]", |
| 1636 |
langErr("1:5: `!` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1637 |
), |
| 1638 |
errCase( |
| 1639 |
"[[ ! && ]]", |
| 1640 |
langErr("1:4: `!` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1641 |
), |
| 1642 |
errCase( |
| 1643 |
"[[ (-e ) ]]", |
| 1644 |
langErr("1:5: `-e` must be followed by a word", LangBash|LangMirBSDKorn|LangZsh), |
| 1645 |
), |
| 1646 |
errCase( |
| 1647 |
"[[ (a) == b ]]", |
| 1648 |
langErr("1:8: expected `&&`, `||` or `]]` after complex expr", LangBash|LangMirBSDKorn|LangZsh), |
| 1649 |
), |
| 1650 |
errCase( |
| 1651 |
"[[ a =~ ; ]]", |
| 1652 |
langErr("1:6: `=~` must be followed by a word", LangBash|LangZsh), |
| 1653 |
), |
| 1654 |
errCase( |
| 1655 |
"[[ a =~ )", |
| 1656 |
langErr("1:6: `=~` must be followed by a word", LangBash|LangZsh), |
| 1657 |
), |
| 1658 |
errCase( |
| 1659 |
"[[ a =~ ())", |
| 1660 |
langErr("1:1: reached `)` without matching `[[` with `]]`", LangBash|LangZsh), |
| 1661 |
), |
| 1662 |
errCase( |
| 1663 |
"[[ >", |
| 1664 |
langErr("1:1: `[[` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1665 |
), |
| 1666 |
errCase( |
| 1667 |
"local (", |
| 1668 |
langErr("1:7: `local` must be followed by names or assignments", LangBash), |
| 1669 |
langErr("1:7: reached EOF without matching `(` with `)`", LangZsh), |
| 1670 |
), |
| 1671 |
errCase( |
| 1672 |
"declare 0=${o}", |
| 1673 |
langErr("1:9: invalid var name", LangBash|LangZsh), |
| 1674 |
), |
| 1675 |
errCase( |
| 1676 |
"declare ab=${o})", |
| 1677 |
langErr("1:16: statements must be separated by &, ; or a newline", LangBash|LangZsh), |
| 1678 |
), |
| 1679 |
errCase( |
| 1680 |
"export ab=$0(", |
| 1681 |
langErr("1:13: `export` must be followed by names or assignments", LangBash), |
| 1682 |
langErr("1:13: reached EOF without matching `(` with `)`", LangZsh), |
| 1683 |
), |
| 1684 |
errCase( |
| 1685 |
"declare {x,y}=(1 2)", |
| 1686 |
langErr("1:15: `declare` must be followed by names or assignments", LangBash), |
| 1687 |
), |
| 1688 |
errCase( |
| 1689 |
"a=(<)", |
| 1690 |
langErr("1:4: array element values must be words", LangBash|LangMirBSDKorn|LangZsh), |
| 1691 |
), |
| 1692 |
errCase( |
| 1693 |
"a=([)", |
| 1694 |
langErr("1:4: `[` must be followed by an expression", LangBash|LangZsh), |
| 1695 |
), |
| 1696 |
errCase( |
| 1697 |
"a=([i)", |
| 1698 |
langErr("1:4: reached `)` without matching `[` with `]`", LangBash|LangZsh), |
| 1699 |
), |
| 1700 |
errCase( |
| 1701 |
"a=([i])", |
| 1702 |
langErr("1:4: `[x]` must be followed by `=`", LangBash|LangZsh), |
| 1703 |
flipConfirmAll, // TODO: why is this valid? |
| 1704 |
), |
| 1705 |
errCase( |
| 1706 |
"a[i]=(y)", |
| 1707 |
langErr("1:5: arrays cannot be nested", LangBash), |
| 1708 |
), |
| 1709 |
errCase( |
| 1710 |
"a=([i]=(y))", |
| 1711 |
langErr("1:7: arrays cannot be nested", LangBash|LangZsh), |
| 1712 |
), |
| 1713 |
errCase( |
| 1714 |
"o=([0]=#", |
| 1715 |
langErr("1:8: array element values must be words", LangBash|LangZsh), |
| 1716 |
), |
| 1717 |
errCase( |
| 1718 |
"a[b] ==[", |
| 1719 |
langErr("1:1: `a[b]` must be followed by `=`", LangBash|LangZsh), |
| 1720 |
flipConfirmAll, // stringifies |
| 1721 |
), |
| 1722 |
errCase( |
| 1723 |
"a[b] +=c", |
| 1724 |
langErr("1:1: `a[b]` must be followed by `=`", LangBash|LangZsh), |
| 1725 |
flipConfirmAll, // stringifies |
| 1726 |
), |
| 1727 |
errCase( |
| 1728 |
"a=(x y) foo", |
| 1729 |
langErr("1:1: inline variables cannot be arrays", LangBash|LangZsh), |
| 1730 |
flipConfirmAll, // stringifies |
| 1731 |
), |
| 1732 |
errCase( |
| 1733 |
"a[2]=x foo", |
| 1734 |
langErr("1:1: inline variables cannot be arrays", LangBash|LangZsh), |
| 1735 |
flipConfirmAll, // stringifies |
| 1736 |
), |
| 1737 |
errCase( |
| 1738 |
"function", |
| 1739 |
langErr("1:1: `function` must be followed by a name", LangBash|LangMirBSDKorn), |
| 1740 |
langErr("1:1: `foo()` must be followed by a statement", LangZsh), |
| 1741 |
), |
| 1742 |
errCase( |
| 1743 |
"function foo(", |
| 1744 |
langErr("1:1: `function foo(` must be followed by `)`", LangBash|LangMirBSDKorn|LangZsh), |
| 1745 |
), |
| 1746 |
errCase( |
| 1747 |
"function `function", |
| 1748 |
langErr("1:1: `function` must be followed by a name", LangBash|LangMirBSDKorn), |
| 1749 |
langErr("1:11: `foo()` must be followed by a statement", LangZsh), |
| 1750 |
), |
| 1751 |
errCase( |
| 1752 |
`function "foo"(){}`, |
| 1753 |
langErr("1:1: `function` must be followed by a name", LangBash|LangMirBSDKorn), |
| 1754 |
langErr("1:10: invalid func name", LangZsh), |
| 1755 |
), |
| 1756 |
errCase( |
| 1757 |
"function foo()", |
| 1758 |
langErr("1:1: `foo()` must be followed by a statement", LangBash|LangMirBSDKorn|LangZsh), |
| 1759 |
), |
| 1760 |
errCase( |
| 1761 |
"@test", |
| 1762 |
langErr("1:1: `@test` must be followed by a description word", LangBats), |
| 1763 |
), |
| 1764 |
errCase( |
| 1765 |
"@test 'desc'", |
| 1766 |
langErr("1:1: `@test \"desc\"` must be followed by a statement", LangBats), |
| 1767 |
), |
| 1768 |
errCase( |
| 1769 |
"echo <<<", |
| 1770 |
langErr("1:6: `<<<` must be followed by a word", LangBash|LangMirBSDKorn|LangZsh), |
| 1771 |
), |
| 1772 |
errCase( |
| 1773 |
"a[", |
| 1774 |
langErr("1:2: `[` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1775 |
), |
| 1776 |
errCase( |
| 1777 |
"a[b", |
| 1778 |
langErr("1:2: reached EOF without matching `[` with `]`", LangBash|LangMirBSDKorn|LangZsh), |
| 1779 |
), |
| 1780 |
errCase( |
| 1781 |
"a[]", |
| 1782 |
langErr("1:2: `[` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1783 |
flipConfirmAll, // is cmd |
| 1784 |
), |
| 1785 |
errCase( |
| 1786 |
"a[[", |
| 1787 |
langErr("1:3: `[` must follow a name like a[i]", LangBash|LangMirBSDKorn|LangZsh), |
| 1788 |
), |
| 1789 |
errCase( |
| 1790 |
"echo $((a[))", |
| 1791 |
langErr("1:10: `[` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1792 |
), |
| 1793 |
errCase( |
| 1794 |
"echo $((a[b))", |
| 1795 |
langErr("1:10: reached `)` without matching `[` with `]`", LangBash|LangMirBSDKorn|LangZsh), |
| 1796 |
), |
| 1797 |
errCase( |
| 1798 |
"echo $((a[]))", |
| 1799 |
langErr("1:10: `[` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1800 |
flipConfirm(LangMirBSDKorn), // wrong? |
| 1801 |
), |
| 1802 |
errCase( |
| 1803 |
"echo $((x$t[", |
| 1804 |
langErr("1:12: `[` must follow a name like a[i]", LangBash|LangMirBSDKorn), |
| 1805 |
langErr("1:12: `[` must be followed by an expression", LangZsh), |
| 1806 |
), |
| 1807 |
errCase( |
| 1808 |
"a[1]", |
| 1809 |
langErr("1:1: `a[b]` must be followed by `=`", LangBash|LangMirBSDKorn|LangZsh), |
| 1810 |
flipConfirmAll, // is cmd |
| 1811 |
), |
| 1812 |
errCase( |
| 1813 |
"a[i]+", |
| 1814 |
langErr("1:1: `a[b]+` must be followed by `=`", LangBash|LangMirBSDKorn|LangZsh), |
| 1815 |
flipConfirmAll, // is cmd |
| 1816 |
), |
| 1817 |
errCase( |
| 1818 |
"a[1]#", |
| 1819 |
langErr("1:1: `a[b]` must be followed by `=`", LangBash|LangMirBSDKorn|LangZsh), |
| 1820 |
flipConfirmAll, // is cmd |
| 1821 |
), |
| 1822 |
errCase( |
| 1823 |
"echo $[foo", |
| 1824 |
langErr("1:6: reached EOF without matching `$[` with `]`", LangBash), |
| 1825 |
), |
| 1826 |
errCase( |
| 1827 |
"echo $'", |
| 1828 |
langErr("1:6: reached EOF without closing quote `'`", LangBash|LangMirBSDKorn|LangZsh), |
| 1829 |
), |
| 1830 |
errCase( |
| 1831 |
`echo $"`, |
| 1832 |
langErr("1:6: reached EOF without closing quote `\"`", LangBash|LangMirBSDKorn), |
| 1833 |
), |
| 1834 |
errCase( |
| 1835 |
"echo @(", |
| 1836 |
langErr("1:6: reached EOF without matching `@(` with `)`", LangBash|LangMirBSDKorn), |
| 1837 |
), |
| 1838 |
errCase( |
| 1839 |
"echo @(a", |
| 1840 |
langErr("1:6: reached EOF without matching `@(` with `)`", LangBash|LangMirBSDKorn), |
| 1841 |
), |
| 1842 |
errCase( |
| 1843 |
"echo @([abc)])", |
| 1844 |
langErr("1:14: a command can only contain words and redirects; encountered `)`", LangBash|LangMirBSDKorn), |
| 1845 |
), |
| 1846 |
errCase( |
| 1847 |
"((@(", |
| 1848 |
langErr("1:4: not a valid arithmetic operator: `(`", LangBash|LangMirBSDKorn), |
| 1849 |
), |
| 1850 |
errCase( |
| 1851 |
"time { foo;", |
| 1852 |
langErr("1:6: reached EOF without matching `{` with `}`", LangBash|LangMirBSDKorn|LangZsh), |
| 1853 |
), |
| 1854 |
errCase( |
| 1855 |
"time ! foo", |
| 1856 |
langErr("1:6: `!` can only be used in full statements", LangBash|LangMirBSDKorn|LangZsh), |
| 1857 |
flipConfirm(LangBash), // TODO: why is this valid? |
| 1858 |
), |
| 1859 |
errCase( |
| 1860 |
"coproc", |
| 1861 |
langErr("1:1: coproc clause requires a command", LangBash), |
| 1862 |
), |
| 1863 |
errCase( |
| 1864 |
"coproc\n$", |
| 1865 |
langErr("1:1: coproc clause requires a command", LangBash), |
| 1866 |
), |
| 1867 |
errCase( |
| 1868 |
"coproc declare (", |
| 1869 |
langErr("1:16: `declare` must be followed by names or assignments", LangBash), |
| 1870 |
), |
| 1871 |
errCase( |
| 1872 |
"echo ${foo[1 2]}", |
| 1873 |
langErr("1:14: not a valid arithmetic operator: `2`", LangBash|LangMirBSDKorn|LangZsh), |
| 1874 |
), |
| 1875 |
errCase( |
| 1876 |
"echo ${foo[}", |
| 1877 |
langErr("1:11: `[` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1878 |
), |
| 1879 |
errCase( |
| 1880 |
"echo ${foo]}", |
| 1881 |
langErr("1:11: not a valid parameter expansion operator: `]`", LangBash|LangMirBSDKorn|LangZsh), |
| 1882 |
), |
| 1883 |
errCase( |
| 1884 |
"echo ${foo[]}", |
| 1885 |
langErr("1:11: `[` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1886 |
flipConfirm(LangMirBSDKorn), // TODO: why is this valid? |
| 1887 |
), |
| 1888 |
errCase( |
| 1889 |
"echo ${a/\n", |
| 1890 |
langErr("1:6: reached EOF without matching `${` with `}`", LangBash|LangMirBSDKorn), |
| 1891 |
), |
| 1892 |
errCase( |
| 1893 |
"echo ${a/''", |
| 1894 |
langErr("1:6: reached EOF without matching `${` with `}`", LangBash|LangMirBSDKorn), |
| 1895 |
), |
| 1896 |
errCase( |
| 1897 |
"echo ${a-\n", |
| 1898 |
langErr("1:6: reached EOF without matching `${` with `}`", LangBash|LangMirBSDKorn), |
| 1899 |
), |
| 1900 |
errCase( |
| 1901 |
"echo ${foo:", |
| 1902 |
langErr("1:11: `:` must be followed by an expression", LangBash|LangMirBSDKorn), |
| 1903 |
), |
| 1904 |
errCase( |
| 1905 |
"foo=force_expansion; echo ${foo:1 2}", |
| 1906 |
langErr("1:35: not a valid arithmetic operator: `2`", LangBash|LangMirBSDKorn), |
| 1907 |
), |
| 1908 |
errCase( |
| 1909 |
"echo ${foo:1", |
| 1910 |
langErr("1:6: reached EOF without matching `${` with `}`", LangBash|LangMirBSDKorn), |
| 1911 |
), |
| 1912 |
errCase( |
| 1913 |
"echo ${foo:1:", |
| 1914 |
langErr("1:13: `:` must be followed by an expression", LangBash|LangMirBSDKorn), |
| 1915 |
), |
| 1916 |
errCase( |
| 1917 |
"echo ${foo:1:2", |
| 1918 |
langErr("1:6: reached EOF without matching `${` with `}`", LangBash|LangMirBSDKorn), |
| 1919 |
), |
| 1920 |
errCase( |
| 1921 |
// A slice is always arithmetic, unlike a subscript such as ${foo[1,#]}. |
| 1922 |
"echo ${foo:1:#2}", |
| 1923 |
langErr("1:13: `:` must be followed by an expression", LangBash|LangMirBSDKorn|LangZsh), |
| 1924 |
flipConfirmAll, // the shells only fail at expansion time |
| 1925 |
), |
| 1926 |
errCase( |
| 1927 |
"echo ${foo:h", |
| 1928 |
langErr("1:6: reached EOF without matching `${` with `}`", LangZsh), |
| 1929 |
), |
| 1930 |
errCase( |
| 1931 |
"echo ${foo,", |
| 1932 |
langErr("1:6: reached EOF without matching `${` with `}`", LangBash), |
| 1933 |
), |
| 1934 |
errCase( |
| 1935 |
"echo ${foo@", |
| 1936 |
langErr("1:11: @ expansion operator requires a literal", LangBash), |
| 1937 |
), |
| 1938 |
errCase( |
| 1939 |
"foo=force_expansion; echo ${foo@}", |
| 1940 |
langErr("1:33: @ expansion operator requires a literal", LangBash), |
| 1941 |
), |
| 1942 |
errCase( |
| 1943 |
"echo ${foo@Q", |
| 1944 |
langErr("1:6: reached EOF without matching `${` with `}`", LangBash), |
| 1945 |
), |
| 1946 |
errCase( |
| 1947 |
"foo=force_expansion; echo ${foo@bar}", |
| 1948 |
langErr("1:33: invalid @ expansion operator `bar`", LangBash), |
| 1949 |
), |
| 1950 |
errCase( |
| 1951 |
"foo=force_expansion; echo ${foo@'Q'}", |
| 1952 |
langErr("1:33: @ expansion operator requires a literal", LangBash), |
| 1953 |
), |
| 1954 |
errCase( |
| 1955 |
`echo $((echo a); (echo b))`, |
| 1956 |
langErr("1:14: not a valid arithmetic operator: `a`", LangBash|LangMirBSDKorn|LangZsh), |
| 1957 |
flipConfirmAll, // note that we don't backtrack |
| 1958 |
), |
| 1959 |
errCase( |
| 1960 |
`((echo a); (echo b))`, |
| 1961 |
langErr("1:8: not a valid arithmetic operator: `a`", LangBash|LangMirBSDKorn|LangZsh), |
| 1962 |
flipConfirmAll, // note that we don't backtrack |
| 1963 |
), |
| 1964 |
errCase( |
| 1965 |
"for ((;;", |
| 1966 |
langErr("1:5: reached EOF without matching `((` with `))`", LangBash), |
| 1967 |
), |
| 1968 |
errCase( |
| 1969 |
"for ((;;0000000", |
| 1970 |
langErr("1:5: reached EOF without matching `((` with `))`", LangBash), |
| 1971 |
), |
| 1972 |
errCase( |
| 1973 |
"echo <(", |
| 1974 |
langErr("1:6: `<` must be followed by a word", LangPOSIX|LangMirBSDKorn), |
| 1975 |
), |
| 1976 |
errCase( |
| 1977 |
"echo >(", |
| 1978 |
langErr("1:6: `>` must be followed by a word", LangPOSIX|LangMirBSDKorn), |
| 1979 |
), |
| 1980 |
errCase( |
| 1981 |
"echo {var}>foo", |
| 1982 |
langErr("1:6: `{varname}` redirects are a bash/zsh feature; tried parsing as LANG", LangPOSIX|LangMirBSDKorn), |
| 1983 |
// shells treat {var} as an argument, but we are a bit stricter |
| 1984 |
// so that users won't think this will work like they expect in POSIX shell. |
| 1985 |
flipConfirmAll, |
| 1986 |
), |
| 1987 |
errCase( |
| 1988 |
"echo ;&", |
| 1989 |
langErr("1:7: `&` can only immediately follow a statement", LangPOSIX), |
| 1990 |
langErr("1:6: `;&` can only be used in a case clause", LangBash|LangMirBSDKorn|LangZsh), |
| 1991 |
), |
| 1992 |
errCase( |
| 1993 |
"echo ;;&", |
| 1994 |
langErr("1:6: `;;` can only be used in a case clause", LangPOSIX|LangMirBSDKorn), |
| 1995 |
), |
| 1996 |
errCase( |
| 1997 |
"echo ;|", |
| 1998 |
langErr("1:7: `|` can only immediately follow a statement", LangPOSIX|LangBash), |
| 1999 |
langErr("1:6: `;|` can only be used in a case clause", LangMirBSDKorn|LangZsh), |
| 2000 |
), |
| 2001 |
errCase( |
| 2002 |
"for i in 1 2 3; { echo; }", |
| 2003 |
langErr("1:17: for loops with braces are a bash/mksh feature; tried parsing as LANG", LangPOSIX), |
| 2004 |
), |
| 2005 |
errCase( |
| 2006 |
"echo !(a)", |
| 2007 |
langErr("1:6: extended globs are a bash/mksh feature; tried parsing as LANG", LangPOSIX), |
| 2008 |
), |
| 2009 |
errCase( |
| 2010 |
"echo $a@(b)", |
| 2011 |
langErr("1:8: extended globs are a bash/mksh feature; tried parsing as LANG", LangPOSIX), |
| 2012 |
), |
| 2013 |
errCase( |
| 2014 |
"foo=(1 2)", |
| 2015 |
langErr("1:5: arrays are a bash/mksh/zsh feature; tried parsing as LANG", LangPOSIX), |
| 2016 |
), |
| 2017 |
errCase( |
| 2018 |
"a=$c\n'", |
| 2019 |
langErr("2:1: reached EOF without closing quote `'`"), |
| 2020 |
), |
| 2021 |
errCase( |
| 2022 |
"echo ${!foo[@]}", |
| 2023 |
langErr("1:6: `${!foo}` is a bash/mksh feature; tried parsing as LANG", LangPOSIX), |
| 2024 |
), |
| 2025 |
errCase( |
| 2026 |
"foo << < bar", |
| 2027 |
langErr("1:5: `<<` must be followed by a word", LangPOSIX), |
| 2028 |
), |
| 2029 |
errCase( |
| 2030 |
"echo ${foo,bar}", |
| 2031 |
langErr("1:11: this expansion operator is a bash feature; tried parsing as LANG", LangPOSIX|LangMirBSDKorn), |
| 2032 |
), |
| 2033 |
errCase( |
| 2034 |
"echo ${foo@Q}", |
| 2035 |
langErr("1:11: this expansion operator is a bash/mksh feature; tried parsing as LANG", LangPOSIX), |
| 2036 |
), |
| 2037 |
errCase( |
| 2038 |
"echo ${foo@a}", |
| 2039 |
langErr("1:12: this expansion operator is a bash feature; tried parsing as LANG", LangMirBSDKorn), |
| 2040 |
), |
| 2041 |
errCase( |
| 2042 |
"echo ${foo@u}", |
| 2043 |
langErr("1:12: this expansion operator is a bash feature; tried parsing as LANG", LangMirBSDKorn), |
| 2044 |
), |
| 2045 |
errCase( |
| 2046 |
"echo ${foo@A}", |
| 2047 |
langErr("1:12: this expansion operator is a bash feature; tried parsing as LANG", LangMirBSDKorn), |
| 2048 |
), |
| 2049 |
errCase( |
| 2050 |
"echo ${foo@E}", |
| 2051 |
langErr("1:12: this expansion operator is a bash feature; tried parsing as LANG", LangMirBSDKorn), |
| 2052 |
), |
| 2053 |
errCase( |
| 2054 |
"echo ${foo@K}", |
| 2055 |
langErr("1:12: this expansion operator is a bash feature; tried parsing as LANG", LangMirBSDKorn), |
| 2056 |
), |
| 2057 |
errCase( |
| 2058 |
"echo ${foo@k}", |
| 2059 |
langErr("1:12: this expansion operator is a bash feature; tried parsing as LANG", LangMirBSDKorn), |
| 2060 |
), |
| 2061 |
errCase( |
| 2062 |
"echo ${foo@L}", |
| 2063 |
langErr("1:12: this expansion operator is a bash feature; tried parsing as LANG", LangMirBSDKorn), |
| 2064 |
), |
| 2065 |
errCase( |
| 2066 |
"echo ${foo@P}", |
| 2067 |
langErr("1:12: this expansion operator is a bash feature; tried parsing as LANG", LangMirBSDKorn), |
| 2068 |
), |
| 2069 |
errCase( |
| 2070 |
"echo ${foo@U}", |
| 2071 |
langErr("1:12: this expansion operator is a bash feature; tried parsing as LANG", LangMirBSDKorn), |
| 2072 |
), |
| 2073 |
errCase( |
| 2074 |
"foo=force_expansion; echo ${foo@#}", |
| 2075 |
langErr("1:33: this expansion operator is a mksh feature; tried parsing as LANG", LangBash), |
| 2076 |
), |
| 2077 |
errCase( |
| 2078 |
"`\"`\\", |
| 2079 |
langErr("1:2: reached \"`\" without closing quote `\"`"), |
| 2080 |
), |
| 2081 |
} |
| 2082 |
|
| 2083 |
func TestInputName(t *testing.T) { |
| 2084 |
t.Parallel() |
| 2085 |
in := "(" |
| 2086 |
want := "some-file.sh:1:1: `(` must be followed by a statement list" |
| 2087 |
p := NewParser() |
| 2088 |
_, err := p.Parse(strings.NewReader(in), "some-file.sh") |
| 2089 |
if err == nil { |
| 2090 |
t.Fatalf("Expected error in %q: %v", in, want) |
| 2091 |
} |
| 2092 |
got := err.Error() |
| 2093 |
if got != want { |
| 2094 |
t.Fatalf("Error mismatch in %q\nwant: %s\ngot: %s", |
| 2095 |
in, want, got) |
| 2096 |
} |
| 2097 |
} |
| 2098 |
|
| 2099 |
var errBadReader = fmt.Errorf("write: expected error") |
| 2100 |
|
| 2101 |
type badReader struct{} |
| 2102 |
|
| 2103 |
func (b badReader) Read(p []byte) (int, error) { return 0, errBadReader } |
| 2104 |
|
| 2105 |
func TestReadErr(t *testing.T) { |
| 2106 |
t.Parallel() |
| 2107 |
p := NewParser() |
| 2108 |
_, err := p.Parse(badReader{}, "") |
| 2109 |
if err == nil { |
| 2110 |
t.Fatalf("Expected error with bad reader") |
| 2111 |
} |
| 2112 |
if err != errBadReader { |
| 2113 |
t.Fatalf("Error mismatch with bad reader:\nwant: %v\ngot: %v", |
| 2114 |
errBadReader, err) |
| 2115 |
} |
| 2116 |
} |
| 2117 |
|
| 2118 |
type strictStringReader struct { |
| 2119 |
*strings.Reader |
| 2120 |
gaveEOF bool |
| 2121 |
} |
| 2122 |
|
| 2123 |
func newStrictReader(s string) *strictStringReader { |
| 2124 |
return &strictStringReader{Reader: strings.NewReader(s)} |
| 2125 |
} |
| 2126 |
|
| 2127 |
func (r *strictStringReader) Read(p []byte) (int, error) { |
| 2128 |
n, err := r.Reader.Read(p) |
| 2129 |
if err == io.EOF { |
| 2130 |
if r.gaveEOF { |
| 2131 |
return n, fmt.Errorf("duplicate EOF read") |
| 2132 |
} |
| 2133 |
r.gaveEOF = true |
| 2134 |
} |
| 2135 |
return n, err |
| 2136 |
} |
| 2137 |
|
| 2138 |
func TestParseStmtsSeq(t *testing.T) { |
| 2139 |
t.Parallel() |
| 2140 |
p := NewParser() |
| 2141 |
inReader, inWriter := io.Pipe() |
| 2142 |
recv := make(chan bool, 10) |
| 2143 |
errc := make(chan error, 1) |
| 2144 |
go func() { |
| 2145 |
var firstErr error |
| 2146 |
for _, err := range p.StmtsSeq(inReader) { |
| 2147 |
recv <- true |
| 2148 |
if firstErr == nil && err != nil { |
| 2149 |
firstErr = err |
| 2150 |
} |
| 2151 |
} |
| 2152 |
errc <- firstErr |
| 2153 |
}() |
| 2154 |
io.WriteString(inWriter, "foo\n") |
| 2155 |
<-recv |
| 2156 |
io.WriteString(inWriter, "bar; baz") |
| 2157 |
inWriter.Close() |
| 2158 |
<-recv |
| 2159 |
<-recv |
| 2160 |
if err := <-errc; err != nil { |
| 2161 |
t.Fatalf("Expected no error: %v", err) |
| 2162 |
} |
| 2163 |
} |
| 2164 |
|
| 2165 |
func TestParseStmtsSeqStopEarly(t *testing.T) { |
| 2166 |
t.Parallel() |
| 2167 |
p := NewParser() |
| 2168 |
inReader, inWriter := io.Pipe() |
| 2169 |
defer inWriter.Close() |
| 2170 |
recv := make(chan bool, 10) |
| 2171 |
errc := make(chan error, 1) |
| 2172 |
go func() { |
| 2173 |
var firstErr error |
| 2174 |
for stmt, err := range p.StmtsSeq(inReader) { |
| 2175 |
recv <- true |
| 2176 |
if firstErr == nil && err != nil { |
| 2177 |
firstErr = err |
| 2178 |
} |
| 2179 |
if stmt.Background { |
| 2180 |
break |
| 2181 |
} |
| 2182 |
} |
| 2183 |
errc <- firstErr |
| 2184 |
}() |
| 2185 |
io.WriteString(inWriter, "a\n") |
| 2186 |
<-recv |
| 2187 |
io.WriteString(inWriter, "b &\n") // stop here |
| 2188 |
<-recv |
| 2189 |
if err := <-errc; err != nil { |
| 2190 |
t.Fatalf("Expected no error: %v", err) |
| 2191 |
} |
| 2192 |
} |
| 2193 |
|
| 2194 |
func TestParseStmtsSeqError(t *testing.T) { |
| 2195 |
t.Parallel() |
| 2196 |
for _, in := range []string{ |
| 2197 |
"foo; )", |
| 2198 |
"bar; <<EOF", |
| 2199 |
} { |
| 2200 |
t.Run("", func(t *testing.T) { |
| 2201 |
p := NewParser() |
| 2202 |
recv := make(chan bool, 10) |
| 2203 |
errc := make(chan error, 1) |
| 2204 |
go func() { |
| 2205 |
var firstErr error |
| 2206 |
for _, err := range p.StmtsSeq(strings.NewReader(in)) { |
| 2207 |
recv <- true |
| 2208 |
if firstErr == nil && err != nil { |
| 2209 |
firstErr = err |
| 2210 |
} |
| 2211 |
} |
| 2212 |
errc <- firstErr |
| 2213 |
}() |
| 2214 |
<-recv |
| 2215 |
if err := <-errc; err == nil { |
| 2216 |
t.Fatalf("Expected an error in %q, but got nil", in) |
| 2217 |
} |
| 2218 |
}) |
| 2219 |
} |
| 2220 |
} |
| 2221 |
|
| 2222 |
func TestParseWords(t *testing.T) { |
| 2223 |
t.Parallel() |
| 2224 |
p := NewParser() |
| 2225 |
inReader, inWriter := io.Pipe() |
| 2226 |
recv := make(chan bool, 10) |
| 2227 |
errc := make(chan error, 1) |
| 2228 |
go func() { |
| 2229 |
errc <- p.Words(inReader, func(w *Word) bool { |
| 2230 |
recv <- true |
| 2231 |
return true |
| 2232 |
}) |
| 2233 |
}() |
| 2234 |
// TODO: Allow a single space to end parsing a word. At the moment, the |
| 2235 |
// parser must read the next non-space token (the next literal or |
| 2236 |
// newline, in this case) to finish parsing a word. |
| 2237 |
io.WriteString(inWriter, "foo ") |
| 2238 |
io.WriteString(inWriter, "bar\n") |
| 2239 |
<-recv |
| 2240 |
io.WriteString(inWriter, "baz etc") |
| 2241 |
inWriter.Close() |
| 2242 |
<-recv |
| 2243 |
<-recv |
| 2244 |
<-recv |
| 2245 |
if err := <-errc; err != nil { |
| 2246 |
t.Fatalf("Expected no error: %v", err) |
| 2247 |
} |
| 2248 |
} |
| 2249 |
|
| 2250 |
func TestParseWordsStopEarly(t *testing.T) { |
| 2251 |
t.Parallel() |
| 2252 |
p := NewParser() |
| 2253 |
r := strings.NewReader("a\nb\nc\n") |
| 2254 |
parsed := 0 |
| 2255 |
err := p.Words(r, func(w *Word) bool { |
| 2256 |
parsed++ |
| 2257 |
return w.Lit() != "b" |
| 2258 |
}) |
| 2259 |
if err != nil { |
| 2260 |
t.Fatalf("Expected no error: %v", err) |
| 2261 |
} |
| 2262 |
if want := 2; parsed != want { |
| 2263 |
t.Fatalf("wanted %d words parsed, got %d", want, parsed) |
| 2264 |
} |
| 2265 |
} |
| 2266 |
|
| 2267 |
func TestParseWordsError(t *testing.T) { |
| 2268 |
t.Parallel() |
| 2269 |
in := "foo )" |
| 2270 |
p := NewParser() |
| 2271 |
recv := make(chan bool, 10) |
| 2272 |
errc := make(chan error, 1) |
| 2273 |
go func() { |
| 2274 |
errc <- p.Words(strings.NewReader(in), func(w *Word) bool { |
| 2275 |
recv <- true |
| 2276 |
return true |
| 2277 |
}) |
| 2278 |
}() |
| 2279 |
<-recv |
| 2280 |
want := "1:5: `)` is not a valid word" |
| 2281 |
got := fmt.Sprintf("%v", <-errc) |
| 2282 |
if got != want { |
| 2283 |
t.Fatalf("Expected %q as an error, but got %q", want, got) |
| 2284 |
} |
| 2285 |
} |
| 2286 |
|
| 2287 |
var documentTests = []struct { |
| 2288 |
in string |
| 2289 |
want []WordPart |
| 2290 |
}{ |
| 2291 |
{ |
| 2292 |
"foo", |
| 2293 |
[]WordPart{lit("foo")}, |
| 2294 |
}, |
| 2295 |
{ |
| 2296 |
" foo $bar", |
| 2297 |
[]WordPart{ |
| 2298 |
lit(" foo "), |
| 2299 |
litParamExp("bar"), |
| 2300 |
}, |
| 2301 |
}, |
| 2302 |
{ |
| 2303 |
"$bar\n\n", |
| 2304 |
[]WordPart{ |
| 2305 |
litParamExp("bar"), |
| 2306 |
lit("\n\n"), |
| 2307 |
}, |
| 2308 |
}, |
| 2309 |
} |
| 2310 |
|
| 2311 |
func TestParseDocument(t *testing.T) { |
| 2312 |
t.Parallel() |
| 2313 |
p := NewParser() |
| 2314 |
|
| 2315 |
for _, tc := range documentTests { |
| 2316 |
t.Run("", func(t *testing.T) { |
| 2317 |
got, err := p.Document(strings.NewReader(tc.in)) |
| 2318 |
if err != nil { |
| 2319 |
t.Fatal(err) |
| 2320 |
} |
| 2321 |
Walk(got, sanityChecker{tb: t, src: tc.in}.visit) |
| 2322 |
want := &Word{Parts: tc.want} |
| 2323 |
qt.Assert(t, qt.CmpEquals(got, want, cmpOpt)) |
| 2324 |
}) |
| 2325 |
} |
| 2326 |
} |
| 2327 |
|
| 2328 |
func TestParseDocumentError(t *testing.T) { |
| 2329 |
t.Parallel() |
| 2330 |
in := "foo $(" |
| 2331 |
p := NewParser() |
| 2332 |
_, err := p.Document(strings.NewReader(in)) |
| 2333 |
want := "1:5: reached EOF without matching `$(` with `)`" |
| 2334 |
got := fmt.Sprintf("%v", err) |
| 2335 |
if got != want { |
| 2336 |
t.Fatalf("Expected %q as an error, but got %q", want, got) |
| 2337 |
} |
| 2338 |
} |
| 2339 |
|
| 2340 |
var arithmeticTests = []struct { |
| 2341 |
in string |
| 2342 |
want ArithmExpr |
| 2343 |
}{ |
| 2344 |
{ |
| 2345 |
"foo", |
| 2346 |
litWord("foo"), |
| 2347 |
}, |
| 2348 |
{ |
| 2349 |
"3 + 4", |
| 2350 |
&BinaryArithm{ |
| 2351 |
Op: Add, |
| 2352 |
X: litWord("3"), |
| 2353 |
Y: litWord("4"), |
| 2354 |
}, |
| 2355 |
}, |
| 2356 |
{ |
| 2357 |
"3 + 4 + 5", |
| 2358 |
&BinaryArithm{ |
| 2359 |
Op: Add, |
| 2360 |
X: &BinaryArithm{ |
| 2361 |
Op: Add, |
| 2362 |
X: litWord("3"), |
| 2363 |
Y: litWord("4"), |
| 2364 |
}, |
| 2365 |
Y: litWord("5"), |
| 2366 |
}, |
| 2367 |
}, |
| 2368 |
{ |
| 2369 |
"1 ? 0 : 2", |
| 2370 |
&BinaryArithm{ |
| 2371 |
Op: TernQuest, |
| 2372 |
X: litWord("1"), |
| 2373 |
Y: &BinaryArithm{ |
| 2374 |
Op: TernColon, |
| 2375 |
X: litWord("0"), |
| 2376 |
Y: litWord("2"), |
| 2377 |
}, |
| 2378 |
}, |
| 2379 |
}, |
| 2380 |
{ |
| 2381 |
"a = 3, ++a, a--", |
| 2382 |
&BinaryArithm{ |
| 2383 |
Op: Comma, |
| 2384 |
X: &BinaryArithm{ |
| 2385 |
Op: Comma, |
| 2386 |
X: &BinaryArithm{ |
| 2387 |
Op: Assgn, |
| 2388 |
X: litWord("a"), |
| 2389 |
Y: litWord("3"), |
| 2390 |
}, |
| 2391 |
Y: &UnaryArithm{ |
| 2392 |
Op: Inc, |
| 2393 |
X: litWord("a"), |
| 2394 |
}, |
| 2395 |
}, |
| 2396 |
Y: &UnaryArithm{ |
| 2397 |
Op: Dec, |
| 2398 |
Post: true, |
| 2399 |
X: litWord("a"), |
| 2400 |
}, |
| 2401 |
}, |
| 2402 |
}, |
| 2403 |
} |
| 2404 |
|
| 2405 |
func TestParseArithmetic(t *testing.T) { |
| 2406 |
t.Parallel() |
| 2407 |
p := NewParser() |
| 2408 |
|
| 2409 |
for _, tc := range arithmeticTests { |
| 2410 |
t.Run("", func(t *testing.T) { |
| 2411 |
got, err := p.Arithmetic(strings.NewReader(tc.in)) |
| 2412 |
if err != nil { |
| 2413 |
t.Fatal(err) |
| 2414 |
} |
| 2415 |
Walk(got, sanityChecker{tb: t, src: tc.in}.visit) |
| 2416 |
qt.Assert(t, qt.CmpEquals(got, tc.want, cmpOpt)) |
| 2417 |
}) |
| 2418 |
} |
| 2419 |
} |
| 2420 |
|
| 2421 |
func TestParseArithmeticError(t *testing.T) { |
| 2422 |
t.Parallel() |
| 2423 |
in := "3 +" |
| 2424 |
p := NewParser() |
| 2425 |
_, err := p.Arithmetic(strings.NewReader(in)) |
| 2426 |
want := "1:3: `+` must be followed by an expression" |
| 2427 |
got := fmt.Sprintf("%v", err) |
| 2428 |
if got != want { |
| 2429 |
t.Fatalf("Expected %q as an error, but got %q", want, got) |
| 2430 |
} |
| 2431 |
} |
| 2432 |
|
| 2433 |
var stopAtTests = []struct { |
| 2434 |
in string |
| 2435 |
stop string |
| 2436 |
want any |
| 2437 |
}{ |
| 2438 |
{ |
| 2439 |
"foo bar", "$$", |
| 2440 |
litCall("foo", "bar"), |
| 2441 |
}, |
| 2442 |
{ |
| 2443 |
"$foo $", "$$", |
| 2444 |
call(word(litParamExp("foo")), litWord("$")), |
| 2445 |
}, |
| 2446 |
{ |
| 2447 |
"echo foo $$", "$$", |
| 2448 |
litCall("echo", "foo"), |
| 2449 |
}, |
| 2450 |
{ |
| 2451 |
"$$", "$$", |
| 2452 |
&File{}, |
| 2453 |
}, |
| 2454 |
{ |
| 2455 |
"echo foo\n$$\n", "$$", |
| 2456 |
litCall("echo", "foo"), |
| 2457 |
}, |
| 2458 |
{ |
| 2459 |
"echo foo; $$", "$$", |
| 2460 |
litCall("echo", "foo"), |
| 2461 |
}, |
| 2462 |
{ |
| 2463 |
"echo foo; $$", "$$", |
| 2464 |
litCall("echo", "foo"), |
| 2465 |
}, |
| 2466 |
{ |
| 2467 |
"echo foo;$$", "$$", |
| 2468 |
litCall("echo", "foo"), |
| 2469 |
}, |
| 2470 |
{ |
| 2471 |
"echo '$$'", "$$", |
| 2472 |
call(litWord("echo"), word(sglQuoted("$$"))), |
| 2473 |
}, |
| 2474 |
{ |
| 2475 |
// A trailing backslash empties the read buffer as we peek at what |
| 2476 |
// follows it, so the stop word cannot be matched against it. |
| 2477 |
"\\", "0", |
| 2478 |
litCall("\\"), |
| 2479 |
}, |
| 2480 |
} |
| 2481 |
|
| 2482 |
func TestParseStopAt(t *testing.T) { |
| 2483 |
t.Parallel() |
| 2484 |
for _, c := range stopAtTests { |
| 2485 |
p := NewParser(StopAt(c.stop)) |
| 2486 |
want := fullProg(c.want) |
| 2487 |
t.Run("", singleParse(p, c.in, want)) |
| 2488 |
} |
| 2489 |
} |
| 2490 |
|
| 2491 |
func TestValidName(t *testing.T) { |
| 2492 |
t.Parallel() |
| 2493 |
tests := []struct { |
| 2494 |
name string |
| 2495 |
in string |
| 2496 |
want bool |
| 2497 |
}{ |
| 2498 |
{"Empty", "", false}, |
| 2499 |
{"Simple", "foo", true}, |
| 2500 |
{"MixedCase", "Foo", true}, |
| 2501 |
{"Underscore", "_foo", true}, |
| 2502 |
{"NumberPrefix", "3foo", false}, |
| 2503 |
{"NumberSuffix", "foo3", true}, |
| 2504 |
} |
| 2505 |
for _, tc := range tests { |
| 2506 |
t.Run(tc.name, func(t *testing.T) { |
| 2507 |
got := ValidName(tc.in) |
| 2508 |
if got != tc.want { |
| 2509 |
t.Fatalf("ValidName(%q) got %t, wanted %t", |
| 2510 |
tc.in, got, tc.want) |
| 2511 |
} |
| 2512 |
}) |
| 2513 |
} |
| 2514 |
} |
| 2515 |
|
| 2516 |
func TestIsIncomplete(t *testing.T) { |
| 2517 |
t.Parallel() |
| 2518 |
|
| 2519 |
tests := []struct { |
| 2520 |
in string |
| 2521 |
notWords bool |
| 2522 |
want bool |
| 2523 |
}{ |
| 2524 |
{in: "foo\n", want: false}, |
| 2525 |
{in: "foo;", want: false}, |
| 2526 |
{in: "\n", want: false}, |
| 2527 |
{in: "badsyntax)", want: false}, |
| 2528 |
{in: "foo 'incomp", want: true}, |
| 2529 |
{in: `foo "incomp`, want: true}, |
| 2530 |
{in: "foo ${incomp", want: true}, |
| 2531 |
|
| 2532 |
{in: "foo; 'incomp", notWords: true, want: true}, |
| 2533 |
{in: `foo; "incomp`, notWords: true, want: true}, |
| 2534 |
{in: " (incomp", notWords: true, want: true}, |
| 2535 |
} |
| 2536 |
p := NewParser() |
| 2537 |
for i, tc := range tests { |
| 2538 |
t.Run(fmt.Sprintf("Parse%02d", i), func(t *testing.T) { |
| 2539 |
r := strings.NewReader(tc.in) |
| 2540 |
_, err := p.Parse(r, "") |
| 2541 |
if got := IsIncomplete(err); got != tc.want { |
| 2542 |
t.Fatalf("%q got %t, wanted %t", tc.in, got, tc.want) |
| 2543 |
} |
| 2544 |
}) |
| 2545 |
t.Run(fmt.Sprintf("Interactive%02d", i), func(t *testing.T) { |
| 2546 |
r := strings.NewReader(tc.in) |
| 2547 |
var firstErr error |
| 2548 |
for _, err := range p.InteractiveSeq(r) { |
| 2549 |
if firstErr == nil && err != nil { |
| 2550 |
firstErr = err |
| 2551 |
} |
| 2552 |
} |
| 2553 |
if got := IsIncomplete(firstErr); got != tc.want { |
| 2554 |
t.Fatalf("%q got %t, wanted %t", tc.in, got, tc.want) |
| 2555 |
} |
| 2556 |
}) |
| 2557 |
if !tc.notWords { |
| 2558 |
t.Run(fmt.Sprintf("WordsSeq%02d", i), func(t *testing.T) { |
| 2559 |
r := strings.NewReader(tc.in) |
| 2560 |
var firstErr error |
| 2561 |
for _, err := range p.WordsSeq(r) { |
| 2562 |
if firstErr == nil && err != nil { |
| 2563 |
firstErr = err |
| 2564 |
} |
| 2565 |
} |
| 2566 |
if got := IsIncomplete(firstErr); got != tc.want { |
| 2567 |
t.Fatalf("%q got %t, wanted %t", tc.in, got, tc.want) |
| 2568 |
} |
| 2569 |
}) |
| 2570 |
} |
| 2571 |
} |
| 2572 |
} |
| 2573 |
|
| 2574 |
func TestPosEdgeCases(t *testing.T) { |
| 2575 |
in := "`\\\\foo`\n" + // one escaped backslash and 3 bytes |
| 2576 |
"\x00foo\x00bar\n" // 8 bytes and newline |
| 2577 |
p := NewParser() |
| 2578 |
f, err := p.Parse(strings.NewReader(in), "") |
| 2579 |
qt.Assert(t, qt.IsNil(err)) |
| 2580 |
cmdSubst := f.Stmts[0].Cmd.(*CallExpr).Args[0].Parts[0].(*CmdSubst) |
| 2581 |
lit := cmdSubst.Stmts[0].Cmd.(*CallExpr).Args[0].Parts[0].(*Lit) |
| 2582 |
|
| 2583 |
qt.Check(t, qt.Equals(lit.Value, lit.Value)) |
| 2584 |
// Note that positions of literals with escape sequences inside backquote command substitutions |
| 2585 |
// are weird, since we effectively skip over the double escaping in the literal value and positions. |
| 2586 |
// Even though the input source has '\\foo' between columns 2 and 7 (length 5) |
| 2587 |
// we end up keeping '\foo' between columns 3 and 7 (length 4). |
| 2588 |
qt.Check(t, qt.Equals(lit.ValuePos.String(), "1:3")) |
| 2589 |
qt.Check(t, qt.Equals(lit.ValueEnd.String(), "1:7")) |
| 2590 |
|
| 2591 |
// Check that we skip over null bytes when counting columns. |
| 2592 |
qt.Check(t, qt.Equals(f.Stmts[1].Pos().String(), "2:2")) |
| 2593 |
qt.Check(t, qt.Equals(f.Stmts[1].End().String(), "2:9")) |
| 2594 |
} |
| 2595 |
|
| 2596 |
func TestPosAddCol(t *testing.T) { |
| 2597 |
t.Parallel() |
| 2598 |
|
| 2599 |
tests := []struct { |
| 2600 |
name string |
| 2601 |
pos Pos |
| 2602 |
n int |
| 2603 |
want string // as printed by [Pos.String] |
| 2604 |
wantOffset uint |
| 2605 |
}{ |
| 2606 |
{"Add", NewPos(10, 5, 3), 2, "5:5", 12}, |
| 2607 |
{"Subtract", NewPos(10, 5, 3), -2, "5:1", 8}, |
| 2608 |
{"UnknownCol", NewPos(10, 5, 0), 2, "5:?", 12}, |
| 2609 |
{"ColOverflow", NewPos(10, 5, colMax), 2, "5:?", 12}, |
| 2610 |
{"ColUnderflow", NewPos(10, 5, 1), -2, "5:?", 8}, |
| 2611 |
{"OffsetOverflow", NewPos(offsetMax, 5, 3), 2, "5:5", offsetMax}, |
| 2612 |
{"OffsetUnderflow", NewPos(0, 1, 1), -2, "1:?", 0}, |
| 2613 |
} |
| 2614 |
for _, test := range tests { |
| 2615 |
t.Run(test.name, func(t *testing.T) { |
| 2616 |
t.Parallel() |
| 2617 |
|
| 2618 |
got := posAddCol(test.pos, test.n) |
| 2619 |
qt.Check(t, qt.IsTrue(got.IsValid())) |
| 2620 |
qt.Check(t, qt.Equals(got.String(), test.want)) |
| 2621 |
qt.Check(t, qt.Equals(got.Offset(), test.wantOffset)) |
| 2622 |
}) |
| 2623 |
} |
| 2624 |
} |
| 2625 |
|
| 2626 |
func TestParseHighControlRunes(t *testing.T) { |
| 2627 |
t.Parallel() |
| 2628 |
// U+0080 and U+0081 must parse as regular characters even though the |
| 2629 |
// lexer uses rune sentinels for "reached EOF" and "escaped newline". |
| 2630 |
tests := []struct { |
| 2631 |
in, want string // want is an error string when wantErr |
| 2632 |
wantErr bool |
| 2633 |
}{ |
| 2634 |
{in: "echo a\u0080b", want: "echo a\u0080b\n"}, |
| 2635 |
{in: "echo 'a\u0080b'", want: "echo 'a\u0080b'\n"}, |
| 2636 |
{in: "echo \"a\u0080b\"", want: "echo \"a\u0080b\"\n"}, |
| 2637 |
{in: "echo a\u0081b", want: "echo a\u0081b\n"}, |
| 2638 |
{in: "echo 'a\u0081b'", want: "echo 'a\u0081b'\n"}, |
| 2639 |
{in: "echo \"a\u0081b\"", want: "echo \"a\u0081b\"\n"}, |
| 2640 |
} |
| 2641 |
p := NewParser() |
| 2642 |
printer := NewPrinter() |
| 2643 |
for _, tc := range tests { |
| 2644 |
f, err := p.Parse(strings.NewReader(tc.in), "") |
| 2645 |
if tc.wantErr { |
| 2646 |
qt.Assert(t, qt.ErrorMatches(err, regexp.QuoteMeta(tc.want)), qt.Commentf("input: %q", tc.in)) |
| 2647 |
continue |
| 2648 |
} |
| 2649 |
qt.Assert(t, qt.IsNil(err), qt.Commentf("input: %q", tc.in)) |
| 2650 |
var sb strings.Builder |
| 2651 |
qt.Assert(t, qt.IsNil(printer.Print(&sb, f))) |
| 2652 |
qt.Check(t, qt.Equals(sb.String(), tc.want), qt.Commentf("input: %q", tc.in)) |
| 2653 |
} |
| 2654 |
} |
| 2655 |
|
| 2656 |
func TestNodeEndPos(t *testing.T) { |
| 2657 |
t.Parallel() |
| 2658 |
|
| 2659 |
// An array element with an index but no value ends after the "]=". |
| 2660 |
p := NewParser() |
| 2661 |
f, err := p.Parse(strings.NewReader("declare -A x=([index]=)"), "") |
| 2662 |
qt.Assert(t, qt.IsNil(err)) |
| 2663 |
elem := f.Stmts[0].Cmd.(*DeclClause).Args[1].Array.Elems[0] |
| 2664 |
qt.Check(t, qt.Equals(elem.End().Offset(), uint(22))) |
| 2665 |
|
| 2666 |
// A trailing comment ends the file even when a leading comment exists. |
| 2667 |
p = NewParser(KeepComments(true)) |
| 2668 |
f, err = p.Parse(strings.NewReader("# lead\nfoo # trail\n"), "") |
| 2669 |
qt.Assert(t, qt.IsNil(err)) |
| 2670 |
qt.Check(t, qt.Equals(f.End().Offset(), uint(18))) |
| 2671 |
|
| 2672 |
// A naked indexed assignment ends after the "]"; there is no "=". |
| 2673 |
f, err = p.Parse(strings.NewReader("declare a[1]"), "") |
| 2674 |
qt.Assert(t, qt.IsNil(err)) |
| 2675 |
qt.Check(t, qt.Equals(f.Stmts[0].Cmd.(*DeclClause).Args[0].End().Offset(), uint(12))) |
| 2676 |
|
| 2677 |
// The mksh brace forms of for and case clauses end after the "}", |
| 2678 |
// not four bytes as if it were "done" or "esac". |
| 2679 |
p = NewParser(Variant(LangMirBSDKorn)) |
| 2680 |
f, err = p.Parse(strings.NewReader("for i in a; { b; }"), "") |
| 2681 |
qt.Assert(t, qt.IsNil(err)) |
| 2682 |
qt.Check(t, qt.Equals(f.Stmts[0].Cmd.(*ForClause).End().Offset(), uint(18))) |
| 2683 |
|
| 2684 |
f, err = p.Parse(strings.NewReader("case x { a) b ;; }"), "") |
| 2685 |
qt.Assert(t, qt.IsNil(err)) |
| 2686 |
qt.Check(t, qt.Equals(f.Stmts[0].Cmd.(*CaseClause).End().Offset(), uint(18))) |
| 2687 |
} |
| 2688 |
|
| 2689 |
func TestParseRecoverErrors(t *testing.T) { |
| 2690 |
t.Parallel() |
| 2691 |
|
| 2692 |
tests := []struct { |
| 2693 |
src string |
| 2694 |
|
| 2695 |
wantErr bool |
| 2696 |
wantMissing int |
| 2697 |
}{ |
| 2698 |
{src: "foo;"}, |
| 2699 |
{src: "foo"}, |
| 2700 |
{ |
| 2701 |
src: "'incomp", |
| 2702 |
wantMissing: 1, |
| 2703 |
}, |
| 2704 |
{ |
| 2705 |
src: "foo; 'incomp", |
| 2706 |
wantMissing: 1, |
| 2707 |
}, |
| 2708 |
{ |
| 2709 |
src: "{ incomp", |
| 2710 |
wantMissing: 1, |
| 2711 |
}, |
| 2712 |
{ |
| 2713 |
src: "(incomp", |
| 2714 |
wantMissing: 1, |
| 2715 |
}, |
| 2716 |
{ |
| 2717 |
src: "(incomp; foo", |
| 2718 |
wantMissing: 1, |
| 2719 |
}, |
| 2720 |
{ |
| 2721 |
src: "$(incomp", |
| 2722 |
wantMissing: 1, |
| 2723 |
}, |
| 2724 |
{ |
| 2725 |
src: "((incomp", |
| 2726 |
wantMissing: 1, |
| 2727 |
}, |
| 2728 |
{ |
| 2729 |
src: "$((incomp", |
| 2730 |
wantMissing: 1, |
| 2731 |
}, |
| 2732 |
{ |
| 2733 |
src: "$[incomp", |
| 2734 |
wantMissing: 1, |
| 2735 |
}, |
| 2736 |
{ |
| 2737 |
src: "if foo", |
| 2738 |
wantMissing: 3, |
| 2739 |
}, |
| 2740 |
{ |
| 2741 |
src: "if foo; then bar", |
| 2742 |
wantMissing: 1, |
| 2743 |
}, |
| 2744 |
{ |
| 2745 |
src: "for i in 1 2 3; echo $i; done", |
| 2746 |
wantMissing: 1, |
| 2747 |
}, |
| 2748 |
{ |
| 2749 |
src: `"incomp`, |
| 2750 |
wantMissing: 1, |
| 2751 |
}, |
| 2752 |
{ |
| 2753 |
src: "`incomp", |
| 2754 |
wantMissing: 1, |
| 2755 |
}, |
| 2756 |
{ |
| 2757 |
src: "incomp >", |
| 2758 |
wantMissing: 1, |
| 2759 |
}, |
| 2760 |
{ |
| 2761 |
src: "${incomp", |
| 2762 |
wantMissing: 1, |
| 2763 |
}, |
| 2764 |
{ |
| 2765 |
src: "incomp | ", |
| 2766 |
wantMissing: 1, |
| 2767 |
}, |
| 2768 |
{ |
| 2769 |
src: "incomp || ", |
| 2770 |
wantMissing: 1, |
| 2771 |
}, |
| 2772 |
{ |
| 2773 |
src: "incomp && ", |
| 2774 |
wantMissing: 1, |
| 2775 |
}, |
| 2776 |
{ |
| 2777 |
src: `(one | { two >`, |
| 2778 |
wantMissing: 3, |
| 2779 |
}, |
| 2780 |
{ |
| 2781 |
src: `(one > ; two | ); { three`, |
| 2782 |
wantMissing: 3, |
| 2783 |
}, |
| 2784 |
{ |
| 2785 |
src: "badsyntax)", |
| 2786 |
wantErr: true, |
| 2787 |
}, |
| 2788 |
} |
| 2789 |
parser := NewParser(RecoverErrors(3)) |
| 2790 |
printer := NewPrinter() |
| 2791 |
for _, tc := range tests { |
| 2792 |
t.Run("", func(t *testing.T) { |
| 2793 |
t.Logf("input: %s", tc.src) |
| 2794 |
r := strings.NewReader(tc.src) |
| 2795 |
f, err := parser.Parse(r, "") |
| 2796 |
if tc.wantErr { |
| 2797 |
qt.Assert(t, qt.Not(qt.IsNil(err))) |
| 2798 |
} else { |
| 2799 |
qt.Assert(t, qt.IsNil(err)) |
| 2800 |
switch len(f.Stmts) { |
| 2801 |
case 0: |
| 2802 |
t.Fatalf("result has no statements") |
| 2803 |
case 1: |
| 2804 |
if f.Stmts[0].Pos().IsRecovered() { |
| 2805 |
t.Fatalf("result is only a recovered statement") |
| 2806 |
} |
| 2807 |
} |
| 2808 |
} |
| 2809 |
qt.Assert(t, qt.Equals(countRecoveredPositions(reflect.ValueOf(f)), tc.wantMissing)) |
| 2810 |
|
| 2811 |
// Check that walking or printing the syntax tree still appears to work |
| 2812 |
// even when the input source was incomplete. |
| 2813 |
Walk(f, func(node Node) bool { |
| 2814 |
if node == nil { |
| 2815 |
return true |
| 2816 |
} |
| 2817 |
// Each position should either be valid, pointing to an offset within the input, |
| 2818 |
// or invalid, which could be due to the position being recovered. |
| 2819 |
for _, pos := range []Pos{node.Pos(), node.End()} { |
| 2820 |
qt.Assert(t, qt.IsFalse(pos.IsValid() && pos.IsRecovered()), qt.Commentf("positions cannot be valid and recovered")) |
| 2821 |
if !pos.IsValid() { |
| 2822 |
qt.Assert(t, qt.Equals(pos.Offset(), 0), qt.Commentf("invalid positions have no offset")) |
| 2823 |
qt.Assert(t, qt.Equals(pos.Line(), 0), qt.Commentf("invalid positions have no line")) |
| 2824 |
qt.Assert(t, qt.Equals(pos.Col(), 0), qt.Commentf("invalid positions have no column")) |
| 2825 |
} |
| 2826 |
} |
| 2827 |
return true |
| 2828 |
}) |
| 2829 |
// Note that we don't particularly care about good formatting here. |
| 2830 |
printer.Print(io.Discard, f) |
| 2831 |
}) |
| 2832 |
} |
| 2833 |
} |
| 2834 |
|
| 2835 |
func countRecoveredPositions(x reflect.Value) int { |
| 2836 |
switch x.Kind() { |
| 2837 |
case reflect.Interface: |
| 2838 |
return countRecoveredPositions(x.Elem()) |
| 2839 |
case reflect.Pointer: |
| 2840 |
if !x.IsNil() { |
| 2841 |
return countRecoveredPositions(x.Elem()) |
| 2842 |
} |
| 2843 |
case reflect.Slice: |
| 2844 |
n := 0 |
| 2845 |
for i := range x.Len() { |
| 2846 |
n += countRecoveredPositions(x.Index(i)) |
| 2847 |
} |
| 2848 |
return n |
| 2849 |
case reflect.Struct: |
| 2850 |
if pos, ok := reflect.TypeAssert[Pos](x); ok { |
| 2851 |
if pos.IsRecovered() { |
| 2852 |
return 1 |
| 2853 |
} |
| 2854 |
return 0 |
| 2855 |
} |
| 2856 |
n := 0 |
| 2857 |
for _, field := range x.Fields() { |
| 2858 |
n += countRecoveredPositions(field) |
| 2859 |
} |
| 2860 |
return n |
| 2861 |
} |
| 2862 |
return 0 |
| 2863 |
} |