diff --git a/runtime/parser/declaration.go b/runtime/parser/declaration.go index 5a3353f397..709c862853 100644 --- a/runtime/parser/declaration.go +++ b/runtime/parser/declaration.go @@ -877,7 +877,7 @@ func parseEventDeclaration( // if this is a `ResourceDestroyed` event (i.e., a default event declaration), parse default arguments parseDefaultArguments := ast.IsResourceDestructionDefaultEvent(identifier.Identifier) - parameterList, err := parseParameterList(p, parseDefaultArguments) + parameterList, err := parseParameterList(p, parseDefaultArguments, false) if err != nil { return nil, err } @@ -1889,7 +1889,7 @@ func parseSpecialFunctionDeclaration( startPos := ast.EarliestPosition(identifier.Pos, accessPos, purityPos, staticPos, nativePos) parameterList, returnTypeAnnotation, functionBlock, err := - parseFunctionParameterListAndRest(p, functionBlockIsOptional) + parseFunctionParameterListAndRest(p, functionBlockIsOptional, nativePos != nil) if err != nil { return nil, err } diff --git a/runtime/parser/declaration_test.go b/runtime/parser/declaration_test.go index 7f9795c4af..1661be44b0 100644 --- a/runtime/parser/declaration_test.go +++ b/runtime/parser/declaration_test.go @@ -389,7 +389,7 @@ func TestParseParameterList(t *testing.T) { nil, []byte(input), func(p *parser) (*ast.ParameterList, error) { - return parseParameterList(p, false) + return parseParameterList(p, false, false) }, Config{}, ) diff --git a/runtime/parser/expression.go b/runtime/parser/expression.go index b9d1b5c8e5..a9b87dc65c 100644 --- a/runtime/parser/expression.go +++ b/runtime/parser/expression.go @@ -846,7 +846,7 @@ func defineIdentifierExpression() { func parseFunctionExpression(p *parser, token lexer.Token, purity ast.FunctionPurity) (*ast.FunctionExpression, error) { parameterList, returnTypeAnnotation, functionBlock, err := - parseFunctionParameterListAndRest(p, false) + parseFunctionParameterListAndRest(p, false, false) if err != nil { return nil, err } diff --git a/runtime/parser/function.go b/runtime/parser/function.go index 69b202b1b4..0b380c0b42 100644 --- a/runtime/parser/function.go +++ b/runtime/parser/function.go @@ -32,7 +32,11 @@ func parsePurityAnnotation(p *parser) ast.FunctionPurity { return ast.FunctionPurityUnspecified } -func parseParameterList(p *parser, expectDefaultArguments bool) (*ast.ParameterList, error) { +func parseParameterList( + p *parser, + expectDefaultArguments bool, + acceptDefaultArguments bool, +) (*ast.ParameterList, error) { var parameters []*ast.Parameter p.skipSpaceAndComments() @@ -63,7 +67,11 @@ func parseParameterList(p *parser, expectDefaultArguments bool) (*ast.ParameterL Pos: p.current.StartPos, }) } - parameter, err := parseParameter(p, expectDefaultArguments) + parameter, err := parseParameter( + p, + expectDefaultArguments, + acceptDefaultArguments, + ) if err != nil { return nil, err } @@ -120,7 +128,11 @@ func parseParameterList(p *parser, expectDefaultArguments bool) (*ast.ParameterL ), nil } -func parseParameter(p *parser, expectDefaultArgument bool) (*ast.Parameter, error) { +func parseParameter( + p *parser, + expectDefaultArgument bool, + acceptDefaultArgument bool, +) (*ast.Parameter, error) { p.skipSpaceAndComments() startPos := p.current.StartPos @@ -187,6 +199,14 @@ func parseParameter(p *parser, expectDefaultArgument bool) (*ast.Parameter, erro return nil, err } + } else if acceptDefaultArgument && p.current.Is(lexer.TokenEqual) { + // Skip the = + p.nextSemanticToken() + + defaultArgument, err = parseExpression(p, lowestBindingPower) + if err != nil { + return nil, err + } } else if p.current.Is(lexer.TokenEqual) { return nil, p.syntaxError("cannot use a default argument for this function") } @@ -354,7 +374,11 @@ func parseFunctionDeclaration( } parameterList, returnTypeAnnotation, functionBlock, err := - parseFunctionParameterListAndRest(p, functionBlockIsOptional) + parseFunctionParameterListAndRest( + p, + functionBlockIsOptional, + nativePos != nil, + ) if err != nil { return nil, err @@ -379,6 +403,7 @@ func parseFunctionDeclaration( func parseFunctionParameterListAndRest( p *parser, functionBlockIsOptional bool, + isNative bool, ) ( parameterList *ast.ParameterList, returnTypeAnnotation *ast.TypeAnnotation, @@ -387,7 +412,7 @@ func parseFunctionParameterListAndRest( ) { // Parameter list - parameterList, err = parseParameterList(p, false) + parameterList, err = parseParameterList(p, false, isNative) if err != nil { return } diff --git a/runtime/parser/statement.go b/runtime/parser/statement.go index a5f986b308..445428e70a 100644 --- a/runtime/parser/statement.go +++ b/runtime/parser/statement.go @@ -205,7 +205,7 @@ func parseFunctionDeclarationOrFunctionExpressionStatement( } parameterList, returnTypeAnnotation, functionBlock, err := - parseFunctionParameterListAndRest(p, false) + parseFunctionParameterListAndRest(p, false, false) if err != nil { return nil, err @@ -227,7 +227,7 @@ func parseFunctionDeclarationOrFunctionExpressionStatement( ), nil } else { parameterList, returnTypeAnnotation, functionBlock, err := - parseFunctionParameterListAndRest(p, false) + parseFunctionParameterListAndRest(p, false, false) if err != nil { return nil, err } diff --git a/runtime/parser/transaction.go b/runtime/parser/transaction.go index 1ccff2a7a6..cfb3356db0 100644 --- a/runtime/parser/transaction.go +++ b/runtime/parser/transaction.go @@ -52,7 +52,7 @@ func parseTransactionDeclaration(p *parser, docString string) (*ast.TransactionD var err error if p.current.Is(lexer.TokenParenOpen) { - parameterList, err = parseParameterList(p, false) + parameterList, err = parseParameterList(p, false, false) if err != nil { return nil, err }