refactoring: update ast structure of "StaticCall" node

This commit is contained in:
Vadym Slizov 2020-12-27 23:19:49 +02:00
parent 0f2341bfa9
commit ad884c99df
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
9 changed files with 91 additions and 12 deletions

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -3688,7 +3688,7 @@ function_call:
} }
| class_name T_PAAMAYIM_NEKUDOTAYIM variable_name function_call_parameter_list | class_name T_PAAMAYIM_NEKUDOTAYIM variable_name function_call_parameter_list
{ {
$$ = &ast.ExprStaticCall{ staticCall := &ast.ExprStaticCall{
Position: yylex.(*Parser).builder.NewNodesPosition($1, $4), Position: yylex.(*Parser).builder.NewNodesPosition($1, $4),
Class: $1, Class: $1,
DoubleColonTkn: $2, DoubleColonTkn: $2,
@ -3698,6 +3698,14 @@ function_call:
SeparatorTkns: $4.(*ast.ArgumentList).SeparatorTkns, SeparatorTkns: $4.(*ast.ArgumentList).SeparatorTkns,
CloseParenthesisTkn: $4.(*ast.ArgumentList).CloseParenthesisTkn, CloseParenthesisTkn: $4.(*ast.ArgumentList).CloseParenthesisTkn,
} }
if brackets, ok := $3.(*ast.ParserBrackets); ok {
staticCall.OpenCurlyBracketTkn = brackets.OpenBracketTkn
staticCall.Call = brackets.Child
staticCall.CloseCurlyBracketTkn = brackets.CloseBracketTkn
}
$$ = staticCall
} }
| class_name T_PAAMAYIM_NEKUDOTAYIM variable_without_objects function_call_parameter_list | class_name T_PAAMAYIM_NEKUDOTAYIM variable_without_objects function_call_parameter_list
{ {
@ -3714,7 +3722,7 @@ function_call:
} }
| variable_class_name T_PAAMAYIM_NEKUDOTAYIM variable_name function_call_parameter_list | variable_class_name T_PAAMAYIM_NEKUDOTAYIM variable_name function_call_parameter_list
{ {
$$ = &ast.ExprStaticCall{ staticCall := &ast.ExprStaticCall{
Position: yylex.(*Parser).builder.NewNodesPosition($1, $4), Position: yylex.(*Parser).builder.NewNodesPosition($1, $4),
Class: $1, Class: $1,
DoubleColonTkn: $2, DoubleColonTkn: $2,
@ -3724,6 +3732,14 @@ function_call:
SeparatorTkns: $4.(*ast.ArgumentList).SeparatorTkns, SeparatorTkns: $4.(*ast.ArgumentList).SeparatorTkns,
CloseParenthesisTkn: $4.(*ast.ArgumentList).CloseParenthesisTkn, CloseParenthesisTkn: $4.(*ast.ArgumentList).CloseParenthesisTkn,
} }
if brackets, ok := $3.(*ast.ParserBrackets); ok {
staticCall.OpenCurlyBracketTkn = brackets.OpenBracketTkn
staticCall.Call = brackets.Child
staticCall.CloseCurlyBracketTkn = brackets.CloseBracketTkn
}
$$ = staticCall
} }
| variable_class_name T_PAAMAYIM_NEKUDOTAYIM variable_without_objects function_call_parameter_list | variable_class_name T_PAAMAYIM_NEKUDOTAYIM variable_without_objects function_call_parameter_list
{ {

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -3321,7 +3321,7 @@ function_call:
} }
| class_name T_PAAMAYIM_NEKUDOTAYIM member_name argument_list | class_name T_PAAMAYIM_NEKUDOTAYIM member_name argument_list
{ {
$$ = &ast.ExprStaticCall{ staticCall := &ast.ExprStaticCall{
Position: yylex.(*Parser).builder.NewNodesPosition($1, $4), Position: yylex.(*Parser).builder.NewNodesPosition($1, $4),
Class: $1, Class: $1,
DoubleColonTkn: $2, DoubleColonTkn: $2,
@ -3331,10 +3331,18 @@ function_call:
SeparatorTkns: $4.(*ast.ArgumentList).SeparatorTkns, SeparatorTkns: $4.(*ast.ArgumentList).SeparatorTkns,
CloseParenthesisTkn: $4.(*ast.ArgumentList).CloseParenthesisTkn, CloseParenthesisTkn: $4.(*ast.ArgumentList).CloseParenthesisTkn,
} }
if brackets, ok := $3.(*ast.ParserBrackets); ok {
staticCall.OpenCurlyBracketTkn = brackets.OpenBracketTkn
staticCall.Call = brackets.Child
staticCall.CloseCurlyBracketTkn = brackets.CloseBracketTkn
}
$$ = staticCall
} }
| variable_class_name T_PAAMAYIM_NEKUDOTAYIM member_name argument_list | variable_class_name T_PAAMAYIM_NEKUDOTAYIM member_name argument_list
{ {
$$ = &ast.ExprStaticCall{ staticCall := &ast.ExprStaticCall{
Position: yylex.(*Parser).builder.NewNodesPosition($1, $4), Position: yylex.(*Parser).builder.NewNodesPosition($1, $4),
Class: $1, Class: $1,
DoubleColonTkn: $2, DoubleColonTkn: $2,
@ -3344,6 +3352,14 @@ function_call:
SeparatorTkns: $4.(*ast.ArgumentList).SeparatorTkns, SeparatorTkns: $4.(*ast.ArgumentList).SeparatorTkns,
CloseParenthesisTkn: $4.(*ast.ArgumentList).CloseParenthesisTkn, CloseParenthesisTkn: $4.(*ast.ArgumentList).CloseParenthesisTkn,
} }
if brackets, ok := $3.(*ast.ParserBrackets); ok {
staticCall.OpenCurlyBracketTkn = brackets.OpenBracketTkn
staticCall.Call = brackets.Child
staticCall.CloseCurlyBracketTkn = brackets.CloseBracketTkn
}
$$ = staticCall
} }
| callable_expr argument_list | callable_expr argument_list
{ {

View File

@ -1681,14 +1681,16 @@ func (n *ExprShellExec) GetPosition() *position.Position {
// ExprStaticCall node // ExprStaticCall node
type ExprStaticCall struct { type ExprStaticCall struct {
Position *position.Position Position *position.Position
Class Vertex Class Vertex
DoubleColonTkn *token.Token DoubleColonTkn *token.Token
Call Vertex OpenCurlyBracketTkn *token.Token
OpenParenthesisTkn *token.Token Call Vertex
Arguments []Vertex CloseCurlyBracketTkn *token.Token
SeparatorTkns []*token.Token OpenParenthesisTkn *token.Token
CloseParenthesisTkn *token.Token Arguments []Vertex
SeparatorTkns []*token.Token
CloseParenthesisTkn *token.Token
} }
func (n *ExprStaticCall) Accept(v NodeVisitor) { func (n *ExprStaticCall) Accept(v NodeVisitor) {

View File

@ -1473,7 +1473,9 @@ func (v *Dumper) ExprStaticCall(n *ast.ExprStaticCall) {
v.dumpPosition(n.Position) v.dumpPosition(n.Position)
v.dumpVertex("Class", n.Class) v.dumpVertex("Class", n.Class)
v.dumpToken("DoubleColonTkn", n.DoubleColonTkn) v.dumpToken("DoubleColonTkn", n.DoubleColonTkn)
v.dumpToken("OpenCurlyBracketTkn", n.OpenCurlyBracketTkn)
v.dumpVertex("Call", n.Call) v.dumpVertex("Call", n.Call)
v.dumpToken("CloseCurlyBracketTkn", n.CloseCurlyBracketTkn)
v.dumpToken("OpenParenthesisTkn", n.OpenParenthesisTkn) v.dumpToken("OpenParenthesisTkn", n.OpenParenthesisTkn)
v.dumpVertexList("Arguments", n.Arguments) v.dumpVertexList("Arguments", n.Arguments)
v.dumpTokenList("SeparatorTkns", n.SeparatorTkns) v.dumpTokenList("SeparatorTkns", n.SeparatorTkns)

View File

@ -1394,6 +1394,17 @@ func (f *formatter) ExprShellExec(n *ast.ExprShellExec) {
func (f *formatter) ExprStaticCall(n *ast.ExprStaticCall) { func (f *formatter) ExprStaticCall(n *ast.ExprStaticCall) {
n.Class.Accept(f) n.Class.Accept(f)
n.DoubleColonTkn = f.newToken(token.T_PAAMAYIM_NEKUDOTAYIM, []byte("::")) n.DoubleColonTkn = f.newToken(token.T_PAAMAYIM_NEKUDOTAYIM, []byte("::"))
n.OpenCurlyBracketTkn = nil
n.CloseCurlyBracketTkn = nil
switch n.Call.(type) {
case *ast.Identifier:
case *ast.ExprVariable:
default:
n.OpenCurlyBracketTkn = f.newToken('{', []byte("{"))
n.CloseCurlyBracketTkn = f.newToken('}', []byte("}"))
}
n.Call.Accept(f) n.Call.Accept(f)
n.OpenParenthesisTkn = f.newToken('(', []byte("(")) n.OpenParenthesisTkn = f.newToken('(', []byte("("))

View File

@ -4692,6 +4692,36 @@ func TestFormatter_ExprStaticCall(t *testing.T) {
} }
} }
func TestFormatter_ExprStaticCall_Expr(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.ExprStaticCall{
Class: &ast.NameName{
Parts: []ast.Vertex{
&ast.NameNamePart{
Value: []byte("foo"),
},
},
},
Call: &ast.ScalarString{
Value: []byte("'bar'"),
},
}
f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1)
n.Accept(f)
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n.Accept(p)
expected := `foo::{'bar'}()`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestFormatter_ExprStaticCall_Arguments(t *testing.T) { func TestFormatter_ExprStaticCall_Arguments(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")

View File

@ -840,7 +840,9 @@ func (p *printer) ExprShellExec(n *ast.ExprShellExec) {
func (p *printer) ExprStaticCall(n *ast.ExprStaticCall) { func (p *printer) ExprStaticCall(n *ast.ExprStaticCall) {
p.printNode(n.Class) p.printNode(n.Class)
p.printToken(n.DoubleColonTkn, []byte("::")) p.printToken(n.DoubleColonTkn, []byte("::"))
p.printToken(n.OpenCurlyBracketTkn, nil)
p.printNode(n.Call) p.printNode(n.Call)
p.printToken(n.CloseCurlyBracketTkn, nil)
p.printToken(n.OpenParenthesisTkn, p.ifNodeList(n.Arguments, []byte("("))) p.printToken(n.OpenParenthesisTkn, p.ifNodeList(n.Arguments, []byte("(")))
p.printSeparatedList(n.Arguments, n.SeparatorTkns, []byte(",")) p.printSeparatedList(n.Arguments, n.SeparatorTkns, []byte(","))
p.printToken(n.CloseParenthesisTkn, p.ifNodeList(n.Arguments, []byte(")"))) p.printToken(n.CloseParenthesisTkn, p.ifNodeList(n.Arguments, []byte(")")))