refactoring: update ast structure of "Variable" node
This commit is contained in:
parent
69919594fd
commit
2c09138600
@ -16726,90 +16726,82 @@ func TestStmtGlobal_Vars(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
VarName: &ast.ParserBrackets{
|
||||
OpenCurlyBracketTkn: &token.Token{
|
||||
ID: token.ID(123),
|
||||
Value: []byte("{"),
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 24,
|
||||
EndPos: 31,
|
||||
EndPos: 25,
|
||||
},
|
||||
OpenBracketTkn: &token.Token{
|
||||
ID: token.ID(123),
|
||||
Value: []byte("{"),
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 24,
|
||||
EndPos: 25,
|
||||
},
|
||||
},
|
||||
VarName: &ast.ExprFunctionCall{
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 25,
|
||||
EndPos: 30,
|
||||
},
|
||||
Child: &ast.ExprFunctionCall{
|
||||
Function: &ast.NameName{
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 25,
|
||||
EndPos: 30,
|
||||
EndPos: 28,
|
||||
},
|
||||
Function: &ast.NameName{
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 25,
|
||||
EndPos: 28,
|
||||
},
|
||||
Parts: []ast.Vertex{
|
||||
&ast.NameNamePart{
|
||||
Parts: []ast.Vertex{
|
||||
&ast.NameNamePart{
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 25,
|
||||
EndPos: 28,
|
||||
},
|
||||
StringTkn: &token.Token{
|
||||
ID: token.T_STRING,
|
||||
Value: []byte("foo"),
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 25,
|
||||
EndPos: 28,
|
||||
},
|
||||
StringTkn: &token.Token{
|
||||
ID: token.T_STRING,
|
||||
Value: []byte("foo"),
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 25,
|
||||
EndPos: 28,
|
||||
},
|
||||
},
|
||||
Value: []byte("foo"),
|
||||
},
|
||||
},
|
||||
},
|
||||
OpenParenthesisTkn: &token.Token{
|
||||
ID: token.ID(40),
|
||||
Value: []byte("("),
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 28,
|
||||
EndPos: 29,
|
||||
},
|
||||
},
|
||||
CloseParenthesisTkn: &token.Token{
|
||||
ID: token.ID(41),
|
||||
Value: []byte(")"),
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 29,
|
||||
EndPos: 30,
|
||||
Value: []byte("foo"),
|
||||
},
|
||||
},
|
||||
},
|
||||
CloseBracketTkn: &token.Token{
|
||||
ID: token.ID(125),
|
||||
Value: []byte("}"),
|
||||
OpenParenthesisTkn: &token.Token{
|
||||
ID: token.ID(40),
|
||||
Value: []byte("("),
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 30,
|
||||
EndPos: 31,
|
||||
StartPos: 28,
|
||||
EndPos: 29,
|
||||
},
|
||||
},
|
||||
CloseParenthesisTkn: &token.Token{
|
||||
ID: token.ID(41),
|
||||
Value: []byte(")"),
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 29,
|
||||
EndPos: 30,
|
||||
},
|
||||
},
|
||||
},
|
||||
CloseCurlyBracketTkn: &token.Token{
|
||||
ID: token.ID(125),
|
||||
Value: []byte("}"),
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 30,
|
||||
EndPos: 31,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
BIN
internal/php5/php5.go
generated
BIN
internal/php5/php5.go
generated
Binary file not shown.
@ -2022,14 +2022,11 @@ global_var:
|
||||
| '$' '{' expr '}'
|
||||
{
|
||||
$$ = &ast.ExprVariable{
|
||||
Position: yylex.(*Parser).builder.NewTokensPosition($1, $4),
|
||||
DollarTkn: $1,
|
||||
VarName: &ast.ParserBrackets{
|
||||
Position: yylex.(*Parser).builder.NewTokensPosition($2, $4),
|
||||
OpenBracketTkn: $2,
|
||||
Child: $3,
|
||||
CloseBracketTkn: $4,
|
||||
},
|
||||
Position: yylex.(*Parser).builder.NewTokensPosition($1, $4),
|
||||
DollarTkn: $1,
|
||||
OpenCurlyBracketTkn: $2,
|
||||
VarName: $3,
|
||||
CloseCurlyBracketTkn: $4,
|
||||
}
|
||||
}
|
||||
;
|
||||
@ -5017,14 +5014,11 @@ compound_variable:
|
||||
| '$' '{' expr '}'
|
||||
{
|
||||
$$ = &ast.ExprVariable{
|
||||
Position: yylex.(*Parser).builder.NewTokensPosition($1, $4),
|
||||
DollarTkn: $1,
|
||||
VarName: &ast.ParserBrackets{
|
||||
Position: yylex.(*Parser).builder.NewTokensPosition($2, $4),
|
||||
OpenBracketTkn: $2,
|
||||
Child: $3,
|
||||
CloseBracketTkn: $4,
|
||||
},
|
||||
Position: yylex.(*Parser).builder.NewTokensPosition($1, $4),
|
||||
DollarTkn: $1,
|
||||
OpenCurlyBracketTkn: $2,
|
||||
VarName: $3,
|
||||
CloseCurlyBracketTkn: $4,
|
||||
}
|
||||
}
|
||||
;
|
||||
|
@ -18412,90 +18412,82 @@ func TestStmtGlobal_Vars(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
VarName: &ast.ParserBrackets{
|
||||
OpenCurlyBracketTkn: &token.Token{
|
||||
ID: token.ID(123),
|
||||
Value: []byte("{"),
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 24,
|
||||
EndPos: 31,
|
||||
EndPos: 25,
|
||||
},
|
||||
OpenBracketTkn: &token.Token{
|
||||
ID: token.ID(123),
|
||||
Value: []byte("{"),
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 24,
|
||||
EndPos: 25,
|
||||
},
|
||||
},
|
||||
VarName: &ast.ExprFunctionCall{
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 25,
|
||||
EndPos: 30,
|
||||
},
|
||||
Child: &ast.ExprFunctionCall{
|
||||
Function: &ast.NameName{
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 25,
|
||||
EndPos: 30,
|
||||
EndPos: 28,
|
||||
},
|
||||
Function: &ast.NameName{
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 25,
|
||||
EndPos: 28,
|
||||
},
|
||||
Parts: []ast.Vertex{
|
||||
&ast.NameNamePart{
|
||||
Parts: []ast.Vertex{
|
||||
&ast.NameNamePart{
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 25,
|
||||
EndPos: 28,
|
||||
},
|
||||
StringTkn: &token.Token{
|
||||
ID: token.T_STRING,
|
||||
Value: []byte("foo"),
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 25,
|
||||
EndPos: 28,
|
||||
},
|
||||
StringTkn: &token.Token{
|
||||
ID: token.T_STRING,
|
||||
Value: []byte("foo"),
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 25,
|
||||
EndPos: 28,
|
||||
},
|
||||
},
|
||||
Value: []byte("foo"),
|
||||
},
|
||||
},
|
||||
},
|
||||
OpenParenthesisTkn: &token.Token{
|
||||
ID: token.ID(40),
|
||||
Value: []byte("("),
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 28,
|
||||
EndPos: 29,
|
||||
},
|
||||
},
|
||||
CloseParenthesisTkn: &token.Token{
|
||||
ID: token.ID(41),
|
||||
Value: []byte(")"),
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 29,
|
||||
EndPos: 30,
|
||||
Value: []byte("foo"),
|
||||
},
|
||||
},
|
||||
},
|
||||
CloseBracketTkn: &token.Token{
|
||||
ID: token.ID(125),
|
||||
Value: []byte("}"),
|
||||
OpenParenthesisTkn: &token.Token{
|
||||
ID: token.ID(40),
|
||||
Value: []byte("("),
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 30,
|
||||
EndPos: 31,
|
||||
StartPos: 28,
|
||||
EndPos: 29,
|
||||
},
|
||||
},
|
||||
CloseParenthesisTkn: &token.Token{
|
||||
ID: token.ID(41),
|
||||
Value: []byte(")"),
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 29,
|
||||
EndPos: 30,
|
||||
},
|
||||
},
|
||||
},
|
||||
CloseCurlyBracketTkn: &token.Token{
|
||||
ID: token.ID(125),
|
||||
Value: []byte("}"),
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 30,
|
||||
EndPos: 31,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
BIN
internal/php7/php7.go
generated
BIN
internal/php7/php7.go
generated
Binary file not shown.
@ -3790,14 +3790,11 @@ simple_variable:
|
||||
| '$' '{' expr '}'
|
||||
{
|
||||
$$ = &ast.ExprVariable{
|
||||
Position: yylex.(*Parser).builder.NewTokensPosition($1, $4),
|
||||
DollarTkn: $1,
|
||||
VarName: &ast.ParserBrackets{
|
||||
Position: yylex.(*Parser).builder.NewTokensPosition($2, $4),
|
||||
OpenBracketTkn: $2,
|
||||
Child: $3,
|
||||
CloseBracketTkn: $4,
|
||||
},
|
||||
Position: yylex.(*Parser).builder.NewTokensPosition($1, $4),
|
||||
DollarTkn: $1,
|
||||
OpenCurlyBracketTkn: $2,
|
||||
VarName: $3,
|
||||
CloseCurlyBracketTkn: $4,
|
||||
}
|
||||
}
|
||||
| '$' simple_variable
|
||||
|
@ -1761,9 +1761,11 @@ func (n *ExprUnaryPlus) GetPosition() *position.Position {
|
||||
|
||||
// ExprVariable node
|
||||
type ExprVariable struct {
|
||||
Position *position.Position
|
||||
DollarTkn *token.Token
|
||||
VarName Vertex
|
||||
Position *position.Position
|
||||
DollarTkn *token.Token
|
||||
OpenCurlyBracketTkn *token.Token
|
||||
VarName Vertex
|
||||
CloseCurlyBracketTkn *token.Token
|
||||
}
|
||||
|
||||
func (n *ExprVariable) Accept(v NodeVisitor) {
|
||||
|
@ -1537,7 +1537,9 @@ func (v *Dumper) ExprVariable(n *ast.ExprVariable) {
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpToken("DollarTkn", n.DollarTkn)
|
||||
v.dumpToken("OpenCurlyBracketTkn", n.OpenCurlyBracketTkn)
|
||||
v.dumpVertex("VarName", n.VarName)
|
||||
v.dumpToken("CloseCurlyBracketTkn", n.CloseCurlyBracketTkn)
|
||||
|
||||
v.indent--
|
||||
v.print(v.indent, "},\n")
|
||||
|
@ -1417,6 +1417,16 @@ func (f *formatter) ExprVariable(n *ast.ExprVariable) {
|
||||
n.DollarTkn = f.newToken('$', []byte("$"))
|
||||
}
|
||||
|
||||
n.OpenCurlyBracketTkn = nil
|
||||
n.CloseCurlyBracketTkn = nil
|
||||
switch n.VarName.(type) {
|
||||
case *ast.Identifier:
|
||||
case *ast.ExprVariable:
|
||||
default:
|
||||
n.OpenCurlyBracketTkn = f.newToken('{', []byte("{"))
|
||||
n.CloseCurlyBracketTkn = f.newToken('}', []byte("}"))
|
||||
}
|
||||
|
||||
n.VarName.Accept(f)
|
||||
}
|
||||
|
||||
|
@ -4877,6 +4877,29 @@ func TestFormatter_ExprVariable_Variable(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatter_ExprVariable_Expression(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
n := &ast.ExprVariable{
|
||||
VarName: &ast.ScalarString{
|
||||
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_ExprYield(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
|
@ -868,7 +868,9 @@ func (p *printer) ExprUnaryPlus(n *ast.ExprUnaryPlus) {
|
||||
|
||||
func (p *printer) ExprVariable(n *ast.ExprVariable) {
|
||||
p.printToken(n.DollarTkn, nil)
|
||||
p.printToken(n.OpenCurlyBracketTkn, nil)
|
||||
p.printNode(n.VarName)
|
||||
p.printToken(n.CloseCurlyBracketTkn, nil)
|
||||
}
|
||||
|
||||
func (p *printer) ExprYield(n *ast.ExprYield) {
|
||||
|
@ -2609,13 +2609,19 @@ func TestPrinterPrintVariable(t *testing.T) {
|
||||
DollarTkn: &token.Token{
|
||||
Value: []byte("$"),
|
||||
},
|
||||
OpenCurlyBracketTkn: &token.Token{
|
||||
Value: []byte("{"),
|
||||
},
|
||||
VarName: &ast.ExprVariable{
|
||||
VarName: &ast.Identifier{Value: []byte("$var")},
|
||||
},
|
||||
CloseCurlyBracketTkn: &token.Token{
|
||||
Value: []byte("}"),
|
||||
},
|
||||
}
|
||||
n.Accept(p)
|
||||
|
||||
expected := `$$var`
|
||||
expected := `${$var}`
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
|
Loading…
Reference in New Issue
Block a user