Skip to content

Commit 642ec89

Browse files
committed
Merge pull request google#1 from google/fix_build
Fix build errors, implement parseCommaList
2 parents 4a8db91 + 2f91602 commit 642ec89

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

ast.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ type astUnary struct {
477477

478478
// astVar represents variables.
479479
type astVar struct {
480+
astNodeBase
480481
id identifier
481482
original identifier
482483
}

parser.go

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,50 @@ func (p *parser) peek() *token {
7070
}
7171

7272
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)
7474
if err != nil {
7575
return identifiers{}, false, err
7676
}
7777
var ids identifiers
78-
for n := range exprs {
79-
v, ok := n.(astVar)
78+
for _, n := range exprs {
79+
v, ok := n.(*astVar)
8080
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())
8282
}
8383
ids = append(ids, v.id)
8484
}
85+
return ids, got_comma, nil
8586
}
8687

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+
}
88108

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+
}
89117
}
90118

91119
func (p *parser) parse(prec precedence) (astNode, error) {
@@ -176,14 +204,14 @@ func (p *parser) parse(prec precedence) (astNode, error) {
176204
if err != nil {
177205
return nil, err
178206
}
179-
return astFunction{
207+
return &astFunction{
180208
astNodeBase: astNodeBase{locFromTokenAST(begin, body)},
181209
parameters: params,
182-
trailingComma, got_comma,
210+
trailingComma: got_comma,
183211
body: body,
184-
}
212+
}, nil
185213
} 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)
187215
}
188216
}
189217

0 commit comments

Comments
 (0)