remove nodesWithEndToken type

This commit is contained in:
z7zmey 2018-06-03 09:35:44 +03:00
parent 2abe1dfb84
commit a488f43496
17 changed files with 1393 additions and 1251 deletions

View File

@ -103,10 +103,12 @@ nodes := &stmt.StmtList{
&stmt.ClassMethod{
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
MethodName: &node.Identifier{Value: "greet"},
Stmts: []node.Node{
&stmt.Echo{
Exprs: []node.Node{
&scalar.String{Value: "'Hello world'"},
Stmt: &stmt.StmtList{
Stmts: []node.Node{
&stmt.Echo{
Exprs: []node.Node{
&scalar.String{Value: "'Hello world'"},
},
},
},
},

View File

@ -13,11 +13,11 @@ type ClassMethod struct {
Modifiers []node.Node
Params []node.Node
ReturnType node.Node
Stmts []node.Node
Stmt node.Node
}
// NewClassMethod node constructor
func NewClassMethod(MethodName node.Node, Modifiers []node.Node, ReturnsRef bool, Params []node.Node, ReturnType node.Node, Stmts []node.Node, PhpDocComment string) *ClassMethod {
func NewClassMethod(MethodName node.Node, Modifiers []node.Node, ReturnsRef bool, Params []node.Node, ReturnType node.Node, Stmt node.Node, PhpDocComment string) *ClassMethod {
return &ClassMethod{
ReturnsRef,
PhpDocComment,
@ -25,7 +25,7 @@ func NewClassMethod(MethodName node.Node, Modifiers []node.Node, ReturnsRef bool
Modifiers,
Params,
ReturnType,
Stmts,
Stmt,
}
}
@ -72,13 +72,9 @@ func (n *ClassMethod) Walk(v walker.Visitor) {
n.ReturnType.Walk(vv)
}
if n.Stmts != nil {
vv := v.GetChildrenVisitor("Stmts")
for _, nn := range n.Stmts {
if nn != nil {
nn.Walk(vv)
}
}
if n.Stmt != nil {
vv := v.GetChildrenVisitor("Stmt")
n.Stmt.Walk(vv)
}
v.LeaveNode(n)

View File

@ -2,9 +2,10 @@ package stmt_test
import (
"bytes"
"github.com/z7zmey/php-parser/node/name"
"testing"
"github.com/z7zmey/php-parser/node/name"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/php5"
@ -22,7 +23,9 @@ func TestSimpleClassMethod(t *testing.T) {
&stmt.ClassMethod{
PhpDocComment: "",
MethodName: &node.Identifier{Value: "bar"},
Stmts: []node.Node{},
Stmt: &stmt.StmtList{
Stmts: []node.Node{},
},
},
},
},
@ -56,7 +59,9 @@ func TestPrivateProtectedClassMethod(t *testing.T) {
&node.Identifier{Value: "final"},
&node.Identifier{Value: "private"},
},
Stmts: []node.Node{},
Stmt: &stmt.StmtList{
Stmts: []node.Node{},
},
},
&stmt.ClassMethod{
PhpDocComment: "",
@ -65,7 +70,9 @@ func TestPrivateProtectedClassMethod(t *testing.T) {
Modifiers: []node.Node{
&node.Identifier{Value: "protected"},
},
Stmts: []node.Node{},
Stmt: &stmt.StmtList{
Stmts: []node.Node{},
},
},
},
},
@ -99,7 +106,9 @@ func TestPhp5ClassMethod(t *testing.T) {
&node.Identifier{Value: "public"},
&node.Identifier{Value: "static"},
},
Stmts: []node.Node{},
Stmt: &stmt.StmtList{
Stmts: []node.Node{},
},
},
},
},
@ -133,7 +142,9 @@ func TestPhp7ClassMethod(t *testing.T) {
&name.NamePart{Value: "void"},
},
},
Stmts: []node.Node{},
Stmt: &stmt.StmtList{
Stmts: []node.Node{},
},
},
},
},
@ -163,6 +174,7 @@ func TestAbstractClassMethod(t *testing.T) {
&node.Identifier{Value: "abstract"},
&node.Identifier{Value: "public"},
},
Stmt: &stmt.Nop{},
},
},
},
@ -201,6 +213,7 @@ func TestPhp7AbstractClassMethod(t *testing.T) {
&name.NamePart{Value: "void"},
},
},
Stmt: &stmt.Nop{},
},
},
},

