Skip to content

Commit

Permalink
Allow native function declarations to accept default arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
m-Peter committed Nov 8, 2023
1 parent 7e955a3 commit 7f22168
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 12 deletions.
4 changes: 2 additions & 2 deletions runtime/parser/declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/parser/declaration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
)
Expand Down
2 changes: 1 addition & 1 deletion runtime/parser/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
35 changes: 30 additions & 5 deletions runtime/parser/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -354,7 +374,11 @@ func parseFunctionDeclaration(
}

parameterList, returnTypeAnnotation, functionBlock, err :=
parseFunctionParameterListAndRest(p, functionBlockIsOptional)
parseFunctionParameterListAndRest(
p,
functionBlockIsOptional,
nativePos != nil,
)

if err != nil {
return nil, err
Expand All @@ -379,6 +403,7 @@ func parseFunctionDeclaration(
func parseFunctionParameterListAndRest(
p *parser,
functionBlockIsOptional bool,
isNative bool,
) (
parameterList *ast.ParameterList,
returnTypeAnnotation *ast.TypeAnnotation,
Expand All @@ -387,7 +412,7 @@ func parseFunctionParameterListAndRest(
) {
// Parameter list

parameterList, err = parseParameterList(p, false)
parameterList, err = parseParameterList(p, false, isNative)
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/parser/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func parseFunctionDeclarationOrFunctionExpressionStatement(
}

parameterList, returnTypeAnnotation, functionBlock, err :=
parseFunctionParameterListAndRest(p, false)
parseFunctionParameterListAndRest(p, false, false)

if err != nil {
return nil, err
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/parser/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 7f22168

Please sign in to comment.