[#82] property type

This commit is contained in:
z7zmey 2019-12-29 16:36:56 +02:00
parent 7b4c72a3af
commit dc7aa7302d
10 changed files with 715 additions and 574 deletions

View File

@ -12,14 +12,16 @@ type PropertyList struct {
FreeFloating freefloating.Collection
Position *position.Position
Modifiers []node.Node
Type node.Node
Properties []node.Node
}
// NewPropertyList node constructor
func NewPropertyList(Modifiers []node.Node, Properties []node.Node) *PropertyList {
func NewPropertyList(Modifiers []node.Node, Type node.Node, Properties []node.Node) *PropertyList {
return &PropertyList{
FreeFloating: nil,
Modifiers: Modifiers,
Type: Type,
Properties: Properties,
}
}
@ -60,6 +62,12 @@ func (n *PropertyList) Walk(v walker.Visitor) {
v.LeaveChildList("Modifiers", n)
}
if n.Type != nil {
v.EnterChildNode("Type", n)
n.Type.Walk(v)
v.LeaveChildNode("Type", n)
}
if n.Properties != nil {
v.EnterChildList("Properties", n)
for _, nn := range n.Properties {

View File

@ -6,6 +6,7 @@ import (
"gotest.tools/assert"
"github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node/name"
"github.com/z7zmey/php-parser/node/scalar"
"github.com/z7zmey/php-parser/position"
@ -378,3 +379,109 @@ func TestProperties2(t *testing.T) {
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
}
func TestPropertyType(t *testing.T) {
src := `<? class foo {var bar $a;}`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 26,
},
Stmts: []node.Node{
&stmt.Class{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 26,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 12,
},
Value: "foo",
},
Stmts: []node.Node{
&stmt.PropertyList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 25,
},
Modifiers: []node.Node{
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 17,
},
Value: "var",
},
},
Type: &name.Name{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 21,
},
Parts: []node.Node{
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 21,
},
Value: "bar",
},
},
},
Properties: []node.Node{
&stmt.Property{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 22,
EndPos: 24,
},
PhpDocComment: "",
Variable: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 22,
EndPos: 24,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 22,
EndPos: 24,
},
Value: "a",
},
},
},
},
},
},
},
},
}
php7parser := php7.NewParser([]byte(src), "7.4")
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
}

View File

@ -323,9 +323,10 @@ var nodesToTest = []struct {
{
&stmt.PropertyList{
Modifiers: []node.Node{&stmt.Expression{}},
Type: &name.Name{},
Properties: []node.Node{&stmt.Expression{}},
},
[]string{"Modifiers", "Properties"},
[]string{"Modifiers", "Type", "Properties"},
nil,
},
{

View File

@ -4904,7 +4904,7 @@ yydefault:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:2703
{
yyVAL.node = stmt.NewPropertyList(yyDollar[1].list, yyDollar[2].list)
yyVAL.node = stmt.NewPropertyList(yyDollar[1].list, nil, yyDollar[2].list)
// save position
yyVAL.node.SetPosition(yylex.(*Parser).positionBuilder.NewNodeListTokenPosition(yyDollar[1].list, yyDollar[3].token))

View File

@ -2701,7 +2701,7 @@ class_statement_list:
class_statement:
variable_modifiers class_variable_declaration ';'
{
$$ = stmt.NewPropertyList($1, $2)
$$ = stmt.NewPropertyList($1, nil, $2)
// save position
$$.SetPosition(yylex.(*Parser).positionBuilder.NewNodeListTokenPosition($1, $3))

File diff suppressed because it is too large Load Diff

View File

@ -2496,17 +2496,17 @@ class_statement_list:
;
class_statement:
variable_modifiers property_list ';'
variable_modifiers optional_type property_list ';'
{
$$ = stmt.NewPropertyList($1, $2)
$$ = stmt.NewPropertyList($1, $2, $3)
// save position
$$.SetPosition(yylex.(*Parser).positionBuilder.NewNodeListTokenPosition($1, $3))
$$.SetPosition(yylex.(*Parser).positionBuilder.NewNodeListTokenPosition($1, $4))
// save comments
yylex.(*Parser).MoveFreeFloating($1[0], $$)
yylex.(*Parser).setFreeFloating($$, freefloating.PropertyList, $3.FreeFloating)
yylex.(*Parser).setFreeFloating($$, freefloating.SemiColon, yylex.(*Parser).GetFreeFloatingToken($3))
yylex.(*Parser).setFreeFloating($$, freefloating.PropertyList, $4.FreeFloating)
yylex.(*Parser).setFreeFloating($$, freefloating.SemiColon, yylex.(*Parser).GetFreeFloatingToken($4))
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
}

View File

@ -2888,6 +2888,12 @@ func (p *Printer) printStmtPropertyList(n node.Node) {
p.Print(m)
}
if nn.Type != nil && nn.Type.GetFreeFloating().IsEmpty() {
io.WriteString(p.w, " ")
}
p.Print(nn.Type)
if nn.Properties[0].GetFreeFloating().IsEmpty() {
io.WriteString(p.w, " ")
}

View File

@ -1256,7 +1256,7 @@ func TestParseAndPrintPropertyList(t *testing.T) {
class Foo {
var $a = '' , $b = null ;
private $c ;
public static $d ;
public static Bar $d ;
}`

View File

@ -3928,6 +3928,13 @@ func TestPrinterPrintPropertyList(t *testing.T) {
&node.Identifier{Value: "public"},
&node.Identifier{Value: "static"},
},
Type: &name.Name{
Parts: []node.Node{
&name.NamePart{
Value: "Foo",
},
},
},
Properties: []node.Node{
&stmt.Property{
Variable: &expr.Variable{
@ -3943,7 +3950,7 @@ func TestPrinterPrintPropertyList(t *testing.T) {
},
})
expected := `public static $a='a',$b;`
expected := `public static Foo $a='a',$b;`
actual := o.String()
if expected != actual {