refactoring: create "ScalarEncapsedStringBrackets" node

This commit is contained in:
Vadym Slizov 2020-12-28 00:53:48 +02:00
parent 38141ce97f
commit 2990f0cc2d
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
14 changed files with 110 additions and 68 deletions

View File

@ -3975,14 +3975,14 @@ func TestScalarEncapsed_StringVarCurveOpen(t *testing.T) {
Value: []byte("$a"),
},
},
&ast.ParserBrackets{
&ast.ScalarEncapsedStringBrackets{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 7,
EndPos: 11,
},
OpenBracketTkn: &token.Token{
OpenCurlyBracketTkn: &token.Token{
ID: token.T_CURLY_OPEN,
Value: []byte("{"),
Position: &position.Position{
@ -3992,7 +3992,7 @@ func TestScalarEncapsed_StringVarCurveOpen(t *testing.T) {
EndPos: 8,
},
},
Child: &ast.ExprVariable{
Var: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
@ -4019,7 +4019,7 @@ func TestScalarEncapsed_StringVarCurveOpen(t *testing.T) {
Value: []byte("$b"),
},
},
CloseBracketTkn: &token.Token{
CloseCurlyBracketTkn: &token.Token{
ID: token.ID(125),
Value: []byte("}"),
Position: &position.Position{
@ -4690,14 +4690,14 @@ func TestScalarEncapsed_CurlyOpenMethodCall(t *testing.T) {
},
Value: []byte("test "),
},
&ast.ParserBrackets{
&ast.ScalarEncapsedStringBrackets{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 22,
},
OpenBracketTkn: &token.Token{
OpenCurlyBracketTkn: &token.Token{
ID: token.T_CURLY_OPEN,
Value: []byte("{"),
Position: &position.Position{
@ -4707,7 +4707,7 @@ func TestScalarEncapsed_CurlyOpenMethodCall(t *testing.T) {
EndPos: 10,
},
},
Child: &ast.ExprMethodCall{
Var: &ast.ExprMethodCall{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
@ -4791,7 +4791,7 @@ func TestScalarEncapsed_CurlyOpenMethodCall(t *testing.T) {
},
},
},
CloseBracketTkn: &token.Token{
CloseCurlyBracketTkn: &token.Token{
ID: token.ID(125),
Value: []byte("}"),
Position: &position.Position{

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -5450,11 +5450,11 @@ encaps_var:
}
| T_CURLY_OPEN variable '}'
{
$$ = &ast.ParserBrackets{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $3),
OpenBracketTkn: $1,
Child: $2,
CloseBracketTkn: $3,
$$ = &ast.ScalarEncapsedStringBrackets{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $3),
OpenCurlyBracketTkn: $1,
Var: $2,
CloseCurlyBracketTkn: $3,
}
}
;

View File

@ -4332,14 +4332,14 @@ func TestScalarEncapsed_StringVarCurveOpen(t *testing.T) {
Value: []byte("$a"),
},
},
&ast.ParserBrackets{
&ast.ScalarEncapsedStringBrackets{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 7,
EndPos: 11,
},
OpenBracketTkn: &token.Token{
OpenCurlyBracketTkn: &token.Token{
ID: token.T_CURLY_OPEN,
Value: []byte("{"),
Position: &position.Position{
@ -4349,7 +4349,7 @@ func TestScalarEncapsed_StringVarCurveOpen(t *testing.T) {
EndPos: 8,
},
},
Child: &ast.ExprVariable{
Var: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
@ -4376,7 +4376,7 @@ func TestScalarEncapsed_StringVarCurveOpen(t *testing.T) {
Value: []byte("$b"),
},
},
CloseBracketTkn: &token.Token{
CloseCurlyBracketTkn: &token.Token{
ID: token.ID(125),
Value: []byte("}"),
Position: &position.Position{
@ -5047,14 +5047,14 @@ func TestScalarEncapsed_CurlyOpenMethodCall(t *testing.T) {
},
Value: []byte("test "),
},
&ast.ParserBrackets{
&ast.ScalarEncapsedStringBrackets{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 22,
},
OpenBracketTkn: &token.Token{
OpenCurlyBracketTkn: &token.Token{
ID: token.T_CURLY_OPEN,
Value: []byte("{"),
Position: &position.Position{
@ -5064,7 +5064,7 @@ func TestScalarEncapsed_CurlyOpenMethodCall(t *testing.T) {
EndPos: 10,
},
},
Child: &ast.ExprMethodCall{
Var: &ast.ExprMethodCall{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
@ -5148,7 +5148,7 @@ func TestScalarEncapsed_CurlyOpenMethodCall(t *testing.T) {
},
},
},
CloseBracketTkn: &token.Token{
CloseCurlyBracketTkn: &token.Token{
ID: token.ID(125),
Value: []byte("}"),
Position: &position.Position{

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -4208,11 +4208,11 @@ encaps_var:
}
| T_CURLY_OPEN variable '}'
{
$$ = &ast.ParserBrackets{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $3),
OpenBracketTkn: $1,
Child: $2,
CloseBracketTkn: $3,
$$ = &ast.ScalarEncapsedStringBrackets{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $3),
OpenCurlyBracketTkn: $1,
Var: $2,
CloseCurlyBracketTkn: $3,
}
}
;

View File

@ -176,6 +176,7 @@ type NodeVisitor interface {
ScalarEncapsed(n *ScalarEncapsed)
ScalarEncapsedStringPart(n *ScalarEncapsedStringPart)
ScalarEncapsedStringVar(n *ScalarEncapsedStringVar)
ScalarEncapsedStringBrackets(n *ScalarEncapsedStringBrackets)
ScalarHeredoc(n *ScalarHeredoc)
ScalarLnumber(n *ScalarLnumber)
ScalarMagicConstant(n *ScalarMagicConstant)
@ -185,6 +186,4 @@ type NodeVisitor interface {
NameFullyQualified(n *NameFullyQualified)
NameRelative(n *NameRelative)
NameNamePart(n *NameNamePart)
ParserBrackets(n *ParserBrackets)
}

View File

@ -150,6 +150,22 @@ func (n *ScalarEncapsedStringVar) GetPosition() *position.Position {
return n.Position
}
// ScalarEncapsedStringVar node
type ScalarEncapsedStringBrackets struct {
Position *position.Position
OpenCurlyBracketTkn *token.Token
Var Vertex
CloseCurlyBracketTkn *token.Token
}
func (n *ScalarEncapsedStringBrackets) Accept(v NodeVisitor) {
v.ScalarEncapsedStringBrackets(n)
}
func (n *ScalarEncapsedStringBrackets) GetPosition() *position.Position {
return n.Position
}
// ScalarHeredoc node
type ScalarHeredoc struct {
Position *position.Position
@ -2695,7 +2711,7 @@ type ParserBrackets struct {
}
func (n *ParserBrackets) Accept(v NodeVisitor) {
v.ParserBrackets(n)
// do nothing
}
func (n *ParserBrackets) GetPosition() *position.Position {

View File

@ -2472,6 +2472,18 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.Traverse(nn.Dim)
t.visitor.Leave("Dim", true)
}
case *ast.ScalarEncapsedStringBrackets:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.Var != nil {
t.visitor.Enter("Var", true)
t.Traverse(nn.Var)
t.visitor.Leave("Var", true)
}
case *ast.ScalarHeredoc:
if nn == nil {
return
@ -2556,18 +2568,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
if !t.visitor.EnterNode(nn) {
return
}
case *ast.ParserBrackets:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.Child != nil {
t.visitor.Enter("Child", true)
t.Traverse(nn.Child)
t.visitor.Leave("Child", true)
}
default:
panic("unexpected type of node")
}

View File

@ -2274,6 +2274,19 @@ func (v *Dumper) ScalarEncapsedStringVar(n *ast.ScalarEncapsedStringVar) {
v.print(v.indent, "},\n")
}
func (v *Dumper) ScalarEncapsedStringBrackets(n *ast.ScalarEncapsedStringBrackets) {
v.print(0, "&ast.ScalarEncapsedStringBrackets{\n")
v.indent++
v.dumpPosition(n.Position)
v.dumpToken("OpenCurlyBracketTkn", n.OpenCurlyBracketTkn)
v.dumpVertex("Var", n.Var)
v.dumpToken("CloseCurlyBracketTkn", n.CloseCurlyBracketTkn)
v.indent--
v.print(v.indent, "},\n")
}
func (v *Dumper) ScalarHeredoc(n *ast.ScalarHeredoc) {
v.print(0, "&ast.ScalarHeredoc{\n")
v.indent++
@ -2374,16 +2387,3 @@ func (v *Dumper) NameNamePart(n *ast.NameNamePart) {
v.indent--
v.print(v.indent, "},\n")
}
func (v *Dumper) ParserBrackets(n *ast.ParserBrackets) {
v.print(0, "&ast.ParserBrackets{\n")
v.indent++
v.dumpPosition(n.Position)
v.dumpToken("OpenBracketTkn", n.OpenBracketTkn)
v.dumpVertex("Child", n.Child)
v.dumpToken("CloseBracketTkn", n.CloseBracketTkn)
v.indent--
v.print(v.indent, "},\n")
}

View File

@ -1985,6 +1985,12 @@ func (f *formatter) ScalarEncapsedStringVar(n *ast.ScalarEncapsedStringVar) {
n.CloseCurlyBracketTkn = f.newToken('}', []byte("}"))
}
func (f *formatter) ScalarEncapsedStringBrackets(n *ast.ScalarEncapsedStringBrackets) {
n.OpenCurlyBracketTkn = f.newToken('{', []byte("{"))
n.Var.Accept(f)
n.CloseCurlyBracketTkn = f.newToken('}', []byte("}"))
}
func (f *formatter) ScalarHeredoc(n *ast.ScalarHeredoc) {
n.OpenHeredocTkn = f.newToken(token.T_START_HEREDOC, []byte("<<<EOT\n"))
for _, p := range n.Parts {
@ -2062,7 +2068,3 @@ func (f *formatter) NameNamePart(n *ast.NameNamePart) {
n.StringTkn.FreeFloating = f.getFreeFloating()
}
}
func (f *formatter) ParserBrackets(n *ast.ParserBrackets) {
// TODO
}

View File

@ -6694,6 +6694,31 @@ func TestFormatter_ScalarEncapsedStringVar_Dim(t *testing.T) {
}
}
func TestFormatter_ScalarEncapsedStringBrackets(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.ScalarEncapsedStringBrackets{
Var: &ast.ExprVariable{
VarName: &ast.Identifier{
Value: []byte("$foo"),
},
},
}
f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1)
n.Accept(f)
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n.Accept(p)
expected := `{$foo}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestFormatter_ScalarHeredoc(t *testing.T) {
o := bytes.NewBufferString("")

View File

@ -618,6 +618,10 @@ func (v *Null) ScalarEncapsedStringPart(_ *ast.ScalarEncapsedStringPart) {
// do nothing
}
func (v *Null) ScalarEncapsedStringBrackets(_ *ast.ScalarEncapsedStringBrackets) {
// do nothing
}
func (v *Null) ScalarEncapsedStringVar(_ *ast.ScalarEncapsedStringVar) {
// do nothing
}
@ -653,7 +657,3 @@ func (v *Null) NameRelative(_ *ast.NameRelative) {
func (v *Null) NameNamePart(_ *ast.NameNamePart) {
// do nothing
}
func (v *Null) ParserBrackets(_ *ast.ParserBrackets) {
// do nothing
}

View File

@ -1208,6 +1208,12 @@ func (p *printer) ScalarEncapsedStringVar(n *ast.ScalarEncapsedStringVar) {
p.printToken(n.CloseCurlyBracketTkn, []byte("}"))
}
func (p *printer) ScalarEncapsedStringBrackets(n *ast.ScalarEncapsedStringBrackets) {
p.printToken(n.OpenCurlyBracketTkn, []byte("{"))
p.printNode(n.Var)
p.printToken(n.CloseCurlyBracketTkn, []byte("}"))
}
func (p *printer) ScalarHeredoc(n *ast.ScalarHeredoc) {
p.printToken(n.OpenHeredocTkn, []byte("<<<EOT\n"))
p.printList(n.Parts)
@ -1245,9 +1251,3 @@ func (p *printer) NameRelative(n *ast.NameRelative) {
func (p *printer) NameNamePart(n *ast.NameNamePart) {
p.printToken(n.StringTkn, n.Value)
}
func (p *printer) ParserBrackets(n *ast.ParserBrackets) {
p.printToken(n.OpenBracketTkn, nil)
p.printNode(n.Child)
p.printToken(n.CloseBracketTkn, nil)
}