[#82] property type
This commit is contained in:
parent
7b4c72a3af
commit
dc7aa7302d
@ -12,14 +12,16 @@ type PropertyList struct {
|
|||||||
FreeFloating freefloating.Collection
|
FreeFloating freefloating.Collection
|
||||||
Position *position.Position
|
Position *position.Position
|
||||||
Modifiers []node.Node
|
Modifiers []node.Node
|
||||||
|
Type node.Node
|
||||||
Properties []node.Node
|
Properties []node.Node
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPropertyList node constructor
|
// 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{
|
return &PropertyList{
|
||||||
FreeFloating: nil,
|
FreeFloating: nil,
|
||||||
Modifiers: Modifiers,
|
Modifiers: Modifiers,
|
||||||
|
Type: Type,
|
||||||
Properties: Properties,
|
Properties: Properties,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -60,6 +62,12 @@ func (n *PropertyList) Walk(v walker.Visitor) {
|
|||||||
v.LeaveChildList("Modifiers", n)
|
v.LeaveChildList("Modifiers", n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if n.Type != nil {
|
||||||
|
v.EnterChildNode("Type", n)
|
||||||
|
n.Type.Walk(v)
|
||||||
|
v.LeaveChildNode("Type", n)
|
||||||
|
}
|
||||||
|
|
||||||
if n.Properties != nil {
|
if n.Properties != nil {
|
||||||
v.EnterChildList("Properties", n)
|
v.EnterChildList("Properties", n)
|
||||||
for _, nn := range n.Properties {
|
for _, nn := range n.Properties {
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"gotest.tools/assert"
|
"gotest.tools/assert"
|
||||||
|
|
||||||
"github.com/z7zmey/php-parser/node/expr"
|
"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/node/scalar"
|
||||||
"github.com/z7zmey/php-parser/position"
|
"github.com/z7zmey/php-parser/position"
|
||||||
|
|
||||||
@ -378,3 +379,109 @@ func TestProperties2(t *testing.T) {
|
|||||||
actual = php5parser.GetRootNode()
|
actual = php5parser.GetRootNode()
|
||||||
assert.DeepEqual(t, expected, actual)
|
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)
|
||||||
|
}
|
||||||
|
@ -323,9 +323,10 @@ var nodesToTest = []struct {
|
|||||||
{
|
{
|
||||||
&stmt.PropertyList{
|
&stmt.PropertyList{
|
||||||
Modifiers: []node.Node{&stmt.Expression{}},
|
Modifiers: []node.Node{&stmt.Expression{}},
|
||||||
|
Type: &name.Name{},
|
||||||
Properties: []node.Node{&stmt.Expression{}},
|
Properties: []node.Node{&stmt.Expression{}},
|
||||||
},
|
},
|
||||||
[]string{"Modifiers", "Properties"},
|
[]string{"Modifiers", "Type", "Properties"},
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -4904,7 +4904,7 @@ yydefault:
|
|||||||
yyDollar = yyS[yypt-3 : yypt+1]
|
yyDollar = yyS[yypt-3 : yypt+1]
|
||||||
//line php5/php5.y:2703
|
//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
|
// save position
|
||||||
yyVAL.node.SetPosition(yylex.(*Parser).positionBuilder.NewNodeListTokenPosition(yyDollar[1].list, yyDollar[3].token))
|
yyVAL.node.SetPosition(yylex.(*Parser).positionBuilder.NewNodeListTokenPosition(yyDollar[1].list, yyDollar[3].token))
|
||||||
|
@ -2701,7 +2701,7 @@ class_statement_list:
|
|||||||
class_statement:
|
class_statement:
|
||||||
variable_modifiers class_variable_declaration ';'
|
variable_modifiers class_variable_declaration ';'
|
||||||
{
|
{
|
||||||
$$ = stmt.NewPropertyList($1, $2)
|
$$ = stmt.NewPropertyList($1, nil, $2)
|
||||||
|
|
||||||
// save position
|
// save position
|
||||||
$$.SetPosition(yylex.(*Parser).positionBuilder.NewNodeListTokenPosition($1, $3))
|
$$.SetPosition(yylex.(*Parser).positionBuilder.NewNodeListTokenPosition($1, $3))
|
||||||
|
1138
php7/php7.go
1138
php7/php7.go
File diff suppressed because it is too large
Load Diff
10
php7/php7.y
10
php7/php7.y
@ -2496,17 +2496,17 @@ class_statement_list:
|
|||||||
;
|
;
|
||||||
|
|
||||||
class_statement:
|
class_statement:
|
||||||
variable_modifiers property_list ';'
|
variable_modifiers optional_type property_list ';'
|
||||||
{
|
{
|
||||||
$$ = stmt.NewPropertyList($1, $2)
|
$$ = stmt.NewPropertyList($1, $2, $3)
|
||||||
|
|
||||||
// save position
|
// save position
|
||||||
$$.SetPosition(yylex.(*Parser).positionBuilder.NewNodeListTokenPosition($1, $3))
|
$$.SetPosition(yylex.(*Parser).positionBuilder.NewNodeListTokenPosition($1, $4))
|
||||||
|
|
||||||
// save comments
|
// save comments
|
||||||
yylex.(*Parser).MoveFreeFloating($1[0], $$)
|
yylex.(*Parser).MoveFreeFloating($1[0], $$)
|
||||||
yylex.(*Parser).setFreeFloating($$, freefloating.PropertyList, $3.FreeFloating)
|
yylex.(*Parser).setFreeFloating($$, freefloating.PropertyList, $4.FreeFloating)
|
||||||
yylex.(*Parser).setFreeFloating($$, freefloating.SemiColon, yylex.(*Parser).GetFreeFloatingToken($3))
|
yylex.(*Parser).setFreeFloating($$, freefloating.SemiColon, yylex.(*Parser).GetFreeFloatingToken($4))
|
||||||
|
|
||||||
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
|
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
|
||||||
}
|
}
|
||||||
|
@ -2888,6 +2888,12 @@ func (p *Printer) printStmtPropertyList(n node.Node) {
|
|||||||
p.Print(m)
|
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() {
|
if nn.Properties[0].GetFreeFloating().IsEmpty() {
|
||||||
io.WriteString(p.w, " ")
|
io.WriteString(p.w, " ")
|
||||||
}
|
}
|
||||||
|
@ -1256,7 +1256,7 @@ func TestParseAndPrintPropertyList(t *testing.T) {
|
|||||||
class Foo {
|
class Foo {
|
||||||
var $a = '' , $b = null ;
|
var $a = '' , $b = null ;
|
||||||
private $c ;
|
private $c ;
|
||||||
public static $d ;
|
public static Bar $d ;
|
||||||
|
|
||||||
}`
|
}`
|
||||||
|
|
||||||
|
@ -3928,6 +3928,13 @@ func TestPrinterPrintPropertyList(t *testing.T) {
|
|||||||
&node.Identifier{Value: "public"},
|
&node.Identifier{Value: "public"},
|
||||||
&node.Identifier{Value: "static"},
|
&node.Identifier{Value: "static"},
|
||||||
},
|
},
|
||||||
|
Type: &name.Name{
|
||||||
|
Parts: []node.Node{
|
||||||
|
&name.NamePart{
|
||||||
|
Value: "Foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
Properties: []node.Node{
|
Properties: []node.Node{
|
||||||
&stmt.Property{
|
&stmt.Property{
|
||||||
Variable: &expr.Variable{
|
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()
|
actual := o.String()
|
||||||
|
|
||||||
if expected != actual {
|
if expected != actual {
|
||||||
|
Loading…
Reference in New Issue
Block a user