refactoring: create "ScalarEncapsedStringVar" node

This commit is contained in:
Vadym Slizov 2020-12-28 00:38:39 +02:00
parent e4321b5e90
commit 38141ce97f
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
14 changed files with 272 additions and 214 deletions

View File

@ -4335,14 +4335,14 @@ func TestScalarEncapsed_DollarOpenCurlyBraces(t *testing.T) {
}, },
Value: []byte("test "), Value: []byte("test "),
}, },
&ast.ParserBrackets{ &ast.ScalarEncapsedStringVar{
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
EndLine: 1, EndLine: 1,
StartPos: 9, StartPos: 9,
EndPos: 15, EndPos: 15,
}, },
OpenBracketTkn: &token.Token{ DollarOpenCurlyBracketTkn: &token.Token{
ID: token.T_DOLLAR_OPEN_CURLY_BRACES, ID: token.T_DOLLAR_OPEN_CURLY_BRACES,
Value: []byte("${"), Value: []byte("${"),
Position: &position.Position{ Position: &position.Position{
@ -4352,13 +4352,6 @@ func TestScalarEncapsed_DollarOpenCurlyBraces(t *testing.T) {
EndPos: 11, EndPos: 11,
}, },
}, },
Child: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 14,
},
VarName: &ast.Identifier{ VarName: &ast.Identifier{
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -4378,8 +4371,7 @@ func TestScalarEncapsed_DollarOpenCurlyBraces(t *testing.T) {
}, },
Value: []byte("foo"), Value: []byte("foo"),
}, },
}, CloseCurlyBracketTkn: &token.Token{
CloseBracketTkn: &token.Token{
ID: token.ID(125), ID: token.ID(125),
Value: []byte("}"), Value: []byte("}"),
Position: &position.Position{ Position: &position.Position{
@ -4501,14 +4493,14 @@ func TestScalarEncapsed_DollarOpenCurlyBracesDimNumber(t *testing.T) {
}, },
Value: []byte("test "), Value: []byte("test "),
}, },
&ast.ParserBrackets{ &ast.ScalarEncapsedStringVar{
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
EndLine: 1, EndLine: 1,
StartPos: 9, StartPos: 9,
EndPos: 18, EndPos: 15,
}, },
OpenBracketTkn: &token.Token{ DollarOpenCurlyBracketTkn: &token.Token{
ID: token.T_DOLLAR_OPEN_CURLY_BRACES, ID: token.T_DOLLAR_OPEN_CURLY_BRACES,
Value: []byte("${"), Value: []byte("${"),
Position: &position.Position{ Position: &position.Position{
@ -4518,20 +4510,6 @@ func TestScalarEncapsed_DollarOpenCurlyBracesDimNumber(t *testing.T) {
EndPos: 11, EndPos: 11,
}, },
}, },
Child: &ast.ExprArrayDimFetch{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 17,
},
Var: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 14,
},
VarName: &ast.Identifier{ VarName: &ast.Identifier{
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -4551,8 +4529,7 @@ func TestScalarEncapsed_DollarOpenCurlyBracesDimNumber(t *testing.T) {
}, },
Value: []byte("foo"), Value: []byte("foo"),
}, },
}, OpenSquareBracketTkn: &token.Token{
OpenBracketTkn: &token.Token{
ID: token.ID(91), ID: token.ID(91),
Value: []byte("["), Value: []byte("["),
Position: &position.Position{ Position: &position.Position{
@ -4581,7 +4558,7 @@ func TestScalarEncapsed_DollarOpenCurlyBracesDimNumber(t *testing.T) {
}, },
Value: []byte("0"), Value: []byte("0"),
}, },
CloseBracketTkn: &token.Token{ CloseSquareBracketTkn: &token.Token{
ID: token.ID(93), ID: token.ID(93),
Value: []byte("]"), Value: []byte("]"),
Position: &position.Position{ Position: &position.Position{
@ -4591,8 +4568,7 @@ func TestScalarEncapsed_DollarOpenCurlyBracesDimNumber(t *testing.T) {
EndPos: 17, EndPos: 17,
}, },
}, },
}, CloseCurlyBracketTkn: &token.Token{
CloseBracketTkn: &token.Token{
ID: token.ID(125), ID: token.ID(125),
Value: []byte("}"), Value: []byte("}"),
Position: &position.Position{ Position: &position.Position{

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -5412,52 +5412,40 @@ encaps_var:
} }
| T_DOLLAR_OPEN_CURLY_BRACES expr '}' | T_DOLLAR_OPEN_CURLY_BRACES expr '}'
{ {
$$ = &ast.ParserBrackets{ $$ = &ast.ScalarEncapsedStringVar{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $3), Position: yylex.(*Parser).builder.NewTokensPosition($1, $3),
OpenBracketTkn: $1, DollarOpenCurlyBracketTkn: $1,
Child: &ast.ExprVariable{
Position: yylex.(*Parser).builder.NewNodePosition($2),
VarName: $2, VarName: $2,
}, CloseCurlyBracketTkn: $3,
CloseBracketTkn: $3,
} }
} }
| T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '}' | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '}'
{ {
$$ = &ast.ParserBrackets{ $$ = &ast.ScalarEncapsedStringVar{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $3), Position: yylex.(*Parser).builder.NewTokensPosition($1, $3),
OpenBracketTkn: $1, DollarOpenCurlyBracketTkn: $1,
Child: &ast.ExprVariable{
Position: yylex.(*Parser).builder.NewTokenPosition($2),
VarName: &ast.Identifier{ VarName: &ast.Identifier{
Position: yylex.(*Parser).builder.NewTokenPosition($2), Position: yylex.(*Parser).builder.NewTokenPosition($2),
IdentifierTkn: $2, IdentifierTkn: $2,
Value: $2.Value, Value: $2.Value,
}, },
}, CloseCurlyBracketTkn: $3,
CloseBracketTkn: $3,
} }
} }
| T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}' | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}'
{ {
$$ = &ast.ParserBrackets{ $$ = &ast.ScalarEncapsedStringVar{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $6), Position: yylex.(*Parser).builder.NewTokensPosition($1, $3),
OpenBracketTkn: $1, DollarOpenCurlyBracketTkn: $1,
Child: &ast.ExprArrayDimFetch{
Position: yylex.(*Parser).builder.NewTokensPosition($2, $5),
Var: &ast.ExprVariable{
Position: yylex.(*Parser).builder.NewTokenPosition($2),
VarName: &ast.Identifier{ VarName: &ast.Identifier{
Position: yylex.(*Parser).builder.NewTokenPosition($2), Position: yylex.(*Parser).builder.NewTokenPosition($2),
IdentifierTkn: $2, IdentifierTkn: $2,
Value: $2.Value, Value: $2.Value,
}, },
}, OpenSquareBracketTkn: $3,
OpenBracketTkn: $3,
Dim: $4, Dim: $4,
CloseBracketTkn: $5, CloseSquareBracketTkn: $5,
}, CloseCurlyBracketTkn: $6,
CloseBracketTkn: $6,
} }
} }
| T_CURLY_OPEN variable '}' | T_CURLY_OPEN variable '}'

