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
{
$$ = &ast.ExprStaticCall{
staticCall := &ast.ExprStaticCall{
Position: yylex.(*Parser).builder.NewNodesPosition($1, $4),
Class: $1,
DoubleColonTkn: $2,
@ -3698,6 +3698,14 @@ function_call:
SeparatorTkns: $4.(*ast.ArgumentList).SeparatorTkns,
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
{
@ -3714,7 +3722,7 @@ function_call:
}
| variable_class_name T_PAAMAYIM_NEKUDOTAYIM variable_name function_call_parameter_list
{
$$ = &ast.ExprStaticCall{
staticCall := &ast.ExprStaticCall{
Position: yylex.(*Parser).builder.NewNodesPosition($1, $4),
Class: $1,
DoubleColonTkn: $2,
@ -3724,6 +3732,14 @@ function_call:
SeparatorTkns: $4.(*ast.ArgumentList).SeparatorTkns,
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
{

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
{
$$ = &ast.ExprStaticCall{
staticCall := &ast.ExprStaticCall{
Position: yylex.(*Parser).builder.NewNodesPosition($1, $4),
Class: $1,
DoubleColonTkn: $2,
@ -3331,10 +3331,18 @@ function_call:
SeparatorTkns: $4.(*ast.ArgumentList).SeparatorTkns,
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
{
$$ = &ast.ExprStaticCall{
staticCall := &ast.ExprStaticCall{
Position: yylex.(*Parser).builder.NewNodesPosition($1, $4),
Class: $1,
DoubleColonTkn: $2,
@ -3344,6 +3352,14 @@ function_call:
SeparatorTkns: $4.(*ast.ArgumentList).SeparatorTkns,
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
{

View File

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

View File

@ -1473,7 +1473,9 @@ func (v *Dumper) ExprStaticCall(n *ast.ExprStaticCall) {
v.dumpPosition(n.Position)
v.dumpVertex("Class", n.Class)
v.dumpToken("DoubleColonTkn", n.DoubleColonTkn)
v.dumpToken("OpenCurlyBracketTkn", n.OpenCurlyBracketTkn)
v.dumpVertex("Call", n.Call)
v.dumpToken("CloseCurlyBracketTkn", n.CloseCurlyBracketTkn)
v.dumpToken("OpenParenthesisTkn", n.OpenParenthesisTkn)
v.dumpVertexList("Arguments", n.Arguments)
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) {
n.Class.Accept(f)
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.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) {
o := bytes.NewBufferString("")

View File

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