Debian Codegraph @

syntax/filetests_test.go

raw
1 // Copyright (c) 2016, Daniel Martí <[email protected]>
2 // See LICENSE for licensing information
3
4 package syntax
5
6 import (
7 "reflect"
8 "strings"
9 "testing"
10 )
11
12 func lit(s string) *Lit { return &Lit{Value: s} }
13 func lits(strs ...string) []*Lit {
14 l := make([]*Lit, 0, len(strs))
15 for _, s := range strs {
16 l = append(l, lit(s))
17 }
18 return l
19 }
20 func word(ps ...WordPart) *Word { return &Word{Parts: ps} }
21 func litWord(s string) *Word { return word(lit(s)) }
22 func litWords(strs ...string) []*Word {
23 l := make([]*Word, 0, len(strs))
24 for _, s := range strs {
25 l = append(l, litWord(s))
26 }
27 return l
28 }
29
30 func litAssigns(pairs ...string) []*Assign {
31 l := make([]*Assign, len(pairs))
32 for i, pair := range pairs {
33 name, val, ok := strings.Cut(pair, "=")
34 if !ok {
35 l[i] = &Assign{Naked: true, Name: lit(name)}
36 } else if val == "" {
37 l[i] = &Assign{Name: lit(name)}
38 } else {
39 l[i] = &Assign{Name: lit(name), Value: litWord(val)}
40 }
41 }
42 return l
43 }
44
45 func call(words ...*Word) *CallExpr { return &CallExpr{Args: words} }
46 func litCall(strs ...string) *CallExpr { return call(litWords(strs...)...) }
47
48 func stmt(cmd Command) *Stmt { return &Stmt{Cmd: cmd} }
49 func stmts(cmds ...Command) []*Stmt {
50 l := make([]*Stmt, len(cmds))
51 for i, cmd := range cmds {
52 l[i] = stmt(cmd)
53 }
54 return l
55 }
56
57 func litStmt(strs ...string) *Stmt { return stmt(litCall(strs...)) }
58 func litStmts(strs ...string) []*Stmt {
59 l := make([]*Stmt, len(strs))
60 for i, s := range strs {
61 l[i] = litStmt(s)
62 }
63 return l
64 }
65
66 func sglQuoted(s string) *SglQuoted { return &SglQuoted{Value: s} }
67 func sglDQuoted(s string) *SglQuoted { return &SglQuoted{Dollar: true, Value: s} }
68 func dblQuoted(ps ...WordPart) *DblQuoted { return &DblQuoted{Parts: ps} }
69 func dblDQuoted(ps ...WordPart) *DblQuoted { return &DblQuoted{Dollar: true, Parts: ps} }
70 func block(sts ...*Stmt) *Block { return &Block{Stmts: sts} }
71 func subshell(sts ...*Stmt) *Subshell { return &Subshell{Stmts: sts} }
72 func arithmExp(e ArithmExpr) *ArithmExp { return &ArithmExp{X: e} }
73 func arithmExpBr(e ArithmExpr) *ArithmExp { return &ArithmExp{Bracket: true, X: e} }
74 func arithmCmd(e ArithmExpr) *ArithmCmd { return &ArithmCmd{X: e} }
75 func parenArit(e ArithmExpr) *ParenArithm { return &ParenArithm{X: e} }
76 func parenTest(e TestExpr) *ParenTest { return &ParenTest{X: e} }
77
78 func cmdSubst(sts ...*Stmt) *CmdSubst { return &CmdSubst{Stmts: sts} }
79 func litParamExp(s string) *ParamExp {
80 return &ParamExp{Short: true, Param: lit(s)}
81 }
82
83 func letClause(exps ...ArithmExpr) *LetClause {
84 return &LetClause{Exprs: exps}
85 }
86
87 func arrValues(words ...*Word) *ArrayExpr {
88 ae := &ArrayExpr{}
89 for _, w := range words {
90 ae.Elems = append(ae.Elems, &ArrayElem{Value: w})
91 }
92 return ae
93 }
94
95 func fullProg(v any) *File {
96 f := &File{}
97 switch v := v.(type) {
98 case *File:
99 return v
100 case []*Stmt:
101 f.Stmts = v
102 return f
103 case *Stmt:
104 f.Stmts = append(f.Stmts, v)
105 return f
106 case []Command:
107 for _, cmd := range v {
108 f.Stmts = append(f.Stmts, stmt(cmd))
109 }
110 return f
111 case *Word:
112 return fullProg(call(v))
113 case WordPart:
114 return fullProg(word(v))
115 case Command:
116 return fullProg(stmt(v))
117 case nil:
118 default:
119 panic(reflect.TypeOf(v))
120 }
121 return nil
122 }
123
124 type fileTestCase struct {
125 inputs []string // input sources; the first is the canonical formatting
126
127 // printedAs is the canonical formatting when it differs from inputs[0],
128 // such as a lone trailing backslash being escaped by the printer.
129 printedAs string
130
131 // Each language in [langResolvedVariants] has an entry:
132 // - nil: nothing to test
133 // - *File: parse as the given syntax tree
134 // - string: parse error with the given string, substituting LANG
135 byLangIndex [langResolvedVariantsCount]any
136
137 // The real shells where testing the input succeeds or fails in the opposite way.
138 flipConfirmSet LangVariant
139 }
140
141 func flipConfirm2(langSet LangVariant) func(*fileTestCase) {
142 return func(c *fileTestCase) { c.flipConfirmSet = langSet }
143 }
144
145 func (c *fileTestCase) setForLangs(val any, langSets ...LangVariant) {
146 // The parameter is a slice to allow omitting the argument.
147 switch len(langSets) {
148 case 0:
149 for i := range c.byLangIndex {
150 c.byLangIndex[i] = val
151 }
152 return
153 case 1:
154 for lang := range langSets[0].bits() {
155 c.byLangIndex[lang.index()] = val
156 }
157 default:
158 panic("use a LangVariant bitset")
159 }
160 }
161
162 func fileTest(in []string, opts ...func(*fileTestCase)) fileTestCase {
163 c := fileTestCase{inputs: in}
164 for _, o := range opts {
165 o(&c)
166 }
167 return c
168 }
169
170 func printsAs(s string) func(*fileTestCase) {
171 return func(c *fileTestCase) { c.printedAs = s }
172 }
173
174 func langSkip(langSets ...LangVariant) func(*fileTestCase) {
175 return func(c *fileTestCase) { c.setForLangs(nil, langSets...) }
176 }
177
178 func langFile(wantNode any, langSets ...LangVariant) func(*fileTestCase) {
179 return func(c *fileTestCase) {
180 c.setForLangs(fullProg(wantNode), langSets...)
181 }
182 }
183
184 func langErr2(wantErr string, langSets ...LangVariant) func(*fileTestCase) {
185 return func(c *fileTestCase) { c.setForLangs(wantErr, langSets...) }
186 }
187
188 var fileTests = []fileTestCase{
189 fileTest(
190 []string{"", " ", "\t", "\n \n", "\r \r\n"},
191 langFile(&File{}),
192 ),
193 fileTest(
194 []string{"", "# foo", "# foo ( bar", "# foo'bar"},
195 langFile(&File{}),
196 ),
197 fileTest(
198 []string{"foo", "foo ", " foo", "foo # bar"},
199 langFile(litWord("foo")),
200 ),
201 fileTest(
202 []string{`\`},
203 printsAs(`\\`),
204 langFile(litWord(`\`)),
205 ),
206 fileTest(
207 []string{`foo\`, "f\\\noo\\"},
208 printsAs(`foo\\`),
209 langFile(litWord(`foo\`)),
210 ),
211 fileTest(
212 []string{`foo\\`},
213 langFile(litWord(`foo\\`)),
214 ),
215 fileTest(
216 []string{`foo\\\`},
217 printsAs(`foo\\\\`),
218 langFile(litWord(`foo\\\`)),
219 ),
220 fileTest(
221 []string{`foo\a`, "f\\\noo\\a"},
222 langFile(litWord(`foo\a`)),
223 ),
224 fileTest(
225 []string{
226 "foo\nbar",
227 "foo; bar;",
228 "foo;bar;",
229 "\nfoo\nbar\n",
230 "foo\r\nbar\r\n",
231 },
232 langFile(litStmts("foo", "bar")),
233 ),
234 fileTest(
235 []string{"foo a b", " foo a b ", "foo \\\n a b", "foo \\\r\n a b"},
236 langFile(litCall("foo", "a", "b")),
237 ),
238 fileTest(
239 []string{"foobar", "foo\\\nbar", "foo\\\nba\\\nr"},
240 langFile(litWord("foobar")),
241 ),
242 fileTest(
243 []string{"foo", "foo \\\n", "foo \\\r\n"},
244 langFile(litWord("foo")),
245 ),
246 fileTest(
247 []string{"foo'bar'"},
248 langFile(word(lit("foo"), sglQuoted("bar"))),
249 ),
250 fileTest(
251 []string{"(foo)", "(foo;)", "(\nfoo\n)"},
252 langFile(subshell(litStmt("foo"))),
253 ),
254 fileTest(
255 []string{"(\n\tfoo\n\tbar\n)", "(foo; bar)"},
256 langFile(subshell(litStmt("foo"), litStmt("bar"))),
257 ),
258 fileTest(
259 []string{"{ foo; }", "{\nfoo\n}"},
260 langFile(block(litStmt("foo"))),
261 ),
262 fileTest(
263 []string{
264 "{ if a; then b; fi; }",
265 "{ if a; then b; fi }",
266 },
267 langFile(block(stmt(&IfClause{
268 Cond: litStmts("a"),
269 Then: litStmts("b"),
270 }))),
271 ),
272 fileTest(
273 []string{
274 "if a; then b; fi",
275 "if a\nthen\nb\nfi",
276 "if a;\nthen\nb\nfi",
277 "if a \nthen\nb\nfi",
278 "if\x00 a; th\x00en b; \x00fi",
279 },
280 langFile(&IfClause{
281 Cond: litStmts("a"),
282 Then: litStmts("b"),
283 }),
284 ),
285 fileTest(
286 []string{
287 "if a; then b; else c; fi",
288 "if a\nthen b\nelse\nc\nfi",
289 },
290 langFile(&IfClause{
291 Cond: litStmts("a"),
292 Then: litStmts("b"),
293 Else: &IfClause{
294 Then: litStmts("c"),
295 },
296 }),
297 ),
298 fileTest(
299 []string{
300 "if a; then a; elif b; then b; else c; fi",
301 },
302 langFile(&IfClause{
303 Cond: litStmts("a"),
304 Then: litStmts("a"),
305 Else: &IfClause{
306 Cond: litStmts("b"),
307 Then: litStmts("b"),
308 Else: &IfClause{
309 Then: litStmts("c"),
310 },
311 },
312 }),
313 ),
314 fileTest(
315 []string{
316 "if a; then a; elif b; then b; elif c; then c; else d; fi",
317 "if a\nthen a\nelif b\nthen b\nelif c\nthen c\nelse\nd\nfi",
318 },
319 langFile(&IfClause{
320 Cond: litStmts("a"),
321 Then: litStmts("a"),
322 Else: &IfClause{
323 Cond: litStmts("b"),
324 Then: litStmts("b"),
325 Else: &IfClause{
326 Cond: litStmts("c"),
327 Then: litStmts("c"),
328 Else: &IfClause{
329 Then: litStmts("d"),
330 },
331 },
332 },
333 }),
334 ),
335 fileTest(
336 []string{
337 "if\n\ta1\n\ta2 foo\n\ta3 bar\nthen b; fi",
338 "if a1; a2 foo; a3 bar; then b; fi",
339 },
340 langFile(&IfClause{
341 Cond: []*Stmt{
342 litStmt("a1"),
343 litStmt("a2", "foo"),
344 litStmt("a3", "bar"),
345 },
346
347 Then: litStmts("b"),
348 }),
349 ),
350 fileTest(
351 []string{`((a == 2))`},
352 langFile(arithmCmd(&BinaryArithm{
353 Op: Eql,
354 X: litWord("a"),
355 Y: litWord("2"),
356 }), LangBash|LangMirBSDKorn|LangZsh),
357 langFile(subshell(stmt(subshell(litStmt("a", "==", "2")))), LangPOSIX),
358 ),
359 fileTest(
360 []string{"if (($# > 2)); then b; fi"},
361 langFile(&IfClause{
362 Cond: stmts(arithmCmd(&BinaryArithm{
363 Op: Gtr,
364 X: word(litParamExp("#")),
365 Y: litWord("2"),
366 })),
367 Then: litStmts("b"),
368 }, LangBash|LangMirBSDKorn|LangZsh),
369 ),
370 fileTest(
371 []string{
372 "(($(date -u) > DATE))",
373 "((`date -u` > DATE))",
374 },
375 langFile(arithmCmd(&BinaryArithm{
376 Op: Gtr,
377 X: word(cmdSubst(litStmt("date", "-u"))),
378 Y: litWord("DATE"),
379 }), LangBash|LangMirBSDKorn|LangZsh),
380 ),
381 fileTest(
382 []string{": $((0x$foo == 10))"},
383 langFile(call(
384 litWord(":"),
385 word(arithmExp(&BinaryArithm{
386 Op: Eql,
387 X: word(lit("0x"), litParamExp("foo")),
388 Y: litWord("10"),
389 })),
390 )),
391 ),
392 fileTest(
393 []string{"((# 1 + 2))", "(( # 1 + 2 ))"},
394 langFile(&ArithmCmd{
395 X: &BinaryArithm{
396 Op: Add,
397 X: litWord("1"),
398 Y: litWord("2"),
399 },
400 Unsigned: true,
401 }, LangMirBSDKorn),
402 langErr2("1:1: unsigned expressions are a mksh feature; tried parsing as LANG", LangBash),
403 ),
404 fileTest(
405 []string{"$((# 1 + 2))", "$(( # 1 + 2 ))"},
406 langFile(&ArithmExp{
407 X: &BinaryArithm{
408 Op: Add,
409 X: litWord("1"),
410 Y: litWord("2"),
411 },
412 Unsigned: true,
413 }, LangMirBSDKorn),
414 langErr2("1:1: unsigned expressions are a mksh feature; tried parsing as LANG", LangBash),
415 ),
416 fileTest(
417 []string{"((3#20))"},
418 langFile(arithmCmd(litWord("3#20")), LangBash|LangMirBSDKorn|LangZsh),
419 ),
420 fileTest(
421 []string{"((1.2 > 0.3))"},
422 langFile(arithmCmd(&BinaryArithm{
423 Op: Gtr,
424 X: litWord("1.2"),
425 Y: litWord("0.3"),
426 }), LangZsh),
427 langErr2("1:4: floating point arithmetic is a zsh feature; tried parsing as LANG", LangBash|LangMirBSDKorn),
428 ),
429 fileTest(
430 []string{
431 "while a; do b; done",
432 "wh\\\nile a; do b; done",
433 "wh\\\r\nile a; do b; done",
434 "while a\ndo\nb\ndone",
435 "while a;\ndo\nb\ndone",
436 },
437 langFile(&WhileClause{
438 Cond: litStmts("a"),
439 Do: litStmts("b"),
440 }),
441 ),
442 fileTest(
443 []string{"while { a; }; do b; done", "while { a; } do b; done"},
444 langFile(&WhileClause{
445 Cond: stmts(block(litStmt("a"))),
446 Do: litStmts("b"),
447 }),
448 ),
449 fileTest(
450 []string{"while (a); do b; done", "while (a) do b; done"},
451 langFile(&WhileClause{
452 Cond: stmts(subshell(litStmt("a"))),
453 Do: litStmts("b"),
454 }),
455 ),
456 fileTest(
457 []string{"while ((1 > 2)); do b; done"},
458 langFile(&WhileClause{
459 Cond: stmts(arithmCmd(&BinaryArithm{
460 Op: Gtr,
461 X: litWord("1"),
462 Y: litWord("2"),
463 })),
464 Do: litStmts("b"),
465 }, LangBash|LangMirBSDKorn|LangZsh),
466 ),
467 fileTest(
468 []string{"until a; do b; done", "until a\ndo\nb\ndone"},
469 langFile(&WhileClause{
470 Until: true,
471 Cond: litStmts("a"),
472 Do: litStmts("b"),
473 }),
474 ),
475 fileTest(
476 []string{
477 "for i; do foo; done",
478 "for i do foo; done",
479 "for i\ndo foo\ndone",
480 "for i;\ndo foo\ndone",
481 "for i in; do foo; done",
482 },
483 langFile(&ForClause{
484 Loop: &WordIter{Name: lit("i")},
485 Do: litStmts("foo"),
486 }),
487 ),
488 fileTest(
489 []string{
490 "for i in 1 2 3; do echo $i; done",
491 "for i in 1 2 3\ndo echo $i\ndone",
492 "for i in 1 2 3;\ndo echo $i\ndone",
493 "for i in 1 2 3 #foo\ndo echo $i\ndone",
494 },
495 langFile(&ForClause{
496 Loop: &WordIter{
497 Name: lit("i"),
498 Items: litWords("1", "2", "3"),
499 },
500 Do: stmts(call(
501 litWord("echo"),
502 word(litParamExp("i")),
503 )),
504 }),
505 ),
506 fileTest(
507 []string{
508 "for i in \\\n\t1 2 3; do #foo\n\techo $i\ndone",
509 "for i #foo\n\tin 1 2 3; do\n\techo $i\ndone",
510 },
511 langFile(&ForClause{
512 Loop: &WordIter{
513 Name: lit("i"),
514 Items: litWords("1", "2", "3"),
515 },
516 Do: stmts(call(
517 litWord("echo"),
518 word(litParamExp("i")),
519 )),
520 }),
521 ),
522 fileTest(
523 []string{
524 "for i; do foo; done",
525 "for i; { foo; }",
526 },
527 langFile(&ForClause{
528 Loop: &WordIter{Name: lit("i")},
529 Do: litStmts("foo"),
530 }, LangBash|LangMirBSDKorn),
531 ),
532 fileTest(
533 []string{
534 "for i in 1 2 3; do echo $i; done",
535 "for i in 1 2 3; { echo $i; }",
536 },
537 langFile(&ForClause{
538 Loop: &WordIter{
539 Name: lit("i"),
540 Items: litWords("1", "2", "3"),
541 },
542 Do: stmts(call(
543 litWord("echo"),
544 word(litParamExp("i")),
545 )),
546 }, LangBash|LangMirBSDKorn),
547 ),
548 fileTest(
549 []string{
550 "for ((i = 0; i < 10; i++)); do echo $i; done",
551 "for ((i=0;i<10;i++)) do echo $i; done",
552 "for (( i = 0 ; i < 10 ; i++ ))\ndo echo $i\ndone",
553 "for (( i = 0 ; i < 10 ; i++ ));\ndo echo $i\ndone",
554 },
555 langFile(&ForClause{
556 Loop: &CStyleLoop{
557 Init: &BinaryArithm{
558 Op: Assgn,
559 X: litWord("i"),
560 Y: litWord("0"),
561 },
562 Cond: &BinaryArithm{
563 Op: Lss,
564 X: litWord("i"),
565 Y: litWord("10"),
566 },
567 Post: &UnaryArithm{
568 Op: Inc,
569 Post: true,
570 X: litWord("i"),
571 },
572 },
573 Do: stmts(call(
574 litWord("echo"),
575 word(litParamExp("i")),
576 )),
577 }, LangBash|LangZsh),
578 langErr2("1:5: c-style fors are a bash/zsh feature; tried parsing as LANG", LangPOSIX|LangMirBSDKorn),
579 ),
580 fileTest(
581 []string{
582 "for (( ; ; )); do foo; done",
583 "for ((;;)); do foo; done",
584 },
585 langFile(&ForClause{
586 Loop: &CStyleLoop{},
587 Do: litStmts("foo"),
588 }, LangBash),
589 ),
590 fileTest(
591 []string{
592 "for ((i = 0; ; )); do foo; done",
593 "for ((i = 0;;)); do foo; done",
594 },
595 langFile(&ForClause{
596 Loop: &CStyleLoop{
597 Init: &BinaryArithm{
598 Op: Assgn,
599 X: litWord("i"),
600 Y: litWord("0"),
601 },
602 },
603 Do: litStmts("foo"),
604 }, LangBash),
605 ),
606 fileTest(
607 []string{
608 "select i; do foo; done",
609 "select i in; do foo; done",
610 },
611 langFile(&ForClause{
612 Select: true,
613 Loop: &WordIter{Name: lit("i")},
614 Do: litStmts("foo"),
615 }, LangBash|LangMirBSDKorn|LangZsh),
616 ),
617 fileTest(
618 []string{
619 "select i in 1 2 3; do echo $i; done",
620 "select i in 1 2 3\ndo echo $i\ndone",
621 "select i in 1 2 3 #foo\ndo echo $i\ndone",
622 },
623 langFile(&ForClause{
624 Select: true,
625 Loop: &WordIter{
626 Name: lit("i"),
627 Items: litWords("1", "2", "3"),
628 },
629 Do: stmts(call(
630 litWord("echo"),
631 word(litParamExp("i")),
632 )),
633 }, LangBash|LangMirBSDKorn|LangZsh),
634 ),
635 fileTest(
636 []string{"select foo bar"},
637 langFile(litStmt("select", "foo", "bar"), LangPOSIX),
638 ),
639 fileTest(
640 []string{`' ' "foo bar"`},
641 langFile(call(
642 word(sglQuoted(" ")),
643 word(dblQuoted(lit("foo bar"))),
644 )),
645 ),
646 fileTest(
647 []string{`"foo \" bar"`},
648 langFile(word(dblQuoted(lit(`foo \" bar`)))),
649 ),
650 fileTest(
651 []string{"\">foo\" \"\nbar\""},
652 langFile(call(
653 word(dblQuoted(lit(">foo"))),
654 word(dblQuoted(lit("\nbar"))),
655 )),
656 ),
657 fileTest(
658 []string{`foo \" bar`},
659 langFile(litCall(`foo`, `\"`, `bar`)),
660 ),
661 fileTest(
662 []string{`'"'`},
663 langFile(sglQuoted(`"`)),
664 ),
665 fileTest(
666 []string{"'`'"},
667 langFile(sglQuoted("`")),
668 ),
669 fileTest(
670 []string{`"'"`},
671 langFile(dblQuoted(lit("'"))),
672 ),
673 fileTest(
674 []string{`""`},
675 langFile(dblQuoted()),
676 ),
677 fileTest(
678 []string{"=a s{s s=s"},
679 langFile(litCall("=a", "s{s", "s=s")),
680 langSkip(LangZsh),
681 ),
682 fileTest(
683 []string{"foo && bar", "foo&&bar", "foo &&\nbar"},
684 langFile(&BinaryCmd{
685 Op: AndStmt,
686 X: litStmt("foo"),
687 Y: litStmt("bar"),
688 }),
689 ),
690 fileTest(
691 []string{"foo &&\n\tbar"},
692 langFile(&BinaryCmd{
693 Op: AndStmt,
694 X: litStmt("foo"),
695 Y: litStmt("bar"),
696 }),
697 ),
698 fileTest(
699 []string{"foo || bar", "foo||bar", "foo ||\nbar"},
700 langFile(&BinaryCmd{
701 Op: OrStmt,
702 X: litStmt("foo"),
703 Y: litStmt("bar"),
704 }),
705 ),
706 fileTest(
707 []string{"if a; then b; fi || while a; do b; done"},
708 langFile(&BinaryCmd{
709 Op: OrStmt,
710 X: stmt(&IfClause{
711 Cond: litStmts("a"),
712 Then: litStmts("b"),
713 }),
714 Y: stmt(&WhileClause{
715 Cond: litStmts("a"),
716 Do: litStmts("b"),
717 }),
718 }),
719 ),
720 fileTest(
721 []string{"foo && bar1 || bar2"},
722 langFile(&BinaryCmd{
723 Op: OrStmt,
724 X: stmt(&BinaryCmd{
725 Op: AndStmt,
726 X: litStmt("foo"),
727 Y: litStmt("bar1"),
728 }),
729 Y: litStmt("bar2"),
730 }),
731 ),
732 fileTest(
733 []string{"a || b || c || d"},
734 langFile(&BinaryCmd{
735 Op: OrStmt,
736 X: stmt(&BinaryCmd{
737 Op: OrStmt,
738 X: stmt(&BinaryCmd{
739 Op: OrStmt,
740 X: litStmt("a"),
741 Y: litStmt("b"),
742 }),
743 Y: litStmt("c"),
744 }),
745 Y: litStmt("d"),
746 }),
747 ),
748 fileTest(
749 []string{"foo | bar", "foo|bar", "foo |\n#etc\nbar"},
750 langFile(&BinaryCmd{
751 Op: Pipe,
752 X: litStmt("foo"),
753 Y: litStmt("bar"),
754 }),
755 ),
756 fileTest(
757 []string{"foo | bar | extra"},
758 langFile(&BinaryCmd{
759 Op: Pipe,
760 X: stmt(&BinaryCmd{
761 Op: Pipe,
762 X: litStmt("foo"),
763 Y: litStmt("bar"),
764 }),
765 Y: litStmt("extra"),
766 }),
767 ),
768 fileTest(
769 []string{"foo | a=b bar"},
770 langFile(&BinaryCmd{
771 Op: Pipe,
772 X: litStmt("foo"),
773 Y: stmt(&CallExpr{
774 Assigns: litAssigns("a=b"),
775 Args: litWords("bar"),
776 }),
777 }),
778 ),
779 fileTest(
780 []string{"foo |&"},
781 langFile(&Stmt{Cmd: litCall("foo"), Coprocess: true}, LangMirBSDKorn),
782 ),
783 fileTest(
784 []string{"foo \\\n\t|&"},
785 langFile(&Stmt{Cmd: litCall("foo"), Coprocess: true}, LangMirBSDKorn),
786 ),
787 fileTest(
788 []string{"foo |& bar", "foo|&bar"},
789 langFile(&BinaryCmd{
790 Op: PipeAll,
791 X: litStmt("foo"),
792 Y: litStmt("bar"),
793 }, LangBash|LangZsh),
794 langFile([]*Stmt{
795 {Cmd: litCall("foo"), Coprocess: true},
796 litStmt("bar"),
797 }, LangMirBSDKorn),
798 ),
799 fileTest(
800 []string{"foo &|", "foo &!"},
801 langFile(&Stmt{Cmd: litCall("foo"), Disown: true}, LangZsh),
802 ),
803 fileTest(
804 []string{"foo &|\nbar"},
805 langFile([]*Stmt{
806 {Cmd: litCall("foo"), Disown: true},
807 litStmt("bar"),
808 }, LangZsh),
809 ),
810 fileTest(
811 []string{
812 "foo() {\n\ta\n\tb\n}",
813 "foo() { a; b; }",
814 "foo ( ) {\na\nb\n}",
815 "foo()\n{\na\nb\n}",
816 },
817 langFile(&FuncDecl{
818 Parens: true,
819 Name: lit("foo"),
820 Body: stmt(block(litStmt("a"), litStmt("b"))),
821 }),
822 langSkip(LangZsh), // fails on foo ( )
823 ),
824 fileTest(
825 []string{"foo() { a; }\nbar", "foo() {\na\n}; bar"},
826 langFile([]Command{
827 &FuncDecl{
828 Parens: true,
829 Name: lit("foo"),
830 Body: stmt(block(litStmt("a"))),
831 },
832 litCall("bar"),
833 }),
834 ),
835 fileTest(
836 []string{"foO_123() { a; }"},
837 langFile(&FuncDecl{
838 Parens: true,
839 Name: lit("foO_123"),
840 Body: stmt(block(litStmt("a"))),
841 }),
842 ),
843 fileTest(
844 []string{"-foo_.,+-bar() { a; }"},
845 langFile(&FuncDecl{
846 Parens: true,
847 Name: lit("-foo_.,+-bar"),
848 Body: stmt(block(litStmt("a"))),
849 }, LangBash|LangMirBSDKorn),
850 ),
851 fileTest(
852 []string{
853 "function foo() {\n\ta\n\tb\n}",
854 "function foo() { a; b; }",
855 },
856 langFile(&FuncDecl{
857 RsrvWord: true,
858 Parens: true,
859 Name: lit("foo"),
860 Body: stmt(block(litStmt("a"), litStmt("b"))),
861 }, LangBash|LangMirBSDKorn|LangZsh),
862 langErr2("1:13: the `function` builtin is a bash feature; tried parsing as LANG", LangPOSIX),
863 ),
864 fileTest(
865 []string{
866 "function foo {\n\ta\n\tb\n}",
867 "function foo { a; b; }",
868 },
869 langFile(&FuncDecl{
870 RsrvWord: true,
871 Name: lit("foo"),
872 Body: stmt(block(litStmt("a"), litStmt("b"))),
873 }, LangBash|LangMirBSDKorn|LangZsh),
874 ),
875 fileTest(
876 []string{"function foo() (a)"},
877 langFile(&FuncDecl{
878 RsrvWord: true,
879 Parens: true,
880 Name: lit("foo"),
881 Body: stmt(subshell(litStmt("a"))),
882 }, LangBash),
883 langErr2("1:13: the `function` builtin is a bash feature; tried parsing as LANG", LangPOSIX),
884 ),
885 fileTest(
886 []string{"function f1 f2 f3() {\n\ta\n}"},
887 langFile(&FuncDecl{
888 RsrvWord: true,
889 Parens: true,
890 Names: lits("f1", "f2", "f3"),
891 Body: stmt(block(litStmt("a"))),
892 }, LangZsh),
893 ),
894 fileTest(
895 []string{"function f1 f2 f3() {\n\ta\n}"},
896 langFile(&FuncDecl{
897 RsrvWord: true,
898 Parens: true,
899 Names: lits("f1", "f2", "f3"),
900 Body: stmt(block(litStmt("a"))),
901 }, LangZsh),
902 langErr2("1:1: multi-name functions are a zsh feature; tried parsing as LANG", LangBash|LangMirBSDKorn),
903 ),
904 fileTest(
905 []string{"function {\n\ta\n}"},
906 langFile(&FuncDecl{
907 RsrvWord: true,
908 Body: stmt(block(litStmt("a"))),
909 }, LangZsh),
910 langErr2("1:1: anonymous functions are a zsh feature; tried parsing as LANG", LangBash|LangMirBSDKorn),
911 ),
912 // Note that zsh also supports `f1 f2 f3 () { body; }`,
913 // but it seems rare and hard to implement well,
914 // so leave it out for now.
915 fileTest(
916 []string{"() {\n\ta\n}"},
917 langFile(&FuncDecl{
918 Parens: true,
919 Body: stmt(block(litStmt("a"))),
920 }, LangZsh),
921 langErr2("1:1: anonymous functions are a zsh feature; tried parsing as LANG", LangBash|LangMirBSDKorn),
922 ),
923 fileTest(
924 []string{"a=b foo=$bar foo=start$bar"},
925 langFile(&CallExpr{
926 Assigns: []*Assign{
927 {Name: lit("a"), Value: litWord("b")},
928 {Name: lit("foo"), Value: word(litParamExp("bar"))},
929 {Name: lit("foo"), Value: word(
930 lit("start"),
931 litParamExp("bar"),
932 )},
933 },
934 }),
935 ),
936 fileTest(
937 []string{"a=\"\nbar\""},
938 langFile(&CallExpr{
939 Assigns: []*Assign{{
940 Name: lit("a"),
941 Value: word(dblQuoted(lit("\nbar"))),
942 }},
943 }),
944 ),
945 fileTest(
946 []string{"A_3a= foo"},
947 langFile(&CallExpr{
948 Assigns: litAssigns("A_3a="),
949 Args: litWords("foo"),
950 }),
951 ),
952 fileTest(
953 []string{"a=b=c"},
954 langFile(&CallExpr{
955 Assigns: litAssigns("a=b=c"),
956 }),
957 ),
958 fileTest(
959 []string{"à=b foo"},
960 langFile(litStmt("à=b", "foo")),
961 ),
962 fileTest(
963 []string{
964 "foo >a >>b <c",
965 "foo > a >> b < c",
966 ">a >>b <c foo",
967 },
968 langFile(&Stmt{
969 Cmd: litCall("foo"),
970 Redirs: []*Redirect{
971 {Op: RdrOut, Word: litWord("a")},
972 {Op: AppOut, Word: litWord("b")},
973 {Op: RdrIn, Word: litWord("c")},
974 },
975 }),
976 ),
977 fileTest(
978 []string{
979 "foo bar >a",
980 "foo >a bar",
981 },
982 langFile(&Stmt{
983 Cmd: litCall("foo", "bar"),
984 Redirs: []*Redirect{
985 {Op: RdrOut, Word: litWord("a")},
986 },
987 }),
988 ),
989 fileTest(
990 []string{`>a >\b`},
991 langFile(&Stmt{
992 Redirs: []*Redirect{
993 {Op: RdrOut, Word: litWord("a")},
994 {Op: RdrOut, Word: litWord(`\b`)},
995 },
996 }),
997 ),
998 fileTest(
999 []string{">a\n>b", ">a; >b"},
1000 langFile([]*Stmt{
1001 {Redirs: []*Redirect{
1002 {Op: RdrOut, Word: litWord("a")},
1003 }},
1004 {Redirs: []*Redirect{
1005 {Op: RdrOut, Word: litWord("b")},
1006 }},
1007 }),
1008 ),
1009 fileTest(
1010 []string{"foo1\nfoo2 >r2", "foo1; >r2 foo2"},
1011 langFile([]*Stmt{
1012 litStmt("foo1"),
1013 {
1014 Cmd: litCall("foo2"),
1015 Redirs: []*Redirect{
1016 {Op: RdrOut, Word: litWord("r2")},
1017 },
1018 },
1019 }),
1020 ),
1021 fileTest(
1022 []string{"foo >bar$(etc)", "foo >b\\\nar`etc`"},
1023 langFile(&Stmt{
1024 Cmd: litCall("foo"),
1025 Redirs: []*Redirect{
1026 {Op: RdrOut, Word: word(
1027 lit("bar"),
1028 cmdSubst(litStmt("etc")),
1029 )},
1030 },
1031 }),
1032 ),
1033 fileTest(
1034 []string{
1035 "a=b c=d foo >x <y",
1036 "a=b c=d >x <y foo",
1037 ">x a=b c=d <y foo",
1038 ">x <y a=b c=d foo",
1039 "a=b >x c=d foo <y",
1040 },
1041 langFile(&Stmt{
1042 Cmd: &CallExpr{
1043 Assigns: litAssigns("a=b", "c=d"),
1044 Args: litWords("foo"),
1045 },
1046 Redirs: []*Redirect{
1047 {Op: RdrOut, Word: litWord("x")},
1048 {Op: RdrIn, Word: litWord("y")},
1049 },
1050 }),
1051 ),
1052 fileTest(
1053 []string{
1054 "foo <<EOF\nbar\nEOF",
1055 "foo <<EOF \nbar\nEOF",
1056 "foo <<EOF\t\nbar\nEOF",
1057 "foo <<EOF\r\nbar\r\nEOF\r\n",
1058 },
1059 langFile(&Stmt{
1060 Cmd: litCall("foo"),
1061 Redirs: []*Redirect{{
1062 Op: Hdoc,
1063 Word: litWord("EOF"),
1064 Hdoc: litWord("bar\n"),
1065 }},
1066 }),
1067 ),
1068 fileTest(
1069 []string{"foo <<EOF\n\nbar\nEOF"},
1070 langFile(&Stmt{
1071 Cmd: litCall("foo"),
1072 Redirs: []*Redirect{{
1073 Op: Hdoc,
1074 Word: litWord("EOF"),
1075 Hdoc: litWord("\nbar\n"),
1076 }},
1077 }),
1078 ),
1079 fileTest(
1080 []string{"foo <<EOF\nbar\n\nEOF"},
1081 langFile(&Stmt{
1082 Cmd: litCall("foo"),
1083 Redirs: []*Redirect{{
1084 Op: Hdoc,
1085 Word: litWord("EOF"),
1086 Hdoc: litWord("bar\n\n"),
1087 }},
1088 }),
1089 ),
1090 fileTest(
1091 []string{"foo <<EOF\n1\n2\n3\nEOF"},
1092 langFile(&Stmt{
1093 Cmd: litCall("foo"),
1094 Redirs: []*Redirect{{
1095 Op: Hdoc,
1096 Word: litWord("EOF"),
1097 Hdoc: litWord("1\n2\n3\n"),
1098 }},
1099 }),
1100 ),
1101 fileTest(
1102 []string{"a <<EOF\nfoo$bar\nEOF"},
1103 langFile(&Stmt{
1104 Cmd: litCall("a"),
1105 Redirs: []*Redirect{{
1106 Op: Hdoc,
1107 Word: litWord("EOF"),
1108 Hdoc: word(
1109 lit("foo"),
1110 litParamExp("bar"),
1111 lit("\n"),
1112 ),
1113 }},
1114 }),
1115 ),
1116 fileTest(
1117 []string{"a <<EOF\n\"$bar\"\nEOF"},
1118 langFile(&Stmt{
1119 Cmd: litCall("a"),
1120 Redirs: []*Redirect{{
1121 Op: Hdoc,
1122 Word: litWord("EOF"),
1123 Hdoc: word(
1124 lit(`"`),
1125 litParamExp("bar"),
1126 lit("\"\n"),
1127 ),
1128 }},
1129 }),
1130 ),
1131 fileTest(
1132 []string{"a <<EOF\n$''$bar\nEOF"},
1133 langFile(&Stmt{
1134 Cmd: litCall("a"),
1135 Redirs: []*Redirect{{
1136 Op: Hdoc,
1137 Word: litWord("EOF"),
1138 Hdoc: word(
1139 lit("$"),
1140 lit("''"),
1141 litParamExp("bar"),
1142 lit("\n"),
1143 ),
1144 }},
1145 }, LangBash),
1146 ),
1147 fileTest(
1148 []string{
1149 "a <<EOF\n$(b)\nc\nEOF",
1150 "a <<EOF\n`b`\nc\nEOF",
1151 },
1152 langFile(&Stmt{
1153 Cmd: litCall("a"),
1154 Redirs: []*Redirect{{
1155 Op: Hdoc,
1156 Word: litWord("EOF"),
1157 Hdoc: word(
1158 cmdSubst(litStmt("b")),
1159 lit("\nc\n"),
1160 ),
1161 }},
1162 }),
1163 ),
1164 fileTest(
1165 []string{
1166 "a <<EOF\nfoo$(bar)baz\nEOF",
1167 "a <<EOF\nfoo`bar`baz\nEOF",
1168 },
1169 langFile(&Stmt{
1170 Cmd: litCall("a"),
1171 Redirs: []*Redirect{{
1172 Op: Hdoc,
1173 Word: litWord("EOF"),
1174 Hdoc: word(
1175 lit("foo"),
1176 cmdSubst(litStmt("bar")),
1177 lit("baz\n"),
1178 ),
1179 }},
1180 }),
1181 ),
1182 fileTest(
1183 []string{"a <<EOF\n\\${\nEOF"},
1184 langFile(&Stmt{
1185 Cmd: litCall("a"),
1186 Redirs: []*Redirect{{
1187 Op: Hdoc,
1188 Word: litWord("EOF"),
1189 Hdoc: litWord("\\${\n"),
1190 }},
1191 }),
1192 ),
1193 fileTest(
1194 []string{
1195 "{\n\tfoo <<EOF\nbar\nEOF\n}",
1196 "{ foo <<EOF\nbar\nEOF\n}",
1197 },
1198 langFile(block(&Stmt{
1199 Cmd: litCall("foo"),
1200 Redirs: []*Redirect{{
1201 Op: Hdoc,
1202 Word: litWord("EOF"),
1203 Hdoc: litWord("bar\n"),
1204 }},
1205 })),
1206 ),
1207 fileTest(
1208 []string{
1209 "$(\n\tfoo <<EOF\nbar\nEOF\n)",
1210 "$(foo <<EOF\nbar\nEOF\n)",
1211 "`\nfoo <<EOF\nbar\nEOF\n`",
1212 "`foo <<EOF\nbar\nEOF`",
1213 },
1214 langFile(cmdSubst(&Stmt{
1215 Cmd: litCall("foo"),
1216 Redirs: []*Redirect{{
1217 Op: Hdoc,
1218 Word: litWord("EOF"),
1219 Hdoc: litWord("bar\n"),
1220 }},
1221 })),
1222 ),
1223 fileTest(
1224 []string{
1225 "foo <<EOF\nbar\nEOF$(oops)\nEOF",
1226 "foo <<EOF\nbar\nEOF`oops`\nEOF",
1227 },
1228 langFile(&Stmt{
1229 Cmd: litCall("foo"),
1230 Redirs: []*Redirect{{
1231 Op: Hdoc,
1232 Word: litWord("EOF"),
1233 Hdoc: word(
1234 lit("bar\nEOF"),
1235 cmdSubst(litStmt("oops")),
1236 lit("\n"),
1237 ),
1238 }},
1239 }),
1240 ),
1241 fileTest(
1242 []string{
1243 "foo <<EOF\nbar\nNOTEOF$(oops)\nEOF",
1244 "foo <<EOF\nbar\nNOTEOF`oops`\nEOF",
1245 },
1246 langFile(&Stmt{
1247 Cmd: litCall("foo"),
1248 Redirs: []*Redirect{{
1249 Op: Hdoc,
1250 Word: litWord("EOF"),
1251 Hdoc: word(
1252 lit("bar\nNOTEOF"),
1253 cmdSubst(litStmt("oops")),
1254 lit("\n"),
1255 ),
1256 }},
1257 }),
1258 ),
1259 fileTest(
1260 []string{
1261 "$(\n\tfoo <<'EOF'\nbar\nEOF\n)",
1262 "$(foo <<'EOF'\nbar\nEOF\n)",
1263 "`\nfoo <<'EOF'\nbar\nEOF\n`",
1264 "`foo <<'EOF'\nbar\nEOF`",
1265 },
1266 langFile(cmdSubst(&Stmt{
1267 Cmd: litCall("foo"),
1268 Redirs: []*Redirect{{
1269 Op: Hdoc,
1270 Word: word(sglQuoted("EOF")),
1271 Hdoc: litWord("bar\n"),
1272 }},
1273 })),
1274 ),
1275 fileTest(
1276 []string{"foo <<'EOF'\nbar\nEOF`oops`\nEOF"},
1277 langFile(&Stmt{
1278 Cmd: litCall("foo"),
1279 Redirs: []*Redirect{{
1280 Op: Hdoc,
1281 Word: word(sglQuoted("EOF")),
1282 Hdoc: litWord("bar\nEOF`oops`\n"),
1283 }},
1284 }),
1285 ),
1286 fileTest(
1287 []string{"foo <<'EOF'\nbar\nNOTEOF`oops`\nEOF"},
1288 langFile(&Stmt{
1289 Cmd: litCall("foo"),
1290 Redirs: []*Redirect{{
1291 Op: Hdoc,
1292 Word: word(sglQuoted("EOF")),
1293 Hdoc: litWord("bar\nNOTEOF`oops`\n"),
1294 }},
1295 }),
1296 ),
1297 fileTest(
1298 []string{"$(<foo)", "`<foo`"},
1299 langFile(cmdSubst(&Stmt{
1300 Redirs: []*Redirect{{
1301 Op: RdrIn,
1302 Word: litWord("foo"),
1303 }},
1304 })),
1305 langSkip(LangZsh), // actually tries to read foo when confirming
1306 ),
1307 fileTest(
1308 []string{"foo <<EOF >f\nbar\nEOF"},
1309 langFile(&Stmt{
1310 Cmd: litCall("foo"),
1311 Redirs: []*Redirect{
1312 {
1313 Op: Hdoc,
1314 Word: litWord("EOF"),
1315 Hdoc: litWord("bar\n"),
1316 },
1317 {Op: RdrOut, Word: litWord("f")},
1318 },
1319 }),
1320 ),
1321 fileTest(
1322 []string{"foo <<EOF && {\nbar\nEOF\n\tetc\n}"},
1323 langFile(&BinaryCmd{
1324 Op: AndStmt,
1325 X: &Stmt{
1326 Cmd: litCall("foo"),
1327 Redirs: []*Redirect{{
1328 Op: Hdoc,
1329 Word: litWord("EOF"),
1330 Hdoc: litWord("bar\n"),
1331 }},
1332 },
1333 Y: stmt(block(litStmt("etc"))),
1334 }),
1335 ),
1336 fileTest(
1337 []string{
1338 "$(\n\tfoo\n) <<EOF\nbar\nEOF",
1339 "<<EOF $(\n\tfoo\n)\nbar\nEOF",
1340 },
1341 // note that dash won't accept the second one
1342 langFile(&Stmt{
1343 Cmd: call(word(cmdSubst(litStmt("foo")))),
1344 Redirs: []*Redirect{{
1345 Op: Hdoc,
1346 Word: litWord("EOF"),
1347 Hdoc: litWord("bar\n"),
1348 }},
1349 }, LangBash|LangMirBSDKorn),
1350 ),
1351 fileTest(
1352 []string{
1353 "$(\n\tfoo\n) <<EOF\nbar\nEOF",
1354 "`\n\tfoo\n` <<EOF\nbar\nEOF",
1355 "<<EOF `\n\tfoo\n`\nbar\nEOF",
1356 },
1357 langFile(&Stmt{
1358 Cmd: call(word(cmdSubst(litStmt("foo")))),
1359 Redirs: []*Redirect{{
1360 Op: Hdoc,
1361 Word: litWord("EOF"),
1362 Hdoc: litWord("bar\n"),
1363 }},
1364 }),
1365 ),
1366 fileTest(
1367 []string{
1368 "$((foo)) <<EOF\nbar\nEOF",
1369 "<<EOF $((\n\tfoo\n))\nbar\nEOF",
1370 },
1371 langFile(&Stmt{
1372 Cmd: call(word(arithmExp(litWord("foo")))),
1373 Redirs: []*Redirect{{
1374 Op: Hdoc,
1375 Word: litWord("EOF"),
1376 Hdoc: litWord("bar\n"),
1377 }},
1378 }),
1379 ),
1380 fileTest(
1381 []string{"if true; then\n\tfoo <<-EOF\n\t\tbar\n\tEOF\nfi"},
1382 langFile(&IfClause{
1383 Cond: litStmts("true"),
1384 Then: []*Stmt{{
1385 Cmd: litCall("foo"),
1386 Redirs: []*Redirect{{
1387 Op: DashHdoc,
1388 Word: litWord("EOF"),
1389 Hdoc: litWord("\t\tbar\n\t"),
1390 }},
1391 }},
1392 }),
1393 ),
1394 fileTest(
1395 []string{"if true; then\n\tfoo <<-EOF\n\tEOF\nfi"},
1396 langFile(&IfClause{
1397 Cond: litStmts("true"),
1398 Then: []*Stmt{{
1399 Cmd: litCall("foo"),
1400 Redirs: []*Redirect{{
1401 Op: DashHdoc,
1402 Word: litWord("EOF"),
1403 Hdoc: litWord("\t"),
1404 }},
1405 }},
1406 }),
1407 ),
1408 fileTest(
1409 []string{"foo <<EOF\nEOF_body\nEOF\nfoo2"},
1410 langFile([]*Stmt{
1411 {
1412 Cmd: litCall("foo"),
1413 Redirs: []*Redirect{{
1414 Op: Hdoc,
1415 Word: litWord("EOF"),
1416 Hdoc: litWord("EOF_body\n"),
1417 }},
1418 },
1419 litStmt("foo2"),
1420 }),
1421 ),
1422 fileTest(
1423 []string{"foo <<FOOBAR\nbar\nFOOBAR"},
1424 langFile(&Stmt{
1425 Cmd: litCall("foo"),
1426 Redirs: []*Redirect{{
1427 Op: Hdoc,
1428 Word: litWord("FOOBAR"),
1429 Hdoc: litWord("bar\n"),
1430 }},
1431 }),
1432 ),
1433 fileTest(
1434 []string{"foo <<\"EOF\"\nbar\nEOF"},
1435 langFile(&Stmt{
1436 Cmd: litCall("foo"),
1437 Redirs: []*Redirect{{
1438 Op: Hdoc,
1439 Word: word(dblQuoted(lit("EOF"))),
1440 Hdoc: litWord("bar\n"),
1441 }},
1442 }),
1443 ),
1444 fileTest(
1445 []string{"foo <<'EOF'\nEOF_body\nEOF\nfoo2"},
1446 langFile([]*Stmt{
1447 {
1448 Cmd: litCall("foo"),
1449 Redirs: []*Redirect{{
1450 Op: Hdoc,
1451 Word: word(sglQuoted("EOF")),
1452 Hdoc: litWord("EOF_body\n"),
1453 }},
1454 },
1455 litStmt("foo2"),
1456 }),
1457 ),
1458 fileTest(
1459 []string{"foo <<'EOF'\n${\nEOF"},
1460 langFile(&Stmt{
1461 Cmd: litCall("foo"),
1462 Redirs: []*Redirect{{
1463 Op: Hdoc,
1464 Word: word(sglQuoted("EOF")),
1465 Hdoc: litWord("${\n"),
1466 }},
1467 }),
1468 ),
1469 fileTest(
1470 []string{"foo <<'EOF'\nEOF"},
1471 langFile(&Stmt{
1472 Cmd: litCall("foo"),
1473 Redirs: []*Redirect{{
1474 Op: Hdoc,
1475 Word: word(sglQuoted("EOF")),
1476 }},
1477 }),
1478 ),
1479 fileTest(
1480 []string{"foo <<\"EOF\"2\nbar\nEOF2"},
1481 langFile(&Stmt{
1482 Cmd: litCall("foo"),
1483 Redirs: []*Redirect{{
1484 Op: Hdoc,
1485 Word: word(dblQuoted(lit("EOF")), lit("2")),
1486 Hdoc: litWord("bar\n"),
1487 }},
1488 }),
1489 ),
1490 fileTest(
1491 []string{"foo <<\\EOF\nbar\nEOF"},
1492 langFile(&Stmt{
1493 Cmd: litCall("foo"),
1494 Redirs: []*Redirect{{
1495 Op: Hdoc,
1496 Word: litWord("\\EOF"),
1497 Hdoc: litWord("bar\n"),
1498 }},
1499 }),
1500 ),
1501 fileTest(
1502 []string{"foo <<EOF\nbar\\\nbaz\nEOF"},
1503 langFile(&Stmt{
1504 Cmd: litCall("foo"),
1505 Redirs: []*Redirect{{
1506 Op: Hdoc,
1507 Word: litWord("EOF"),
1508 Hdoc: word(lit("bar"), lit("baz\n")),
1509 }},
1510 }),
1511 ),
1512 fileTest(
1513 []string{
1514 "foo <<'EOF'\nbar\\\nEOF",
1515 "foo <<'EOF'\nbar\\\r\nEOF",
1516 "foo <<'EOF'\nbar\\\r\nEOF\r\n",
1517 },
1518 langFile(&Stmt{
1519 Cmd: litCall("foo"),
1520 Redirs: []*Redirect{{
1521 Op: Hdoc,
1522 Word: word(sglQuoted("EOF")),
1523 Hdoc: litWord("bar\\\n"),
1524 }},
1525 }),
1526 ),
1527 fileTest(
1528 []string{
1529 "foo <<-EOF\n\tbar\nEOF",
1530 "foo <<-EOF\r\n\tbar\r\nEOF\r\n",
1531 },
1532 langFile(&Stmt{
1533 Cmd: litCall("foo"),
1534 Redirs: []*Redirect{{
1535 Op: DashHdoc,
1536 Word: litWord("EOF"),
1537 Hdoc: litWord("\tbar\n"),
1538 }},
1539 }),
1540 ),
1541 fileTest(
1542 []string{"foo <<EOF\nEOF"},
1543 langFile(&Stmt{
1544 Cmd: litCall("foo"),
1545 Redirs: []*Redirect{{
1546 Op: Hdoc,
1547 Word: litWord("EOF"),
1548 }},
1549 }),
1550 ),
1551 fileTest(
1552 []string{"foo <<-EOF\nEOF"},
1553 langFile(&Stmt{
1554 Cmd: litCall("foo"),
1555 Redirs: []*Redirect{{
1556 Op: DashHdoc,
1557 Word: litWord("EOF"),
1558 }},
1559 }),
1560 ),
1561 fileTest(
1562 []string{"foo <<-EOF\n\tbar\nEOF"},
1563 langFile(&Stmt{
1564 Cmd: litCall("foo"),
1565 Redirs: []*Redirect{{
1566 Op: DashHdoc,
1567 Word: litWord("EOF"),
1568 Hdoc: litWord("\tbar\n"),
1569 }},
1570 }),
1571 ),
1572 fileTest(
1573 []string{"foo <<-'EOF'\n\tbar\nEOF"},
1574 langFile(&Stmt{
1575 Cmd: litCall("foo"),
1576 Redirs: []*Redirect{{
1577 Op: DashHdoc,
1578 Word: word(sglQuoted("EOF")),
1579 Hdoc: litWord("\tbar\n"),
1580 }},
1581 }),
1582 ),
1583 fileTest(
1584 []string{
1585 "f1 <<EOF1\nh1\nEOF1\nf2 <<EOF2\nh2\nEOF2",
1586 "f1 <<EOF1; f2 <<EOF2\nh1\nEOF1\nh2\nEOF2",
1587 },
1588 langFile([]*Stmt{
1589 {
1590 Cmd: litCall("f1"),
1591 Redirs: []*Redirect{{
1592 Op: Hdoc,
1593 Word: litWord("EOF1"),
1594 Hdoc: litWord("h1\n"),
1595 }},
1596 },
1597 {
1598 Cmd: litCall("f2"),
1599 Redirs: []*Redirect{{
1600 Op: Hdoc,
1601 Word: litWord("EOF2"),
1602 Hdoc: litWord("h2\n"),
1603 }},
1604 },
1605 }),
1606 ),
1607 fileTest(
1608 []string{
1609 "a <<EOF\nfoo\nEOF\nb\nb\nb\nb\nb\nb\nb\nb\nb",
1610 "a <<EOF;b;b;b;b;b;b;b;b;b\nfoo\nEOF",
1611 },
1612 langFile([]*Stmt{
1613 {
1614 Cmd: litCall("a"),
1615 Redirs: []*Redirect{{
1616 Op: Hdoc,
1617 Word: litWord("EOF"),
1618 Hdoc: litWord("foo\n"),
1619 }},
1620 },
1621 litStmt("b"), litStmt("b"), litStmt("b"),
1622 litStmt("b"), litStmt("b"), litStmt("b"),
1623 litStmt("b"), litStmt("b"), litStmt("b"),
1624 }),
1625 ),
1626 fileTest(
1627 []string{
1628 "foo \"\narg\" <<EOF\nbar\nEOF",
1629 "foo <<EOF \"\narg\"\nbar\nEOF",
1630 },
1631 langFile(&Stmt{
1632 Cmd: call(
1633 litWord("foo"),
1634 word(dblQuoted(lit("\narg"))),
1635 ),
1636 Redirs: []*Redirect{{
1637 Op: Hdoc,
1638 Word: litWord("EOF"),
1639 Hdoc: litWord("bar\n"),
1640 }},
1641 }),
1642 ),
1643 fileTest(
1644 []string{"foo >&2 <&0 2>file 345>file <>f2"},
1645 langFile(&Stmt{
1646 Cmd: litCall("foo"),
1647 Redirs: []*Redirect{
1648 {Op: DplOut, Word: litWord("2")},
1649 {Op: DplIn, Word: litWord("0")},
1650 {Op: RdrOut, N: lit("2"), Word: litWord("file")},
1651 {Op: RdrOut, N: lit("345"), Word: litWord("file")},
1652 {Op: RdrInOut, Word: litWord("f2")},
1653 },
1654 }),
1655 ),
1656 fileTest(
1657 []string{
1658 "foo bar >file",
1659 "foo bar>file",
1660 },
1661 langFile(&Stmt{
1662 Cmd: litCall("foo", "bar"),
1663 Redirs: []*Redirect{
1664 {Op: RdrOut, Word: litWord("file")},
1665 },
1666 }),
1667 ),
1668 fileTest(
1669 []string{"true &>a"},
1670 langFile(&Stmt{
1671 Cmd: litCall("true"),
1672 Redirs: []*Redirect{
1673 {Op: RdrAll, Word: litWord("a")},
1674 },
1675 }, LangBash|LangMirBSDKorn|LangZsh),
1676 langErr2("1:6: `&>` redirects are a bash/mksh/zsh feature; tried parsing as LANG", LangPOSIX),
1677 flipConfirm2(LangPOSIX), // POSIX shells tend to parse &> as & > hence it runs as two commands
1678 ),
1679 fileTest(
1680 []string{"true &>>b"},
1681 langFile(&Stmt{
1682 Cmd: litCall("true"),
1683 Redirs: []*Redirect{
1684 {Op: AppAll, Word: litWord("b")},
1685 },
1686 }, LangBash|LangMirBSDKorn|LangZsh),
1687 langErr2("1:6: `&>>` redirects are a bash/mksh/zsh feature; tried parsing as LANG", LangPOSIX),
1688 flipConfirm2(LangPOSIX), // POSIX shells tend to parse &> as & > hence it runs as two commands
1689 ),
1690 fileTest(
1691 []string{"foo 2>file bar", "2>file foo bar"},
1692 langFile(&Stmt{
1693 Cmd: litCall("foo", "bar"),
1694 Redirs: []*Redirect{
1695 {Op: RdrOut, N: lit("2"), Word: litWord("file")},
1696 },
1697 }),
1698 ),
1699 fileTest(
1700 []string{"a >f1\nb >f2", "a >f1; b >f2"},
1701 langFile([]*Stmt{
1702 {
1703 Cmd: litCall("a"),
1704 Redirs: []*Redirect{{Op: RdrOut, Word: litWord("f1")}},
1705 },
1706 {
1707 Cmd: litCall("b"),
1708 Redirs: []*Redirect{{Op: RdrOut, Word: litWord("f2")}},
1709 },
1710 }),
1711 ),
1712 fileTest(
1713 []string{"foo >|bar"},
1714 langFile(&Stmt{
1715 Cmd: litCall("foo"),
1716 Redirs: []*Redirect{
1717 {Op: RdrClob, Word: litWord("bar")},
1718 },
1719 }),
1720 ),
1721 fileTest(
1722 []string{"foo >>|a &>|b &>>|c"},
1723 langErr2("1:5: `>>|` redirects are a zsh feature; tried parsing as LANG"),
1724 langFile(&Stmt{
1725 Cmd: litCall("foo"),
1726 Redirs: []*Redirect{
1727 {Op: AppClob, Word: litWord("a")},
1728 {Op: RdrAllClob, Word: litWord("b")},
1729 {Op: AppAllClob, Word: litWord("c")},
1730 },
1731 }, LangZsh),
1732 ),
1733 fileTest(
1734 []string{
1735 "foo >|a >>|b &>|c &>>|d",
1736 "foo >!a >>!b &>!c &>>!d",
1737 },
1738 langFile(&Stmt{
1739 Cmd: litCall("foo"),
1740 Redirs: []*Redirect{
1741 {Op: RdrClob, Word: litWord("a")},
1742 {Op: AppClob, Word: litWord("b")},
1743 {Op: RdrAllClob, Word: litWord("c")},
1744 {Op: AppAllClob, Word: litWord("d")},
1745 },
1746 }, LangZsh),
1747 ),
1748 fileTest(
1749 []string{
1750 "foo &>|a &>>c &>>|d",
1751 "foo >&|a >>&c >>&|d",
1752 "foo &>!a >>&c >>&!d",
1753 "foo >&!a >>&c >>&!d",
1754 },
1755 langFile(&Stmt{
1756 Cmd: litCall("foo"),
1757 Redirs: []*Redirect{
1758 {Op: RdrAllClob, Word: litWord("a")},
1759 {Op: AppAll, Word: litWord("c")},
1760 {Op: AppAllClob, Word: litWord("d")},
1761 },
1762 }, LangZsh),
1763 ),
1764 fileTest(
1765 []string{
1766 "foo <<<input",
1767 "foo <<< input",
1768 },
1769 langFile(&Stmt{
1770 Cmd: litCall("foo"),
1771 Redirs: []*Redirect{{
1772 Op: WordHdoc,
1773 Word: litWord("input"),
1774 }},
1775 }, LangBash|LangMirBSDKorn|LangZsh),
1776 langErr2("1:5: herestrings are a bash/mksh/zsh feature; tried parsing as LANG", LangPOSIX),
1777 ),
1778 fileTest(
1779 []string{
1780 `foo <<<"spaced input"`,
1781 `foo <<< "spaced input"`,
1782 },
1783 langFile(&Stmt{
1784 Cmd: litCall("foo"),
1785 Redirs: []*Redirect{{
1786 Op: WordHdoc,
1787 Word: word(dblQuoted(lit("spaced input"))),
1788 }},
1789 }, LangBash|LangMirBSDKorn|LangZsh),
1790 ),
1791 fileTest(
1792 []string{"foo >(foo)"},
1793 langFile(call(
1794 litWord("foo"),
1795 word(&ProcSubst{
1796 Op: CmdOut,
1797 Stmts: litStmts("foo"),
1798 }),
1799 ), LangBash),
1800 ),
1801 fileTest(
1802 []string{"foo < <(foo)"},
1803 langFile(&Stmt{
1804 Cmd: litCall("foo"),
1805 Redirs: []*Redirect{{
1806 Op: RdrIn,
1807 Word: word(&ProcSubst{
1808 Op: CmdIn,
1809 Stmts: litStmts("foo"),
1810 }),
1811 }},
1812 }, LangBash),
1813 ),
1814 fileTest(
1815 []string{"a<(b) c>(d)"},
1816 langFile(call(
1817 word(lit("a"), &ProcSubst{
1818 Op: CmdIn,
1819 Stmts: litStmts("b"),
1820 }),
1821 word(lit("c"), &ProcSubst{
1822 Op: CmdOut,
1823 Stmts: litStmts("d"),
1824 }),
1825 ), LangBash|LangZsh),
1826 ),
1827 fileTest(
1828 []string{"foo =(bar)"},
1829 langErr2("1:5: `=(` process substitutions are a zsh feature; tried parsing as LANG"),
1830 langFile(call(
1831 litWord("foo"), word(&ProcSubst{
1832 Op: CmdInTemp,
1833 Stmts: litStmts("bar"),
1834 }),
1835 ), LangZsh),
1836 ),
1837 fileTest(
1838 []string{"foo {fd}<f"},
1839 langFile(&Stmt{
1840 Cmd: litCall("foo"),
1841 Redirs: []*Redirect{
1842 {Op: RdrIn, N: lit("{fd}"), Word: litWord("f")},
1843 },
1844 }, LangBash|LangZsh),
1845 ),
1846 // Only bash allows an array element as the fd variable. See issue #719.
1847 fileTest(
1848 []string{"foo {fds[3]}<f"},
1849 langFile(&Stmt{
1850 Cmd: litCall("foo"),
1851 Redirs: []*Redirect{
1852 {Op: RdrIn, N: lit("{fds[3]}"), Word: litWord("f")},
1853 },
1854 }, LangBash),
1855 ),
1856 fileTest(
1857 []string{"! foo"},
1858 langFile(&Stmt{
1859 Negated: true,
1860 Cmd: litCall("foo"),
1861 }),
1862 langSkip(LangZsh), // fails to confirm?
1863 ),
1864 fileTest(
1865 []string{"foo &\nbar", "foo & bar", "foo&bar"},
1866 langFile([]*Stmt{
1867 {Cmd: litCall("foo"), Background: true},
1868 litStmt("bar"),
1869 }),
1870 ),
1871 fileTest(
1872 []string{
1873 "! if foo; then bar; fi >/dev/null &",
1874 "! if foo; then bar; fi>/dev/null&",
1875 },
1876 langFile(&Stmt{
1877 Negated: true,
1878 Cmd: &IfClause{
1879 Cond: litStmts("foo"),
1880 Then: litStmts("bar"),
1881 },
1882 Redirs: []*Redirect{
1883 {Op: RdrOut, Word: litWord("/dev/null")},
1884 },
1885 Background: true,
1886 }),
1887 ),
1888 fileTest(
1889 []string{
1890 // TODO: should we allow formatting a redirect at the start?
1891 "if foo; then bar; fi >/dev/null",
1892 ">/dev/null if foo; then bar; fi",
1893 },
1894 langFile(&Stmt{
1895 Cmd: &IfClause{
1896 Cond: litStmts("foo"),
1897 Then: litStmts("bar"),
1898 },
1899 Redirs: []*Redirect{
1900 {Op: RdrOut, Word: litWord("/dev/null")},
1901 },
1902 }, LangZsh),
1903 ),
1904 fileTest(
1905 []string{"! foo && bar"},
1906 langFile(&BinaryCmd{
1907 Op: AndStmt,
1908 X: &Stmt{
1909 Cmd: litCall("foo"),
1910 Negated: true,
1911 },
1912 Y: litStmt("bar"),
1913 }),
1914 langSkip(LangZsh), // fails to confirm?
1915 ),
1916 fileTest(
1917 []string{"! foo | bar"},
1918 langFile(&Stmt{
1919 Cmd: &BinaryCmd{
1920 Op: Pipe,
1921 X: litStmt("foo"),
1922 Y: litStmt("bar"),
1923 },
1924 Negated: true,
1925 }),
1926 langSkip(LangZsh), // fails to confirm?
1927 ),
1928 fileTest(
1929 []string{
1930 "a && b &\nc",
1931 "a && b & c",
1932 },
1933 langFile([]*Stmt{
1934 {
1935 Cmd: &BinaryCmd{
1936 Op: AndStmt,
1937 X: litStmt("a"),
1938 Y: litStmt("b"),
1939 },
1940 Background: true,
1941 },
1942 litStmt("c"),
1943 }),
1944 ),
1945 fileTest(
1946 []string{"a | b &"},
1947 langFile(&Stmt{
1948 Cmd: &BinaryCmd{
1949 Op: Pipe,
1950 X: litStmt("a"),
1951 Y: litStmt("b"),
1952 },
1953 Background: true,
1954 }),
1955 ),
1956 fileTest(
1957 []string{"foo#bar"},
1958 langFile(litWord("foo#bar")),
1959 ),
1960 fileTest(
1961 []string{"$foo#bar foo#$bar"},
1962 langFile(call(
1963 word(litParamExp("foo"), lit("#bar")),
1964 word(lit("foo#"), litParamExp("bar")),
1965 )),
1966 ),
1967 fileTest(
1968 []string{"$(foo)#bar", "`foo`#bar"},
1969 langFile(call(
1970 word(cmdSubst(litStmt("foo")), lit("#bar")),
1971 )),
1972 ),
1973 fileTest(
1974 []string{"{ foo } }; }"},
1975 langFile(block(litStmt("foo", "}", "}"))),
1976 // TODO: turn these nil files into error tests
1977 langSkip(LangZsh),
1978 ),
1979 fileTest(
1980 []string{"foo {"},
1981 langFile(litStmt("foo", "{")),
1982 ),
1983 fileTest(
1984 []string{"foo }"},
1985 langFile(litStmt("foo", "}"), LangBash),
1986 ),
1987 fileTest(
1988 // TODO: should shfmt lean towards no semicolons in Zsh mode?
1989 // Note that zsh seems to support "{foo}" too, but that is undocumented,
1990 // and hopefully noone actually relies on that.
1991 []string{"{ foo; }", "{ foo }"},
1992 langFile(block(litStmt("foo")), LangZsh),
1993 ),
1994 fileTest(
1995 []string{"{ }"},
1996 langErr2("1:1: `{` must be followed by a statement list"),
1997 langFile(block(), LangZsh|LangMirBSDKorn),
1998 ),
1999 fileTest(
2000 []string{"{ }", "{}"}, // Note that "{}" is a command in POSIX/Bash.
2001 langFile(block(), LangZsh),
2002 ),
2003 fileTest(
2004 []string{"( )"},
2005 langErr2("1:1: `(` must be followed by a statement list"),
2006 langFile(subshell(), LangZsh|LangMirBSDKorn),
2007 ),
2008 fileTest(
2009 []string{"if; then; fi"},
2010 langErr2("1:1: `if` must be followed by a statement list"),
2011 langFile(&IfClause{}, LangZsh|LangMirBSDKorn),
2012 langSkip(LangMirBSDKorn), // only allows empty lists with newlines?
2013 ),
2014 fileTest(
2015 []string{"if foo; then; fi"},
2016 langErr2("1:9: `then` must be followed by a statement list"),
2017 langFile(&IfClause{Cond: litStmts("foo")}, LangZsh|LangMirBSDKorn),
2018 langSkip(LangMirBSDKorn), // only allows empty lists with newlines?
2019 ),
2020 fileTest(
2021 []string{"while; do exit; done", "while\ndo exit\ndone"},
2022 langErr2("1:1: `while` must be followed by a statement list"),
2023 langFile(&WhileClause{Do: litStmts("exit")}, LangZsh|LangMirBSDKorn),
2024 langSkip(LangMirBSDKorn), // only allows empty lists with newlines?
2025 ),
2026 fileTest(
2027 []string{"while false; do; done"},
2028 langErr2("1:14: `do` must be followed by a statement list"),
2029 langFile(&WhileClause{Cond: litStmts("false")}, LangZsh|LangMirBSDKorn),
2030 langSkip(LangMirBSDKorn), // only allows empty lists with newlines?
2031 ),
2032 fileTest(
2033 []string{"$({ foo; })"},
2034 langFile(cmdSubst(stmt(
2035 block(litStmt("foo")),
2036 ))),
2037 ),
2038 fileTest(
2039 []string{
2040 "$( (echo foo bar) )",
2041 "$( (echo foo bar))",
2042 "`(echo foo bar)`",
2043 },
2044 langFile(cmdSubst(stmt(
2045 subshell(litStmt("echo", "foo", "bar")),
2046 ))),
2047 ),
2048 fileTest(
2049 []string{"$()"},
2050 langFile(cmdSubst()),
2051 ),
2052 fileTest(
2053 []string{
2054 "$(\n\t(a)\n\tb\n)",
2055 "$( (a); b)",
2056 "`(a); b`",
2057 },
2058 langFile(cmdSubst(
2059 stmt(subshell(litStmt("a"))),
2060 litStmt("b"),
2061 )),
2062 ),
2063 fileTest(
2064 []string{
2065 `$(echo \')`,
2066 "`" + `echo \\'` + "`",
2067 },
2068 langFile(cmdSubst(litStmt("echo", `\'`))),
2069 ),
2070 fileTest(
2071 []string{
2072 `$(echo \\)`,
2073 "`" + `echo \\\\` + "`",
2074 },
2075 langFile(cmdSubst(litStmt("echo", `\\`))),
2076 ),
2077 fileTest(
2078 []string{
2079 `$(echo '\' 'a\b' "\\" "a\a")`,
2080 "`" + `echo '\' 'a\\b' "\\\\" "a\a"` + "`",
2081 },
2082 langFile(cmdSubst(stmt(call(
2083 litWord("echo"),
2084 word(sglQuoted(`\`)),
2085 word(sglQuoted(`a\b`)),
2086 word(dblQuoted(lit(`\\`))),
2087 word(dblQuoted(lit(`a\a`))),
2088 )))),
2089 ),
2090 fileTest(
2091 []string{
2092 "$(echo $(x))",
2093 "`echo \\`x\\``",
2094 },
2095 langFile(cmdSubst(stmt(call(
2096 litWord("echo"),
2097 word(cmdSubst(litStmt("x"))),
2098 )))),
2099 ),
2100 fileTest(
2101 []string{
2102 "$($(foo bar))",
2103 "`\\`foo bar\\``",
2104 },
2105 langFile(cmdSubst(stmt(call(
2106 word(cmdSubst(litStmt("foo", "bar"))),
2107 )))),
2108 ),
2109 // In a backquote command substitution within double quotes,
2110 // backslashes escape double quotes as well. See issue #1083.
2111 fileTest(
2112 []string{
2113 `"$(echo "foobar")"`,
2114 "\"`echo \\\"foobar\\\"`\"",
2115 },
2116 langFile(word(dblQuoted(cmdSubst(stmt(call(
2117 litWord("echo"),
2118 word(dblQuoted(lit("foobar"))),
2119 )))))),
2120 ),
2121 fileTest(
2122 []string{
2123 `"$(echo '"')"`,
2124 "\"`echo '\\\"'`\"",
2125 },
2126 langFile(word(dblQuoted(cmdSubst(stmt(call(
2127 litWord("echo"),
2128 word(sglQuoted(`"`)),
2129 )))))),
2130 ),
2131 fileTest(
2132 []string{"$( (a) | b)"},
2133 langFile(cmdSubst(
2134 stmt(&BinaryCmd{
2135 Op: Pipe,
2136 X: stmt(subshell(litStmt("a"))),
2137 Y: litStmt("b"),
2138 }),
2139 )),
2140 ),
2141 fileTest(
2142 []string{`"$( (foo) )"`},
2143 langFile(dblQuoted(cmdSubst(stmt(
2144 subshell(litStmt("foo")),
2145 )))),
2146 ),
2147 fileTest(
2148 []string{"\"foo\\\nbar\""},
2149 langFile(dblQuoted(lit("foo"), lit("bar"))),
2150 ),
2151 fileTest(
2152 []string{"'foo\\\nbar'", "'foo\\\r\nbar'"},
2153 langFile(sglQuoted("foo\\\nbar")),
2154 ),
2155 fileTest(
2156 []string{"$({ echo; })", "`{ echo; }`"},
2157 langFile(cmdSubst(stmt(
2158 block(litStmt("echo")),
2159 ))),
2160 ),
2161 fileTest(
2162 []string{`{foo}`},
2163 langFile(litWord(`{foo}`)),
2164 ),
2165 fileTest(
2166 []string{`{"foo"`},
2167 langFile(word(lit("{"), dblQuoted(lit("foo")))),
2168 langSkip(LangZsh),
2169 ),
2170 fileTest(
2171 []string{`foo"bar"`, "fo\\\no\"bar\"", "fo\\\r\no\"bar\""},
2172 langFile(word(lit("foo"), dblQuoted(lit("bar")))),
2173 ),
2174 fileTest(
2175 []string{`!foo`},
2176 langFile(litWord(`!foo`)),
2177 ),
2178 fileTest(
2179 []string{"$(foo bar)", "`foo bar`"},
2180 langFile(cmdSubst(litStmt("foo", "bar"))),
2181 ),
2182 fileTest(
2183 []string{"$(foo | bar)", "`foo | bar`"},
2184 langFile(cmdSubst(
2185 stmt(&BinaryCmd{
2186 Op: Pipe,
2187 X: litStmt("foo"),
2188 Y: litStmt("bar"),
2189 }),
2190 )),
2191 ),
2192 fileTest(
2193 []string{"$(foo | >f)", "`foo | >f`"},
2194 langFile(cmdSubst(
2195 stmt(&BinaryCmd{
2196 Op: Pipe,
2197 X: litStmt("foo"),
2198 Y: &Stmt{Redirs: []*Redirect{{
2199 Op: RdrOut,
2200 Word: litWord("f"),
2201 }}},
2202 }),
2203 )),
2204 ),
2205 fileTest(
2206 []string{"$(foo $(b1 b2))"},
2207 langFile(cmdSubst(stmt(call(
2208 litWord("foo"),
2209 word(cmdSubst(litStmt("b1", "b2"))),
2210 )))),
2211 ),
2212 fileTest(
2213 []string{`"$(foo "bar")"`},
2214 langFile(dblQuoted(cmdSubst(stmt(call(
2215 litWord("foo"),
2216 word(dblQuoted(lit("bar"))),
2217 ))))),
2218 ),
2219 fileTest(
2220 []string{"$(foo)", "`fo\\\no`"},
2221 langFile(cmdSubst(litStmt("foo"))),
2222 ),
2223 fileTest(
2224 []string{"foo $(bar)", "foo `bar`"},
2225 langFile(call(
2226 litWord("foo"),
2227 word(cmdSubst(litStmt("bar"))),
2228 )),
2229 ),
2230 fileTest(
2231 []string{"$(foo 'bar')", "`foo 'bar'`"},
2232 langFile(cmdSubst(stmt(call(
2233 litWord("foo"),
2234 word(sglQuoted("bar")),
2235 )))),
2236 ),
2237 fileTest(
2238 []string{`$(foo "bar")`, "`foo \"bar\"`"},
2239 langFile(cmdSubst(stmt(call(
2240 litWord("foo"),
2241 word(dblQuoted(lit("bar"))),
2242 )))),
2243 ),
2244 fileTest(
2245 []string{`"$(foo "bar")"`, "\"`foo \"bar\"`\""},
2246 langFile(dblQuoted(cmdSubst(stmt(call(
2247 litWord("foo"),
2248 word(dblQuoted(lit("bar"))),
2249 ))))),
2250 ),
2251 fileTest(
2252 []string{"${ foo;}", "${\nfoo; }", "${\n\tfoo; }", "${\tfoo;}"},
2253 langFile(&CmdSubst{
2254 Stmts: litStmts("foo"),
2255 TempFile: true,
2256 }, LangBash|LangMirBSDKorn),
2257 langErr2("1:1: `${ stmts;}` is a bash/mksh feature; tried parsing as LANG", LangPOSIX),
2258 ),
2259 fileTest(
2260 []string{"${\n\tfoo\n\tbar\n}", "${ foo; bar;}"},
2261 langFile(&CmdSubst{
2262 Stmts: litStmts("foo", "bar"),
2263 TempFile: true,
2264 }, LangBash|LangMirBSDKorn),
2265 ),
2266 fileTest(
2267 []string{"${|foo;}", "${| foo; }"},
2268 langFile(&CmdSubst{
2269 Stmts: litStmts("foo"),
2270 ReplyVar: true,
2271 }, LangBash|LangMirBSDKorn),
2272 langErr2("1:1: `${|stmts;}` is a bash/mksh feature; tried parsing as LANG", LangPOSIX),
2273 ),
2274 fileTest(
2275 []string{"${|\n\tfoo\n\tbar\n}", "${|foo; bar;}"},
2276 langFile(&CmdSubst{
2277 Stmts: litStmts("foo", "bar"),
2278 ReplyVar: true,
2279 }, LangBash|LangMirBSDKorn),
2280 ),
2281 fileTest(
2282 []string{`"${ foo;}"`, `"${ foo; }"`},
2283 langFile(dblQuoted(&CmdSubst{
2284 Stmts: litStmts("foo"),
2285 TempFile: true,
2286 }), LangBash|LangMirBSDKorn),
2287 langErr2("1:2: `${ stmts;}` is a bash/mksh feature; tried parsing as LANG", LangPOSIX),
2288 ),
2289 fileTest(
2290 []string{`"${|foo;}"`, `"${| foo; }"`},
2291 langFile(dblQuoted(&CmdSubst{
2292 Stmts: litStmts("foo"),
2293 ReplyVar: true,
2294 }), LangBash|LangMirBSDKorn),
2295 langErr2("1:2: `${|stmts;}` is a bash/mksh feature; tried parsing as LANG", LangPOSIX),
2296 ),
2297 fileTest(
2298 []string{`${ foo;}bar`},
2299 langFile(word(&CmdSubst{
2300 Stmts: litStmts("foo"),
2301 TempFile: true,
2302 }, lit("bar")), LangBash|LangMirBSDKorn),
2303 langErr2("1:1: `${ stmts;}` is a bash/mksh feature; tried parsing as LANG", LangPOSIX),
2304 ),
2305 fileTest(
2306 []string{`"$foo"`},
2307 langFile(dblQuoted(litParamExp("foo"))),
2308 ),
2309 fileTest(
2310 []string{`"#foo"`},
2311 langFile(dblQuoted(lit("#foo"))),
2312 ),
2313 fileTest(
2314 []string{`$2foo $3[bar] "ba$4z"`},
2315 langFile(call(
2316 word(litParamExp("2"), lit("foo")),
2317 word(litParamExp("3"), lit("[bar]")),
2318 word(dblQuoted(lit("ba"), litParamExp("4"), lit("z"))),
2319 )),
2320 ),
2321 fileTest(
2322 []string{`$@a $*a $#a $$a $?a $!a $-a $0a $30a $_a`},
2323 langFile(call(
2324 word(litParamExp("@"), lit("a")),
2325 word(litParamExp("*"), lit("a")),
2326 word(litParamExp("#"), lit("a")),
2327 word(litParamExp("$"), lit("a")),
2328 word(litParamExp("?"), lit("a")),
2329 word(litParamExp("!"), lit("a")),
2330 word(litParamExp("-"), lit("a")),
2331 word(litParamExp("0"), lit("a")),
2332 word(litParamExp("3"), lit("0a")),
2333 word(litParamExp("_a")),
2334 )),
2335 langSkip(LangZsh), // TODO: $#a parses as ParamExp, but $!a does not
2336 ),
2337 fileTest(
2338 []string{`"$!"`},
2339 langFile(dblQuoted(litParamExp("!"))),
2340 ),
2341 fileTest(
2342 []string{`"$#" $#"$foo" $#`},
2343 langFile(call(
2344 word(dblQuoted(litParamExp("#"))),
2345 word(litParamExp("#"), dblQuoted(litParamExp("foo"))),
2346 word(litParamExp("#")),
2347 )),
2348 ),
2349 fileTest(
2350 []string{`"$+" $+"$foo" $+`},
2351 langFile(call(
2352 word(dblQuoted(lit("$"), lit("+"))),
2353 word(lit("$"), lit("+"), dblQuoted(litParamExp("foo"))),
2354 word(lit("$"), lit("+")),
2355 )),
2356 ),
2357 fileTest(
2358 []string{`"$%" $%"$foo" $%foo`},
2359 langFile(call(
2360 word(dblQuoted(lit("$"), lit("%"))),
2361 word(lit("$"), lit("%"), dblQuoted(litParamExp("foo"))),
2362 word(lit("$"), lit("%foo")),
2363 )),
2364 ),
2365 fileTest(
2366 []string{`$="$foo" $~"$foo" $^"$foo"`},
2367 langFile(call(
2368 word(lit("$"), lit("="), dblQuoted(litParamExp("foo"))),
2369 word(lit("$"), lit("~"), dblQuoted(litParamExp("foo"))),
2370 word(lit("$"), lit("^"), dblQuoted(litParamExp("foo"))),
2371 )),
2372 ),
2373 fileTest(
2374 []string{`$`, `$ #`},
2375 langFile(litWord("$")),
2376 ),
2377 fileTest(
2378 []string{`${@} ${*} ${#} ${$} ${?} ${!} ${0} ${29} ${-}`},
2379 langFile(call(
2380 word(&ParamExp{Param: lit("@")}),
2381 word(&ParamExp{Param: lit("*")}),
2382 word(&ParamExp{Param: lit("#")}),
2383 word(&ParamExp{Param: lit("$")}),
2384 word(&ParamExp{Param: lit("?")}),
2385 word(&ParamExp{Param: lit("!")}),
2386 word(&ParamExp{Param: lit("0")}),
2387 word(&ParamExp{Param: lit("29")}),
2388 word(&ParamExp{Param: lit("-")}),
2389 )),
2390 ),
2391 fileTest(
2392 []string{`${#$} ${#@} ${#*} ${##}`},
2393 langFile(call(
2394 word(&ParamExp{Length: true, Param: lit("$")}),
2395 word(&ParamExp{Length: true, Param: lit("@")}),
2396 word(&ParamExp{Length: true, Param: lit("*")}),
2397 word(&ParamExp{Length: true, Param: lit("#")}),
2398 )),
2399 ),
2400 fileTest(
2401 []string{`${foo}`},
2402 langFile(&ParamExp{Param: lit("foo")}),
2403 ),
2404 fileTest(
2405 []string{`${}`},
2406 langErr2("1:3: invalid parameter name"),
2407 langFile(&ParamExp{}, LangZsh),
2408 ),
2409 fileTest(
2410 []string{`${foo}"bar"`},
2411 langFile(word(
2412 &ParamExp{Param: lit("foo")},
2413 dblQuoted(lit("bar")),
2414 )),
2415 ),
2416 fileTest(
2417 []string{`$a/b $a-b $a:b $a}b $a]b $a.b $a,b $a*b $a_b $a2b`},
2418 langFile(call(
2419 word(litParamExp("a"), lit("/b")),
2420 word(litParamExp("a"), lit("-b")),
2421 word(litParamExp("a"), lit(":b")),
2422 word(litParamExp("a"), lit("}b")),
2423 word(litParamExp("a"), lit("]b")),
2424 word(litParamExp("a"), lit(".b")),
2425 word(litParamExp("a"), lit(",b")),
2426 word(litParamExp("a"), lit("*b")),
2427 word(litParamExp("a_b")),
2428 word(litParamExp("a2b")),
2429 )),
2430 ),
2431 fileTest(
2432 []string{`$aàb $àb $,b`},
2433 langFile(call(
2434 word(litParamExp("a"), lit("àb")),
2435 word(lit("$"), lit("àb")),
2436 word(lit("$"), lit(",b")),
2437 )),
2438 ),
2439 fileTest(
2440 []string{"$à", "$\\\nà", "$\\\r\nà"},
2441 langFile(word(lit("$"), lit("à"))),
2442 ),
2443 fileTest(
2444 []string{"$foobar", "$foo\\\nbar"},
2445 langFile(call(
2446 word(litParamExp("foobar")),
2447 )),
2448 ),
2449 fileTest(
2450 []string{"$foo\\bar"},
2451 langFile(call(
2452 word(litParamExp("foo"), lit("\\bar")),
2453 )),
2454 ),
2455 fileTest(
2456 []string{`echo -e "$foo\nbar"`},
2457 langFile(call(
2458 litWord("echo"), litWord("-e"),
2459 word(dblQuoted(
2460 litParamExp("foo"), lit(`\nbar`),
2461 )),
2462 )),
2463 ),
2464 fileTest(
2465 []string{`${foo-bar}`},
2466 langFile(&ParamExp{
2467 Param: lit("foo"),
2468 Exp: &Expansion{
2469 Op: DefaultUnset,
2470 Word: litWord("bar"),
2471 },
2472 }),
2473 ),
2474 fileTest(
2475 []string{`${foo+}"bar"`},
2476 langFile(word(
2477 &ParamExp{
2478 Param: lit("foo"),
2479 Exp: &Expansion{Op: AlternateUnset},
2480 },
2481 dblQuoted(lit("bar")),
2482 )),
2483 ),
2484 fileTest(
2485 []string{`${foo:=<"bar"}`},
2486 langFile(&ParamExp{
2487 Param: lit("foo"),
2488 Exp: &Expansion{
2489 Op: AssignUnsetOrNull,
2490 Word: word(lit("<"), dblQuoted(lit("bar"))),
2491 },
2492 }),
2493 ),
2494 fileTest(
2495 []string{
2496 "${foo:=b${c}$(d)}",
2497 "${foo:=b${c}`d`}",
2498 },
2499 langFile(&ParamExp{
2500 Param: lit("foo"),
2501 Exp: &Expansion{
2502 Op: AssignUnsetOrNull,
2503 Word: word(
2504 lit("b"),
2505 &ParamExp{Param: lit("c")},
2506 cmdSubst(litStmt("d")),
2507 ),
2508 },
2509 }),
2510 ),
2511 fileTest(
2512 []string{`${foo?"${bar}"}`},
2513 langFile(&ParamExp{
2514 Param: lit("foo"),
2515 Exp: &Expansion{
2516 Op: ErrorUnset,
2517 Word: word(dblQuoted(
2518 &ParamExp{Param: lit("bar")},
2519 )),
2520 },
2521 }),
2522 ),
2523 fileTest(
2524 []string{`${foo:?bar1 bar2}`},
2525 langFile(&ParamExp{
2526 Param: lit("foo"),
2527 Exp: &Expansion{
2528 Op: ErrorUnsetOrNull,
2529 Word: litWord("bar1 bar2"),
2530 },
2531 }),
2532 ),
2533 fileTest(
2534 []string{`${a:+b}${a:-b}${a=b}`},
2535 langFile(word(
2536 &ParamExp{
2537 Param: lit("a"),
2538 Exp: &Expansion{
2539 Op: AlternateUnsetOrNull,
2540 Word: litWord("b"),
2541 },
2542 },
2543 &ParamExp{
2544 Param: lit("a"),
2545 Exp: &Expansion{
2546 Op: DefaultUnsetOrNull,
2547 Word: litWord("b"),
2548 },
2549 },
2550 &ParamExp{
2551 Param: lit("a"),
2552 Exp: &Expansion{
2553 Op: AssignUnset,
2554 Word: litWord("b"),
2555 },
2556 },
2557 )),
2558 ),
2559 fileTest(
2560 []string{`${3:-'$x'}`},
2561 langFile(&ParamExp{
2562 Param: lit("3"),
2563 Exp: &Expansion{
2564 Op: DefaultUnsetOrNull,
2565 Word: word(sglQuoted("$x")),
2566 },
2567 }),
2568 ),
2569 fileTest(
2570 []string{`${@:-$x}`},
2571 langFile(&ParamExp{
2572 Param: lit("@"),
2573 Exp: &Expansion{
2574 Op: DefaultUnsetOrNull,
2575 Word: word(litParamExp("x")),
2576 },
2577 }),
2578 ),
2579 fileTest(
2580 []string{`${var#*'="'}`},
2581 langFile(&ParamExp{
2582 Param: lit("var"),
2583 Exp: &Expansion{
2584 Op: RemSmallPrefix,
2585 Word: word(lit("*"), sglQuoted(`="`)),
2586 },
2587 }),
2588 ),
2589 fileTest(
2590 []string{`${var/'a'/b'c'd}`},
2591 langFile(&ParamExp{
2592 Param: lit("var"),
2593 Repl: &Replace{
2594 Orig: word(sglQuoted("a")),
2595 With: word(lit("b"), sglQuoted("c"), lit("d")),
2596 },
2597 }, LangBash|LangMirBSDKorn|LangZsh),
2598 ),
2599 fileTest(
2600 []string{`${foo%bar}${foo%%bar*}`},
2601 langFile(word(
2602 &ParamExp{
2603 Param: lit("foo"),
2604 Exp: &Expansion{
2605 Op: RemSmallSuffix,
2606 Word: litWord("bar"),
2607 },
2608 },
2609 &ParamExp{
2610 Param: lit("foo"),
2611 Exp: &Expansion{
2612 Op: RemLargeSuffix,
2613 Word: litWord("bar*"),
2614 },
2615 },
2616 )),
2617 ),
2618 fileTest(
2619 []string{`${3#bar}${-##bar*}`},
2620 langFile(word(
2621 &ParamExp{
2622 Param: lit("3"),
2623 Exp: &Expansion{
2624 Op: RemSmallPrefix,
2625 Word: litWord("bar"),
2626 },
2627 },
2628 &ParamExp{
2629 Param: lit("-"),
2630 Exp: &Expansion{
2631 Op: RemLargePrefix,
2632 Word: litWord("bar*"),
2633 },
2634 },
2635 )),
2636 ),
2637 fileTest(
2638 []string{`${foo%?}`},
2639 langFile(&ParamExp{
2640 Param: lit("foo"),
2641 Exp: &Expansion{
2642 Op: RemSmallSuffix,
2643 Word: litWord("?"),
2644 },
2645 }),
2646 ),
2647 fileTest(
2648 []string{"foo=force_expansion\necho ${foo:#bar}"},
2649 langErr2("2:11: ${name:#arg} is a zsh feature; tried parsing as LANG"),
2650 langFile(stmts(
2651 &CallExpr{Assigns: litAssigns("foo=force_expansion")},
2652 call(litWord("echo"), word(&ParamExp{
2653 Param: lit("foo"),
2654 Exp: &Expansion{
2655 Op: MatchEmpty,
2656 Word: litWord("bar"),
2657 },
2658 })),
2659 ), LangZsh),
2660 flipConfirm2(LangMirBSDKorn), // TODO: why is this a valid substitution in mksh?
2661 ),
2662 fileTest(
2663 []string{"foo=force_expansion\necho ${foo:|bar}"},
2664 langErr2("2:11: ${name:|arg} is a zsh feature; tried parsing as LANG"),
2665 langFile(stmts(
2666 &CallExpr{Assigns: litAssigns("foo=force_expansion")},
2667 call(litWord("echo"), word(&ParamExp{
2668 Param: lit("foo"),
2669 Exp: &Expansion{
2670 Op: ArrayExclude,
2671 Word: litWord("bar"),
2672 },
2673 })),
2674 ), LangZsh),
2675 ),
2676 fileTest(
2677 []string{"foo=force_expansion\necho ${foo:*bar}"},
2678 langErr2("2:11: ${name:*arg} is a zsh feature; tried parsing as LANG"),
2679 langFile(stmts(
2680 &CallExpr{Assigns: litAssigns("foo=force_expansion")},
2681 call(litWord("echo"), word(&ParamExp{
2682 Param: lit("foo"),
2683 Exp: &Expansion{
2684 Op: ArrayIntersect,
2685 Word: litWord("bar"),
2686 },
2687 })),
2688 ), LangZsh),
2689 ),
2690 fileTest(
2691 []string{
2692 `${foo[1]}`,
2693 `${foo[ 1 ]}`,
2694 },
2695 langFile(&ParamExp{
2696 Param: lit("foo"),
2697 Index: litWord("1"),
2698 }, LangBash|LangMirBSDKorn|LangZsh),
2699 langErr2("1:6: arrays are a bash/mksh/zsh feature; tried parsing as LANG", LangPOSIX),
2700 ),
2701 fileTest(
2702 []string{`$foo[1]`},
2703 langFile(&ParamExp{
2704 Short: true,
2705 Param: lit("foo"),
2706 Index: litWord("1"),
2707 }, LangZsh),
2708 ),
2709 fileTest(
2710 []string{`a$foo[1]b`},
2711 langFile(word(lit("a"), litParamExp("foo"), lit("[1]b"))),
2712 langFile(word(
2713 lit("a"),
2714 &ParamExp{
2715 Short: true,
2716 Param: lit("foo"),
2717 Index: litWord("1"),
2718 },
2719 lit("b"),
2720 ), LangZsh),
2721 ),
2722 fileTest(
2723 []string{`"a$foo[1]b"`},
2724 langFile(dblQuoted(
2725 lit("a"),
2726 &ParamExp{
2727 Short: true,
2728 Param: lit("foo"),
2729 Index: litWord("1"),
2730 },
2731 lit("b"),
2732 ), LangZsh),
2733 ),
2734 fileTest(
2735 []string{`${foo[1,3]}`, `${foo[ 1 , 3 ]}`},
2736 langFile(&ParamExp{
2737 Param: lit("foo"),
2738 Index: &BinaryArithm{
2739 Op: Comma,
2740 X: litWord("1"),
2741 Y: litWord("3"),
2742 },
2743 }, LangBash|LangMirBSDKorn|LangZsh),
2744 langErr2("1:6: arrays are a bash/mksh/zsh feature; tried parsing as LANG", LangPOSIX),
2745 ),
2746 fileTest(
2747 []string{`${a:(1):(2)}`},
2748 langFile(&ParamExp{
2749 Param: lit("a"),
2750 Slice: &Slice{
2751 Offset: parenArit(litWord("1")),
2752 Length: parenArit(litWord("2")),
2753 },
2754 }, LangBash|LangMirBSDKorn|LangZsh),
2755 langErr2("1:4: slicing is a bash/mksh/zsh feature; tried parsing as LANG", LangPOSIX),
2756 ),
2757 fileTest(
2758 []string{`${args[cmd,#]}`},
2759 langFile(&ParamExp{
2760 Param: lit("args"),
2761 Index: &BinaryArithm{
2762 Op: Comma,
2763 X: litWord("cmd"),
2764 Y: litWord("#"),
2765 },
2766 }, LangBash|LangMirBSDKorn|LangZsh),
2767 langErr2("1:7: arrays are a bash/mksh/zsh feature; tried parsing as LANG", LangPOSIX),
2768 ),
2769 fileTest(
2770 []string{`${foo[1,-1]}`},
2771 langFile(&ParamExp{
2772 Param: lit("foo"),
2773 Index: &BinaryArithm{
2774 Op: Comma,
2775 X: litWord("1"),
2776 Y: &UnaryArithm{
2777 Op: Minus,
2778 X: litWord("1"),
2779 },
2780 },
2781 }, LangBash|LangMirBSDKorn|LangZsh),
2782 langErr2("1:6: arrays are a bash/mksh/zsh feature; tried parsing as LANG", LangPOSIX),
2783 ),
2784 fileTest(
2785 []string{`${@[-1]}`},
2786 langFile(&ParamExp{
2787 Param: lit("@"),
2788 Index: &UnaryArithm{
2789 Op: Minus,
2790 X: litWord("1"),
2791 },
2792 }, LangZsh),
2793 ),
2794 fileTest(
2795 []string{`${*[-1]}`},
2796 langFile(&ParamExp{
2797 Param: lit("*"),
2798 Index: &UnaryArithm{
2799 Op: Minus,
2800 X: litWord("1"),
2801 },
2802 }, LangZsh),
2803 ),
2804 fileTest(
2805 []string{`$foo[1,3]`},
2806 langFile(&ParamExp{
2807 Short: true,
2808 Param: lit("foo"),
2809 Index: &BinaryArithm{
2810 Op: Comma,
2811 X: litWord("1"),
2812 Y: litWord("3"),
2813 },
2814 }, LangZsh),
2815 ),
2816 fileTest(
2817 []string{`${signals[(i)QUIT]}`},
2818 langFile(&ParamExp{
2819 Param: lit("signals"),
2820 Index: &FlagsArithm{
2821 Flags: lit("i"),
2822 X: litWord("QUIT"),
2823 },
2824 }, LangZsh),
2825 langErr2("1:11: subscript flags are a zsh feature; tried parsing as LANG", LangBash|LangMirBSDKorn),
2826 ),
2827 fileTest(
2828 []string{`${ZSH_VERSION[(s:.:w)2]}`},
2829 langFile(&ParamExp{
2830 Param: lit("ZSH_VERSION"),
2831 Index: &FlagsArithm{
2832 Flags: lit("s:.:w"),
2833 X: litWord("2"),
2834 },
2835 }, LangZsh),
2836 ),
2837 fileTest(
2838 []string{`$foo[(r)pattern]`},
2839 langFile(&ParamExp{
2840 Short: true,
2841 Param: lit("foo"),
2842 Index: &FlagsArithm{
2843 Flags: lit("r"),
2844 X: litWord("pattern"),
2845 },
2846 }, LangZsh),
2847 ),
2848 fileTest(
2849 []string{`${array[(i)]}`},
2850 langFile(&ParamExp{
2851 Param: lit("array"),
2852 Index: &FlagsArithm{
2853 Flags: lit("i"),
2854 },
2855 }, LangZsh),
2856 ),
2857 fileTest(
2858 []string{`${array[(i)--]}`},
2859 langFile(&ParamExp{
2860 Param: lit("array"),
2861 Index: &FlagsArithm{
2862 Flags: lit("i"),
2863 X: litWord("--"),
2864 },
2865 }, LangZsh),
2866 ),
2867 fileTest(
2868 []string{`${array[(R)/*]}`},
2869 langFile(&ParamExp{
2870 Param: lit("array"),
2871 Index: &FlagsArithm{
2872 Flags: lit("R"),
2873 X: litWord("/*"),
2874 },
2875 }, LangZsh),
2876 ),
2877 fileTest(
2878 []string{`${array[(i)&]}`},
2879 langFile(&ParamExp{
2880 Param: lit("array"),
2881 Index: &FlagsArithm{
2882 Flags: lit("i"),
2883 X: litWord("&"),
2884 },
2885 }, LangZsh),
2886 ),
2887 fileTest(
2888 []string{`${foo[(r)ab,(r)cd]}`},
2889 langFile(&ParamExp{
2890 Param: lit("foo"),
2891 Index: &BinaryArithm{
2892 Op: Comma,
2893 X: &FlagsArithm{
2894 Flags: lit("r"),
2895 X: litWord("ab"),
2896 },
2897 Y: &FlagsArithm{
2898 Flags: lit("r"),
2899 X: litWord("cd"),
2900 },
2901 },
2902 }, LangZsh),
2903 ),
2904 fileTest(
2905 []string{`${foo[-1]}`},
2906 langFile(&ParamExp{
2907 Param: lit("foo"),
2908 Index: &UnaryArithm{
2909 Op: Minus,
2910 X: litWord("1"),
2911 },
2912 }, LangBash|LangMirBSDKorn|LangZsh),
2913 ),
2914 fileTest(
2915 []string{`${foo[@]}`},
2916 langFile(&ParamExp{
2917 Param: lit("foo"),
2918 Index: litWord("@"),
2919 }, LangBash|LangMirBSDKorn|LangZsh),
2920 ),
2921 fileTest(
2922 []string{`${foo[*]-etc}`},
2923 langFile(&ParamExp{
2924 Param: lit("foo"),
2925 Index: litWord("*"),
2926 Exp: &Expansion{
2927 Op: DefaultUnset,
2928 Word: litWord("etc"),
2929 },
2930 }, LangBash|LangMirBSDKorn|LangZsh),
2931 ),
2932 fileTest(
2933 []string{`${foo[bar]}`},
2934 langFile(&ParamExp{
2935 Param: lit("foo"),
2936 Index: litWord("bar"),
2937 }, LangBash|LangMirBSDKorn|LangZsh),
2938 ),
2939 fileTest(
2940 []string{`${foo[$bar]}`},
2941 langFile(&ParamExp{
2942 Param: lit("foo"),
2943 Index: word(litParamExp("bar")),
2944 }, LangBash|LangMirBSDKorn|LangZsh),
2945 ),
2946 fileTest(
2947 []string{`${foo[${bar}]}`},
2948 langFile(&ParamExp{
2949 Param: lit("foo"),
2950 Index: word(&ParamExp{Param: lit("bar")}),
2951 }, LangBash|LangMirBSDKorn|LangZsh),
2952 ),
2953 fileTest(
2954 []string{`${foo:1}`, `${foo: 1 }`},
2955 langFile(&ParamExp{
2956 Param: lit("foo"),
2957 Slice: &Slice{Offset: litWord("1")},
2958 }, LangBash|LangMirBSDKorn|LangZsh),
2959 langErr2("1:6: slicing is a bash/mksh/zsh feature; tried parsing as LANG", LangPOSIX),
2960 ),
2961 fileTest(
2962 []string{`${foo:1:2}`, `${foo: 1 : 2 }`},
2963 langFile(&ParamExp{
2964 Param: lit("foo"),
2965 Slice: &Slice{
2966 Offset: litWord("1"),
2967 Length: litWord("2"),
2968 },
2969 }, LangBash|LangMirBSDKorn|LangZsh),
2970 ),
2971 fileTest(
2972 []string{`${foo:a:b}`},
2973 langFile(&ParamExp{
2974 Param: lit("foo"),
2975 Slice: &Slice{
2976 Offset: litWord("a"),
2977 Length: litWord("b"),
2978 },
2979 }, LangBash|LangMirBSDKorn),
2980 ),
2981 fileTest(
2982 []string{`${foo:1:-2}`},
2983 langFile(&ParamExp{
2984 Param: lit("foo"),
2985 Slice: &Slice{
2986 Offset: litWord("1"),
2987 Length: &UnaryArithm{Op: Minus, X: litWord("2")},
2988 },
2989 }, LangBash|LangMirBSDKorn), // TODO: zsh -n is buggy here
2990 ),
2991 fileTest(
2992 []string{`${foo::+3}`},
2993 langFile(&ParamExp{
2994 Param: lit("foo"),
2995 Slice: &Slice{
2996 Length: &UnaryArithm{Op: Plus, X: litWord("3")},
2997 },
2998 }, LangBash|LangMirBSDKorn|LangZsh),
2999 ),
3000 fileTest(
3001 []string{`${foo: -1}`},
3002 langFile(&ParamExp{
3003 Param: lit("foo"),
3004 Slice: &Slice{
3005 Offset: &UnaryArithm{Op: Minus, X: litWord("1")},
3006 },
3007 }, LangBash|LangMirBSDKorn|LangZsh),
3008 ),
3009 fileTest(
3010 []string{`${foo: +2+3}`},
3011 langFile(&ParamExp{
3012 Param: lit("foo"),
3013 Slice: &Slice{
3014 Offset: &BinaryArithm{
3015 Op: Add,
3016 X: &UnaryArithm{Op: Plus, X: litWord("2")},
3017 Y: litWord("3"),
3018 },
3019 },
3020 }, LangBash|LangMirBSDKorn|LangZsh),
3021 ),
3022 fileTest(
3023 []string{`${foo:a?1:2:3}`},
3024 langFile(&ParamExp{
3025 Param: lit("foo"),
3026 Slice: &Slice{
3027 Offset: &BinaryArithm{
3028 Op: TernQuest,
3029 X: litWord("a"),
3030 Y: &BinaryArithm{
3031 Op: TernColon,
3032 X: litWord("1"),
3033 Y: litWord("2"),
3034 },
3035 },
3036 Length: litWord("3"),
3037 },
3038 }, LangBash|LangMirBSDKorn),
3039 ),
3040 fileTest(
3041 []string{`${foo/a/b}`},
3042 langFile(&ParamExp{
3043 Param: lit("foo"),
3044 Repl: &Replace{Orig: litWord("a"), With: litWord("b")},
3045 }, LangBash|LangMirBSDKorn|LangZsh),
3046 langErr2("1:6: search and replace is a bash/mksh/zsh feature; tried parsing as LANG", LangPOSIX),
3047 ),
3048 fileTest(
3049 []string{"${foo/ /\t}"},
3050 langFile(&ParamExp{
3051 Param: lit("foo"),
3052 Repl: &Replace{Orig: litWord(" "), With: litWord("\t")},
3053 }, LangBash|LangMirBSDKorn|LangZsh),
3054 ),
3055 fileTest(
3056 []string{`${foo/[/]-}`},
3057 langFile(&ParamExp{
3058 Param: lit("foo"),
3059 Repl: &Replace{Orig: litWord("["), With: litWord("]-")},
3060 }, LangBash|LangMirBSDKorn), // TODO: zsh parses as a pattern?
3061 ),
3062 fileTest(
3063 []string{`${foo/bar/b/a/r}`},
3064 langFile(&ParamExp{
3065 Param: lit("foo"),
3066 Repl: &Replace{
3067 Orig: litWord("bar"),
3068 With: litWord("b/a/r"),
3069 },
3070 }, LangBash|LangMirBSDKorn|LangZsh),
3071 ),
3072 fileTest(
3073 []string{`${foo/$a/$'\''}`},
3074 langFile(&ParamExp{
3075 Param: lit("foo"),
3076 Repl: &Replace{
3077 Orig: word(litParamExp("a")),
3078 With: word(sglDQuoted(`\'`)),
3079 },
3080 }, LangBash|LangMirBSDKorn|LangZsh),
3081 ),
3082 fileTest(
3083 []string{`${foo//b1/b2}`},
3084 langFile(&ParamExp{
3085 Param: lit("foo"),
3086 Repl: &Replace{
3087 All: true,
3088 Orig: litWord("b1"),
3089 With: litWord("b2"),
3090 },
3091 }, LangBash|LangMirBSDKorn|LangZsh),
3092 ),
3093 fileTest(
3094 []string{`${foo///}`, `${foo//}`},
3095 langFile(&ParamExp{
3096 Param: lit("foo"),
3097 Repl: &Replace{All: true},
3098 }, LangBash|LangMirBSDKorn|LangZsh),
3099 ),
3100 fileTest(
3101 []string{`${foo/-//}`},
3102 langFile(&ParamExp{
3103 Param: lit("foo"),
3104 Repl: &Replace{Orig: litWord("-"), With: litWord("/")},
3105 }, LangBash|LangMirBSDKorn|LangZsh),
3106 ),
3107 fileTest(
3108 []string{`${foo//#/}`, `${foo//#}`},
3109 langFile(&ParamExp{
3110 Param: lit("foo"),
3111 Repl: &Replace{All: true, Orig: litWord("#")},
3112 }, LangBash|LangMirBSDKorn|LangZsh),
3113 ),
3114 fileTest(
3115 []string{`${foo//[42]/}`},
3116 langFile(&ParamExp{
3117 Param: lit("foo"),
3118 Repl: &Replace{All: true, Orig: litWord("[42]")},
3119 }, LangBash|LangMirBSDKorn|LangZsh),
3120 ),
3121 fileTest(
3122 []string{`${a^b} ${a^^b} ${a,b} ${a,,b}`},
3123 langFile(call(
3124 word(&ParamExp{
3125 Param: lit("a"),
3126 Exp: &Expansion{
3127 Op: UpperFirst,
3128 Word: litWord("b"),
3129 },
3130 }),
3131 word(&ParamExp{
3132 Param: lit("a"),
3133 Exp: &Expansion{
3134 Op: UpperAll,
3135 Word: litWord("b"),
3136 },
3137 }),
3138 word(&ParamExp{
3139 Param: lit("a"),
3140 Exp: &Expansion{
3141 Op: LowerFirst,
3142 Word: litWord("b"),
3143 },
3144 }),
3145 word(&ParamExp{
3146 Param: lit("a"),
3147 Exp: &Expansion{
3148 Op: LowerAll,
3149 Word: litWord("b"),
3150 },
3151 }),
3152 ), LangBash),
3153 ),
3154 fileTest(
3155 []string{`${a@E} ${b@a} ${@@Q} ${!ref@P}`},
3156 langFile(call(
3157 word(&ParamExp{
3158 Param: lit("a"),
3159 Exp: &Expansion{
3160 Op: OtherParamOps,
3161 Word: litWord("E"),
3162 },
3163 }),
3164 word(&ParamExp{
3165 Param: lit("b"),
3166 Exp: &Expansion{
3167 Op: OtherParamOps,
3168 Word: litWord("a"),
3169 },
3170 }),
3171 word(&ParamExp{
3172 Param: lit("@"),
3173 Exp: &Expansion{
3174 Op: OtherParamOps,
3175 Word: litWord("Q"),
3176 },
3177 }),
3178 word(&ParamExp{
3179 Excl: true,
3180 Param: lit("ref"),
3181 Exp: &Expansion{
3182 Op: OtherParamOps,
3183 Word: litWord("P"),
3184 },
3185 }),
3186 ), LangBash),
3187 ),
3188 fileTest(
3189 []string{`${a@K} ${b@k}`},
3190 langFile(call(
3191 word(&ParamExp{
3192 Param: lit("a"),
3193 Exp: &Expansion{
3194 Op: OtherParamOps,
3195 Word: litWord("K"),
3196 },
3197 }),
3198 word(&ParamExp{
3199 Param: lit("b"),
3200 Exp: &Expansion{
3201 Op: OtherParamOps,
3202 Word: litWord("k"),
3203 },
3204 }),
3205 ), LangBash),
3206 ),
3207 fileTest(
3208 []string{`${a@Q} ${b@#}`},
3209 langFile(call(
3210 word(&ParamExp{
3211 Param: lit("a"),
3212 Exp: &Expansion{
3213 Op: OtherParamOps,
3214 Word: litWord("Q"),
3215 },
3216 }),
3217 word(&ParamExp{
3218 Param: lit("b"),
3219 Exp: &Expansion{
3220 Op: OtherParamOps,
3221 Word: litWord("#"),
3222 },
3223 }),
3224 ), LangMirBSDKorn),
3225 ),
3226 fileTest(
3227 []string{`${#foo}`},
3228 langFile(&ParamExp{
3229 Length: true,
3230 Param: lit("foo"),
3231 }),
3232 ),
3233 fileTest(
3234 []string{`${%foo}`},
3235 langFile(&ParamExp{
3236 Width: true,
3237 Param: lit("foo"),
3238 }, LangMirBSDKorn),
3239 ),
3240 fileTest(
3241 []string{`${!foo} ${!bar[@]}`},
3242 langFile(call(
3243 word(&ParamExp{
3244 Excl: true,
3245 Param: lit("foo"),
3246 }),
3247 word(&ParamExp{
3248 Excl: true,
3249 Param: lit("bar"),
3250 Index: litWord("@"),
3251 }),
3252 ), LangBash|LangMirBSDKorn),
3253 langErr2("1:1: `${!foo}` is a bash/mksh feature; tried parsing as LANG", LangPOSIX),
3254 ),
3255 fileTest(
3256 []string{`${!foo*} ${!bar@}`},
3257 langFile(call(
3258 word(&ParamExp{
3259 Excl: true,
3260 Param: lit("foo"),
3261 Names: NamesPrefix,
3262 }),
3263 word(&ParamExp{
3264 Excl: true,
3265 Param: lit("bar"),
3266 Names: NamesPrefixWords,
3267 }),
3268 ), LangBash),
3269 langErr2("1:1: `${!foo}` is a bash/mksh feature; tried parsing as LANG", LangPOSIX),
3270 langErr2("1:1: `${!foo*}` is a bash feature; tried parsing as LANG", LangMirBSDKorn),
3271 ),
3272 fileTest(
3273 []string{`${#?}`},
3274 langFile(call(
3275 word(&ParamExp{Length: true, Param: lit("?")}),
3276 )),
3277 ),
3278 fileTest(
3279 []string{`${#-foo} ${#?bar}`},
3280 langFile(call(
3281 word(&ParamExp{
3282 Param: lit("#"),
3283 Exp: &Expansion{
3284 Op: DefaultUnset,
3285 Word: litWord("foo"),
3286 },
3287 }),
3288 word(&ParamExp{
3289 Param: lit("#"),
3290 Exp: &Expansion{
3291 Op: ErrorUnset,
3292 Word: litWord("bar"),
3293 },
3294 }),
3295 )),
3296 langSkip(LangZsh),
3297 ),
3298 fileTest(
3299 []string{`"${foo}"`},
3300 langFile(dblQuoted(&ParamExp{Param: lit("foo")})),
3301 ),
3302 fileTest(
3303 []string{`"(foo)"`},
3304 langFile(dblQuoted(lit("(foo)"))),
3305 ),
3306 fileTest(
3307 []string{`"${foo}>"`},
3308 langFile(dblQuoted(
3309 &ParamExp{Param: lit("foo")},
3310 lit(">"),
3311 )),
3312 ),
3313 fileTest(
3314 []string{`"$(foo)"`, "\"`foo`\""},
3315 langFile(dblQuoted(cmdSubst(litStmt("foo")))),
3316 ),
3317 fileTest(
3318 []string{
3319 `"$(foo bar)"`,
3320 `"$(foo bar)"`,
3321 "\"`foo bar`\"",
3322 "\"`foo bar`\"",
3323 },
3324 langFile(dblQuoted(cmdSubst(litStmt("foo", "bar")))),
3325 ),
3326 fileTest(
3327 []string{`'${foo}'`},
3328 langFile(sglQuoted("${foo}")),
3329 ),
3330 fileTest(
3331 []string{"$((1))"},
3332 langFile(arithmExp(litWord("1"))),
3333 ),
3334 fileTest(
3335 []string{"$((1 + 3))", "$((1+3))"},
3336 langFile(arithmExp(&BinaryArithm{
3337 Op: Add,
3338 X: litWord("1"),
3339 Y: litWord("3"),
3340 })),
3341 ),
3342 fileTest(
3343 []string{`"$((foo))"`},
3344 langFile(dblQuoted(arithmExp(
3345 litWord("foo"),
3346 ))),
3347 ),
3348 fileTest(
3349 []string{`$((a)) b`},
3350 langFile(call(
3351 word(arithmExp(litWord("a"))),
3352 litWord("b"),
3353 )),
3354 ),
3355 fileTest(
3356 []string{`$((arr[0]++))`},
3357 langFile(arithmExp(&UnaryArithm{
3358 Op: Inc, Post: true,
3359 X: word(&ParamExp{
3360 Short: true,
3361 Param: lit("arr"),
3362 Index: litWord("0"),
3363 }),
3364 }), LangBash|LangMirBSDKorn|LangZsh),
3365 ),
3366 fileTest(
3367 []string{`$((++arr[0]))`},
3368 langFile(arithmExp(&UnaryArithm{
3369 Op: Inc,
3370 X: word(&ParamExp{
3371 Short: true,
3372 Param: lit("arr"),
3373 Index: litWord("0"),
3374 }),
3375 }), LangBash|LangMirBSDKorn|LangZsh),
3376 ),
3377 fileTest(
3378 []string{`$((${a:-1}))`},
3379 langFile(arithmExp(word(&ParamExp{
3380 Param: lit("a"),
3381 Exp: &Expansion{
3382 Op: DefaultUnsetOrNull,
3383 Word: litWord("1"),
3384 },
3385 })), LangBash|LangMirBSDKorn|LangZsh),
3386 ),
3387 fileTest(
3388 []string{"$((5 * 2 - 1))", "$((5*2-1))"},
3389 langFile(arithmExp(&BinaryArithm{
3390 Op: Sub,
3391 X: &BinaryArithm{
3392 Op: Mul,
3393 X: litWord("5"),
3394 Y: litWord("2"),
3395 },
3396 Y: litWord("1"),
3397 })),
3398 ),
3399 fileTest(
3400 []string{"$((i | 13))"},
3401 langFile(arithmExp(&BinaryArithm{
3402 Op: Or,
3403 X: litWord("i"),
3404 Y: litWord("13"),
3405 })),
3406 ),
3407 fileTest(
3408 []string{
3409 "$(((a) + ((b))))",
3410 "$((\n(a) + \n(\n(b)\n)\n))",
3411 },
3412 langFile(arithmExp(&BinaryArithm{
3413 Op: Add,
3414 X: parenArit(litWord("a")),
3415 Y: parenArit(parenArit(litWord("b"))),
3416 })),
3417 ),
3418 fileTest(
3419 []string{
3420 "$((3 % 7))",
3421 "$((3\n% 7))",
3422 "$((3\\\n % 7))",
3423 "$((3\\\r\n % 7))",
3424 },
3425 langFile(arithmExp(&BinaryArithm{
3426 Op: Rem,
3427 X: litWord("3"),
3428 Y: litWord("7"),
3429 })),
3430 ),
3431 fileTest(
3432 []string{`"$((1 / 3))"`},
3433 langFile(dblQuoted(arithmExp(&BinaryArithm{
3434 Op: Quo,
3435 X: litWord("1"),
3436 Y: litWord("3"),
3437 }))),
3438 ),
3439 fileTest(
3440 []string{"$((2 ** 10))"},
3441 langFile(arithmExp(&BinaryArithm{
3442 Op: Pow,
3443 X: litWord("2"),
3444 Y: litWord("10"),
3445 })),
3446 ),
3447 fileTest(
3448 []string{`$(((1) ^ 3))`},
3449 langFile(arithmExp(&BinaryArithm{
3450 Op: Xor,
3451 X: parenArit(litWord("1")),
3452 Y: litWord("3"),
3453 })),
3454 ),
3455 fileTest(
3456 []string{`$((1 >> (3 << 2)))`},
3457 langFile(arithmExp(&BinaryArithm{
3458 Op: Shr,
3459 X: litWord("1"),
3460 Y: parenArit(&BinaryArithm{
3461 Op: Shl,
3462 X: litWord("3"),
3463 Y: litWord("2"),
3464 }),
3465 })),
3466 ),
3467 fileTest(
3468 []string{`$((-(1)))`},
3469 langFile(arithmExp(&UnaryArithm{
3470 Op: Minus,
3471 X: parenArit(litWord("1")),
3472 })),
3473 ),
3474 fileTest(
3475 []string{`$((i++))`},
3476 langFile(arithmExp(&UnaryArithm{
3477 Op: Inc,
3478 Post: true,
3479 X: litWord("i"),
3480 })),
3481 ),
3482 fileTest(
3483 []string{`$((--i))`},
3484 langFile(arithmExp(&UnaryArithm{Op: Dec, X: litWord("i")})),
3485 ),
3486 fileTest(
3487 []string{`$((! i))`, `$((!i))`},
3488 langFile(arithmExp(&UnaryArithm{Op: Not, X: litWord("i")})),
3489 ),
3490 fileTest(
3491 []string{`$((~i))`},
3492 langFile(arithmExp(&UnaryArithm{Op: BitNegation, X: litWord("i")})),
3493 ),
3494 fileTest(
3495 []string{`$((-! +i))`, `$((-!+i))`},
3496 langFile(arithmExp(&UnaryArithm{
3497 Op: Minus,
3498 X: &UnaryArithm{
3499 Op: Not,
3500 X: &UnaryArithm{Op: Plus, X: litWord("i")},
3501 },
3502 })),
3503 ),
3504 fileTest(
3505 []string{`$((! ! i))`, `$((!!i))`},
3506 langFile(arithmExp(&UnaryArithm{
3507 Op: Not,
3508 X: &UnaryArithm{Op: Not, X: litWord("i")},
3509 })),
3510 ),
3511 fileTest(
3512 []string{`$((~~i))`},
3513 langFile(arithmExp(&UnaryArithm{
3514 Op: BitNegation,
3515 X: &UnaryArithm{Op: BitNegation, X: litWord("i")},
3516 })),
3517 ),
3518 fileTest(
3519 []string{`$((1 < 3))`},
3520 langFile(arithmExp(&BinaryArithm{
3521 Op: Lss,
3522 X: litWord("1"),
3523 Y: litWord("3"),
3524 })),
3525 ),
3526 fileTest(
3527 []string{`$((i = 2))`, `$((i=2))`},
3528 langFile(arithmExp(&BinaryArithm{
3529 Op: Assgn,
3530 X: litWord("i"),
3531 Y: litWord("2"),
3532 })),
3533 ),
3534 fileTest(
3535 []string{`((a[i] = 4))`, `((a[i]=4))`},
3536 langFile(arithmCmd(&BinaryArithm{
3537 Op: Assgn,
3538 X: word(&ParamExp{
3539 Short: true,
3540 Param: lit("a"),
3541 Index: litWord("i"),
3542 }),
3543 Y: litWord("4"),
3544 }), LangBash|LangMirBSDKorn|LangZsh),
3545 ),
3546 fileTest(
3547 []string{"$((a += 2, b -= 3))"},
3548 langFile(arithmExp(&BinaryArithm{
3549 Op: Comma,
3550 X: &BinaryArithm{
3551 Op: AddAssgn,
3552 X: litWord("a"),
3553 Y: litWord("2"),
3554 },
3555 Y: &BinaryArithm{
3556 Op: SubAssgn,
3557 X: litWord("b"),
3558 Y: litWord("3"),
3559 },
3560 })),
3561 ),
3562 fileTest(
3563 []string{"$((a >>= 2, b <<= 3))"},
3564 langFile(arithmExp(&BinaryArithm{
3565 Op: Comma,
3566 X: &BinaryArithm{
3567 Op: ShrAssgn,
3568 X: litWord("a"),
3569 Y: litWord("2"),
3570 },
3571 Y: &BinaryArithm{
3572 Op: ShlAssgn,
3573 X: litWord("b"),
3574 Y: litWord("3"),
3575 },
3576 })),
3577 ),
3578 fileTest(
3579 []string{"$((a ^^ 2))"},
3580 langFile(arithmExp(&BinaryArithm{
3581 Op: XorBool,
3582 X: litWord("a"),
3583 Y: litWord("2"),
3584 }), LangZsh),
3585 ),
3586 fileTest(
3587 []string{"$((a ^^= 2, b **= 3))"},
3588 langFile(arithmExp(&BinaryArithm{
3589 Op: Comma,
3590 X: &BinaryArithm{
3591 Op: XorBoolAssgn,
3592 X: litWord("a"),
3593 Y: litWord("2"),
3594 },
3595 Y: &BinaryArithm{
3596 Op: PowAssgn,
3597 X: litWord("b"),
3598 Y: litWord("3"),
3599 },
3600 }), LangZsh),
3601 ),
3602 fileTest(
3603 []string{"$((a &&= 2, b ||= 3))"},
3604 langFile(arithmExp(&BinaryArithm{
3605 Op: Comma,
3606 X: &BinaryArithm{
3607 Op: AndBoolAssgn,
3608 X: litWord("a"),
3609 Y: litWord("2"),
3610 },
3611 Y: &BinaryArithm{
3612 Op: OrBoolAssgn,
3613 X: litWord("b"),
3614 Y: litWord("3"),
3615 },
3616 }), LangZsh),
3617 ),
3618 fileTest(
3619 []string{"$((a == b && c > d))"},
3620 langFile(arithmExp(&BinaryArithm{
3621 Op: AndArit,
3622 X: &BinaryArithm{
3623 Op: Eql,
3624 X: litWord("a"),
3625 Y: litWord("b"),
3626 },
3627 Y: &BinaryArithm{
3628 Op: Gtr,
3629 X: litWord("c"),
3630 Y: litWord("d"),
3631 },
3632 })),
3633 ),
3634 fileTest(
3635 []string{"$((a != b))"},
3636 langFile(arithmExp(&BinaryArithm{
3637 Op: Neq,
3638 X: litWord("a"),
3639 Y: litWord("b"),
3640 })),
3641 ),
3642 fileTest(
3643 []string{"$((a &= b))"},
3644 langFile(arithmExp(&BinaryArithm{
3645 Op: AndAssgn,
3646 X: litWord("a"),
3647 Y: litWord("b"),
3648 })),
3649 ),
3650 fileTest(
3651 []string{"$((a |= b))"},
3652 langFile(arithmExp(&BinaryArithm{
3653 Op: OrAssgn,
3654 X: litWord("a"),
3655 Y: litWord("b"),
3656 })),
3657 ),
3658 fileTest(
3659 []string{"$((a %= b))"},
3660 langFile(arithmExp(&BinaryArithm{
3661 Op: RemAssgn,
3662 X: litWord("a"),
3663 Y: litWord("b"),
3664 })),
3665 ),
3666 fileTest(
3667 []string{"$((a /= b))", "$((a/=b))"},
3668 langFile(arithmExp(&BinaryArithm{
3669 Op: QuoAssgn,
3670 X: litWord("a"),
3671 Y: litWord("b"),
3672 })),
3673 ),
3674 fileTest(
3675 []string{"$((a ^= b))"},
3676 langFile(arithmExp(&BinaryArithm{
3677 Op: XorAssgn,
3678 X: litWord("a"),
3679 Y: litWord("b"),
3680 })),
3681 ),
3682 fileTest(
3683 []string{"$((i *= 3))"},
3684 langFile(arithmExp(&BinaryArithm{
3685 Op: MulAssgn,
3686 X: litWord("i"),
3687 Y: litWord("3"),
3688 })),
3689 ),
3690 fileTest(
3691 []string{"$((2 >= 10))"},
3692 langFile(arithmExp(&BinaryArithm{
3693 Op: Geq,
3694 X: litWord("2"),
3695 Y: litWord("10"),
3696 })),
3697 ),
3698 fileTest(
3699 []string{"$((foo ? b1 : b2))"},
3700 langFile(arithmExp(&BinaryArithm{
3701 Op: TernQuest,
3702 X: litWord("foo"),
3703 Y: &BinaryArithm{
3704 Op: TernColon,
3705 X: litWord("b1"),
3706 Y: litWord("b2"),
3707 },
3708 })),
3709 ),
3710 fileTest(
3711 []string{`$((a <= (1 || 2)))`},
3712 langFile(arithmExp(&BinaryArithm{
3713 Op: Leq,
3714 X: litWord("a"),
3715 Y: parenArit(&BinaryArithm{
3716 Op: OrArit,
3717 X: litWord("1"),
3718 Y: litWord("2"),
3719 }),
3720 })),
3721 ),
3722 fileTest(
3723 []string{"foo$", "foo$\n"},
3724 langFile(word(lit("foo"), lit("$"))),
3725 ),
3726 fileTest(
3727 []string{"foo$", "foo$\\\n", "foo$\\\r\n"},
3728 langFile(word(lit("foo"), lit("$"))),
3729 ),
3730 fileTest(
3731 []string{`$''`},
3732 langFile(sglDQuoted(""), LangBash|LangMirBSDKorn|LangZsh),
3733 langFile(word(lit("$"), sglQuoted("")), LangPOSIX),
3734 ),
3735 fileTest(
3736 []string{`$""`},
3737 langFile(dblDQuoted(), LangBash|LangMirBSDKorn),
3738 langFile(word(lit("$"), dblQuoted()), LangPOSIX|LangZsh),
3739 ),
3740 fileTest(
3741 []string{`$'foo'`},
3742 langFile(sglDQuoted("foo"), LangBash|LangMirBSDKorn|LangZsh),
3743 langFile(word(lit("$"), sglQuoted("foo")), LangPOSIX),
3744 ),
3745 fileTest(
3746 []string{`$'f+oo${'`},
3747 langFile(sglDQuoted("f+oo${"), LangBash|LangMirBSDKorn|LangZsh),
3748 ),
3749 fileTest(
3750 []string{"$'foo bar`'"},
3751 langFile(sglDQuoted("foo bar`"), LangBash|LangMirBSDKorn|LangZsh),
3752 ),
3753 fileTest(
3754 []string{"$'a ${b} c'"},
3755 langFile(sglDQuoted("a ${b} c"), LangBash|LangMirBSDKorn|LangZsh),
3756 ),
3757 fileTest(
3758 []string{`$"a ${b} c"`},
3759 langFile(dblDQuoted(
3760 lit("a "),
3761 &ParamExp{Param: lit("b")},
3762 lit(" c"),
3763 ), LangBash|LangMirBSDKorn),
3764 ),
3765 fileTest(
3766 []string{`"a $b c"`},
3767 langFile(dblQuoted(lit("a "), litParamExp("b"), lit(" c"))),
3768 ),
3769 fileTest(
3770 []string{`$"a $b c"`},
3771 langFile(dblDQuoted(
3772 lit("a "),
3773 litParamExp("b"),
3774 lit(" c"),
3775 ), LangBash|LangMirBSDKorn),
3776 ),
3777 fileTest(
3778 []string{"$'f\\'oo\n'"},
3779 langFile(sglDQuoted("f\\'oo\n"), LangBash|LangMirBSDKorn|LangZsh),
3780 ),
3781 fileTest(
3782 []string{`$"foo"`},
3783 langFile(dblDQuoted(lit("foo")), LangBash|LangMirBSDKorn),
3784 langFile(word(lit("$"), dblQuoted(lit("foo"))), LangPOSIX),
3785 ),
3786 fileTest(
3787 []string{`$"foo$"`},
3788 langFile(dblDQuoted(lit("foo"), lit("$")), LangBash|LangMirBSDKorn),
3789 ),
3790 fileTest(
3791 []string{`$"foo bar"`},
3792 langFile(dblDQuoted(lit("foo bar")), LangBash|LangMirBSDKorn),
3793 ),
3794 fileTest(
3795 []string{`$'f\'oo'`},
3796 langFile(sglDQuoted(`f\'oo`), LangBash|LangMirBSDKorn|LangZsh),
3797 ),
3798 fileTest(
3799 []string{`$"f\"oo"`},
3800 langFile(dblDQuoted(lit(`f\"oo`)), LangBash|LangMirBSDKorn),
3801 ),
3802 fileTest(
3803 []string{`"foo$"`},
3804 langFile(dblQuoted(lit("foo"), lit("$"))),
3805 ),
3806 fileTest(
3807 []string{`"foo$$"`},
3808 langFile(dblQuoted(lit("foo"), litParamExp("$"))),
3809 ),
3810 fileTest(
3811 []string{`"a $\"b\" c"`},
3812 langFile(dblQuoted(lit(`a `), lit(`$`), lit(`\"b\" c`))),
3813 ),
3814 fileTest(
3815 []string{"$(foo$)", "`foo$`"},
3816 langFile(cmdSubst(
3817 stmt(call(word(lit("foo"), lit("$")))),
3818 )),
3819 ),
3820 fileTest(
3821 []string{"foo$bar"},
3822 langFile(word(lit("foo"), litParamExp("bar"))),
3823 ),
3824 fileTest(
3825 []string{"foo$(bar)"},
3826 langFile(word(lit("foo"), cmdSubst(litStmt("bar")))),
3827 ),
3828 fileTest(
3829 []string{"foo${bar}"},
3830 langFile(word(lit("foo"), &ParamExp{Param: lit("bar")})),
3831 ),
3832 fileTest(
3833 []string{"'foo${bar'"},
3834 langFile(sglQuoted("foo${bar")),
3835 ),
3836 fileTest(
3837 []string{"(foo)\nbar", "(foo); bar"},
3838 langFile([]Command{
3839 subshell(litStmt("foo")),
3840 litCall("bar"),
3841 }),
3842 ),
3843 fileTest(
3844 []string{"foo\n(bar)", "foo; (bar)"},
3845 langFile([]Command{
3846 litCall("foo"),
3847 subshell(litStmt("bar")),
3848 }),
3849 ),
3850 fileTest(
3851 []string{"foo\n(bar)", "foo; (bar)"},
3852 langFile([]Command{
3853 litCall("foo"),
3854 subshell(litStmt("bar")),
3855 }),
3856 ),
3857 fileTest(
3858 []string{
3859 "case $i in 1) foo ;; 2 | 3*) bar ;; esac",
3860 "case $i in 1) foo;; 2 | 3*) bar; esac",
3861 "case $i in (1) foo;; 2 | 3*) bar;; esac",
3862 "case $i\nin\n#etc\n1)\nfoo\n;;\n2 | 3*)\nbar\n;;\nesac",
3863 },
3864 langFile(&CaseClause{
3865 Word: word(litParamExp("i")),
3866 Items: []*CaseItem{
3867 {
3868 Op: Break,
3869 Patterns: litWords("1"),
3870 Stmts: litStmts("foo"),
3871 },
3872 {
3873 Op: Break,
3874 Patterns: litWords("2", "3*"),
3875 Stmts: litStmts("bar"),
3876 },
3877 },
3878 }),
3879 ),
3880 fileTest(
3881 []string{"case i in 1) a ;& 2) ;; esac"},
3882 langFile(&CaseClause{
3883 Word: litWord("i"),
3884 Items: []*CaseItem{
3885 {
3886 Op: Fallthrough,
3887 Patterns: litWords("1"),
3888 Stmts: litStmts("a"),
3889 },
3890 {Op: Break, Patterns: litWords("2")},
3891 },
3892 }, LangBash|LangMirBSDKorn|LangZsh),
3893 ),
3894 fileTest(
3895 []string{
3896 "case i in 1) a ;; esac",
3897 "case i { 1) a ;; }",
3898 "case i {\n1) a ;;\n}",
3899 },
3900 langFile(&CaseClause{
3901 Word: litWord("i"),
3902 Items: []*CaseItem{{
3903 Op: Break,
3904 Patterns: litWords("1"),
3905 Stmts: litStmts("a"),
3906 }},
3907 }, LangMirBSDKorn),
3908 ),
3909 fileTest(
3910 []string{"case i in 1) a ;;& 2) b ;; esac"},
3911 langFile(&CaseClause{
3912 Word: litWord("i"),
3913 Items: []*CaseItem{
3914 {
3915 Op: Resume,
3916 Patterns: litWords("1"),
3917 Stmts: litStmts("a"),
3918 },
3919 {
3920 Op: Break,
3921 Patterns: litWords("2"),
3922 Stmts: litStmts("b"),
3923 },
3924 },
3925 }, LangBash),
3926 ),
3927 fileTest(
3928 []string{"case i in 1) a ;| 2) b ;; esac"},
3929 langFile(&CaseClause{
3930 Word: litWord("i"),
3931 Items: []*CaseItem{
3932 {
3933 Op: ResumeKorn,
3934 Patterns: litWords("1"),
3935 Stmts: litStmts("a"),
3936 },
3937 {
3938 Op: Break,
3939 Patterns: litWords("2"),
3940 Stmts: litStmts("b"),
3941 },
3942 },
3943 }, LangMirBSDKorn|LangZsh),
3944 ),
3945 fileTest(
3946 []string{"case $i in 1) cat <<EOF ;;\nfoo\nEOF\nesac"},
3947 langFile(&CaseClause{
3948 Word: word(litParamExp("i")),
3949 Items: []*CaseItem{{
3950 Op: Break,
3951 Patterns: litWords("1"),
3952 Stmts: []*Stmt{{
3953 Cmd: litCall("cat"),
3954 Redirs: []*Redirect{{
3955 Op: Hdoc,
3956 Word: litWord("EOF"),
3957 Hdoc: litWord("foo\n"),
3958 }},
3959 }},
3960 }},
3961 }),
3962 ),
3963 fileTest(
3964 []string{"foo | while read a; do b; done"},
3965 langFile(&BinaryCmd{
3966 Op: Pipe,
3967 X: litStmt("foo"),
3968 Y: stmt(&WhileClause{
3969 Cond: []*Stmt{litStmt("read", "a")},
3970
3971 Do: litStmts("b"),
3972 }),
3973 }),
3974 ),
3975 fileTest(
3976 []string{"while read l; do foo || bar; done"},
3977 langFile(&WhileClause{
3978 Cond: []*Stmt{litStmt("read", "l")},
3979 Do: stmts(&BinaryCmd{
3980 Op: OrStmt,
3981 X: litStmt("foo"),
3982 Y: litStmt("bar"),
3983 }),
3984 }),
3985 ),
3986 fileTest(
3987 []string{"echo if while"},
3988 langFile(litCall("echo", "if", "while")),
3989 ),
3990 fileTest(
3991 []string{"${foo}if"},
3992 langFile(word(&ParamExp{Param: lit("foo")}, lit("if"))),
3993 ),
3994 fileTest(
3995 []string{"$if'|'"},
3996 langFile(word(litParamExp("if"), sglQuoted("|"))),
3997 ),
3998 fileTest(
3999 []string{"if a; then b=; fi", "if a; then b=\nfi"},
4000 langFile(&IfClause{
4001 Cond: litStmts("a"),
4002 Then: stmts(&CallExpr{Assigns: litAssigns("b=")}),
4003 }),
4004 ),
4005 fileTest(
4006 []string{"if a; then >f; fi", "if a; then >f\nfi"},
4007 langFile(&IfClause{
4008 Cond: litStmts("a"),
4009 Then: []*Stmt{{
4010 Redirs: []*Redirect{
4011 {Op: RdrOut, Word: litWord("f")},
4012 },
4013 }},
4014 }),
4015 ),
4016 fileTest(
4017 []string{"if a; then (a); fi", "if a; then (a) fi"},
4018 langFile(&IfClause{
4019 Cond: litStmts("a"),
4020 Then: stmts(subshell(litStmt("a"))),
4021 }),
4022 ),
4023 fileTest(
4024 []string{"a=b\nc=d", "a=b; c=d"},
4025 langFile([]Command{
4026 &CallExpr{Assigns: litAssigns("a=b")},
4027 &CallExpr{Assigns: litAssigns("c=d")},
4028 }),
4029 ),
4030 fileTest(
4031 []string{"foo && write | read"},
4032 langFile(&BinaryCmd{
4033 Op: AndStmt,
4034 X: litStmt("foo"),
4035 Y: stmt(&BinaryCmd{
4036 Op: Pipe,
4037 X: litStmt("write"),
4038 Y: litStmt("read"),
4039 }),
4040 }),
4041 ),
4042 fileTest(
4043 []string{"write | read && bar"},
4044 langFile(&BinaryCmd{
4045 Op: AndStmt,
4046 X: stmt(&BinaryCmd{
4047 Op: Pipe,
4048 X: litStmt("write"),
4049 Y: litStmt("read"),
4050 }),
4051 Y: litStmt("bar"),
4052 }),
4053 ),
4054 fileTest(
4055 []string{"foo >f | bar"},
4056 langFile(&BinaryCmd{
4057 Op: Pipe,
4058 X: &Stmt{
4059 Cmd: litCall("foo"),
4060 Redirs: []*Redirect{
4061 {Op: RdrOut, Word: litWord("f")},
4062 },
4063 },
4064 Y: litStmt("bar"),
4065 }),
4066 ),
4067 fileTest(
4068 []string{"(foo) >f | bar"},
4069 langFile(&BinaryCmd{
4070 Op: Pipe,
4071 X: &Stmt{
4072 Cmd: subshell(litStmt("foo")),
4073 Redirs: []*Redirect{
4074 {Op: RdrOut, Word: litWord("f")},
4075 },
4076 },
4077 Y: litStmt("bar"),
4078 }),
4079 ),
4080 fileTest(
4081 []string{"foo | >f"},
4082 langFile(&BinaryCmd{
4083 Op: Pipe,
4084 X: litStmt("foo"),
4085 Y: &Stmt{Redirs: []*Redirect{
4086 {Op: RdrOut, Word: litWord("f")},
4087 }},
4088 }),
4089 ),
4090 fileTest(
4091 []string{"[[ a ]]"},
4092 langFile(&TestClause{X: litWord("a")}, LangBash|LangMirBSDKorn|LangZsh),
4093 langFile(litStmt("[[", "a", "]]"), LangPOSIX),
4094 ),
4095 fileTest(
4096 []string{"[[ a ]]\nb"},
4097 langFile(stmts(
4098 &TestClause{X: litWord("a")},
4099 litCall("b"),
4100 ), LangBash|LangMirBSDKorn|LangZsh),
4101 ),
4102 fileTest(
4103 []string{"[[ a > b ]]"},
4104 langFile(&TestClause{X: &BinaryTest{
4105 Op: TsAfter,
4106 X: litWord("a"),
4107 Y: litWord("b"),
4108 }}, LangBash|LangMirBSDKorn|LangZsh),
4109 ),
4110 fileTest(
4111 []string{"[[ 1 -nt 2 ]]"},
4112 langFile(&TestClause{X: &BinaryTest{
4113 Op: TsNewer,
4114 X: litWord("1"),
4115 Y: litWord("2"),
4116 }}, LangBash|LangMirBSDKorn|LangZsh),
4117 ),
4118 fileTest(
4119 []string{"[[ 1 -eq 2 ]]"},
4120 langFile(&TestClause{X: &BinaryTest{
4121 Op: TsEql,
4122 X: litWord("1"),
4123 Y: litWord("2"),
4124 }}, LangBash|LangMirBSDKorn|LangZsh),
4125 ),
4126 fileTest(
4127 []string{"[[ -1 -eq -1 ]]"},
4128 langFile(&TestClause{X: &BinaryTest{
4129 Op: TsEql,
4130 // TODO: parse as unary expressions
4131 X: litWord("-1"),
4132 Y: litWord("-1"),
4133 }}, LangBash|LangMirBSDKorn|LangZsh),
4134 ),
4135 fileTest(
4136 []string{"[[ +3 -eq 1+2 ]]"},
4137 langFile(&TestClause{X: &BinaryTest{
4138 Op: TsEql,
4139 // TODO: parse as unary and binary expressions
4140 X: litWord("+3"),
4141 Y: litWord("1+2"),
4142 }}, LangBash|LangMirBSDKorn|LangZsh),
4143 ),
4144 fileTest(
4145 []string{
4146 "[[ -R a ]]",
4147 "[[\n-R a\n]]",
4148 },
4149 langFile(&TestClause{X: &UnaryTest{
4150 Op: TsRefVar,
4151 X: litWord("a"),
4152 }}, LangBash),
4153 ),
4154 fileTest(
4155 // '#' inside [[ ]] is part of a word, not a comment.
4156 []string{"[[ -n $foo#bar ]]"},
4157 langFile(&TestClause{X: &UnaryTest{
4158 Op: TsNempStr,
4159 X: word(litParamExp("foo"), lit("#bar")),
4160 }}, LangBash|LangMirBSDKorn|LangZsh),
4161 ),
4162 fileTest(
4163 []string{"[[ a =~ b ]]", "[[ a =~ b ]];"},
4164 langFile(&TestClause{X: &BinaryTest{
4165 Op: TsReMatch,
4166 X: litWord("a"),
4167 Y: litWord("b"),
4168 }}, LangBash),
4169 ),
4170 fileTest(
4171 []string{`[[ a =~ " foo "$bar ]]`},
4172 langFile(&TestClause{X: &BinaryTest{
4173 Op: TsReMatch,
4174 X: litWord("a"),
4175 Y: word(
4176 dblQuoted(lit(" foo ")),
4177 litParamExp("bar"),
4178 ),
4179 }}, LangBash),
4180 ),
4181 fileTest(
4182 []string{`[[ a =~ foo"bar" ]]`},
4183 langFile(&TestClause{X: &BinaryTest{
4184 Op: TsReMatch,
4185 X: litWord("a"),
4186 Y: word(
4187 lit("foo"),
4188 dblQuoted(lit("bar")),
4189 ),
4190 }}, LangBash),
4191 ),
4192 fileTest(
4193 []string{`[[ a =~ [ab](c |d) ]]`},
4194 langFile(&TestClause{X: &BinaryTest{
4195 Op: TsReMatch,
4196 X: litWord("a"),
4197 Y: litWord("[ab](c |d)"),
4198 }}, LangBash),
4199 ),
4200 fileTest(
4201 []string{`[[ a =~ ( ]]<>;&) ]]`},
4202 langFile(&TestClause{X: &BinaryTest{
4203 Op: TsReMatch,
4204 X: litWord("a"),
4205 Y: litWord("( ]]<>;&)"),
4206 }}, LangBash),
4207 ),
4208 fileTest(
4209 []string{`[[ a =~ ($foo) ]]`},
4210 langFile(&TestClause{X: &BinaryTest{
4211 Op: TsReMatch,
4212 X: litWord("a"),
4213 Y: word(lit("("), litParamExp("foo"), lit(")")),
4214 }}, LangBash),
4215 ),
4216 fileTest(
4217 []string{`[[ a =~ b\ c|d ]]`},
4218 langFile(&TestClause{X: &BinaryTest{
4219 Op: TsReMatch,
4220 X: litWord("a"),
4221 Y: litWord(`b\ c|d`),
4222 }}, LangBash),
4223 ),
4224 fileTest(
4225 []string{`[[ a == -n ]]`},
4226 langFile(&TestClause{X: &BinaryTest{
4227 Op: TsMatch,
4228 X: litWord("a"),
4229 Y: litWord("-n"),
4230 }}, LangBash|LangMirBSDKorn|LangZsh),
4231 ),
4232 fileTest(
4233 []string{`[[ a == (b|c)* ]]`},
4234 langFile(&TestClause{X: &BinaryTest{
4235 Op: TsMatch,
4236 X: litWord("a"),
4237 Y: word(lit("(b|c)"), lit("*")),
4238 }}, LangZsh),
4239 ),
4240 fileTest(
4241 []string{`[[ a == (#i)bar ]]`},
4242 langFile(&TestClause{X: &BinaryTest{
4243 Op: TsMatch,
4244 X: litWord("a"),
4245 Y: word(lit("(#i)"), lit("bar")),
4246 }}, LangZsh),
4247 ),
4248 fileTest(
4249 []string{`echo (foo|bar).baz`},
4250 langFile(call(litWord("echo"), word(lit("(foo|bar)"), lit(".baz"))), LangZsh),
4251 ),
4252 fileTest(
4253 []string{`ls (#i)*.mod`},
4254 langFile(call(litWord("ls"), word(lit("(#i)"), lit("*.mod"))), LangZsh),
4255 ),
4256 fileTest(
4257 []string{`[[ a =~ -n ]]`},
4258 langFile(&TestClause{X: &BinaryTest{
4259 Op: TsReMatch,
4260 X: litWord("a"),
4261 Y: litWord("-n"),
4262 }}, LangBash),
4263 ),
4264 fileTest(
4265 []string{"[[ a =~ b$ || c =~ d$ ]]"},
4266 langFile(&TestClause{X: &BinaryTest{
4267 Op: OrTest,
4268 X: &BinaryTest{
4269 Op: TsReMatch,
4270 X: litWord("a"),
4271 Y: word(lit("b"), lit("$")),
4272 },
4273 Y: &BinaryTest{
4274 Op: TsReMatch,
4275 X: litWord("c"),
4276 Y: word(lit("d"), lit("$")),
4277 },
4278 }}, LangBash),
4279 ),
4280 fileTest(
4281 []string{"[[ -n $a ]]"},
4282 langFile(&TestClause{
4283 X: &UnaryTest{Op: TsNempStr, X: word(litParamExp("a"))},
4284 }, LangBash|LangMirBSDKorn|LangZsh),
4285 ),
4286 fileTest(
4287 []string{"[[ ! $a < 'b' ]]"},
4288 langFile(&TestClause{X: &UnaryTest{
4289 Op: TsNot,
4290 X: &BinaryTest{
4291 Op: TsBefore,
4292 X: word(litParamExp("a")),
4293 Y: word(sglQuoted("b")),
4294 },
4295 }}, LangBash|LangMirBSDKorn|LangZsh),
4296 ),
4297 fileTest(
4298 []string{
4299 "[[ ! -e $a ]]",
4300 "[[ ! -a $a ]]",
4301 "[[\n!\n-a $a\n]]",
4302 },
4303 langFile(&TestClause{X: &UnaryTest{
4304 Op: TsNot,
4305 X: &UnaryTest{Op: TsExists, X: word(litParamExp("a"))},
4306 }}, LangBash|LangMirBSDKorn|LangZsh),
4307 ),
4308 fileTest(
4309 []string{
4310 "[[ a && b ]]",
4311 "[[\na &&\nb ]]",
4312 "[[\n\na &&\n\nb ]]",
4313 },
4314 langFile(&TestClause{X: &BinaryTest{
4315 Op: AndTest,
4316 X: litWord("a"),
4317 Y: litWord("b"),
4318 }}, LangBash|LangMirBSDKorn|LangZsh),
4319 ),
4320 fileTest(
4321 []string{"[[ (a && b) ]]"},
4322 langFile(&TestClause{X: parenTest(&BinaryTest{
4323 Op: AndTest,
4324 X: litWord("a"),
4325 Y: litWord("b"),
4326 })}, LangBash|LangMirBSDKorn|LangZsh),
4327 ),
4328 fileTest(
4329 []string{
4330 "[[ a && (b) ]]",
4331 "[[ a &&\n(\nb) ]]",
4332 },
4333 langFile(&TestClause{X: &BinaryTest{
4334 Op: AndTest,
4335 X: litWord("a"),
4336 Y: parenTest(litWord("b")),
4337 }}, LangBash|LangMirBSDKorn|LangZsh),
4338 ),
4339 fileTest(
4340 []string{"[[ (a && b) || -f c ]]"},
4341 langFile(&TestClause{X: &BinaryTest{
4342 Op: OrTest,
4343 X: parenTest(&BinaryTest{
4344 Op: AndTest,
4345 X: litWord("a"),
4346 Y: litWord("b"),
4347 }),
4348 Y: &UnaryTest{Op: TsRegFile, X: litWord("c")},
4349 }}, LangBash|LangMirBSDKorn|LangZsh),
4350 ),
4351 fileTest(
4352 []string{
4353 "[[ -S a && -L b ]]",
4354 "[[ -S a && -h b ]]",
4355 },
4356 langFile(&TestClause{X: &BinaryTest{
4357 Op: AndTest,
4358 X: &UnaryTest{Op: TsSocket, X: litWord("a")},
4359 Y: &UnaryTest{Op: TsSmbLink, X: litWord("b")},
4360 }}, LangBash|LangMirBSDKorn|LangZsh),
4361 ),
4362 fileTest(
4363 []string{"[[ -k a && -N b ]]"},
4364 langFile(&TestClause{X: &BinaryTest{
4365 Op: AndTest,
4366 X: &UnaryTest{Op: TsSticky, X: litWord("a")},
4367 Y: &UnaryTest{Op: TsModif, X: litWord("b")},
4368 }}, LangBash),
4369 ),
4370 fileTest(
4371 []string{"[[ -G a && -O b ]]"},
4372 langFile(&TestClause{X: &BinaryTest{
4373 Op: AndTest,
4374 X: &UnaryTest{Op: TsGrpOwn, X: litWord("a")},
4375 Y: &UnaryTest{Op: TsUsrOwn, X: litWord("b")},
4376 }}, LangBash|LangMirBSDKorn|LangZsh),
4377 ),
4378 fileTest(
4379 []string{"[[ -d a && -c b ]]"},
4380 langFile(&TestClause{X: &BinaryTest{
4381 Op: AndTest,
4382 X: &UnaryTest{Op: TsDirect, X: litWord("a")},
4383 Y: &UnaryTest{Op: TsCharSp, X: litWord("b")},
4384 }}, LangBash|LangMirBSDKorn|LangZsh),
4385 ),
4386 fileTest(
4387 []string{"[[ -b a && -p b ]]"},
4388 langFile(&TestClause{X: &BinaryTest{
4389 Op: AndTest,
4390 X: &UnaryTest{Op: TsBlckSp, X: litWord("a")},
4391 Y: &UnaryTest{Op: TsNmPipe, X: litWord("b")},
4392 }}, LangBash|LangMirBSDKorn|LangZsh),
4393 ),
4394 fileTest(
4395 []string{"[[ -g a && -u b ]]"},
4396 langFile(&TestClause{X: &BinaryTest{
4397 Op: AndTest,
4398 X: &UnaryTest{Op: TsGIDSet, X: litWord("a")},
4399 Y: &UnaryTest{Op: TsUIDSet, X: litWord("b")},
4400 }}, LangBash|LangMirBSDKorn|LangZsh),
4401 ),
4402 fileTest(
4403 []string{"[[ -r a && -w b ]]"},
4404 langFile(&TestClause{X: &BinaryTest{
4405 Op: AndTest,
4406 X: &UnaryTest{Op: TsRead, X: litWord("a")},
4407 Y: &UnaryTest{Op: TsWrite, X: litWord("b")},
4408 }}, LangBash|LangMirBSDKorn|LangZsh),
4409 ),
4410 fileTest(
4411 []string{"[[ -x a && -s b ]]"},
4412 langFile(&TestClause{X: &BinaryTest{
4413 Op: AndTest,
4414 X: &UnaryTest{Op: TsExec, X: litWord("a")},
4415 Y: &UnaryTest{Op: TsNoEmpty, X: litWord("b")},
4416 }}, LangBash|LangMirBSDKorn|LangZsh),
4417 ),
4418 fileTest(
4419 []string{"[[ -t a && -z b ]]"},
4420 langFile(&TestClause{X: &BinaryTest{
4421 Op: AndTest,
4422 X: &UnaryTest{Op: TsFdTerm, X: litWord("a")},
4423 Y: &UnaryTest{Op: TsEmpStr, X: litWord("b")},
4424 }}, LangBash|LangMirBSDKorn|LangZsh),
4425 ),
4426 fileTest(
4427 []string{"[[ -o a && -v b ]]"},
4428 langFile(&TestClause{X: &BinaryTest{
4429 Op: AndTest,
4430 X: &UnaryTest{Op: TsOptSet, X: litWord("a")},
4431 Y: &UnaryTest{Op: TsVarSet, X: litWord("b")},
4432 }}, LangBash|LangMirBSDKorn|LangZsh),
4433 ),
4434 fileTest(
4435 []string{"[[ a -ot b && c -ef d ]]"},
4436 langFile(&TestClause{X: &BinaryTest{
4437 Op: AndTest,
4438 X: &BinaryTest{
4439 Op: TsOlder,
4440 X: litWord("a"),
4441 Y: litWord("b"),
4442 },
4443 Y: &BinaryTest{
4444 Op: TsDevIno,
4445 X: litWord("c"),
4446 Y: litWord("d"),
4447 },
4448 }}, LangBash|LangMirBSDKorn|LangZsh),
4449 ),
4450 fileTest(
4451 []string{"[[ a = b && c != d ]]"},
4452 langFile(&TestClause{X: &BinaryTest{
4453 Op: AndTest,
4454 X: &BinaryTest{
4455 Op: TsMatchShort,
4456 X: litWord("a"),
4457 Y: litWord("b"),
4458 },
4459 Y: &BinaryTest{
4460 Op: TsNoMatch,
4461 X: litWord("c"),
4462 Y: litWord("d"),
4463 },
4464 }}, LangBash|LangMirBSDKorn|LangZsh),
4465 ),
4466 fileTest(
4467 []string{"[[ a -ne b && c -le d ]]"},
4468 langFile(&TestClause{X: &BinaryTest{
4469 Op: AndTest,
4470 X: &BinaryTest{
4471 Op: TsNeq,
4472 X: litWord("a"),
4473 Y: litWord("b"),
4474 },
4475 Y: &BinaryTest{
4476 Op: TsLeq,
4477 X: litWord("c"),
4478 Y: litWord("d"),
4479 },
4480 }}, LangBash|LangMirBSDKorn|LangZsh),
4481 ),
4482 fileTest(
4483 []string{"[[ c -ge d ]]"},
4484 langFile(&TestClause{X: &BinaryTest{
4485 Op: TsGeq,
4486 X: litWord("c"),
4487 Y: litWord("d"),
4488 }}, LangBash|LangMirBSDKorn|LangZsh),
4489 ),
4490 fileTest(
4491 []string{"[[ a -lt b && c -gt d ]]"},
4492 langFile(&TestClause{X: &BinaryTest{
4493 Op: AndTest,
4494 X: &BinaryTest{
4495 Op: TsLss,
4496 X: litWord("a"),
4497 Y: litWord("b"),
4498 },
4499 Y: &BinaryTest{
4500 Op: TsGtr,
4501 X: litWord("c"),
4502 Y: litWord("d"),
4503 },
4504 }}, LangBash|LangMirBSDKorn|LangZsh),
4505 ),
4506 fileTest(
4507 []string{"[[ 1.2 -gt 0.3 ]]"},
4508 langFile(&TestClause{X: &BinaryTest{
4509 Op: TsGtr,
4510 X: litWord("1.2"),
4511 Y: litWord("0.3"),
4512 }}, LangZsh),
4513 // TODO: reject floating point here, just like with arithmetic expressions
4514 // langErr2(``, LangBash|LangMirBSDKorn),
4515 ),
4516 fileTest(
4517 []string{"declare -f func"},
4518 langFile(litStmt("declare", "-f", "func")),
4519 langFile(&DeclClause{
4520 Variant: lit("declare"),
4521 Args: []*Assign{
4522 {Naked: true, Value: litWord("-f")},
4523 {Naked: true, Name: lit("func")},
4524 },
4525 }, langBashLike|LangZsh),
4526 ),
4527 fileTest(
4528 []string{"(local bar)"},
4529 langFile(subshell(stmt(&DeclClause{
4530 Variant: lit("local"),
4531 Args: litAssigns("bar"),
4532 })), LangBash|LangMirBSDKorn|LangZsh),
4533 langFile(subshell(litStmt("local", "bar")), LangPOSIX),
4534 ),
4535 fileTest(
4536 []string{"local {a,b}_c=1"},
4537 langFile(&DeclClause{
4538 Variant: lit("local"),
4539 Args: []*Assign{{Naked: true, Value: litWord("{a,b}_c=1")}},
4540 }, LangBash|LangMirBSDKorn|LangZsh),
4541 langFile(litStmt("local", "{a,b}_c=1"), LangPOSIX),
4542 ),
4543 fileTest(
4544 []string{"typeset"},
4545 langFile(&DeclClause{Variant: lit("typeset")}, LangBash|LangMirBSDKorn|LangZsh),
4546 langFile(litStmt("typeset"), LangPOSIX),
4547 ),
4548 fileTest(
4549 []string{"export bar"},
4550 langFile(&DeclClause{
4551 Variant: lit("export"),
4552 Args: litAssigns("bar"),
4553 }, LangBash|LangMirBSDKorn|LangZsh),
4554 langFile(litStmt("export", "bar"), LangPOSIX),
4555 ),
4556 fileTest(
4557 []string{"readonly -n"},
4558 langFile(&DeclClause{
4559 Variant: lit("readonly"),
4560 Args: []*Assign{{Naked: true, Value: litWord("-n")}},
4561 }, LangBash|LangMirBSDKorn|LangZsh),
4562 langFile(litStmt("readonly", "-n"), LangPOSIX),
4563 ),
4564 fileTest(
4565 []string{"nameref bar="},
4566 langFile(&DeclClause{
4567 Variant: lit("nameref"),
4568 Args: []*Assign{{
4569 Name: lit("bar"),
4570 }},
4571 }, LangBash|LangMirBSDKorn|LangZsh),
4572 langFile(litStmt("nameref", "bar="), LangPOSIX),
4573 ),
4574 fileTest(
4575 []string{"declare -a +n -b$o foo=bar"},
4576 langFile(&DeclClause{
4577 Variant: lit("declare"),
4578 Args: []*Assign{
4579 {Naked: true, Value: litWord("-a")},
4580 {Naked: true, Value: litWord("+n")},
4581 {Naked: true, Value: word(lit("-b"), litParamExp("o"))},
4582 {Name: lit("foo"), Value: litWord("bar")},
4583 },
4584 }, LangBash),
4585 ),
4586 fileTest(
4587 []string{
4588 "declare -a foo=(b1 $(b2))",
4589 "declare -a foo=(b1 `b2`)",
4590 },
4591 langFile(&DeclClause{
4592 Variant: lit("declare"),
4593 Args: []*Assign{
4594 {Naked: true, Value: litWord("-a")},
4595 {
4596 Name: lit("foo"),
4597 Array: arrValues(
4598 litWord("b1"),
4599 word(cmdSubst(litStmt("b2"))),
4600 ),
4601 },
4602 },
4603 }, LangBash),
4604 langErr2("1:16: the `declare` builtin is a bash feature; tried parsing as LANG", LangPOSIX),
4605 ),
4606 fileTest(
4607 []string{"local -a foo=(b1)"},
4608 langFile(&DeclClause{
4609 Variant: lit("local"),
4610 Args: []*Assign{
4611 {Naked: true, Value: litWord("-a")},
4612 {
4613 Name: lit("foo"),
4614 Array: arrValues(litWord("b1")),
4615 },
4616 },
4617 }, LangBash),
4618 ),
4619 fileTest(
4620 []string{"declare -A foo=([a]=b)"},
4621 langFile(&DeclClause{
4622 Variant: lit("declare"),
4623 Args: []*Assign{
4624 {Naked: true, Value: litWord("-A")},
4625 {
4626 Name: lit("foo"),
4627 Array: &ArrayExpr{Elems: []*ArrayElem{{
4628 Index: litWord("a"),
4629 Value: litWord("b"),
4630 }}},
4631 },
4632 },
4633 }, LangBash),
4634 ),
4635 fileTest(
4636 []string{"declare foo[a]="},
4637 langFile(&DeclClause{
4638 Variant: lit("declare"),
4639 Args: []*Assign{{
4640 Name: lit("foo"),
4641 Index: litWord("a"),
4642 }},
4643 }, LangBash),
4644 ),
4645 fileTest(
4646 []string{"declare foo[*]"},
4647 langFile(&DeclClause{
4648 Variant: lit("declare"),
4649 Args: []*Assign{{
4650 Name: lit("foo"),
4651 Index: litWord("*"),
4652 Naked: true,
4653 }},
4654 }, LangBash),
4655 ),
4656 fileTest(
4657 []string{`declare foo["x y"]`},
4658 langFile(&DeclClause{
4659 Variant: lit("declare"),
4660 Args: []*Assign{{
4661 Name: lit("foo"),
4662 Index: word(dblQuoted(lit("x y"))),
4663 Naked: true,
4664 }},
4665 }, LangBash),
4666 ),
4667 fileTest(
4668 []string{`declare foo['x y']`},
4669 langFile(&DeclClause{
4670 Variant: lit("declare"),
4671 Args: []*Assign{{
4672 Name: lit("foo"),
4673 Index: word(sglQuoted("x y")),
4674 Naked: true,
4675 }},
4676 }, LangBash),
4677 ),
4678 fileTest(
4679 []string{"foo=([)"},
4680 langFile(&CallExpr{Assigns: []*Assign{{
4681 Name: lit("foo"),
4682 Array: arrValues(litWord("[")),
4683 }}}, LangMirBSDKorn),
4684 ),
4685 fileTest(
4686 []string{
4687 "a && b=(c)\nd",
4688 "a && b=(c); d",
4689 },
4690 langFile(stmts(
4691 &BinaryCmd{
4692 Op: AndStmt,
4693 X: litStmt("a"),
4694 Y: stmt(&CallExpr{Assigns: []*Assign{{
4695 Name: lit("b"),
4696 Array: arrValues(litWord("c")),
4697 }}}),
4698 },
4699 litCall("d"),
4700 ), LangBash|LangMirBSDKorn|LangZsh),
4701 ),
4702 fileTest(
4703 []string{"declare -f $func >/dev/null"},
4704 langFile(&Stmt{
4705 Cmd: &DeclClause{
4706 Variant: lit("declare"),
4707 Args: []*Assign{
4708 {Naked: true, Value: litWord("-f")},
4709 {
4710 Naked: true,
4711 Value: word(litParamExp("func")),
4712 },
4713 },
4714 },
4715 Redirs: []*Redirect{
4716 {Op: RdrOut, Word: litWord("/dev/null")},
4717 },
4718 }, LangBash|LangZsh),
4719 ),
4720 fileTest(
4721 []string{"declare a\n{ x; }"},
4722 langFile(stmts(
4723 &DeclClause{
4724 Variant: lit("declare"),
4725 Args: litAssigns("a"),
4726 },
4727 block(litStmt("x")),
4728 ), LangBash),
4729 ),
4730 fileTest(
4731 []string{"eval a=b foo"},
4732 langFile(litStmt("eval", "a=b", "foo")),
4733 ),
4734 fileTest(
4735 []string{"time", "time\n"},
4736 langFile(litStmt("time"), LangPOSIX),
4737 langFile(&TimeClause{}, LangBash|LangMirBSDKorn|LangZsh),
4738 ),
4739 fileTest(
4740 []string{"time -p"},
4741 langFile(litStmt("time", "-p"), LangPOSIX),
4742 langFile(&TimeClause{PosixFormat: true}, LangBash|LangMirBSDKorn|LangZsh),
4743 ),
4744 fileTest(
4745 []string{"time -a"},
4746 langFile(litStmt("time", "-a"), LangPOSIX),
4747 langFile(&TimeClause{Stmt: litStmt("-a")}, LangBash|LangMirBSDKorn|LangZsh),
4748 ),
4749 fileTest(
4750 []string{"time --"},
4751 langFile(litStmt("time", "--"), LangPOSIX),
4752 langFile(&TimeClause{Stmt: litStmt("--")}, LangBash|LangMirBSDKorn|LangZsh),
4753 ),
4754 fileTest(
4755 []string{"time foo"},
4756 langFile(&TimeClause{Stmt: litStmt("foo")}, LangBash|LangMirBSDKorn|LangZsh),
4757 ),
4758 fileTest(
4759 []string{"time { foo; }"},
4760 langFile(&TimeClause{Stmt: stmt(block(litStmt("foo")))}, LangBash|LangMirBSDKorn|LangZsh),
4761 ),
4762 fileTest(
4763 []string{"time\nfoo"},
4764 langFile([]*Stmt{
4765 stmt(&TimeClause{}),
4766 litStmt("foo"),
4767 }, LangBash|LangMirBSDKorn|LangZsh),
4768 ),
4769 fileTest(
4770 []string{"coproc foo bar"},
4771 langFile(litStmt("coproc", "foo", "bar")),
4772 langFile(&CoprocClause{Stmt: litStmt("foo", "bar")}, langBashLike),
4773 ),
4774 fileTest(
4775 []string{"coproc name { foo; }"},
4776 langFile(&CoprocClause{
4777 Name: litWord("name"),
4778 Stmt: stmt(block(litStmt("foo"))),
4779 }, LangBash),
4780 ),
4781 fileTest(
4782 []string{"coproc $namevar { foo; }"},
4783 langFile(&CoprocClause{
4784 Name: word(litParamExp("namevar")),
4785 Stmt: stmt(block(litStmt("foo"))),
4786 }, LangBash),
4787 ),
4788 fileTest(
4789 []string{"coproc foo", "coproc foo;"},
4790 langFile(&CoprocClause{Stmt: litStmt("foo")}, LangBash),
4791 ),
4792 fileTest(
4793 []string{"coproc { foo; }"},
4794 langFile(&CoprocClause{
4795 Stmt: stmt(block(litStmt("foo"))),
4796 }, LangBash),
4797 ),
4798 fileTest(
4799 []string{"coproc (foo)"},
4800 langFile(&CoprocClause{
4801 Stmt: stmt(subshell(litStmt("foo"))),
4802 }, LangBash),
4803 ),
4804 fileTest(
4805 []string{"coproc name foo | bar"},
4806 langFile(&CoprocClause{
4807 Name: litWord("name"),
4808 Stmt: stmt(&BinaryCmd{
4809 Op: Pipe,
4810 X: litStmt("foo"),
4811 Y: litStmt("bar"),
4812 }),
4813 }, LangBash),
4814 ),
4815 fileTest(
4816 []string{"coproc $()", "coproc ``"},
4817 langFile(&CoprocClause{Stmt: stmt(call(
4818 word(cmdSubst()),
4819 ))}, LangBash),
4820 ),
4821 fileTest(
4822 []string{`let i++`},
4823 langFile(letClause(
4824 &UnaryArithm{Op: Inc, Post: true, X: litWord("i")},
4825 ), LangBash|LangMirBSDKorn|LangZsh),
4826 langFile(litStmt("let", "i++"), LangPOSIX),
4827 ),
4828 fileTest(
4829 []string{`let a++ b++ c +d`},
4830 langFile(letClause(
4831 &UnaryArithm{Op: Inc, Post: true, X: litWord("a")},
4832 &UnaryArithm{Op: Inc, Post: true, X: litWord("b")},
4833 litWord("c"),
4834 &UnaryArithm{Op: Plus, X: litWord("d")},
4835 ), LangBash|LangMirBSDKorn|LangZsh),
4836 ),
4837 fileTest(
4838 []string{`let ++i >/dev/null`},
4839 langFile(&Stmt{
4840 Cmd: letClause(&UnaryArithm{Op: Inc, X: litWord("i")}),
4841 Redirs: []*Redirect{{Op: RdrOut, Word: litWord("/dev/null")}},
4842 }, LangBash|LangMirBSDKorn|LangZsh),
4843 ),
4844 fileTest(
4845 []string{
4846 `let a=(1 + 2) b=3+4`,
4847 `let a=(1+2) b=3+4`,
4848 },
4849 langFile(letClause(
4850 &BinaryArithm{
4851 Op: Assgn,
4852 X: litWord("a"),
4853 Y: parenArit(&BinaryArithm{
4854 Op: Add,
4855 X: litWord("1"),
4856 Y: litWord("2"),
4857 }),
4858 },
4859 &BinaryArithm{
4860 Op: Assgn,
4861 X: litWord("b"),
4862 Y: &BinaryArithm{
4863 Op: Add,
4864 X: litWord("3"),
4865 Y: litWord("4"),
4866 },
4867 },
4868 ), LangBash),
4869 langErr2("1:7: the `let` builtin is a bash feature; tried parsing as LANG", LangPOSIX),
4870 ),
4871 fileTest(
4872 []string{
4873 `let a=$(echo 3)`,
4874 "let a=`echo 3`",
4875 },
4876 langFile(letClause(
4877 &BinaryArithm{
4878 Op: Assgn,
4879 X: litWord("a"),
4880 Y: word(cmdSubst(litStmt("echo", "3"))),
4881 },
4882 ), LangBash),
4883 ),
4884 fileTest(
4885 []string{"(foo-bar)"},
4886 langFile(subshell(litStmt("foo-bar"))),
4887 ),
4888 fileTest(
4889 []string{
4890 "let i++\nbar",
4891 "let i++ \nbar",
4892 "let i++; bar",
4893 },
4894 langFile(stmts(
4895 letClause(&UnaryArithm{
4896 Op: Inc,
4897 Post: true,
4898 X: litWord("i"),
4899 }),
4900 litCall("bar"),
4901 ), LangBash|LangMirBSDKorn|LangZsh),
4902 ),
4903 fileTest(
4904 []string{
4905 "let i++\nfoo=(bar)",
4906 "let i++; foo=(bar)",
4907 "let i++; foo=(bar)\n",
4908 },
4909 langFile(stmts(
4910 letClause(&UnaryArithm{
4911 Op: Inc,
4912 Post: true,
4913 X: litWord("i"),
4914 }),
4915 &CallExpr{Assigns: []*Assign{{
4916 Name: lit("foo"),
4917 Array: arrValues(litWord("bar")),
4918 }}},
4919 ), LangBash|LangMirBSDKorn|LangZsh),
4920 ),
4921 fileTest(
4922 []string{
4923 "case a in b) let i++ ;; esac",
4924 "case a in b) let i++;; esac",
4925 },
4926 langFile(&CaseClause{
4927 Word: word(lit("a")),
4928 Items: []*CaseItem{{
4929 Op: Break,
4930 Patterns: litWords("b"),
4931 Stmts: stmts(letClause(&UnaryArithm{
4932 Op: Inc,
4933 Post: true,
4934 X: litWord("i"),
4935 })),
4936 }},
4937 }, LangBash|LangMirBSDKorn|LangZsh),
4938 ),
4939 fileTest(
4940 []string{"a+=1"},
4941 langFile(&CallExpr{
4942 Assigns: []*Assign{{
4943 Append: true,
4944 Name: lit("a"),
4945 Value: litWord("1"),
4946 }},
4947 }, LangBash|LangMirBSDKorn|LangZsh),
4948 langFile(litStmt("a+=1"), LangPOSIX),
4949 ),
4950 fileTest(
4951 []string{"b+=(2 3)"},
4952 langFile(&CallExpr{Assigns: []*Assign{{
4953 Append: true,
4954 Name: lit("b"),
4955 Array: arrValues(litWords("2", "3")...),
4956 }}}, LangBash|LangMirBSDKorn|LangZsh),
4957 ),
4958 fileTest(
4959 []string{"a[1]=(b c)"},
4960 langFile(&CallExpr{Assigns: []*Assign{{
4961 Name: lit("a"),
4962 Index: litWord("1"),
4963 Array: arrValues(litWords("b", "c")...),
4964 }}}, LangZsh),
4965 ),
4966 fileTest(
4967 []string{"a[2]=b c[-3]= d[x]+=e"},
4968 langFile(litStmt("a[2]=b", "c[-3]=", "d[x]+=e"), LangPOSIX),
4969 langFile(&CallExpr{Assigns: []*Assign{
4970 {
4971 Name: lit("a"),
4972 Index: litWord("2"),
4973 Value: litWord("b"),
4974 },
4975 {
4976 Name: lit("c"),
4977 Index: &UnaryArithm{
4978 Op: Minus,
4979 X: litWord("3"),
4980 },
4981 },
4982 {
4983 Name: lit("d"),
4984 Index: litWord("x"),
4985 Append: true,
4986 Value: litWord("e"),
4987 },
4988 }}, LangBash|LangMirBSDKorn|LangZsh),
4989 ),
4990 fileTest(
4991 []string{"*[i]=x"},
4992 langFile(word(lit("*"), lit("[i]=x")), LangBash|LangMirBSDKorn|LangZsh),
4993 langFile(lit("*[i]=x"), LangPOSIX),
4994 ),
4995 fileTest(
4996 []string{"arr[0,1]=x"},
4997 langFile(&CallExpr{Assigns: []*Assign{{
4998 Name: lit("arr"),
4999 Index: &BinaryArithm{
5000 Op: Comma,
5001 X: litWord("0"),
5002 Y: litWord("1"),
5003 },
5004 Value: litWord("x"),
5005 }}}, LangZsh),
5006 langFile(lit("arr[0,1]=x"), LangPOSIX),
5007 ),
5008 fileTest(
5009 []string{
5010 "b[i]+=2",
5011 "b[ i ]+=2",
5012 },
5013 langFile(&CallExpr{Assigns: []*Assign{{
5014 Append: true,
5015 Name: lit("b"),
5016 Index: litWord("i"),
5017 Value: litWord("2"),
5018 }}}, LangBash|LangMirBSDKorn|LangZsh),
5019 ),
5020 fileTest(
5021 []string{`$((a + "b + $c"))`},
5022 langFile(arithmExp(&BinaryArithm{
5023 Op: Add,
5024 X: litWord("a"),
5025 Y: word(dblQuoted(
5026 lit("b + "),
5027 litParamExp("c"),
5028 )),
5029 })),
5030 ),
5031 fileTest(
5032 []string{`let 'i++'`},
5033 langFile(letClause(word(sglQuoted("i++"))), LangBash|LangMirBSDKorn|LangZsh),
5034 ),
5035 fileTest(
5036 []string{`echo ${a["x y"]}`},
5037 langFile(call(litWord("echo"), word(&ParamExp{
5038 Param: lit("a"),
5039 Index: word(dblQuoted(lit("x y"))),
5040 })), LangBash),
5041 ),
5042 fileTest(
5043 []string{
5044 `a[$"x y"]=b`,
5045 `a[ $"x y" ]=b`,
5046 },
5047 langFile(&CallExpr{Assigns: []*Assign{{
5048 Name: lit("a"),
5049 Index: word(&DblQuoted{Dollar: true, Parts: []WordPart{
5050 lit("x y"),
5051 }}),
5052 Value: litWord("b"),
5053 }}}, LangBash),
5054 ),
5055 fileTest(
5056 []string{`((a["x y"] = b))`, `((a["x y"]=b))`},
5057 langFile(arithmCmd(&BinaryArithm{
5058 Op: Assgn,
5059 X: word(&ParamExp{
5060 Short: true,
5061 Param: lit("a"),
5062 Index: word(dblQuoted(lit("x y"))),
5063 }),
5064 Y: litWord("b"),
5065 }), LangBash|LangMirBSDKorn|LangZsh),
5066 ),
5067 fileTest(
5068 []string{
5069 `a=(["x y"]=b)`,
5070 `a=( [ "x y" ]=b)`,
5071 },
5072 langFile(&CallExpr{Assigns: []*Assign{{
5073 Name: lit("a"),
5074 Array: &ArrayExpr{Elems: []*ArrayElem{{
5075 Index: word(dblQuoted(lit("x y"))),
5076 Value: litWord("b"),
5077 }}},
5078 }}}, LangBash),
5079 ),
5080 fileTest(
5081 []string{
5082 "a=([x]= [y]=)",
5083 "a=(\n[x]=\n[y]=\n)",
5084 },
5085 langFile(&CallExpr{Assigns: []*Assign{{
5086 Name: lit("a"),
5087 Array: &ArrayExpr{Elems: []*ArrayElem{
5088 {Index: litWord("x")},
5089 {Index: litWord("y")},
5090 }},
5091 }}}, LangBash),
5092 ),
5093 fileTest(
5094 []string{`a=(foo[0-9])`},
5095 langFile(&CallExpr{Assigns: []*Assign{{
5096 Name: lit("a"),
5097 Array: &ArrayExpr{Elems: []*ArrayElem{
5098 {Value: word(lit("foo"), lit("[0-9]"))},
5099 }},
5100 }}}, LangBash|LangMirBSDKorn|LangZsh),
5101 ),
5102 fileTest(
5103 []string{`a=("foo"[0-9])`},
5104 langFile(&CallExpr{Assigns: []*Assign{{
5105 Name: lit("a"),
5106 Array: &ArrayExpr{Elems: []*ArrayElem{
5107 {Value: word(dblQuoted(lit("foo")), lit("[0-9]"))},
5108 }},
5109 }}}, LangBash|LangMirBSDKorn|LangZsh),
5110 ),
5111 fileTest(
5112 []string{"a]b"},
5113 langFile(litStmt("a]b")),
5114 ),
5115 fileTest(
5116 []string{"echo a[b c[de]f"},
5117 langFile(litStmt("echo", "a[b", "c[de]f"), LangPOSIX),
5118 langFile(call(litWord("echo"),
5119 word(lit("a"), lit("[b")),
5120 word(lit("c"), lit("[de]f")),
5121 ), LangBash|LangMirBSDKorn|LangZsh),
5122 ),
5123 fileTest(
5124 []string{"<<EOF | b\nfoo\nEOF"},
5125 langFile(&BinaryCmd{
5126 Op: Pipe,
5127 X: &Stmt{Redirs: []*Redirect{{
5128 Op: Hdoc,
5129 Word: litWord("EOF"),
5130 Hdoc: litWord("foo\n"),
5131 }}},
5132 Y: litStmt("b"),
5133 }),
5134 ),
5135 fileTest(
5136 []string{"<<EOF1 <<EOF2 | c && d\nEOF1\nEOF2"},
5137 langFile(&BinaryCmd{
5138 Op: AndStmt,
5139 X: stmt(&BinaryCmd{
5140 Op: Pipe,
5141 X: &Stmt{Redirs: []*Redirect{
5142 {Op: Hdoc, Word: litWord("EOF1")},
5143 {Op: Hdoc, Word: litWord("EOF2")},
5144 }},
5145 Y: litStmt("c"),
5146 }),
5147 Y: litStmt("d"),
5148 }),
5149 ),
5150 fileTest(
5151 []string{
5152 "<<EOF && { bar; }\nhdoc\nEOF",
5153 "<<EOF &&\nhdoc\nEOF\n{ bar; }",
5154 },
5155 langFile(&BinaryCmd{
5156 Op: AndStmt,
5157 X: &Stmt{Redirs: []*Redirect{{
5158 Op: Hdoc,
5159 Word: litWord("EOF"),
5160 Hdoc: litWord("hdoc\n"),
5161 }}},
5162 Y: stmt(block(litStmt("bar"))),
5163 }),
5164 ),
5165 fileTest(
5166 []string{"foo() {\n\t<<EOF && { bar; }\nhdoc\nEOF\n}"},
5167 langFile(&FuncDecl{
5168 Parens: true,
5169 Name: lit("foo"),
5170 Body: stmt(block(stmt(&BinaryCmd{
5171 Op: AndStmt,
5172 X: &Stmt{Redirs: []*Redirect{{
5173 Op: Hdoc,
5174 Word: litWord("EOF"),
5175 Hdoc: litWord("hdoc\n"),
5176 }}},
5177 Y: stmt(block(litStmt("bar"))),
5178 }))),
5179 }),
5180 ),
5181 fileTest(
5182 []string{`"a$("")"`, "\"a`\"\"`\""},
5183 langFile(dblQuoted(
5184 lit("a"),
5185 cmdSubst(stmt(call(
5186 word(dblQuoted()),
5187 ))),
5188 )),
5189 ),
5190 fileTest(
5191 []string{"echo ?(b)*(c)+(d)@(e)!(f)"},
5192 langFile(call(litWord("echo"), word(
5193 &ExtGlob{Op: GlobZeroOrOne, Pattern: lit("b")},
5194 &ExtGlob{Op: GlobZeroOrMore, Pattern: lit("c")},
5195 &ExtGlob{Op: GlobOneOrMore, Pattern: lit("d")},
5196 &ExtGlob{Op: GlobOne, Pattern: lit("e")},
5197 &ExtGlob{Op: GlobExcept, Pattern: lit("f")},
5198 )), LangBash|LangMirBSDKorn),
5199 langFile(call(litWord("echo"), word(
5200 lit("?"), lit("(b)"),
5201 lit("*"), lit("(c)"),
5202 lit("+"), lit("(d)"),
5203 lit("@"), lit("(e)"),
5204 lit("!"), lit("(f)"),
5205 )), LangZsh),
5206 ),
5207 fileTest(
5208 []string{"echo foo@(b*(c|d))bar"},
5209 langFile(call(litWord("echo"), word(
5210 lit("foo"),
5211 &ExtGlob{Op: GlobOne, Pattern: lit("b*(c|d)")},
5212 lit("bar"),
5213 )), LangBash|LangMirBSDKorn),
5214 ),
5215 fileTest(
5216 []string{"echo $a@(b)$c?(d)$e*(f)$g+(h)$i!(j)$k"},
5217 langFile(call(litWord("echo"), word(
5218 litParamExp("a"),
5219 &ExtGlob{Op: GlobOne, Pattern: lit("b")},
5220 litParamExp("c"),
5221 &ExtGlob{Op: GlobZeroOrOne, Pattern: lit("d")},
5222 litParamExp("e"),
5223 &ExtGlob{Op: GlobZeroOrMore, Pattern: lit("f")},
5224 litParamExp("g"),
5225 &ExtGlob{Op: GlobOneOrMore, Pattern: lit("h")},
5226 litParamExp("i"),
5227 &ExtGlob{Op: GlobExcept, Pattern: lit("j")},
5228 litParamExp("k"),
5229 )), LangBash|LangMirBSDKorn),
5230 ),
5231 fileTest(
5232 []string{`"^$1[[:space:]]"`},
5233 langFile(dblQuoted(
5234 lit("^"),
5235 litParamExp("1"),
5236 lit("[[:space:]]"),
5237 )),
5238 ),
5239 // Zsh glob qualifiers are parsed as part of the word.
5240 fileTest(
5241 []string{"echo *(.)"},
5242 langFile(call(litWord("echo"), word(lit("*"), lit("(.)"))), LangZsh),
5243 ),
5244 fileTest(
5245 []string{"echo **(/)"},
5246 langFile(call(litWord("echo"), word(lit("**"), lit("(/)"))), LangZsh),
5247 ),
5248 fileTest(
5249 []string{"echo *.txt(@)"},
5250 langFile(call(litWord("echo"), word(lit("*.txt"), lit("(@)"))), LangZsh),
5251 ),
5252 fileTest(
5253 []string{"echo *(om[1,5])"},
5254 langFile(call(litWord("echo"), word(lit("*"), lit("(om[1,5])"))), LangZsh),
5255 ),
5256 fileTest(
5257 []string{"echo /bin/sh(:t)"},
5258 langFile(call(litWord("echo"), word(lit("/bin/sh"), lit("(:t)"))), LangZsh),
5259 ),
5260 // Zsh glob qualifiers on words without glob metacharacters or slashes.
5261 fileTest(
5262 []string{"echo .(:a)"},
5263 langFile(call(litWord("echo"), word(lit("."), lit("(:a)"))), LangZsh),
5264 ),
5265 fileTest(
5266 []string{"echo ~(:t)"},
5267 langFile(call(litWord("echo"), word(lit("~"), lit("(:t)"))), LangZsh),
5268 ),
5269 fileTest(
5270 []string{`echo {go.mod,shfmt}(N*)`},
5271 langFile(call(litWord("echo"), word(lit("{go.mod,shfmt}"), lit("(N*)"))), LangZsh),
5272 ),
5273 fileTest(
5274 []string{"echo $var(Nms-3)"},
5275 langFile(call(litWord("echo"), word(litParamExp("var"), lit("(Nms-3)"))), LangZsh),
5276 ),
5277 fileTest(
5278 []string{"echo a(A) b(B)"},
5279 langFile(call(litWord("echo"), word(lit("a"), lit("(A)")), word(lit("b"), lit("(B)"))), LangZsh),
5280 ),
5281 // Zsh numeric range globs.
5282 fileTest(
5283 []string{"echo <->"},
5284 langFile(litCall("echo", "<->"), LangZsh),
5285 ),
5286 fileTest(
5287 []string{"echo <5-10>"},
5288 langFile(litCall("echo", "<5-10>"), LangZsh),
5289 ),
5290 fileTest(
5291 []string{"echo foo<->.txt"},
5292 langFile(litCall("echo", "foo<->.txt"), LangZsh),
5293 ),
5294 fileTest(
5295 []string{"echo <5->.*"},
5296 langFile(litCall("echo", "<5->.*"), LangZsh),
5297 ),
5298 fileTest(
5299 // <2-3 is a redirect, not a numeric range glob (no closing >).
5300 []string{"echo <2-3"},
5301 langFile(&Stmt{
5302 Cmd: litCall("echo"),
5303 Redirs: []*Redirect{{Op: RdrIn, Word: litWord("2-3")}},
5304 }, LangZsh),
5305 ),
5306 fileTest(
5307 []string{"@test \"desc\" { body; }"},
5308 langFile(&TestDecl{
5309 Description: word(dblQuoted(lit("desc"))),
5310 Body: stmt(block(litStmt("body"))),
5311 }, LangBats),
5312 ),
5313 fileTest(
5314 []string{"@test 'desc' {\n\tmultiple\n\tstatements\n}"},
5315 langFile(&TestDecl{
5316 Description: word(sglQuoted("desc")),
5317 Body: stmt(block(litStmts("multiple", "statements")...)),
5318 }, LangBats),
5319 ),
5320 fileTest(
5321 []string{"${+foo}"},
5322 langFile(&ParamExp{
5323 IsSet: true,
5324 Param: lit("foo"),
5325 }, LangZsh),
5326 ),
5327 fileTest(
5328 []string{"$+foo $#bar"},
5329 langFile(call(
5330 word(&ParamExp{
5331 Short: true,
5332 IsSet: true,
5333 Param: lit("foo"),
5334 }),
5335 word(&ParamExp{
5336 Short: true,
5337 Length: true,
5338 Param: lit("bar"),
5339 }),
5340 ), LangZsh),
5341 ),
5342 fileTest(
5343 []string{"${${foo#head}%tail}"},
5344 langFile(&ParamExp{
5345 NestedParam: &ParamExp{
5346 Param: lit("foo"),
5347 Exp: &Expansion{
5348 Op: RemSmallPrefix,
5349 Word: litWord("head"),
5350 },
5351 },
5352 Exp: &Expansion{
5353 Op: RemSmallSuffix,
5354 Word: litWord("tail"),
5355 },
5356 }, LangZsh),
5357 ),
5358 fileTest(
5359 []string{`${#"${foo}"}`},
5360 langFile(&ParamExp{
5361 Length: true,
5362 NestedParam: dblQuoted(&ParamExp{Param: lit("foo")}),
5363 }, LangZsh),
5364 flipConfirm2(LangZsh), // TODO: why is this a bad substitution in zsh?
5365 ),
5366 fileTest(
5367 []string{"${$(echo footail)%tail}"},
5368 langFile(&ParamExp{
5369 NestedParam: cmdSubst(litStmt("echo", "footail")),
5370 Exp: &Expansion{
5371 Op: RemSmallSuffix,
5372 Word: litWord("tail"),
5373 },
5374 }, LangZsh),
5375 ),
5376 fileTest(
5377 []string{"${foo:u}"},
5378 langFile(&ParamExp{
5379 Param: lit("foo"),
5380 Modifiers: lits("u"),
5381 }, LangZsh),
5382 langFile(&ParamExp{
5383 Param: lit("foo"),
5384 Slice: &Slice{Offset: litWord("u")},
5385 }, LangBash|LangMirBSDKorn),
5386 ),
5387 fileTest(
5388 []string{"${foo:t5:h2:l}"},
5389 langFile(&ParamExp{
5390 Param: lit("foo"),
5391 Modifiers: lits("t5", "h2", "l"),
5392 }, LangZsh),
5393 ),
5394 fileTest(
5395 []string{"$${foo}"},
5396 langFile(word(
5397 litParamExp("$"),
5398 lit("{foo}"),
5399 )),
5400 ),
5401 fileTest(
5402 []string{"${=foo}"},
5403 langFile(&ParamExp{
5404 Split: OptOn,
5405 Param: lit("foo"),
5406 }, LangZsh),
5407 ),
5408 fileTest(
5409 []string{"${~foo}"},
5410 langFile(&ParamExp{
5411 GlobSubst: OptOn,
5412 Param: lit("foo"),
5413 }, LangZsh),
5414 ),
5415 fileTest(
5416 []string{"${^foo}"},
5417 langFile(&ParamExp{
5418 RcExpand: OptOn,
5419 Param: lit("foo"),
5420 }, LangZsh),
5421 ),
5422 fileTest(
5423 []string{"${==foo}"},
5424 langFile(&ParamExp{
5425 Split: OptOff,
5426 Param: lit("foo"),
5427 }, LangZsh),
5428 ),
5429 fileTest(
5430 []string{"${~~foo}"},
5431 langFile(&ParamExp{
5432 GlobSubst: OptOff,
5433 Param: lit("foo"),
5434 }, LangZsh),
5435 ),
5436 fileTest(
5437 []string{"${^^foo}"},
5438 langFile(&ParamExp{
5439 RcExpand: OptOff,
5440 Param: lit("foo"),
5441 }, LangZsh),
5442 ),
5443 fileTest(
5444 []string{"$=foo $^bar $~baz"},
5445 langFile(call(
5446 word(&ParamExp{
5447 Short: true,
5448 Split: OptOn,
5449 Param: lit("foo"),
5450 }),
5451 word(&ParamExp{
5452 Short: true,
5453 RcExpand: OptOn,
5454 Param: lit("bar"),
5455 }),
5456 word(&ParamExp{
5457 Short: true,
5458 GlobSubst: OptOn,
5459 Param: lit("baz"),
5460 }),
5461 ), LangZsh),
5462 ),
5463 fileTest(
5464 []string{"${(o)=items}"},
5465 langFile(&ParamExp{
5466 Flags: lit("o"),
5467 Split: OptOn,
5468 Param: lit("items"),
5469 }, LangZsh),
5470 ),
5471 fileTest(
5472 []string{"${(aO)foo} ${(s/x/)foo}"},
5473 langFile(call(
5474 word(&ParamExp{
5475 Flags: lit("aO"),
5476 Param: lit("foo"),
5477 }),
5478 word(&ParamExp{
5479 Flags: lit("s/x/"),
5480 Param: lit("foo"),
5481 }),
5482 ), LangZsh),
5483 ),
5484 fileTest(
5485 []string{`${:-word}`},
5486 langErr2("1:3: invalid parameter name"),
5487 langFile(&ParamExp{
5488 Exp: &Expansion{
5489 Op: DefaultUnsetOrNull,
5490 Word: litWord("word"),
5491 },
5492 }, LangZsh),
5493 ),
5494 fileTest(
5495 []string{`${:+word}`},
5496 langErr2("1:3: invalid parameter name"),
5497 langFile(&ParamExp{
5498 Exp: &Expansion{
5499 Op: AlternateUnsetOrNull,
5500 Word: litWord("word"),
5501 },
5502 }, LangZsh),
5503 ),
5504 fileTest(
5505 []string{`${(%):-%N}`},
5506 langFile(&ParamExp{
5507 Flags: lit("%"),
5508 Exp: &Expansion{
5509 Op: DefaultUnsetOrNull,
5510 Word: word(lit("%N")),
5511 },
5512 }, LangZsh),
5513 ),
5514 }
5515
5516 // these don't have a canonical format with the same syntax tree
5517 var fileTestsNoPrint = []fileTestCase{
5518 fileTest(
5519 []string{`$[foo]`},
5520 langFile(word(lit("$"), lit("[foo]")), LangPOSIX),
5521 ),
5522 fileTest(
5523 []string{`"$[foo]"`},
5524 langFile(dblQuoted(lit("$"), lit("[foo]")), LangPOSIX),
5525 ),
5526 fileTest(
5527 []string{`"$[1 + 3]"`},
5528 langFile(dblQuoted(arithmExpBr(&BinaryArithm{
5529 Op: Add,
5530 X: litWord("1"),
5531 Y: litWord("3"),
5532 })), LangBash),
5533 ),
5534 }
5535
5536 // these parse with comments
5537 var fileTestsKeepComments = []fileTestCase{
5538 fileTest(
5539 []string{"# foo\ncmd\n# bar"},
5540 langFile(&File{
5541 Stmts: []*Stmt{{
5542 Comments: []Comment{{Text: " foo"}},
5543 Cmd: litCall("cmd"),
5544 }},
5545 Last: []Comment{{Text: " bar"}},
5546 }),
5547 ),
5548 fileTest(
5549 []string{"foo # bar # baz"},
5550 langFile(&File{
5551 Stmts: []*Stmt{{
5552 Comments: []Comment{{Text: " bar # baz"}},
5553 Cmd: litCall("foo"),
5554 }},
5555 }),
5556 ),
5557 fileTest(
5558 []string{
5559 "$(\n\t# foo\n)",
5560 "`\n\t# foo\n`",
5561 "`# foo\n`",
5562 },
5563 langFile(&CmdSubst{
5564 Last: []Comment{{Text: " foo"}},
5565 }),
5566 ),
5567 fileTest(
5568 []string{
5569 "`# foo`",
5570 "` # foo`",
5571 },
5572 langFile(&CmdSubst{
5573 Last: []Comment{{Text: " foo"}},
5574 }),
5575 ),
5576 }
5577
5578 type sanityChecker struct {
5579 tb testing.TB
5580 src string
5581 file *File // nil if not checking a whole file
5582 }
5583
5584 func (c sanityChecker) checkPos(node Node, pos Pos, strs ...string) {
5585 if !pos.IsValid() {
5586 c.tb.Fatalf("invalid Pos in %T", node)
5587 }
5588 offs := pos.Offset()
5589 if offs > uint(len(c.src)) {
5590 c.tb.Errorf("Pos offset %d in %T is out of bounds in %q",
5591 offs, node, c.src)
5592 return
5593 }
5594 if len(strs) == 0 {
5595 return
5596 }
5597 if strings.Contains(c.src, "<<-") {
5598 // since the tab indentation in <<- heredoc bodies
5599 // aren't part of the final literals
5600 return
5601 }
5602 var gotErr string
5603 for i, want := range strs {
5604 got := c.src[offs:]
5605 if i == 0 {
5606 gotErr = got
5607 }
5608 got = strings.ReplaceAll(got, "\x00", "")
5609 got = strings.ReplaceAll(got, "\r\n", "\n")
5610 if !strings.Contains(want, "\\\n") {
5611 // Hack to let "foobar" match the input "foo\\\nbar".
5612 got = strings.ReplaceAll(got, "\\\n", "")
5613 }
5614 if strings.HasPrefix(got, want) {
5615 return
5616 }
5617 }
5618 c.tb.Errorf("Expected one of %q at %s in %q, found %q",
5619 strs, pos, c.src, gotErr)
5620 }
5621
5622 func (c sanityChecker) visit(node Node) bool {
5623 if node == nil {
5624 return true
5625 }
5626 if f := c.file; f != nil {
5627 if !node.Pos().IsValid() && len(f.Stmts) > 0 {
5628 c.tb.Fatalf("Invalid Pos")
5629 }
5630 }
5631 if node.Pos().After(node.End()) {
5632 c.tb.Errorf("Found End() before Pos() in %T", node)
5633 }
5634 switch node := node.(type) {
5635 case *Comment:
5636 if f := c.file; f != nil {
5637 if f.Pos().After(node.Pos()) {
5638 c.tb.Fatalf("A Comment is before its File")
5639 }
5640 if node.End().After(f.End()) {
5641 c.tb.Fatalf("A Comment is after its File")
5642 }
5643 }
5644 c.checkPos(node, node.Hash, "#"+node.Text)
5645 case *Stmt:
5646 endOff := int(node.End().Offset())
5647 if endOff < len(c.src) {
5648 end := c.src[endOff]
5649 switch {
5650 case end == ' ', end == '\n', end == '\t', end == '\r':
5651 // ended by whitespace
5652 case regOps(rune(end)):
5653 // ended by end character
5654 case endOff > 0 && c.src[endOff-1] == ';':
5655 // ended by semicolon
5656 case endOff > 0 && c.src[endOff-1] == '&':
5657 // ended by & or |&
5658 case end == '\\' && c.src[endOff+1] == '`':
5659 // ended by an escaped backquote
5660 default:
5661 c.tb.Errorf("Unexpected Stmt.End() %d %q in %q",
5662 endOff, end, c.src)
5663 }
5664 }
5665 if c.src[node.Position.Offset()] == '#' {
5666 c.tb.Errorf("Stmt.Pos() should not be a comment")
5667 }
5668 c.checkPos(node, node.Position)
5669 if node.Semicolon.IsValid() {
5670 c.checkPos(node, node.Semicolon, ";", "&", "|&")
5671 }
5672 for _, r := range node.Redirs {
5673 strs := []string{r.Op.String()}
5674 // Zsh supports alternate forms of redirect operators:
5675 // ! instead of |, and >& or >>& prefixes instead of &> and &>>.
5676 switch r.Op {
5677 case RdrClob:
5678 strs = append(strs, ">!")
5679 case AppClob:
5680 strs = append(strs, ">>!")
5681 case RdrAllClob:
5682 strs = append(strs, "&>!", ">&|", ">&!")
5683 case AppAll:
5684 strs = append(strs, ">>&")
5685 case AppAllClob:
5686 strs = append(strs, "&>>!", ">>&|", ">>&!")
5687 }
5688 c.checkPos(node, r.OpPos, strs...)
5689 }
5690 case *Lit:
5691 pos, end := int(node.Pos().Offset()), int(node.End().Offset())
5692 want := pos + len(node.Value)
5693 val := node.Value
5694 posLine := node.Pos().Line()
5695 endLine := node.End().Line()
5696 switch {
5697 case strings.Contains(c.src, "\\\n"), strings.Contains(c.src, "\\\r\n"):
5698 case !strings.Contains(node.Value, "\n") && posLine != endLine:
5699 c.tb.Errorf("Lit without newlines has Pos/End lines %d and %d",
5700 posLine, endLine)
5701 case strings.Contains(c.src, "`") && strings.Contains(c.src, "\\"):
5702 // removed backslashes inside backquote cmd substs
5703 val = ""
5704 case end < len(c.src) && (c.src[end] == '\n' || c.src[end] == '`'):
5705 // heredoc literals that end with the
5706 // stop word and a newline or closing backquote
5707 case end == len(c.src):
5708 // same as above, but with word and EOF
5709 case end != want:
5710 c.tb.Errorf("Unexpected Lit %q End() %d (wanted %d for pos %d) in %q",
5711 val, end, want, pos, c.src)
5712 }
5713 c.checkPos(node, node.ValuePos, val)
5714 c.checkPos(node, node.ValueEnd)
5715 case *Subshell:
5716 c.checkPos(node, node.Lparen, "(")
5717 c.checkPos(node, node.Rparen, ")")
5718 case *Block:
5719 c.checkPos(node, node.Lbrace, "{")
5720 c.checkPos(node, node.Rbrace, "}")
5721 case *IfClause:
5722 if node.ThenPos.IsValid() {
5723 c.checkPos(node, node.Position, "if", "elif")
5724 c.checkPos(node, node.ThenPos, "then")
5725 } else {
5726 c.checkPos(node, node.Position, "else")
5727 }
5728 c.checkPos(node, node.FiPos, "fi")
5729 case *WhileClause:
5730 rsrv := "while"
5731 if node.Until {
5732 rsrv = "until"
5733 }
5734 c.checkPos(node, node.WhilePos, rsrv)
5735 c.checkPos(node, node.DoPos, "do")
5736 c.checkPos(node, node.DonePos, "done")
5737 case *ForClause:
5738 if node.Select {
5739 c.checkPos(node, node.ForPos, "select")
5740 } else {
5741 c.checkPos(node, node.ForPos, "for")
5742 }
5743 if node.Braces {
5744 c.checkPos(node, node.DoPos, "{")
5745 c.checkPos(node, node.DonePos, "}")
5746 // Zero out Braces, to not duplicate all the test cases.
5747 // The printer ignores the field anyway.
5748 node.Braces = false
5749 } else {
5750 c.checkPos(node, node.DoPos, "do")
5751 c.checkPos(node, node.DonePos, "done")
5752 }
5753 case *WordIter:
5754 if node.InPos.IsValid() {
5755 c.checkPos(node, node.InPos, "in")
5756 }
5757 case *CStyleLoop:
5758 c.checkPos(node, node.Lparen, "((")
5759 c.checkPos(node, node.Rparen, "))")
5760 case *SglQuoted:
5761 c.checkPos(node, posAddCol(node.End(), -1), "'")
5762 valuePos := posAddCol(node.Left, 1)
5763 if node.Dollar {
5764 valuePos = posAddCol(valuePos, 1)
5765 }
5766 val := node.Value
5767 if strings.Contains(c.src, "`") && strings.Contains(c.src, "\\") {
5768 // removed backslashes inside backquote cmd substs
5769 val = ""
5770 }
5771 c.checkPos(node, valuePos, val)
5772 if node.Dollar {
5773 c.checkPos(node, node.Left, "$'")
5774 } else {
5775 c.checkPos(node, node.Left, "'")
5776 }
5777 c.checkPos(node, node.Right, "'")
5778 case *DblQuoted:
5779 c.checkPos(node, posAddCol(node.End(), -1), `"`)
5780 if node.Dollar {
5781 c.checkPos(node, node.Left, `$"`)
5782 } else {
5783 c.checkPos(node, node.Left, `"`)
5784 }
5785 c.checkPos(node, node.Right, `"`)
5786 case *UnaryArithm:
5787 c.checkPos(node, node.OpPos, node.Op.String())
5788 case *UnaryTest:
5789 strs := []string{node.Op.String()}
5790 switch node.Op {
5791 case TsExists:
5792 strs = append(strs, "-a")
5793 case TsSmbLink:
5794 strs = append(strs, "-h")
5795 }
5796 c.checkPos(node, node.OpPos, strs...)
5797 case *BinaryCmd:
5798 c.checkPos(node, node.OpPos, node.Op.String())
5799 case *BinaryArithm:
5800 c.checkPos(node, node.OpPos, node.Op.String())
5801 case *BinaryTest:
5802 strs := []string{node.Op.String()}
5803 switch node.Op {
5804 case TsMatch:
5805 strs = append(strs, "=")
5806 }
5807 c.checkPos(node, node.OpPos, strs...)
5808 case *ParenArithm:
5809 c.checkPos(node, node.Lparen, "(")
5810 c.checkPos(node, node.Rparen, ")")
5811 case *FlagsArithm:
5812 case *ParenTest:
5813 c.checkPos(node, node.Lparen, "(")
5814 c.checkPos(node, node.Rparen, ")")
5815 case *FuncDecl:
5816 if node.RsrvWord {
5817 c.checkPos(node, node.Position, "function")
5818 } else {
5819 c.checkPos(node, node.Position)
5820 }
5821 case *ParamExp:
5822 if node.nakedIndex() {
5823 // Dollar is unset; Pos falls back to Param.
5824 } else {
5825 c.checkPos(node, node.Dollar, "$")
5826 }
5827 if !node.Short {
5828 c.checkPos(node, node.Rbrace, "}")
5829 } else if node.nakedIndex() {
5830 c.checkPos(node, posAddCol(node.End(), -1), "]")
5831 }
5832 case *ArithmExp:
5833 if node.Bracket {
5834 // deprecated $(( form
5835 c.checkPos(node, node.Left, "$[")
5836 c.checkPos(node, node.Right, "]")
5837 } else {
5838 c.checkPos(node, node.Left, "$((")
5839 c.checkPos(node, node.Right, "))")
5840 }
5841 case *ArithmCmd:
5842 c.checkPos(node, node.Left, "((")
5843 c.checkPos(node, node.Right, "))")
5844 case *CmdSubst:
5845 switch {
5846 case node.TempFile:
5847 c.checkPos(node, node.Left, "${ ", "${\t", "${\n")
5848 c.checkPos(node, node.Right, "}")
5849 case node.ReplyVar:
5850 c.checkPos(node, node.Left, "${|")
5851 c.checkPos(node, node.Right, "}")
5852 case node.Backquotes:
5853 c.checkPos(node, node.Left, "`", "\\`")
5854 c.checkPos(node, node.Right, "`", "\\`")
5855 // Zero out Backquotes, to not duplicate all the test
5856 // cases. The printer ignores the field anyway.
5857 node.Backquotes = false
5858 default:
5859 c.checkPos(node, node.Left, "$(")
5860 c.checkPos(node, node.Right, ")")
5861 }
5862 case *CaseClause:
5863 c.checkPos(node, node.Case, "case")
5864 if node.Braces {
5865 c.checkPos(node, node.In, "{")
5866 c.checkPos(node, node.Esac, "}")
5867 // Zero out Braces, to not duplicate all the test cases.
5868 // The printer ignores the field anyway.
5869 node.Braces = false
5870 } else {
5871 c.checkPos(node, node.In, "in")
5872 c.checkPos(node, node.Esac, "esac")
5873 }
5874 case *CaseItem:
5875 if node.OpPos.IsValid() {
5876 c.checkPos(node, node.OpPos, node.Op.String(), "esac")
5877 }
5878 case *TestClause:
5879 c.checkPos(node, node.Left, "[[")
5880 c.checkPos(node, node.Right, "]]")
5881 case *TimeClause:
5882 c.checkPos(node, node.Time, "time")
5883 case *CoprocClause:
5884 c.checkPos(node, node.Coproc, "coproc")
5885 case *LetClause:
5886 c.checkPos(node, node.Let, "let")
5887 case *TestDecl:
5888 c.checkPos(node, node.Position, "@test")
5889 case *ArrayExpr:
5890 c.checkPos(node, node.Lparen, "(")
5891 c.checkPos(node, node.Rparen, ")")
5892 case *ExtGlob:
5893 c.checkPos(node, node.OpPos, node.Op.String())
5894 c.checkPos(node, posAddCol(node.End(), -1), ")")
5895 case *ProcSubst:
5896 c.checkPos(node, node.OpPos, node.Op.String())
5897 c.checkPos(node, node.Rparen, ")")
5898 }
5899 return true
5900 }