slit While and AltWhile

This commit is contained in:
z7zmey 2018-02-18 19:44:17 +02:00
parent 1c6633e47d
commit d498ea9863
11 changed files with 1384 additions and 1301 deletions

45
node/stmt/n_alt_while.go Normal file
View File

@ -0,0 +1,45 @@
package stmt
import (
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/walker"
)
// AltWhile node
type AltWhile struct {
Cond node.Node
Stmt node.Node
}
// NewAltWhile node constuctor
func NewAltWhile(Cond node.Node, Stmt node.Node) *AltWhile {
return &AltWhile{
Cond,
Stmt,
}
}
// Attributes returns node attributes as map
func (n *AltWhile) Attributes() map[string]interface{} {
return nil
}
// Walk traverses nodes
// Walk is invoked recursively until v.EnterNode returns true
func (n *AltWhile) Walk(v walker.Visitor) {
if v.EnterNode(n) == false {
return
}
if n.Cond != nil {
vv := v.GetChildrenVisitor("Cond")
n.Cond.Walk(vv)
}
if n.Stmt != nil {
vv := v.GetChildrenVisitor("Stmt")
n.Stmt.Walk(vv)
}
v.LeaveNode(n)
}

View File

@ -435,6 +435,14 @@ var nodesToTest = []struct {
[]string{"Cond", "Stmt"}, []string{"Cond", "Stmt"},
map[string]interface{}{}, map[string]interface{}{},
}, },
{
&stmt.AltWhile{
Cond: &expr.Variable{},
Stmt: &stmt.StmtList{},
},
[]string{"Cond", "Stmt"},
map[string]interface{}{},
},
{ {
&stmt.StmtList{ &stmt.StmtList{
Stmts: []node.Node{&stmt.Expression{}}, Stmts: []node.Node{&stmt.Expression{}},

View File

@ -65,7 +65,7 @@ func TestBreak(t *testing.T) {
expected := &stmt.StmtList{ expected := &stmt.StmtList{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.While{ &stmt.AltWhile{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
Stmt: &stmt.StmtList{ Stmt: &stmt.StmtList{
Stmts: []node.Node{ Stmts: []node.Node{

View File

@ -59,3 +59,8 @@ type simpleIndirectReference struct {
all []*expr.Variable all []*expr.Variable
last *expr.Variable last *expr.Variable
} }
type altSyntaxNode struct {
node node.Node
isAlt bool
}

File diff suppressed because it is too large Load Diff

View File

@ -26,7 +26,7 @@ import (
foreachVariable foreachVariable foreachVariable foreachVariable
nodesWithEndToken *nodesWithEndToken nodesWithEndToken *nodesWithEndToken
simpleIndirectReference simpleIndirectReference simpleIndirectReference simpleIndirectReference
// str string altSyntaxNode altSyntaxNode
} }
%type <token> $unk %type <token> $unk
@ -203,7 +203,7 @@ import (
%type <node> variable_name variable_without_objects dynamic_class_name_reference new_expr class_name_reference static_member %type <node> variable_name variable_without_objects dynamic_class_name_reference new_expr class_name_reference static_member
%type <node> function_call fully_qualified_class_name combined_scalar combined_scalar_offset general_constant parenthesis_expr %type <node> function_call fully_qualified_class_name combined_scalar combined_scalar_offset general_constant parenthesis_expr
%type <node> exit_expr yield_expr function_declaration_statement class_declaration_statement constant_declaration %type <node> exit_expr yield_expr function_declaration_statement class_declaration_statement constant_declaration
%type <node> else_single new_else_single while_statement for_statement unset_variable foreach_statement declare_statement %type <node> else_single new_else_single for_statement unset_variable foreach_statement declare_statement
%type <node> finally_statement additional_catch unticked_function_declaration_statement unticked_class_declaration_statement %type <node> finally_statement additional_catch unticked_function_declaration_statement unticked_class_declaration_statement
%type <node> optional_class_type parameter class_entry_type extends_from class_statement class_constant_declaration %type <node> optional_class_type parameter class_entry_type extends_from class_statement class_constant_declaration
%type <node> trait_use_statement function_call_parameter trait_adaptation_statement trait_precedence trait_alias %type <node> trait_use_statement function_call_parameter trait_adaptation_statement trait_precedence trait_alias
@ -227,6 +227,7 @@ import (
%type <foreachVariable> foreach_variable foreach_optional_arg %type <foreachVariable> foreach_variable foreach_optional_arg
%type <nodesWithEndToken> ctor_arguments function_call_parameter_list switch_case_list method_body trait_adaptations %type <nodesWithEndToken> ctor_arguments function_call_parameter_list switch_case_list method_body trait_adaptations
%type <boolWithToken> is_reference is_variadic %type <boolWithToken> is_reference is_variadic
%type <altSyntaxNode> while_statement
%% %%
@ -602,8 +603,12 @@ unticked_statement:
} }
| T_WHILE parenthesis_expr while_statement | T_WHILE parenthesis_expr while_statement
{ {
$$ = stmt.NewWhile($2, $3) if ($3.isAlt) {
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $3)) $$ = stmt.NewAltWhile($2, $3.node)
} else {
$$ = stmt.NewWhile($2, $3.node)
}
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $3.node))
comments.AddComments($$, $1.Comments()) comments.AddComments($$, $1.Comments())
} }
| T_DO statement T_WHILE parenthesis_expr ';' | T_DO statement T_WHILE parenthesis_expr ';'
@ -1109,11 +1114,11 @@ case_separator:
while_statement: while_statement:
statement statement
{ $$ = $1 } { $$ = altSyntaxNode{$1, false} }
| ':' inner_statement_list T_ENDWHILE ';' | ':' inner_statement_list T_ENDWHILE ';'
{ {
$$ = stmt.NewStmtList($2) $$ = altSyntaxNode{stmt.NewStmtList($2), true}
positions.AddPosition($$, positionBuilder.NewTokensPosition($1, $4)) positions.AddPosition($$.node, positionBuilder.NewTokensPosition($1, $4))
} }
; ;

View File

@ -789,7 +789,7 @@ CAD;
}, },
}, },
}, },
&stmt.While{ &stmt.AltWhile{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
Stmt: &stmt.StmtList{ Stmt: &stmt.StmtList{
Stmts: []node.Node{ Stmts: []node.Node{

View File

@ -50,3 +50,8 @@ type boolWithToken struct {
value bool value bool
token *token.Token token *token.Token
} }
type altSyntaxNode struct {
node node.Node
isAlt bool
}

File diff suppressed because it is too large Load Diff

View File

@ -26,6 +26,7 @@ import (
foreachVariable foreachVariable foreachVariable foreachVariable
nodesWithEndToken *nodesWithEndToken nodesWithEndToken *nodesWithEndToken
str string str string
altSyntaxNode altSyntaxNode
} }
%type <token> $unk %type <token> $unk
@ -207,7 +208,7 @@ import (
%type <node> group_use_declaration inline_use_declaration %type <node> group_use_declaration inline_use_declaration
%type <node> mixed_group_use_declaration use_declaration unprefixed_use_declaration %type <node> mixed_group_use_declaration use_declaration unprefixed_use_declaration
%type <node> const_decl inner_statement %type <node> const_decl inner_statement
%type <node> expr optional_expr while_statement for_statement %type <node> expr optional_expr for_statement
%type <node> foreach_statement declare_statement finally_statement unset_variable variable %type <node> foreach_statement declare_statement finally_statement unset_variable variable
%type <node> extends_from parameter optional_type argument expr_without_variable global_var %type <node> extends_from parameter optional_type argument expr_without_variable global_var
%type <node> static_var class_statement trait_adaptation trait_precedence trait_alias %type <node> static_var class_statement trait_adaptation trait_precedence trait_alias
@ -245,6 +246,8 @@ import (
%type <str> backup_doc_comment %type <str> backup_doc_comment
%type <altSyntaxNode> while_statement
%% %%
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
@ -523,8 +526,12 @@ statement:
| alt_if_stmt { $$ = $1; } | alt_if_stmt { $$ = $1; }
| T_WHILE '(' expr ')' while_statement | T_WHILE '(' expr ')' while_statement
{ {
$$ = stmt.NewWhile($3, $5) if ($5.isAlt) {
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $5)) $$ = stmt.NewAltWhile($3, $5.node)
} else {
$$ = stmt.NewWhile($3, $5.node)
}
positions.AddPosition($$, positionBuilder.NewTokenNodePosition($1, $5.node))
comments.AddComments($$, $1.Comments()) comments.AddComments($$, $1.Comments())
} }
| T_DO statement T_WHILE '(' expr ')' ';' | T_DO statement T_WHILE '(' expr ')' ';'
@ -890,12 +897,12 @@ case_separator:
; ;
while_statement: while_statement:
statement { $$ = $1; } statement
{ $$ = altSyntaxNode{$1, false} }
| ':' inner_statement_list T_ENDWHILE ';' | ':' inner_statement_list T_ENDWHILE ';'
{ {
$$ = stmt.NewStmtList($2) $$ = altSyntaxNode{stmt.NewStmtList($2), true}
positions.AddPosition($$, positionBuilder.NewTokensPosition($1, $4)) positions.AddPosition($$.node, positionBuilder.NewTokensPosition($1, $4))
comments.AddComments($$, $1.Comments())
} }
; ;

View File

@ -854,7 +854,7 @@ CAD;
}, },
}, },
}, },
&stmt.While{ &stmt.AltWhile{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
Stmt: &stmt.StmtList{ Stmt: &stmt.StmtList{
Stmts: []node.Node{ Stmts: []node.Node{