refactoring: update ast structure of "Variable" node

This commit is contained in:
Vadym Slizov 2020-12-27 21:55:36 +02:00
parent 69919594fd
commit 2c09138600
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
12 changed files with 164 additions and 144 deletions

View File

@ -16726,14 +16726,7 @@ func TestStmtGlobal_Vars(t *testing.T) {
}, },
}, },
}, },
VarName: &ast.ParserBrackets{ OpenCurlyBracketTkn: &token.Token{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 24,
EndPos: 31,
},
OpenBracketTkn: &token.Token{
ID: token.ID(123), ID: token.ID(123),
Value: []byte("{"), Value: []byte("{"),
Position: &position.Position{ Position: &position.Position{
@ -16743,7 +16736,7 @@ func TestStmtGlobal_Vars(t *testing.T) {
EndPos: 25, EndPos: 25,
}, },
}, },
Child: &ast.ExprFunctionCall{ VarName: &ast.ExprFunctionCall{
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
EndLine: 1, EndLine: 1,
@ -16800,7 +16793,7 @@ func TestStmtGlobal_Vars(t *testing.T) {
}, },
}, },
}, },
CloseBracketTkn: &token.Token{ CloseCurlyBracketTkn: &token.Token{
ID: token.ID(125), ID: token.ID(125),
Value: []byte("}"), Value: []byte("}"),
Position: &position.Position{ Position: &position.Position{
@ -16812,7 +16805,6 @@ func TestStmtGlobal_Vars(t *testing.T) {
}, },
}, },
}, },
},
SeparatorTkns: []*token.Token{ SeparatorTkns: []*token.Token{
{ {
ID: token.ID(44), ID: token.ID(44),

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -2024,12 +2024,9 @@ global_var:
$$ = &ast.ExprVariable{ $$ = &ast.ExprVariable{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $4), Position: yylex.(*Parser).builder.NewTokensPosition($1, $4),
DollarTkn: $1, DollarTkn: $1,
VarName: &ast.ParserBrackets{ OpenCurlyBracketTkn: $2,
Position: yylex.(*Parser).builder.NewTokensPosition($2, $4), VarName: $3,
OpenBracketTkn: $2, CloseCurlyBracketTkn: $4,
Child: $3,
CloseBracketTkn: $4,
},
} }
} }
; ;
@ -5019,12 +5016,9 @@ compound_variable:
$$ = &ast.ExprVariable{ $$ = &ast.ExprVariable{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $4), Position: yylex.(*Parser).builder.NewTokensPosition($1, $4),
DollarTkn: $1, DollarTkn: $1,
VarName: &ast.ParserBrackets{ OpenCurlyBracketTkn: $2,
Position: yylex.(*Parser).builder.NewTokensPosition($2, $4), VarName: $3,
OpenBracketTkn: $2, CloseCurlyBracketTkn: $4,
Child: $3,
CloseBracketTkn: $4,
},
} }
} }
; ;

View File