View File

@ -82,9 +82,9 @@ var nodesToTest = []struct {
Modifiers: []node.Node{&stmt.Expression{}},
Params: []node.Node{&stmt.Expression{}},
ReturnType: &node.Identifier{},
Stmts: []node.Node{&stmt.Expression{}},
Stmt: &stmt.StmtList{},
},
[]string{"MethodName", "Modifiers", "Params", "ReturnType", "Stmts"},
[]string{"MethodName", "Modifiers", "Params", "ReturnType", "Stmt"},
map[string]interface{}{"ReturnsRef": true, "PhpDocComment": "/** */"},
},
{
@ -360,7 +360,7 @@ var nodesToTest = []struct {
},
{
&stmt.Switch{
Cond: &expr.Variable{},
Cond: &expr.Variable{},
CaseList: &stmt.CaseList{},
},
[]string{"Cond", "CaseList"},

View File

@ -289,7 +289,9 @@ func TestPhp7ParameterNode(t *testing.T) {
MethodName: &node.Identifier{Value: "foo"},
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
Params: expectedParams,
Stmts: []node.Node{},
Stmt: &stmt.StmtList{
Stmts: []node.Node{},
},
},
},
},
@ -357,7 +359,9 @@ func TestPhp5ParameterNode(t *testing.T) {
MethodName: &node.Identifier{Value: "foo"},
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
Params: expectedParams,
Stmts: []node.Node{},
Stmt: &stmt.StmtList{
Stmts: []node.Node{},
},
},
},
},

View File

