Skip to content

Commit

Permalink
Merge pull request bazel-contrib#1 from google/fix_build
Browse files Browse the repository at this point in the history
Fix build errors, implement parseCommaList
  • Loading branch information
jbeda committed Feb 21, 2016
2 parents 4a8db91 + 2f91602 commit 642ec89
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
1 change: 1 addition & 0 deletions ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ type astUnary struct {

// astVar represents variables.
type astVar struct {
astNodeBase
id identifier
original identifier
}
Expand Down
46 changes: 37 additions & 9 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,50 @@ func (p *parser) peek() *token {
}

func (p *parser) parseIdentifierList(elementKind string) (identifiers, bool, error) {
exprs, got_comma, err := p.parseCommaList(tokenParenR, elementKind)
_, exprs, got_comma, err := p.parseCommaList(tokenParenR, elementKind)
if err != nil {
return identifiers{}, false, err
}
var ids identifiers
for n := range exprs {
v, ok := n.(astVar)
for _, n := range exprs {
v, ok := n.(*astVar)
if !ok {
return identifiers{}, false, makeStaticError(fmt.Sprintf("Not an identifier: %v", n), n.Loc())
return identifiers{}, false, makeStaticError(fmt.Sprintf("Not an identifier: %v", n), *n.Loc())
}
ids = append(ids, v.id)
}
return ids, got_comma, nil
}

func (p *parser) parseCommaList(tokenKind end, elementkind string) (astNodes, bool, error) {
func (p *parser) parseCommaList(end tokenKind, elementKind string) (*token, astNodes, bool, error) {
var exprs astNodes
got_comma := false
first := true
for {
next := p.peek();
if !first && !got_comma {
if next.kind == tokenComma {
p.pop()
next = p.peek()
got_comma = true
}
}
if next.kind == end {
// got_comma can be true or false here.
return p.pop(), exprs, got_comma, nil
}
if !first && !got_comma {
return nil, nil, false, makeStaticError(fmt.Sprintf("Expected a comma before next %s.", elementKind), next.loc)
}

expr, err := p.parse(maxPrecedence)
if err != nil {
return nil, nil, false, err
}
exprs = append(exprs, expr)
got_comma = false
first = false
}
}

func (p *parser) parse(prec precedence) (astNode, error) {
Expand Down Expand Up @@ -176,14 +204,14 @@ func (p *parser) parse(prec precedence) (astNode, error) {
if err != nil {
return nil, err
}
return astFunction{
return &astFunction{
astNodeBase: astNodeBase{locFromTokenAST(begin, body)},
parameters: params,
trailingComma, got_comma,
trailingComma: got_comma,
body: body,
}
}, nil
} else {
return makeStaticError(fmt.Sprintf("Expected ( but got %v", next), next.loc)
return nil, makeStaticError(fmt.Sprintf("Expected ( but got %v", next), next.loc)
}
}

Expand Down

0 comments on commit 642ec89

Please sign in to comment.