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,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

Binary file not shown.

View File

@ -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,
}
}
;

View File

@ -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

Binary file not shown.

View File

@ -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

View File

@ -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) {

View File

@ -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")

View File

@ -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)
}

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) {
o := bytes.NewBufferString("")

View File

@ -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) {

View File

@ -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 {