func (p *Parser) consumeFunc() *ast.Func { t := p.next() if !p.isExpected(t, token.Func) { return nil } f := ast.Func{ Name: t.String(), } f.File = p.name f.Line = t.Line f.Position = t.Start t = p.next() if !p.isExpected(t, token.LeftParen) { return nil } p.consumeParams(&f) return &f }
func (p *Parser) consumeParams(f *ast.Func) { for { switch p.peek().Type { case token.Quote, token.LeftBrac, token.Func: f.AnonParams = append(f.AnonParams, p.consumeNode()) case token.String: t := p.next() if f.Params == nil { f.Params = make(map[string]interface{}) } if p.expects(p.peek(), []token.Type{token.Colon, token.Equal}) { switch p.next().Type { case token.Colon: case token.Equal: f.Params[t.String()] = p.consumeNode() } } else { return } default: return } if p.expects(p.peek(), []token.Type{token.RightParen, token.Comma}) { DANGLING_COMMA: switch p.peek().Type { case token.RightParen: p.next() return case token.Comma: p.next() if p.peek().Type == token.RightParen { goto DANGLING_COMMA } continue } } } }