@ -174,6 +174,16 @@ func (b *PositionBuilder) NewNodeNodeListPosition(n node.Node, list []node.Node)
)
}
// NewNodeListNodePosition returns new Position
func (b *PositionBuilder) NewNodeListNodePosition(list []node.Node, n node.Node) *position.Position {
return position.NewPosition(
b.getListStartPos(list).startLine,
b.getNodeEndPos(n).endLine,
b.getListStartPos(list).startPos,
b.getNodeEndPos(n).endPos,
)
}
// NewOptionalListTokensPosition returns new Position
func (b *PositionBuilder) NewOptionalListTokensPosition(list []node.Node, t *scanner.Token, endToken *scanner.Token) *position.Position {
if list == nil {

View File

@ -259,6 +259,41 @@ func TestNewNodeNodeListPosition(t *testing.T) {
}
}
func TestNewNodeListNodePosition(t *testing.T) {
n1 := node.NewIdentifier("test node")
n2 := node.NewIdentifier("test node")
n3 := node.NewIdentifier("test node")
builder := parser.PositionBuilder{
Positions: &parser.Positions{
n1: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 8,
},
n2: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 9,
EndPos: 17,
},
n3: &position.Position{
StartLine: 3,
EndLine: 3,
StartPos: 18,
EndPos: 26,
},
},
}
pos := builder.NewNodeListNodePosition([]node.Node{n1, n2}, n3)
if pos.String() != `Pos{Line: 1-3 Pos: 0-26}` {
t.Errorf("token value is not equal\n")
}
}
func TestNewOptionalListTokensPosition(t *testing.T) {
builder := parser.PositionBuilder{}

File diff suppressed because it is too large Load Diff

View File

@ -24,7 +24,6 @@ import (
boolWithToken boolWithToken
list []node.Node
foreachVariable foreachVariable
nodesWithEndToken *nodesWithEndToken
simpleIndirectReference simpleIndirectReference
altSyntaxNode altSyntaxNode
}
@ -245,6 +244,7 @@ import (
%type <node> ctor_arguments function_call_parameter_list
%type <node> trait_adaptations
%type <node> switch_case_list
%type <node> method_body
%type <list> top_statement_list namespace_name use_declarations use_function_declarations use_const_declarations
%type <list> inner_statement_list global_var_list static_var_list encaps_list isset_variables non_empty_array_pair_list
@ -261,7 +261,6 @@ import (
%type <simpleIndirectReference> simple_indirect_reference
%type <foreachVariable> foreach_variable foreach_optional_arg
%type <nodesWithEndToken> method_body
%type <boolWithToken> is_reference is_variadic
%type <altSyntaxNode> while_statement for_statement foreach_statement
@ -1574,8 +1573,14 @@ class_statement:
yylex.(*Parser).positions.AddPosition(name, yylex.(*Parser).positionBuilder.NewTokenPosition($4))
yylex.(*Parser).comments.AddComments(name, $4.Comments())
$$ = stmt.NewClassMethod(name, $1, $3.value, $6, nil, $8.nodes, "")
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewOptionalListTokensPosition($1, $2, $8.endToken))
$$ = stmt.NewClassMethod(name, $1, $3.value, $6, nil, $8, "")
if $1 == nil {
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokenNodePosition($2, $8))
} else {
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewNodeListNodePosition($1, $8))
}
yylex.(*Parser).comments.AddComments($$, yylex.(*Parser).listGetFirstNodeComments($1))
}
;
@ -1712,9 +1717,17 @@ trait_modifiers:
method_body:
';' /* abstract method */
{ $$ = &nodesWithEndToken{nil, $1} }
{
$$ = stmt.NewNop()
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokenPosition($1))
}
| '{' inner_statement_list '}'
{ $$ = &nodesWithEndToken{$2, $3} }
{
$$ = stmt.NewStmtList($2)
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $3))
}
;
variable_modifiers:
@ -3926,11 +3939,6 @@ type foreachVariable struct {
byRef bool
}
type nodesWithEndToken struct {
nodes []node.Node
endToken *scanner.Token
}
type boolWithToken struct {
value bool
token *scanner.Token

View File

@ -514,7 +514,9 @@ func TestPhp5(t *testing.T) {
MethodName: &node.Identifier{Value: "foo"},
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
Params: expectedParams,
Stmts: []node.Node{},
Stmt: &stmt.StmtList{
Stmts: []node.Node{},
},
},
},
},
@ -776,7 +778,9 @@ func TestPhp5(t *testing.T) {
&stmt.ClassMethod{
PhpDocComment: "",
MethodName: &node.Identifier{Value: "bar"},
Stmts: []node.Node{},
Stmt: &stmt.StmtList{
Stmts: []node.Node{},
},
},
},
},
@ -791,7 +795,9 @@ func TestPhp5(t *testing.T) {
&node.Identifier{Value: "public"},
&node.Identifier{Value: "static"},
},
Stmts: []node.Node{},
Stmt: &stmt.StmtList{
Stmts: []node.Node{},
},
},
},
},
@ -806,7 +812,9 @@ func TestPhp5(t *testing.T) {
&node.Identifier{Value: "final"},
&node.Identifier{Value: "private"},
},
Stmts: []node.Node{},
Stmt: &stmt.StmtList{
Stmts: []node.Node{},
},
},
&stmt.ClassMethod{
PhpDocComment: "",
@ -815,7 +823,9 @@ func TestPhp5(t *testing.T) {
Modifiers: []node.Node{
&node.Identifier{Value: "protected"},
},
Stmts: []node.Node{},
Stmt: &stmt.StmtList{
Stmts: []node.Node{},
},
},
},
},
@ -833,6 +843,7 @@ func TestPhp5(t *testing.T) {
&node.Identifier{Value: "abstract"},
&node.Identifier{Value: "public"},
},
Stmt: &stmt.Nop{},
},
},
},

File diff suppressed because it is too large Load Diff

View File

