Skip to content

Commit

Permalink
syntax: accept "declare $name" again
Browse files Browse the repository at this point in the history
This is a regression from v1. Let's keep "declare $foo" where
foo=name=val out of the question for now, as that opens an entire new
world.

Fixes #129.
  • Loading branch information
mvdan committed Jul 3, 2017
1 parent 749d243 commit c2b7a22
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
4 changes: 2 additions & 2 deletions syntax/filetests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3213,14 +3213,14 @@ var fileTests = []testCase{
),
},
{
Strs: []string{"declare -f func >/dev/null"},
Strs: []string{"declare -f $func >/dev/null"},
bash: &Stmt{
Cmd: &DeclClause{
Variant: lit("declare"),
Opts: litWords("-f"),
Assigns: []*Assign{{
Naked: true,
Name: lit("func"),
Value: word(litParamExp("func")),
}},
},
Redirs: []*Redirect{
Expand Down
12 changes: 11 additions & 1 deletion syntax/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ func (*CoprocClause) commandNode() {}
// an associative array. In the former, it's just an arithmetic
// expression. In the latter, it will be a word with a single DblQuoted
// part.
//
// If Naked is true, it's part of a DeclClause and doesn't contain a
// value. In that context, if the name wasn't a literal, it will be in
// Value instead of Name.
type Assign struct {
Append bool // +=
Naked bool // without '='
Expand All @@ -189,7 +193,13 @@ type Assign struct {
Array *ArrayExpr // =(arr)
}

func (a *Assign) Pos() Pos { return a.Name.Pos() }
func (a *Assign) Pos() Pos {
if a.Name == nil {
return a.Value.Pos()
}
return a.Name.Pos()
}

func (a *Assign) End() Pos {
if a.Value != nil {
return a.Value.End()
Expand Down
5 changes: 5 additions & 0 deletions syntax/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1842,6 +1842,11 @@ func (p *Parser) declClause() *DeclClause {
Naked: true,
Name: p.getLit(),
})
} else if w := p.getWord(); w != nil {
ds.Assigns = append(ds.Assigns, &Assign{
Naked: true,
Value: w,
})
} else {
p.followErr(p.pos, ds.Variant.Value, "names or assignments")
}
Expand Down
4 changes: 2 additions & 2 deletions syntax/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1309,8 +1309,8 @@ var shellTests = []errorCase{
bash: `1:7: "local" must be followed by names or assignments`,
},
{
in: "declare 0=${o})",
bash: `1:9: "declare" must be followed by names or assignments`,
in: "declare 0=${o})", // TODO: better error
bash: `1:15: statements must be separated by &, ; or a newline`,
},
{
in: "a=(<)",
Expand Down

0 comments on commit c2b7a22

Please sign in to comment.