@@ -70,22 +70,50 @@ func (p *parser) peek() *token {
70
70
}
71
71
72
72
func (p * parser ) parseIdentifierList (elementKind string ) (identifiers , bool , error ) {
73
- exprs , got_comma , err := p .parseCommaList (tokenParenR , elementKind )
73
+ _ , exprs , got_comma , err := p .parseCommaList (tokenParenR , elementKind )
74
74
if err != nil {
75
75
return identifiers {}, false , err
76
76
}
77
77
var ids identifiers
78
- for n := range exprs {
79
- v , ok := n .(astVar )
78
+ for _ , n := range exprs {
79
+ v , ok := n .(* astVar )
80
80
if ! ok {
81
- return identifiers {}, false , makeStaticError (fmt .Sprintf ("Not an identifier: %v" , n ), n .Loc ())
81
+ return identifiers {}, false , makeStaticError (fmt .Sprintf ("Not an identifier: %v" , n ), * n .Loc ())
82
82
}
83
83
ids = append (ids , v .id )
84
84
}
85
+ return ids , got_comma , nil
85
86
}
86
87
87
- func (p * parser ) parseCommaList (tokenKind end , elementkind string ) (astNodes , bool , error ) {
88
+ func (p * parser ) parseCommaList (end tokenKind , elementKind string ) (* token , astNodes , bool , error ) {
89
+ var exprs astNodes
90
+ got_comma := false
91
+ first := true
92
+ for {
93
+ next := p .peek ();
94
+ if ! first && ! got_comma {
95
+ if next .kind == tokenComma {
96
+ p .pop ()
97
+ next = p .peek ()
98
+ got_comma = true
99
+ }
100
+ }
101
+ if next .kind == end {
102
+ // got_comma can be true or false here.
103
+ return p .pop (), exprs , got_comma , nil
104
+ }
105
+ if ! first && ! got_comma {
106
+ return nil , nil , false , makeStaticError (fmt .Sprintf ("Expected a comma before next %s." , elementKind ), next .loc )
107
+ }
88
108
109
+ expr , err := p .parse (maxPrecedence )
110
+ if err != nil {
111
+ return nil , nil , false , err
112
+ }
113
+ exprs = append (exprs , expr )
114
+ got_comma = false
115
+ first = false
116
+ }
89
117
}
90
118
91
119
func (p * parser ) parse (prec precedence ) (astNode , error ) {
@@ -176,14 +204,14 @@ func (p *parser) parse(prec precedence) (astNode, error) {
176
204
if err != nil {
177
205
return nil , err
178
206
}
179
- return astFunction {
207
+ return & astFunction {
180
208
astNodeBase : astNodeBase {locFromTokenAST (begin , body )},
181
209
parameters : params ,
182
- trailingComma , got_comma ,
210
+ trailingComma : got_comma ,
183
211
body : body ,
184
- }
212
+ }, nil
185
213
} else {
186
- return makeStaticError (fmt .Sprintf ("Expected ( but got %v" , next ), next .loc )
214
+ return nil , makeStaticError (fmt .Sprintf ("Expected ( but got %v" , next ), next .loc )
187
215
}
188
216
}
189
217
0 commit comments