refactoring: update ast structure of "Variable" node
This commit is contained in:
parent
69919594fd
commit
2c09138600
@ -16726,14 +16726,7 @@ func TestStmtGlobal_Vars(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
VarName: &ast.ParserBrackets{
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 24,
|
||||
EndPos: 31,
|
||||
},
|
||||
OpenBracketTkn: &token.Token{
|
||||
OpenCurlyBracketTkn: &token.Token{
|
||||
ID: token.ID(123),
|
||||
Value: []byte("{"),
|
||||
Position: &position.Position{
|
||||
@ -16743,7 +16736,7 @@ func TestStmtGlobal_Vars(t *testing.T) {
|
||||
EndPos: 25,
|
||||
},
|
||||
},
|
||||
Child: &ast.ExprFunctionCall{
|
||||
VarName: &ast.ExprFunctionCall{
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
@ -16800,7 +16793,7 @@ func TestStmtGlobal_Vars(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
CloseBracketTkn: &token.Token{
|
||||
CloseCurlyBracketTkn: &token.Token{
|
||||
ID: token.ID(125),
|
||||
Value: []byte("}"),
|
||||
Position: &position.Position{
|
||||
@ -16812,7 +16805,6 @@ func TestStmtGlobal_Vars(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
SeparatorTkns: []*token.Token{
|
||||
{
|
||||
ID: token.ID(44),
|
||||
|
BIN
internal/php5/php5.go
generated
BIN
internal/php5/php5.go
generated
Binary file not shown.
@ -2024,12 +2024,9 @@ global_var:
|
||||
$$ = &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,
|
||||
},
|
||||
OpenCurlyBracketTkn: $2,
|
||||
VarName: $3,
|
||||
CloseCurlyBracketTkn: $4,
|
||||
}
|
||||
}
|
||||
;
|
||||
@ -5019,12 +5016,9 @@ compound_variable:
|
||||
$$ = &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,
|
||||
},
|
||||
OpenCurlyBracketTkn: $2,
|
||||
VarName: $3,
|
||||
CloseCurlyBracketTkn: $4,
|
||||
}
|
||||
}
|
||||
;
|
||||
|
@ -18412,14 +18412,7 @@ func TestStmtGlobal_Vars(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
VarName: &ast.ParserBrackets{
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
StartPos: 24,
|
||||
EndPos: 31,
|
||||
},
|
||||
OpenBracketTkn: &token.Token{
|
||||
OpenCurlyBracketTkn: &token.Token{
|
||||
ID: token.ID(123),
|
||||
Value: []byte("{"),
|
||||
Position: &position.Position{
|
||||
@ -18429,7 +18422,7 @@ func TestStmtGlobal_Vars(t *testing.T) {
|
||||
EndPos: 25,
|
||||
},
|
||||
},
|
||||
Child: &ast.ExprFunctionCall{
|
||||
VarName: &ast.ExprFunctionCall{
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 1,
|
||||
@ -18486,7 +18479,7 @@ func TestStmtGlobal_Vars(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
CloseBracketTkn: &token.Token{
|
||||
CloseCurlyBracketTkn: &token.Token{
|
||||
ID: token.ID(125),
|
||||
Value: []byte("}"),
|
||||
Position: &position.Position{
|
||||
@ -18498,7 +18491,6 @@ func TestStmtGlobal_Vars(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
SeparatorTkns: []*token.Token{
|
||||
{
|
||||
ID: token.ID(44),
|
||||
|
BIN
internal/php7/php7.go
generated
BIN
internal/php7/php7.go
generated
Binary file not shown.
@ -3792,12 +3792,9 @@ simple_variable:
|
||||
$$ = &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,
|
||||
},
|
||||
OpenCurlyBracketTkn: $2,
|
||||
VarName: $3,
|
||||
CloseCurlyBracketTkn: $4,
|
||||
}
|
||||
}
|
||||
| '$' simple_variable
|
||||
|
@ -1763,7 +1763,9 @@ func (n *ExprUnaryPlus) GetPosition() *position.Position {
|
||||
type ExprVariable struct {
|
||||
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