@ -25,7 +25,6 @@ import (
boolWithToken boolWithToken
list []node.Node
foreachVariable foreachVariable
nodesWithEndToken *nodesWithEndToken
str string
altSyntaxNode altSyntaxNode
}
@ -266,12 +265,12 @@ import (
%type <node> argument_list ctor_arguments
%type <node> trait_adaptations
%type <node> switch_case_list
%type <node> method_body
%type <node> member_modifier
%type <node> use_type
%type <foreachVariable> foreach_variable
%type <nodesWithEndToken> method_body
%type <list> encaps_list backticks_expr namespace_name catch_name_list catch_list class_const_list
%type <list> const_list echo_expr_list for_exprs non_empty_for_exprs global_var_list
@ -1945,11 +1944,15 @@ class_statement:
| method_modifiers T_FUNCTION returns_ref identifier backup_doc_comment '(' parameter_list ')' return_type method_body
{
name := node.NewIdentifier($4.Value)
$$ = stmt.NewClassMethod(name, $1, $3.value, $7, $9, $10.nodes, $5)
$$ = stmt.NewClassMethod(name, $1, $3.value, $7, $9, $10, $5)
// save position
yylex.(*Parser).positions.AddPosition(name, yylex.(*Parser).positionBuilder.NewTokenPosition($4))
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewOptionalListTokensPosition($1, $2, $10.endToken))
if $1 == nil {
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokenNodePosition($2, $10))
} else {
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewNodeListNodePosition($1, $10))
}
// save comments
yylex.(*Parser).comments.AddFromToken($$, $2, comment.FunctionToken)
@ -2130,8 +2133,27 @@ absolute_trait_method_reference:
;
method_body:
';' /* abstract method */ { $$ = &nodesWithEndToken{nil, $1} }
| '{' inner_statement_list '}' { $$ = &nodesWithEndToken{$2, $3} }
';' /* abstract method */
{
$$ = stmt.NewNop()
// save position
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokenPosition($1))
// save comments
yylex.(*Parser).comments.AddFromToken($$, $1, comment.SemiColonToken)
}
| '{' inner_statement_list '}'
{
$$ = stmt.NewStmtList($2)
// save position
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $3))
// save comments
yylex.(*Parser).comments.AddFromToken($$, $1, comment.OpenCurlyBracesToken)
yylex.(*Parser).comments.AddFromToken($$, $3, comment.CloseCurlyBracesToken)
}
;
variable_modifiers:
@ -4223,11 +4245,6 @@ type foreachVariable struct {
byRef bool
}
type nodesWithEndToken struct {
nodes []node.Node
endToken *scanner.Token
}
type boolWithToken struct {
value bool
token *scanner.Token

View File

@ -549,7 +549,9 @@ func TestPhp7(t *testing.T) {
MethodName: &node.Identifier{Value: "foo"},
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
Params: expectedParams,
Stmts: []node.Node{},
Stmt: &stmt.StmtList{
Stmts: []node.Node{},
},
},
},
},
@ -866,7 +868,9 @@ func TestPhp7(t *testing.T) {
&stmt.ClassMethod{
PhpDocComment: "",
MethodName: &node.Identifier{Value: "bar"},
Stmts: []node.Node{},
Stmt: &stmt.StmtList{
Stmts: []node.Node{},
},
},
},
},
@ -881,7 +885,9 @@ func TestPhp7(t *testing.T) {
&node.Identifier{Value: "public"},
&node.Identifier{Value: "static"},
},
Stmts: []node.Node{},
Stmt: &stmt.StmtList{
Stmts: []node.Node{},
},
},
},
},
@ -901,7 +907,9 @@ func TestPhp7(t *testing.T) {
&name.NamePart{Value: "void"},
},
},
Stmts: []node.Node{},
Stmt: &stmt.StmtList{
Stmts: []node.Node{},
},
},
},
},
@ -2966,7 +2974,9 @@ func TestPhp7(t *testing.T) {
&stmt.ClassMethod{
MethodName: &node.Identifier{Value: "class"},
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
Stmts: []node.Node{},
Stmt: &stmt.StmtList{
Stmts: []node.Node{},
},
},
},
},
@ -3072,6 +3082,7 @@ func TestPhp7(t *testing.T) {
&node.Identifier{Value: "protected"},
&node.Identifier{Value: "static"},
},
Stmt: &stmt.Nop{},
},
&stmt.ClassMethod{
MethodName: &node.Identifier{Value: "baz"},
@ -3079,7 +3090,9 @@ func TestPhp7(t *testing.T) {
&node.Identifier{Value: "final"},
&node.Identifier{Value: "private"},
},
Stmts: []node.Node{},
Stmt: &stmt.StmtList{
Stmts: []node.Node{},
},
},
},
},