View File

@ -4692,14 +4692,14 @@ func TestScalarEncapsed_DollarOpenCurlyBraces(t *testing.T) {
}, },
Value: []byte("test "), Value: []byte("test "),
}, },
&ast.ParserBrackets{ &ast.ScalarEncapsedStringVar{
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
EndLine: 1, EndLine: 1,
StartPos: 9, StartPos: 9,
EndPos: 15, EndPos: 15,
}, },
OpenBracketTkn: &token.Token{ DollarOpenCurlyBracketTkn: &token.Token{
ID: token.T_DOLLAR_OPEN_CURLY_BRACES, ID: token.T_DOLLAR_OPEN_CURLY_BRACES,
Value: []byte("${"), Value: []byte("${"),
Position: &position.Position{ Position: &position.Position{
@ -4709,13 +4709,6 @@ func TestScalarEncapsed_DollarOpenCurlyBraces(t *testing.T) {
EndPos: 11, EndPos: 11,
}, },
}, },
Child: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 14,
},
VarName: &ast.Identifier{ VarName: &ast.Identifier{
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -4735,8 +4728,7 @@ func TestScalarEncapsed_DollarOpenCurlyBraces(t *testing.T) {
}, },
Value: []byte("foo"), Value: []byte("foo"),
}, },
}, CloseCurlyBracketTkn: &token.Token{
CloseBracketTkn: &token.Token{
ID: token.ID(125), ID: token.ID(125),
Value: []byte("}"), Value: []byte("}"),
Position: &position.Position{ Position: &position.Position{
@ -4858,14 +4850,14 @@ func TestScalarEncapsed_DollarOpenCurlyBracesDimNumber(t *testing.T) {
}, },
Value: []byte("test "), Value: []byte("test "),
}, },
&ast.ParserBrackets{ &ast.ScalarEncapsedStringVar{
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
EndLine: 1, EndLine: 1,
StartPos: 9, StartPos: 9,
EndPos: 18, EndPos: 15,
}, },
OpenBracketTkn: &token.Token{ DollarOpenCurlyBracketTkn: &token.Token{
ID: token.T_DOLLAR_OPEN_CURLY_BRACES, ID: token.T_DOLLAR_OPEN_CURLY_BRACES,
Value: []byte("${"), Value: []byte("${"),
Position: &position.Position{ Position: &position.Position{
@ -4875,20 +4867,6 @@ func TestScalarEncapsed_DollarOpenCurlyBracesDimNumber(t *testing.T) {
EndPos: 11, EndPos: 11,
}, },
}, },
Child: &ast.ExprArrayDimFetch{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 17,
},
Var: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 14,
},
VarName: &ast.Identifier{ VarName: &ast.Identifier{
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -4908,8 +4886,7 @@ func TestScalarEncapsed_DollarOpenCurlyBracesDimNumber(t *testing.T) {
}, },
Value: []byte("foo"), Value: []byte("foo"),
}, },
}, OpenSquareBracketTkn: &token.Token{
OpenBracketTkn: &token.Token{
ID: token.ID(91), ID: token.ID(91),
Value: []byte("["), Value: []byte("["),
Position: &position.Position{ Position: &position.Position{
@ -4938,7 +4915,7 @@ func TestScalarEncapsed_DollarOpenCurlyBracesDimNumber(t *testing.T) {
}, },
Value: []byte("0"), Value: []byte("0"),
}, },
CloseBracketTkn: &token.Token{ CloseSquareBracketTkn: &token.Token{
ID: token.ID(93), ID: token.ID(93),
Value: []byte("]"), Value: []byte("]"),
Position: &position.Position{ Position: &position.Position{
@ -4948,8 +4925,7 @@ func TestScalarEncapsed_DollarOpenCurlyBracesDimNumber(t *testing.T) {
EndPos: 17, EndPos: 17,
}, },
}, },
}, CloseCurlyBracketTkn: &token.Token{
CloseBracketTkn: &token.Token{
ID: token.ID(125), ID: token.ID(125),
Value: []byte("}"), Value: []byte("}"),
Position: &position.Position{ Position: &position.Position{

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -4170,52 +4170,40 @@ encaps_var:
} }
| T_DOLLAR_OPEN_CURLY_BRACES expr '}' | T_DOLLAR_OPEN_CURLY_BRACES expr '}'
{ {
$$ = &ast.ParserBrackets{ $$ = &ast.ScalarEncapsedStringVar{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $3), Position: yylex.(*Parser).builder.NewTokensPosition($1, $3),
OpenBracketTkn: $1, DollarOpenCurlyBracketTkn: $1,
Child: &ast.ExprVariable{
Position: yylex.(*Parser).builder.NewNodePosition($2),
VarName: $2, VarName: $2,
}, CloseCurlyBracketTkn: $3,
CloseBracketTkn: $3,
} }
} }
| T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '}' | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '}'
{ {
$$ = &ast.ParserBrackets{ $$ = &ast.ScalarEncapsedStringVar{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $3), Position: yylex.(*Parser).builder.NewTokensPosition($1, $3),
OpenBracketTkn: $1, DollarOpenCurlyBracketTkn: $1,
Child: &ast.ExprVariable{
Position: yylex.(*Parser).builder.NewTokenPosition($2),
VarName: &ast.Identifier{ VarName: &ast.Identifier{
Position: yylex.(*Parser).builder.NewTokenPosition($2), Position: yylex.(*Parser).builder.NewTokenPosition($2),
IdentifierTkn: $2, IdentifierTkn: $2,
Value: $2.Value, Value: $2.Value,
}, },
}, CloseCurlyBracketTkn: $3,
CloseBracketTkn: $3,
} }
} }
| T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}' | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}'
{ {
$$ = &ast.ParserBrackets{ $$ = &ast.ScalarEncapsedStringVar{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $6), Position: yylex.(*Parser).builder.NewTokensPosition($1, $3),
OpenBracketTkn: $1, DollarOpenCurlyBracketTkn: $1,
Child: &ast.ExprArrayDimFetch{
Position: yylex.(*Parser).builder.NewTokensPosition($2, $5),
Var: &ast.ExprVariable{
Position: yylex.(*Parser).builder.NewTokenPosition($2),
VarName: &ast.Identifier{ VarName: &ast.Identifier{
Position: yylex.(*Parser).builder.NewTokenPosition($2), Position: yylex.(*Parser).builder.NewTokenPosition($2),
IdentifierTkn: $2, IdentifierTkn: $2,
Value: $2.Value, Value: $2.Value,
}, },
}, OpenSquareBracketTkn: $3,
OpenBracketTkn: $3,
Dim: $4, Dim: $4,
CloseBracketTkn: $5, CloseSquareBracketTkn: $5,
}, CloseCurlyBracketTkn: $6,
CloseBracketTkn: $6,
} }
} }
| T_CURLY_OPEN variable '}' | T_CURLY_OPEN variable '}'

