@@ -376,14 +376,21 @@ func (*WordIter) loopNode() {}
376376func (* CStyleLoop ) loopNode () {}
377377
378378// WordIter represents the iteration of a variable over a series of words in a
379- // for clause.
379+ // for clause. If InPos is an invalid position, the "in" token was missing, so
380+ // the iteration is over the shell's positional parameters.
380381type WordIter struct {
381382 Name * Lit
383+ InPos Pos // position of "in"
382384 Items []* Word
383385}
384386
385387func (w * WordIter ) Pos () Pos { return w .Name .Pos () }
386- func (w * WordIter ) End () Pos { return posMax (w .Name .End (), wordLastEnd (w .Items )) }
388+ func (w * WordIter ) End () Pos {
389+ if len (w .Items ) > 0 {
390+ return wordLastEnd (w .Items )
391+ }
392+ return posMax (w .Name .End (), posAddCol (w .InPos , 2 ))
393+ }
387394
388395// CStyleLoop represents the behaviour of a for clause similar to the C
389396// language.
@@ -781,8 +788,12 @@ func (a *ArrayExpr) Pos() Pos { return a.Lparen }
781788func (a * ArrayExpr ) End () Pos { return posAddCol (a .Rparen , 1 ) }
782789
783790// ArrayElem represents a Bash array element.
791+ //
792+ // Index can be nil; for example, declare -a x=(value).
793+ // Value can be nil; for example, declare -A x=([index]=).
794+ // Finally, neither can be nil; for example, declare -A x=([index]=value)
784795type ArrayElem struct {
785- Index ArithmExpr // [i]=, ["k"]=
796+ Index ArithmExpr
786797 Value * Word
787798 Comments []Comment
788799}
@@ -793,7 +804,12 @@ func (a *ArrayElem) Pos() Pos {
793804 }
794805 return a .Value .Pos ()
795806}
796- func (a * ArrayElem ) End () Pos { return a .Value .End () }
807+ func (a * ArrayElem ) End () Pos {
808+ if a .Value != nil {
809+ return a .Value .End ()
810+ }
811+ return posAddCol (a .Index .Pos (), 1 )
812+ }
797813
798814// ExtGlob represents a Bash extended globbing expression. Note that these are
799815// parsed independently of whether shopt has been called or not.
0 commit comments