@ -18412,14 +18412,7 @@ func TestStmtGlobal_Vars(t *testing.T) {
}, },
}, },
}, },
VarName: &ast.ParserBrackets{ OpenCurlyBracketTkn: &token.Token{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 24,
EndPos: 31,
},
OpenBracketTkn: &token.Token{
ID: token.ID(123), ID: token.ID(123),
Value: []byte("{"), Value: []byte("{"),
Position: &position.Position{ Position: &position.Position{
@ -18429,7 +18422,7 @@ func TestStmtGlobal_Vars(t *testing.T) {
EndPos: 25, EndPos: 25,
}, },
}, },
Child: &ast.ExprFunctionCall{ VarName: &ast.ExprFunctionCall{
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
EndLine: 1, EndLine: 1,
@ -18486,7 +18479,7 @@ func TestStmtGlobal_Vars(t *testing.T) {
}, },
}, },
}, },
CloseBracketTkn: &token.Token{ CloseCurlyBracketTkn: &token.Token{
ID: token.ID(125), ID: token.ID(125),
Value: []byte("}"), Value: []byte("}"),
Position: &position.Position{ Position: &position.Position{
@ -18498,7 +18491,6 @@ func TestStmtGlobal_Vars(t *testing.T) {
}, },
}, },
}, },
},
SeparatorTkns: []*token.Token{ SeparatorTkns: []*token.Token{
{ {
ID: token.ID(44), ID: token.ID(44),

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -3792,12 +3792,9 @@ simple_variable:
$$ = &ast.ExprVariable{ $$ = &ast.ExprVariable{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $4), Position: yylex.(*Parser).builder.NewTokensPosition($1, $4),
DollarTkn: $1, DollarTkn: $1,
VarName: &ast.ParserBrackets{ OpenCurlyBracketTkn: $2,
Position: yylex.(*Parser).builder.NewTokensPosition($2, $4), VarName: $3,
OpenBracketTkn: $2, CloseCurlyBracketTkn: $4,
Child: $3,
CloseBracketTkn: $4,
},
} }
} }
| '$' simple_variable | '$' simple_variable

View File

@ -1763,7 +1763,9 @@ func (n *ExprUnaryPlus) GetPosition() *position.Position {
type ExprVariable struct { type ExprVariable struct {
Position *position.Position Position *position.Position
DollarTkn *token.Token DollarTkn *token.Token
OpenCurlyBracketTkn *token.Token
VarName Vertex VarName Vertex
CloseCurlyBracketTkn *token.Token
} }
func (n *ExprVariable) Accept(v NodeVisitor) { func (n *ExprVariable) Accept(v NodeVisitor) {

View File

@ -1537,7 +1537,9 @@ func (v *Dumper) ExprVariable(n *ast.ExprVariable) {
v.dumpPosition(n.Position) v.dumpPosition(n.Position)
v.dumpToken("DollarTkn", n.DollarTkn) v.dumpToken("DollarTkn", n.DollarTkn)
v.dumpToken("OpenCurlyBracketTkn", n.OpenCurlyBracketTkn)
v.dumpVertex("VarName", n.VarName) v.dumpVertex("VarName", n.VarName)
v.dumpToken("CloseCurlyBracketTkn", n.CloseCurlyBracketTkn)
v.indent-- v.indent--
v.print(v.indent, "},\n") v.print(v.indent, "},\n")

View File

@ -1417,6 +1417,16 @@ func (f *formatter) ExprVariable(n *ast.ExprVariable) {
n.DollarTkn = f.newToken('$', []byte("$")) 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) n.VarName.Accept(f)
} }

View File

@ -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) { func TestFormatter_ExprYield(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")

View File

@ -868,7 +868,9 @@ func (p *printer) ExprUnaryPlus(n *ast.ExprUnaryPlus) {
func (p *printer) ExprVariable(n *ast.ExprVariable) { func (p *printer) ExprVariable(n *ast.ExprVariable) {
p.printToken(n.DollarTkn, nil) p.printToken(n.DollarTkn, nil)
p.printToken(n.OpenCurlyBracketTkn, nil)
p.printNode(n.VarName) p.printNode(n.VarName)
p.printToken(n.CloseCurlyBracketTkn, nil)
} }
func (p *printer) ExprYield(n *ast.ExprYield) { func (p *printer) ExprYield(n *ast.ExprYield) {

View File

@ -2609,13 +2609,19 @@ func TestPrinterPrintVariable(t *testing.T) {
DollarTkn: &token.Token{ DollarTkn: &token.Token{
Value: []byte("$"), Value: []byte("$"),
}, },
OpenCurlyBracketTkn: &token.Token{
Value: []byte("{"),
},
VarName: &ast.ExprVariable{ VarName: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$var")}, VarName: &ast.Identifier{Value: []byte("$var")},
}, },
CloseCurlyBracketTkn: &token.Token{
Value: []byte("}"),
},
} }
n.Accept(p) n.Accept(p)
expected := `$$var` expected := `${$var}`
actual := o.String() actual := o.String()
if expected != actual { if expected != actual {