Skip to content

Commit 0bb52b2

Browse files
committed
syntax: allow [ glob in array elements
Inside `arrayElems`, the lexer always tokenized `[` as `leftBrack`, so the parser treated `foo[0-9]` and `"foo"[0-9]` inside `a=( ... )` as malformed `[idx]=val` assignments. Only emit `leftBrack` when `[` begins a new element (after whitespace, `(`, or a newline); otherwise fall through to `advanceLitNone` so it joins the preceding word as a glob. Fixes #1322.
1 parent 5ca0d0d commit 0bb52b2

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

syntax/filetests_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4922,6 +4922,24 @@ var fileTests = []fileTestCase{
49224922
}},
49234923
}}}, LangBash),
49244924
),
4925+
fileTest(
4926+
[]string{`a=(foo[0-9])`},
4927+
langFile(&CallExpr{Assigns: []*Assign{{
4928+
Name: lit("a"),
4929+
Array: &ArrayExpr{Elems: []*ArrayElem{
4930+
{Value: word(lit("foo"), lit("[0-9]"))},
4931+
}},
4932+
}}}, LangBash|LangMirBSDKorn|LangZsh),
4933+
),
4934+
fileTest(
4935+
[]string{`a=("foo"[0-9])`},
4936+
langFile(&CallExpr{Assigns: []*Assign{{
4937+
Name: lit("a"),
4938+
Array: &ArrayExpr{Elems: []*ArrayElem{
4939+
{Value: word(dblQuoted(lit("foo")), lit("[0-9]"))},
4940+
}},
4941+
}}}, LangBash|LangMirBSDKorn|LangZsh),
4942+
),
49254943
fileTest(
49264944
[]string{"a]b"},
49274945
langFile(litStmt("a]b")),

syntax/lexer.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,9 @@ skipSpace:
325325
}
326326
p.next()
327327
case '[':
328-
if p.quote == arrayElems {
328+
// `[` only starts an `[idx]=val` element when it begins a new word;
329+
// otherwise it continues a glob like `foo[0-9]`.
330+
if p.quote == arrayElems && (p.spaced || p.tok == leftParen || p.tok == _Newl) {
329331
p.rune()
330332
p.tok = leftBrack
331333
} else {

0 commit comments

Comments
 (0)