Example #1
0
func (p *Parser) parseFunctionStmt(inMethod bool) *ast.FunctionStmt {
	stmt := &ast.FunctionStmt{}
	stmt.FunctionDefinition = p.parseFunctionDefinition()
	if !inMethod {
		p.namespace.Functions[stmt.Name] = stmt
	}
	p.scope = ast.NewScope(p.scope, p.FileSet.GlobalScope, p.FileSet.SuperGlobalScope)
	stmt.Body = p.parseBlock()
	p.scope = p.scope.EnclosingScope
	return stmt
}
Example #2
0
func (p *Parser) parseAnonymousFunction() ast.Expr {
	f := &ast.AnonymousFunction{}
	f.Arguments = make([]*ast.FunctionArgument, 0)
	f.ClosureVariables = make([]*ast.FunctionArgument, 0)
	p.expect(token.OpenParen)
	if p.peek().Typ != token.CloseParen {
		f.Arguments = append(f.Arguments, p.parseFunctionArgument())
	}

Loop:
	for {
		switch p.peek().Typ {
		case token.Comma:
			p.expect(token.Comma)
			f.Arguments = append(f.Arguments, p.parseFunctionArgument())
		case token.CloseParen:
			break Loop
		default:
			p.errorf("unexpected argument separator: %s", p.current)
			return f
		}
	}
	p.expect(token.CloseParen)

	// Closure variables
	if p.peek().Typ == token.Use {
		p.expect(token.Use)
		p.expect(token.OpenParen)
		f.ClosureVariables = append(f.ClosureVariables, p.parseFunctionArgument())
	ClosureLoop:
		for {
			switch p.peek().Typ {
			case token.Comma:
				p.expect(token.Comma)
				f.ClosureVariables = append(f.ClosureVariables, p.parseFunctionArgument())
			case token.CloseParen:
				break ClosureLoop
			default:
				p.errorf("unexpected argument separator: %s", p.current)
				return f
			}
		}
		p.expect(token.CloseParen)
	}

	p.scope = ast.NewScope(p.scope, p.FileSet.GlobalScope, p.FileSet.SuperGlobalScope)
	f.Body = p.parseBlock()
	p.scope = p.scope.EnclosingScope
	return f
}