View File

@ -1517,13 +1517,18 @@ func (p *Printer) printStmtClassMethod(n node.Node) {
p.Print(nn.ReturnType)
}
io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "{\n")
p.printNodes(nn.Stmts)
io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "}")
switch s := nn.Stmt.(type) {
case *stmt.StmtList:
io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "{\n")
p.printNodes(s.Stmts)
io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "}")
default:
p.Print(s)
}
}
func (p *Printer) printStmtClass(n node.Node) {

View File

@ -44,10 +44,12 @@ func TestPrintFile(t *testing.T) {
&stmt.ClassMethod{
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
MethodName: &node.Identifier{Value: "greet"},
Stmts: []node.Node{
&stmt.Echo{
Exprs: []node.Node{
&scalar.String{Value: "'Hello world'"},
Stmt: &stmt.StmtList{
Stmts: []node.Node{
&stmt.Echo{
Exprs: []node.Node{
&scalar.String{Value: "'Hello world'"},
},
},
},
},
@ -2453,8 +2455,10 @@ func TestPrintStmtClassMethod(t *testing.T) {
},
},
ReturnType: &name.Name{Parts: []node.Node{&name.NamePart{Value: "void"}}},
Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
Stmt: &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
},
},
})
@ -3387,8 +3391,10 @@ func TestPrintInterface(t *testing.T) {
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
MethodName: &node.Identifier{Value: "foo"},
Params: []node.Node{},
Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
Stmt: &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
},
},
},
},
@ -3833,8 +3839,10 @@ func TestPrintTrait(t *testing.T) {
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
MethodName: &node.Identifier{Value: "foo"},
Params: []node.Node{},
Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
Stmt: &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
},
},
},
},

View File

@ -63,8 +63,8 @@ func ExampleDumper() {
//| "Stmts":
//| [*stmt.ClassMethod]
//| "Position": Pos{Line: 5-9 Pos: 45-134};
//| "PhpDocComment": ;
//| "ReturnsRef": false;
//| "PhpDocComment": ;
//| "MethodName":
//| [*node.Identifier]
//| "Position": Pos{Line: 5-5 Pos: 61-72};
@ -104,16 +104,19 @@ func ExampleDumper() {
//| [*name.NamePart]
//| "Position": Pos{Line: 5-5 Pos: 86-89};
//| "Value": null;
//| "Stmts":
//| [*stmt.Expression]
//| "Position": Pos{Line: 8-8 Pos: 124-128};
//| "Expr":
//| [*expr.Variable]
//| "Position": Pos{Line: 8-8 Pos: 124-127};
//| "Comments":
//| "// some comment\n" before "VariableToken"
//| "VarName":
//| [*node.Identifier]
//| "Stmt":
//| [*stmt.StmtList]
//| "Position": Pos{Line: 6-9 Pos: 96-134};
//| "Stmts":
//| [*stmt.Expression]
//| "Position": Pos{Line: 8-8 Pos: 124-128};
//| "Expr":
//| [*expr.Variable]
//| "Position": Pos{Line: 8-8 Pos: 124-127};
//| "Value": var;
//| "Comments":
//| "// some comment\n" before "VariableToken"
//| "VarName":
//| [*node.Identifier]
//| "Position": Pos{Line: 8-8 Pos: 124-127};
//| "Value": var;
}

View File

@ -544,7 +544,9 @@ func TestResolveMethodName(t *testing.T) {
},
},
ReturnType: &node.Nullable{Expr: nameBC},
Stmts: []node.Node{},
Stmt: &stmt.StmtList{
Stmts: []node.Node{},
},
}
expected := map[node.Node]string{