View File

@ -175,6 +175,7 @@ type NodeVisitor interface {
ScalarDnumber(n *ScalarDnumber) ScalarDnumber(n *ScalarDnumber)
ScalarEncapsed(n *ScalarEncapsed) ScalarEncapsed(n *ScalarEncapsed)
ScalarEncapsedStringPart(n *ScalarEncapsedStringPart) ScalarEncapsedStringPart(n *ScalarEncapsedStringPart)
ScalarEncapsedStringVar(n *ScalarEncapsedStringVar)
ScalarHeredoc(n *ScalarHeredoc) ScalarHeredoc(n *ScalarHeredoc)
ScalarLnumber(n *ScalarLnumber) ScalarLnumber(n *ScalarLnumber)
ScalarMagicConstant(n *ScalarMagicConstant) ScalarMagicConstant(n *ScalarMagicConstant)

View File

@ -131,6 +131,25 @@ func (n *ScalarEncapsedStringPart) GetPosition() *position.Position {
return n.Position return n.Position
} }
// ScalarEncapsedStringVar node
type ScalarEncapsedStringVar struct {
Position *position.Position
DollarOpenCurlyBracketTkn *token.Token
VarName Vertex
OpenSquareBracketTkn *token.Token
Dim Vertex
CloseSquareBracketTkn *token.Token
CloseCurlyBracketTkn *token.Token
}
func (n *ScalarEncapsedStringVar) Accept(v NodeVisitor) {
v.ScalarEncapsedStringVar(n)
}
func (n *ScalarEncapsedStringVar) GetPosition() *position.Position {
return n.Position
}
// ScalarHeredoc node // ScalarHeredoc node
type ScalarHeredoc struct { type ScalarHeredoc struct {
Position *position.Position Position *position.Position

View File

@ -2455,6 +2455,23 @@ func (t *DFS) Traverse(n ast.Vertex) {
if !t.visitor.EnterNode(nn) { if !t.visitor.EnterNode(nn) {
return return
} }
case *ast.ScalarEncapsedStringVar:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.VarName != nil {
t.visitor.Enter("VarName", true)
t.Traverse(nn.VarName)
t.visitor.Leave("VarName", true)
}
if nn.Dim != nil {
t.visitor.Enter("Dim", true)
t.Traverse(nn.Dim)
t.visitor.Leave("Dim", true)
}
case *ast.ScalarHeredoc: case *ast.ScalarHeredoc:
if nn == nil { if nn == nil {
return return

View File

@ -2258,6 +2258,22 @@ func (v *Dumper) ScalarEncapsedStringPart(n *ast.ScalarEncapsedStringPart) {
v.print(v.indent, "},\n") v.print(v.indent, "},\n")
} }
func (v *Dumper) ScalarEncapsedStringVar(n *ast.ScalarEncapsedStringVar) {
v.print(0, "&ast.ScalarEncapsedStringVar{\n")
v.indent++
v.dumpPosition(n.Position)
v.dumpToken("DollarOpenCurlyBracketTkn", n.DollarOpenCurlyBracketTkn)
v.dumpVertex("VarName", n.VarName)
v.dumpToken("OpenSquareBracketTkn", n.OpenSquareBracketTkn)
v.dumpVertex("Dim", n.Dim)
v.dumpToken("CloseSquareBracketTkn", n.CloseSquareBracketTkn)
v.dumpToken("CloseCurlyBracketTkn", n.CloseCurlyBracketTkn)
v.indent--
v.print(v.indent, "},\n")
}
func (v *Dumper) ScalarHeredoc(n *ast.ScalarHeredoc) { func (v *Dumper) ScalarHeredoc(n *ast.ScalarHeredoc) {
v.print(0, "&ast.ScalarHeredoc{\n") v.print(0, "&ast.ScalarHeredoc{\n")
v.indent++ v.indent++

View File

@ -1970,6 +1970,21 @@ func (f *formatter) ScalarEncapsedStringPart(n *ast.ScalarEncapsedStringPart) {
} }
} }
func (f *formatter) ScalarEncapsedStringVar(n *ast.ScalarEncapsedStringVar) {
n.DollarOpenCurlyBracketTkn = f.newToken(token.T_DOLLAR_OPEN_CURLY_BRACES, []byte("${"))
n.VarName.Accept(f)
n.OpenSquareBracketTkn = nil
n.CloseSquareBracketTkn = nil
if n.Dim != nil {
n.OpenSquareBracketTkn = f.newToken('[', []byte("["))
n.Dim.Accept(f)
n.CloseSquareBracketTkn = f.newToken(']', []byte("]"))
}
n.CloseCurlyBracketTkn = f.newToken('}', []byte("}"))
}
func (f *formatter) ScalarHeredoc(n *ast.ScalarHeredoc) { func (f *formatter) ScalarHeredoc(n *ast.ScalarHeredoc) {
n.OpenHeredocTkn = f.newToken(token.T_START_HEREDOC, []byte("<<<EOT\n")) n.OpenHeredocTkn = f.newToken(token.T_START_HEREDOC, []byte("<<<EOT\n"))
for _, p := range n.Parts { for _, p := range n.Parts {

View File

@ -6645,6 +6645,55 @@ func TestFormatter_ScalarEncapsedStringPart(t *testing.T) {
} }
} }
func TestFormatter_ScalarEncapsedStringVar(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.ScalarEncapsedStringVar{
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_ScalarEncapsedStringVar_Dim(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.ScalarEncapsedStringVar{
VarName: &ast.Identifier{
Value: []byte("foo"),
},
Dim: &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_ScalarHeredoc(t *testing.T) { func TestFormatter_ScalarHeredoc(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")

View File

@ -618,6 +618,10 @@ func (v *Null) ScalarEncapsedStringPart(_ *ast.ScalarEncapsedStringPart) {
// do nothing // do nothing
} }
func (v *Null) ScalarEncapsedStringVar(_ *ast.ScalarEncapsedStringVar) {
// do nothing
}
func (v *Null) ScalarHeredoc(_ *ast.ScalarHeredoc) { func (v *Null) ScalarHeredoc(_ *ast.ScalarHeredoc) {
// do nothing // do nothing
} }

View File

@ -1199,6 +1199,15 @@ func (p *printer) ScalarEncapsedStringPart(n *ast.ScalarEncapsedStringPart) {
p.printToken(n.EncapsedStrTkn, n.Value) p.printToken(n.EncapsedStrTkn, n.Value)
} }
func (p *printer) ScalarEncapsedStringVar(n *ast.ScalarEncapsedStringVar) {
p.printToken(n.DollarOpenCurlyBracketTkn, []byte("${"))
p.printNode(n.VarName)
p.printToken(n.OpenSquareBracketTkn, p.ifNode(n.Dim, []byte("[")))
p.printNode(n.Dim)
p.printToken(n.CloseSquareBracketTkn, p.ifNode(n.Dim, []byte("]")))
p.printToken(n.CloseCurlyBracketTkn, []byte("}"))
}
func (p *printer) ScalarHeredoc(n *ast.ScalarHeredoc) { func (p *printer) ScalarHeredoc(n *ast.ScalarHeredoc) {
p.printToken(n.OpenHeredocTkn, []byte("<<<EOT\n")) p.printToken(n.OpenHeredocTkn, []byte("<<<EOT\n"))
p.printList(n.Parts) p.printList(n.Parts)