[refactoring] update ast structure of "Argument" and "ArgumentList" nodes

This commit is contained in:
Vadym Slizov 2020-11-20 22:57:27 +02:00
parent 73d819eb6c
commit 7e2965f53b
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
8 changed files with 106 additions and 116 deletions

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -242,7 +242,7 @@ import (
%type <node> static_scalar_value static_operation static_var_list global_var_list
%type <node> ctor_arguments function_call_parameter_list echo_expr_list
%type <node> trait_adaptations unset_variables declare_list
%type <node> switch_case_list
%type <node> switch_case_list non_empty_function_call_parameter_list
%type <node> method_body
%type <node> foreach_statement for_statement while_statement
%type <node> foreach_variable foreach_optional_arg
@ -258,7 +258,7 @@ import (
%type <list> for_expr case_list catch_statement additional_catches
%type <list> non_empty_additional_catches parameter_list non_empty_parameter_list class_statement_list
%type <list> class_statement_list variable_modifiers method_modifiers class_variable_declaration
%type <list> interface_list non_empty_function_call_parameter_list trait_list trait_adaptation_list non_empty_trait_adaptation_list
%type <list> interface_list trait_list trait_adaptation_list non_empty_trait_adaptation_list
%type <list> trait_reference_list non_empty_member_modifiers backticks_expr static_array_pair_list non_empty_static_array_pair_list
%type <list> chaining_dereference chaining_instance_call chaining_method_or_property instance_call variable_property
@ -2170,38 +2170,40 @@ optional_class_type:
function_call_parameter_list:
'(' ')'
{
$$ = &ast.ArgumentList{ast.Node{}, nil}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $2)
// save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens($$, token.End, $2.SkippedTokens)
$$ = &ast.ArgumentList{
Node: ast.Node{
Position: position.NewTokensPosition($1, $2),
},
OpenParenthesisTkn: $1,
CloseParenthesisTkn: $2,
}
}
| '(' non_empty_function_call_parameter_list ')'
{
$$ = &ast.ArgumentList{ast.Node{}, $2}
argumentList := $2.(*ast.ArgumentList)
argumentList.Position = position.NewTokensPosition($1, $3)
argumentList.OpenParenthesisTkn = $1
argumentList.CloseParenthesisTkn = $3
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens($$, token.End, $3.SkippedTokens)
$$ = argumentList
}
| '(' yield_expr ')'
{
arg := &ast.Argument{ast.Node{}, false, false, $2}
$$ = &ast.ArgumentList{ast.Node{}, []ast.Vertex{arg}}
// save position
arg.GetNode().Position = position.NewNodePosition($2)
$$.GetNode().Position = position.NewTokensPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens($$, token.End, $3.SkippedTokens)
$$ = &ast.ArgumentList{
Node: ast.Node{
Position: position.NewTokensPosition($1, $3),
},
OpenParenthesisTkn: $1,
Arguments: []ast.Vertex{
&ast.Argument{
Node: ast.Node{
Position: position.NewNodePosition($2),
},
Expr: $2,
},
},
CloseParenthesisTkn: $3,
}
}
;
@ -2209,57 +2211,57 @@ function_call_parameter_list:
non_empty_function_call_parameter_list:
function_call_parameter
{
$$ = []ast.Vertex{$1}
$$ = &ast.ArgumentList{
Arguments: []ast.Vertex{$1},
}
}
| non_empty_function_call_parameter_list ',' function_call_parameter
{
$$ = append($1, $3)
$1.(*ast.ArgumentList).SeparatorTkns = append($1.(*ast.ArgumentList).SeparatorTkns, $2)
$1.(*ast.ArgumentList).Arguments = append($1.(*ast.ArgumentList).Arguments, $3)
// save comments
yylex.(*Parser).setFreeFloating(lastNode($1), token.End, $2.SkippedTokens)
$$ = $1
}
;
function_call_parameter:
expr_without_variable
{
$$ = &ast.Argument{ast.Node{}, false, false, $1}
// save position
$$.GetNode().Position = position.NewNodePosition($1)
// save comments
yylex.(*Parser).MoveFreeFloating($1, $$)
$$ = &ast.Argument{
Node: ast.Node{
Position: position.NewNodePosition($1),
},
Expr: $1,
}
}
| variable
{
$$ = &ast.Argument{ast.Node{}, false, false, $1}
// save position
$$.GetNode().Position = position.NewNodePosition($1)
// save comments
yylex.(*Parser).MoveFreeFloating($1, $$)
$$ = &ast.Argument{
Node: ast.Node{
Position: position.NewNodePosition($1),
},
Expr: $1,
}
}
| '&' w_variable
{
$$ = &ast.Argument{ast.Node{}, false, true, $2}
// save position
$$.GetNode().Position = position.NewNodePosition($2)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = &ast.Argument{
Node: ast.Node{
Position: position.NewTokenNodePosition($1, $2),
},
AmpersandTkn: $1,
Expr: $2,
}
}
| T_ELLIPSIS expr
{
$$ = &ast.Argument{ast.Node{}, true, false, $2}
// save position
$$.GetNode().Position = position.NewTokenNodePosition($1, $2)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = &ast.Argument{
Node: ast.Node{
Position: position.NewTokenNodePosition($1, $2),
},
VariadicTkn: $1,
Expr: $2,
}
}
;

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -250,7 +250,7 @@ import (
%type <node> variable_class_name dereferencable_scalar constant dereferencable
%type <node> callable_expr callable_variable static_member new_variable
%type <node> encaps_var encaps_var_offset echo_expr_list catch_name_list
%type <node> if_stmt const_list
%type <node> if_stmt const_list non_empty_argument_list
%type <node> alt_if_stmt
%type <node> if_stmt_without_else
%type <node> class_const_decl
@ -280,7 +280,7 @@ import (
%type <list> unprefixed_use_declarations inline_use_declarations property_list
%type <list> case_list trait_adaptation_list
%type <list> use_declarations lexical_var_list isset_variables non_empty_array_pair_list
%type <list> array_pair_list non_empty_argument_list top_statement_list
%type <list> array_pair_list top_statement_list
%type <list> inner_statement_list parameter_list non_empty_parameter_list class_statement_list
%type <list> method_modifiers variable_modifiers
%type <list> non_empty_member_modifiers name_list class_modifiers
@ -2030,66 +2030,61 @@ return_type:
argument_list:
'(' ')'
{
$$ = &ast.ArgumentList{ast.Node{}, nil}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $2)
// save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens($$, token.End, $2.SkippedTokens)
$$ = &ast.ArgumentList{
Node: ast.Node{
Position: position.NewTokensPosition($1, $2),
},
OpenParenthesisTkn: $1,
CloseParenthesisTkn: $2,
}
}
| '(' non_empty_argument_list possible_comma ')'
{
$$ = &ast.ArgumentList{ast.Node{}, $2}
argumentList := $2.(*ast.ArgumentList)
argumentList.Position = position.NewTokensPosition($1, $4)
argumentList.OpenParenthesisTkn = $1
argumentList.SeparatorTkns = append(argumentList.SeparatorTkns, $3)
argumentList.CloseParenthesisTkn = $4
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.SkippedTokens)
if $3 != nil {
yylex.(*Parser).setFreeFloatingTokens($$, token.End, append($3.SkippedTokens, $4.SkippedTokens...))
} else {
yylex.(*Parser).setFreeFloatingTokens($$, token.End, $4.SkippedTokens)
}
$$ = argumentList
}
;
non_empty_argument_list:
argument
{
$$ = []ast.Vertex{$1}
$$ = &ast.ArgumentList{
Arguments: []ast.Vertex{$1},
}
}
| non_empty_argument_list ',' argument
{
$$ = append($1, $3)
$1.(*ast.ArgumentList).SeparatorTkns = append($1.(*ast.ArgumentList).SeparatorTkns, $2)
$1.(*ast.ArgumentList).Arguments = append($1.(*ast.ArgumentList).Arguments, $3)
// save comments
yylex.(*Parser).setFreeFloating(lastNode($1), token.End, $2.SkippedTokens)
$$ = $1
}
;
argument:
expr
{
$$ = &ast.Argument{ast.Node{}, false, false, $1}
// save position
$$.GetNode().Position = position.NewNodePosition($1)
// save comments
yylex.(*Parser).MoveFreeFloating($1, $$)
$$ = &ast.Argument{
Node: ast.Node{
Position: position.NewNodePosition($1),
},
Expr: $1,
}
}
| T_ELLIPSIS expr
{
$$ = &ast.Argument{ast.Node{}, true, false, $2}
// save position
$$.GetNode().Position = position.NewTokenNodePosition($1, $2)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = &ast.Argument{
Node: ast.Node{
Position: position.NewTokenNodePosition($1, $2),
},
VariadicTkn: $1,
Expr: $2,
}
}
;

View File

@ -90,7 +90,10 @@ func (n *Identifier) Accept(v NodeVisitor) {
// ArgumentList node
type ArgumentList struct {
Node
Arguments []Vertex
OpenParenthesisTkn *token.Token
Arguments []Vertex
SeparatorTkns []*token.Token
CloseParenthesisTkn *token.Token
}
func (n *ArgumentList) Accept(v NodeVisitor) {
@ -100,9 +103,9 @@ func (n *ArgumentList) Accept(v NodeVisitor) {
// Argument node
type Argument struct {
Node
Variadic bool
IsReference bool
Expr Vertex
VariadicTkn *token.Token
AmpersandTkn *token.Token
Expr Vertex
}
func (n *Argument) Accept(v NodeVisitor) {

View File

@ -240,16 +240,6 @@ func (v *Dump) Argument(n *ast.Argument) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.Argument{\n")
v.printNode(n.GetNode())
if n.Variadic {
v.printIndent(v.indent)
v.print("Variadic: true,\n")
}
if n.IsReference {
v.printIndent(v.indent)
v.print("IsReference: true,\n")
}
}
func (v *Dump) StmtBreak(n *ast.StmtBreak) {

View File

@ -465,11 +465,11 @@ func (p *PrettyPrinter) printNodeNullable(n ast.Vertex) {
func (p *PrettyPrinter) printNodeArgument(n ast.Vertex) {
nn := n.(*ast.Argument)
if nn.IsReference {
if nn.AmpersandTkn != nil {
io.WriteString(p.w, "&")
}
if nn.Variadic {
if nn.VariadicTkn != nil {
io.WriteString(p.w, "...")
}

View File

@ -561,12 +561,12 @@ func (p *Printer) printNodeArgument(n ast.Vertex) {
nn := n.(*ast.Argument)
p.printFreeFloating(nn, token.Start)
if nn.IsReference {
if nn.AmpersandTkn != nil {
p.write([]byte("&"))
}
p.printFreeFloating(nn, token.Ampersand)
if nn.Variadic {
if nn.VariadicTkn != nil {
p.write([]byte("..."))
}