create CaseList node
This commit is contained in:
		
							parent
							
								
									f8d9d6d7c2
								
							
						
					
					
						commit
						d1b0cebf9a
					
				| @ -7,15 +7,15 @@ import ( | ||||
| 
 | ||||
| // AltSwitch node | ||||
| type AltSwitch struct { | ||||
| 	Cond  node.Node | ||||
| 	Cases []node.Node | ||||
| 	Cond     node.Node | ||||
| 	CaseList *CaseList | ||||
| } | ||||
| 
 | ||||
| // NewAltSwitch node constructor | ||||
| func NewAltSwitch(Cond node.Node, Cases []node.Node) *AltSwitch { | ||||
| func NewAltSwitch(Cond node.Node, CaseList *CaseList) *AltSwitch { | ||||
| 	return &AltSwitch{ | ||||
| 		Cond, | ||||
| 		Cases, | ||||
| 		CaseList, | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| @ -36,13 +36,9 @@ func (n *AltSwitch) Walk(v walker.Visitor) { | ||||
| 		n.Cond.Walk(vv) | ||||
| 	} | ||||
| 
 | ||||
| 	if n.Cases != nil { | ||||
| 		vv := v.GetChildrenVisitor("Cases") | ||||
| 		for _, nn := range n.Cases { | ||||
| 			if nn != nil { | ||||
| 				nn.Walk(vv) | ||||
| 			} | ||||
| 		} | ||||
| 	if n.CaseList != nil { | ||||
| 		vv := v.GetChildrenVisitor("CaseList") | ||||
| 		n.CaseList.Walk(vv) | ||||
| 	} | ||||
| 
 | ||||
| 	v.LeaveNode(n) | ||||
|  | ||||
							
								
								
									
										42
									
								
								node/stmt/n_case_list.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								node/stmt/n_case_list.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,42 @@ | ||||
| package stmt | ||||
| 
 | ||||
| import ( | ||||
| 	"github.com/z7zmey/php-parser/node" | ||||
| 	"github.com/z7zmey/php-parser/walker" | ||||
| ) | ||||
| 
 | ||||
| // CaseList node | ||||
| type CaseList struct { | ||||
| 	Cases []node.Node | ||||
| } | ||||
| 
 | ||||
| // NewCaseList node constructor | ||||
| func NewCaseList(Cases []node.Node) *CaseList { | ||||
| 	return &CaseList{ | ||||
| 		Cases, | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| // Attributes returns node attributes as map | ||||
| func (n *CaseList) Attributes() map[string]interface{} { | ||||
| 	return nil | ||||
| } | ||||
| 
 | ||||
| // Walk traverses nodes | ||||
| // Walk is invoked recursively until v.EnterNode returns true | ||||
| func (n *CaseList) Walk(v walker.Visitor) { | ||||
| 	if v.EnterNode(n) == false { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	if n.Cases != nil { | ||||
| 		vv := v.GetChildrenVisitor("Cases") | ||||
| 		for _, nn := range n.Cases { | ||||
| 			if nn != nil { | ||||
| 				nn.Walk(vv) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	v.LeaveNode(n) | ||||
| } | ||||
| @ -7,15 +7,15 @@ import ( | ||||
| 
 | ||||
| // Switch node | ||||
| type Switch struct { | ||||
| 	Cond  node.Node | ||||
| 	Cases []node.Node | ||||
| 	Cond          node.Node | ||||
| 	CaseList *CaseList | ||||
| } | ||||
| 
 | ||||
| // NewSwitch node constructor | ||||
| func NewSwitch(Cond node.Node, Cases []node.Node) *Switch { | ||||
| func NewSwitch(Cond node.Node, CaseList *CaseList) *Switch { | ||||
| 	return &Switch{ | ||||
| 		Cond, | ||||
| 		Cases, | ||||
| 		CaseList, | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| @ -36,13 +36,9 @@ func (n *Switch) Walk(v walker.Visitor) { | ||||
| 		n.Cond.Walk(vv) | ||||
| 	} | ||||
| 
 | ||||
| 	if n.Cases != nil { | ||||
| 		vv := v.GetChildrenVisitor("Cases") | ||||
| 		for _, nn := range n.Cases { | ||||
| 			if nn != nil { | ||||
| 				nn.Walk(vv) | ||||
| 			} | ||||
| 		} | ||||
| 	if n.CaseList != nil { | ||||
| 		vv := v.GetChildrenVisitor("CaseList") | ||||
| 		n.CaseList.Walk(vv) | ||||
| 	} | ||||
| 
 | ||||
| 	v.LeaveNode(n) | ||||
|  | ||||
| @ -25,17 +25,19 @@ func TestAltSwitch(t *testing.T) { | ||||
| 		Stmts: []node.Node{ | ||||
| 			&stmt.AltSwitch{ | ||||
| 				Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 				Cases: []node.Node{ | ||||
| 					&stmt.Case{ | ||||
| 						Cond:  &scalar.Lnumber{Value: "1"}, | ||||
| 						Stmts: []node.Node{}, | ||||
| 					}, | ||||
| 					&stmt.Default{ | ||||
| 						Stmts: []node.Node{}, | ||||
| 					}, | ||||
| 					&stmt.Case{ | ||||
| 						Cond:  &scalar.Lnumber{Value: "2"}, | ||||
| 						Stmts: []node.Node{}, | ||||
| 				CaseList: &stmt.CaseList{ | ||||
| 					Cases: []node.Node{ | ||||
| 						&stmt.Case{ | ||||
| 							Cond:  &scalar.Lnumber{Value: "1"}, | ||||
| 							Stmts: []node.Node{}, | ||||
| 						}, | ||||
| 						&stmt.Default{ | ||||
| 							Stmts: []node.Node{}, | ||||
| 						}, | ||||
| 						&stmt.Case{ | ||||
| 							Cond:  &scalar.Lnumber{Value: "2"}, | ||||
| 							Stmts: []node.Node{}, | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| @ -65,14 +67,16 @@ func TestAltSwitchSemicolon(t *testing.T) { | ||||
| 		Stmts: []node.Node{ | ||||
| 			&stmt.AltSwitch{ | ||||
| 				Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 				Cases: []node.Node{ | ||||
| 					&stmt.Case{ | ||||
| 						Cond:  &scalar.Lnumber{Value: "1"}, | ||||
| 						Stmts: []node.Node{}, | ||||
| 					}, | ||||
| 					&stmt.Case{ | ||||
| 						Cond:  &scalar.Lnumber{Value: "2"}, | ||||
| 						Stmts: []node.Node{}, | ||||
| 				CaseList: &stmt.CaseList{ | ||||
| 					Cases: []node.Node{ | ||||
| 						&stmt.Case{ | ||||
| 							Cond:  &scalar.Lnumber{Value: "1"}, | ||||
| 							Stmts: []node.Node{}, | ||||
| 						}, | ||||
| 						&stmt.Case{ | ||||
| 							Cond:  &scalar.Lnumber{Value: "2"}, | ||||
| 							Stmts: []node.Node{}, | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| @ -102,17 +106,19 @@ func TestSwitch(t *testing.T) { | ||||
| 		Stmts: []node.Node{ | ||||
| 			&stmt.Switch{ | ||||
| 				Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 				Cases: []node.Node{ | ||||
| 					&stmt.Case{ | ||||
| 						Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 						Stmts: []node.Node{ | ||||
| 							&stmt.Break{}, | ||||
| 				CaseList: &stmt.CaseList{ | ||||
| 					Cases: []node.Node{ | ||||
| 						&stmt.Case{ | ||||
| 							Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 							Stmts: []node.Node{ | ||||
| 								&stmt.Break{}, | ||||
| 							}, | ||||
| 						}, | ||||
| 					}, | ||||
| 					&stmt.Case{ | ||||
| 						Cond: &scalar.Lnumber{Value: "2"}, | ||||
| 						Stmts: []node.Node{ | ||||
| 							&stmt.Break{}, | ||||
| 						&stmt.Case{ | ||||
| 							Cond: &scalar.Lnumber{Value: "2"}, | ||||
| 							Stmts: []node.Node{ | ||||
| 								&stmt.Break{}, | ||||
| 							}, | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
| @ -143,17 +149,19 @@ func TestSwitchSemicolon(t *testing.T) { | ||||
| 		Stmts: []node.Node{ | ||||
| 			&stmt.Switch{ | ||||
| 				Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 				Cases: []node.Node{ | ||||
| 					&stmt.Case{ | ||||
| 						Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 						Stmts: []node.Node{ | ||||
| 							&stmt.Break{}, | ||||
| 				CaseList: &stmt.CaseList{ | ||||
| 					Cases: []node.Node{ | ||||
| 						&stmt.Case{ | ||||
| 							Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 							Stmts: []node.Node{ | ||||
| 								&stmt.Break{}, | ||||
| 							}, | ||||
| 						}, | ||||
| 					}, | ||||
| 					&stmt.Case{ | ||||
| 						Cond: &scalar.Lnumber{Value: "2"}, | ||||
| 						Stmts: []node.Node{ | ||||
| 							&stmt.Break{}, | ||||
| 						&stmt.Case{ | ||||
| 							Cond: &scalar.Lnumber{Value: "2"}, | ||||
| 							Stmts: []node.Node{ | ||||
| 								&stmt.Break{}, | ||||
| 							}, | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
|  | ||||
| @ -360,18 +360,18 @@ var nodesToTest = []struct { | ||||
| 	}, | ||||
| 	{ | ||||
| 		&stmt.Switch{ | ||||
| 			Cond:  &expr.Variable{}, | ||||
| 			Cases: []node.Node{&stmt.Expression{}}, | ||||
| 			Cond:          &expr.Variable{}, | ||||
| 			CaseList: &stmt.CaseList{}, | ||||
| 		}, | ||||
| 		[]string{"Cond", "Cases"}, | ||||
| 		[]string{"Cond", "CaseList"}, | ||||
| 		map[string]interface{}{}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		&stmt.AltSwitch{ | ||||
| 			Cond:  &expr.Variable{}, | ||||
| 			Cases: []node.Node{&stmt.Expression{}}, | ||||
| 			Cond:     &expr.Variable{}, | ||||
| 			CaseList: &stmt.CaseList{Cases: []node.Node{}}, | ||||
| 		}, | ||||
| 		[]string{"Cond", "Cases"}, | ||||
| 		[]string{"Cond", "CaseList"}, | ||||
| 		map[string]interface{}{}, | ||||
| 	}, | ||||
| 	{ | ||||
| @ -479,6 +479,13 @@ var nodesToTest = []struct { | ||||
| 		[]string{"Stmts"}, | ||||
| 		map[string]interface{}{}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		&stmt.CaseList{ | ||||
| 			Cases: []node.Node{&stmt.Expression{}}, | ||||
| 		}, | ||||
| 		[]string{"Cases"}, | ||||
| 		map[string]interface{}{}, | ||||
| 	}, | ||||
| 	{ | ||||
| 		&stmt.TraitAdaptationList{ | ||||
| 			Adaptations: []node.Node{&stmt.TraitUsePrecedence{}}, | ||||
|  | ||||
							
								
								
									
										1301
									
								
								php5/php5.go
									
									
									
									
									
								
							
							
						
						
									
										1301
									
								
								php5/php5.go
									
									
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										52
									
								
								php5/php5.y
									
									
									
									
									
								
							
							
						
						
									
										52
									
								
								php5/php5.y
									
									
									
									
									
								
							| @ -244,6 +244,7 @@ import ( | ||||
| %type <node> static_scalar_value static_operation | ||||
| %type <node> ctor_arguments function_call_parameter_list | ||||
| %type <node> trait_adaptations | ||||
| %type <node> switch_case_list | ||||
| 
 | ||||
| %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 | ||||
| @ -260,7 +261,7 @@ import ( | ||||
| 
 | ||||
| %type <simpleIndirectReference> simple_indirect_reference | ||||
| %type <foreachVariable> foreach_variable foreach_optional_arg | ||||
| %type <nodesWithEndToken> switch_case_list method_body | ||||
| %type <nodesWithEndToken> method_body | ||||
| %type <boolWithToken> is_reference is_variadic | ||||
| %type <altSyntaxNode> while_statement for_statement foreach_statement | ||||
| 
 | ||||
| @ -684,12 +685,18 @@ unticked_statement: | ||||
|             } | ||||
|     |   T_SWITCH parenthesis_expr switch_case_list | ||||
|             { | ||||
|                 if ($3.endToken.Value == ";") { | ||||
|                     $$ = stmt.NewAltSwitch($2, $3.nodes) | ||||
|                 } else { | ||||
|                     $$ = stmt.NewSwitch($2, $3.nodes) | ||||
|                 switch n := $3.(type) { | ||||
|                 case *stmt.Switch: | ||||
|                     n.Cond = $2 | ||||
|                 case *stmt.AltSwitch: | ||||
|                     n.Cond = $2 | ||||
|                 default: | ||||
|                     panic("unexpected node type") | ||||
|                 } | ||||
|                 yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $3.endToken)) | ||||
| 
 | ||||
|                 $$ = $3 | ||||
| 
 | ||||
|                 yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokenNodePosition($1, $3)) | ||||
|                 yylex.(*Parser).comments.AddComments($$, $1.Comments()) | ||||
|             } | ||||
|     |   T_BREAK ';' | ||||
| @ -1153,13 +1160,38 @@ declare_list: | ||||
| 
 | ||||
| switch_case_list: | ||||
|         '{' case_list '}' | ||||
|             { $$ = &nodesWithEndToken{$2, $3} } | ||||
|             { | ||||
|                 caseList := stmt.NewCaseList($2) | ||||
|                 $$ = stmt.NewSwitch(nil, caseList) | ||||
| 
 | ||||
|                 yylex.(*Parser).positions.AddPosition(caseList, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $3)) | ||||
|                 yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $3)) | ||||
|             } | ||||
|     |   '{' ';' case_list '}' | ||||
|             { $$ = &nodesWithEndToken{$3, $4} } | ||||
|             { | ||||
|                 caseList := stmt.NewCaseList($3) | ||||
|                 $$ = stmt.NewSwitch(nil, caseList) | ||||
| 
 | ||||
|                 yylex.(*Parser).positions.AddPosition(caseList, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $4)) | ||||
|                 yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $4)) | ||||
|             } | ||||
|     |   ':' case_list T_ENDSWITCH ';' | ||||
|             { $$ = &nodesWithEndToken{$2, $4} } | ||||
|             { | ||||
|                 caseList := stmt.NewCaseList($2) | ||||
|                 $$ = stmt.NewAltSwitch(nil, caseList) | ||||
| 
 | ||||
|                 yylex.(*Parser).positions.AddPosition(caseList, yylex.(*Parser).positionBuilder.NewNodeListPosition($2)) | ||||
|                 yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $4)) | ||||
|             } | ||||
|     |   ':' ';' case_list T_ENDSWITCH ';' | ||||
|             { $$ = &nodesWithEndToken{$3, $5} } | ||||
|             { | ||||
|                  | ||||
|                 caseList := stmt.NewCaseList($3) | ||||
|                 $$ = stmt.NewAltSwitch(nil, caseList) | ||||
| 
 | ||||
|                 yylex.(*Parser).positions.AddPosition(caseList, yylex.(*Parser).positionBuilder.NewNodeListPosition($3)) | ||||
|                 yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $5)) | ||||
|             } | ||||
| ; | ||||
| 
 | ||||
| 
 | ||||
|  | ||||
| @ -1354,63 +1354,71 @@ func TestPhp5(t *testing.T) { | ||||
| 			}, | ||||
| 			&stmt.AltSwitch{ | ||||
| 				Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 				Cases: []node.Node{ | ||||
| 					&stmt.Case{ | ||||
| 						Cond:  &scalar.Lnumber{Value: "1"}, | ||||
| 						Stmts: []node.Node{}, | ||||
| 					}, | ||||
| 					&stmt.Default{ | ||||
| 						Stmts: []node.Node{}, | ||||
| 					}, | ||||
| 					&stmt.Case{ | ||||
| 						Cond:  &scalar.Lnumber{Value: "2"}, | ||||
| 						Stmts: []node.Node{}, | ||||
| 				CaseList: &stmt.CaseList{ | ||||
| 					Cases: []node.Node{ | ||||
| 						&stmt.Case{ | ||||
| 							Cond:  &scalar.Lnumber{Value: "1"}, | ||||
| 							Stmts: []node.Node{}, | ||||
| 						}, | ||||
| 						&stmt.Default{ | ||||
| 							Stmts: []node.Node{}, | ||||
| 						}, | ||||
| 						&stmt.Case{ | ||||
| 							Cond:  &scalar.Lnumber{Value: "2"}, | ||||
| 							Stmts: []node.Node{}, | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			&stmt.AltSwitch{ | ||||
| 				Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 				Cases: []node.Node{ | ||||
| 					&stmt.Case{ | ||||
| 						Cond:  &scalar.Lnumber{Value: "1"}, | ||||
| 						Stmts: []node.Node{}, | ||||
| 					}, | ||||
| 					&stmt.Case{ | ||||
| 						Cond:  &scalar.Lnumber{Value: "2"}, | ||||
| 						Stmts: []node.Node{}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			&stmt.Switch{ | ||||
| 				Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 				Cases: []node.Node{ | ||||
| 					&stmt.Case{ | ||||
| 						Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 						Stmts: []node.Node{ | ||||
| 							&stmt.Break{}, | ||||
| 				CaseList: &stmt.CaseList{ | ||||
| 					Cases: []node.Node{ | ||||
| 						&stmt.Case{ | ||||
| 							Cond:  &scalar.Lnumber{Value: "1"}, | ||||
| 							Stmts: []node.Node{}, | ||||
| 						}, | ||||
| 					}, | ||||
| 					&stmt.Case{ | ||||
| 						Cond: &scalar.Lnumber{Value: "2"}, | ||||
| 						Stmts: []node.Node{ | ||||
| 							&stmt.Break{}, | ||||
| 						&stmt.Case{ | ||||
| 							Cond:  &scalar.Lnumber{Value: "2"}, | ||||
| 							Stmts: []node.Node{}, | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			&stmt.Switch{ | ||||
| 				Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 				Cases: []node.Node{ | ||||
| 					&stmt.Case{ | ||||
| 						Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 						Stmts: []node.Node{ | ||||
| 							&stmt.Break{}, | ||||
| 				CaseList: &stmt.CaseList{ | ||||
| 					Cases: []node.Node{ | ||||
| 						&stmt.Case{ | ||||
| 							Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 							Stmts: []node.Node{ | ||||
| 								&stmt.Break{}, | ||||
| 							}, | ||||
| 						}, | ||||
| 						&stmt.Case{ | ||||
| 							Cond: &scalar.Lnumber{Value: "2"}, | ||||
| 							Stmts: []node.Node{ | ||||
| 								&stmt.Break{}, | ||||
| 							}, | ||||
| 						}, | ||||
| 					}, | ||||
| 					&stmt.Case{ | ||||
| 						Cond: &scalar.Lnumber{Value: "2"}, | ||||
| 						Stmts: []node.Node{ | ||||
| 							&stmt.Break{}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			&stmt.Switch{ | ||||
| 				Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 				CaseList: &stmt.CaseList{ | ||||
| 					Cases: []node.Node{ | ||||
| 						&stmt.Case{ | ||||
| 							Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 							Stmts: []node.Node{ | ||||
| 								&stmt.Break{}, | ||||
| 							}, | ||||
| 						}, | ||||
| 						&stmt.Case{ | ||||
| 							Cond: &scalar.Lnumber{Value: "2"}, | ||||
| 							Stmts: []node.Node{ | ||||
| 								&stmt.Break{}, | ||||
| 							}, | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
|  | ||||
							
								
								
									
										1076
									
								
								php7/php7.go
									
									
									
									
									
								
							
							
						
						
									
										1076
									
								
								php7/php7.go
									
									
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										79
									
								
								php7/php7.y
									
									
									
									
									
								
							
							
						
						
									
										79
									
								
								php7/php7.y
									
									
									
									
									
								
							| @ -265,12 +265,13 @@ import ( | ||||
| %type <node> class_modifier | ||||
| %type <node> argument_list ctor_arguments | ||||
| %type <node> trait_adaptations | ||||
| %type <node> switch_case_list | ||||
| 
 | ||||
| %type <node> member_modifier | ||||
| %type <node> use_type | ||||
| %type <foreachVariable> foreach_variable | ||||
| 
 | ||||
| %type <nodesWithEndToken> method_body switch_case_list | ||||
| %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 | ||||
| @ -823,14 +824,19 @@ statement: | ||||
|             } | ||||
|     |   T_SWITCH '(' expr ')' switch_case_list | ||||
|             { | ||||
|                 if ($5.endToken.Value == ";") { | ||||
|                     $$ = stmt.NewAltSwitch($3, $5.nodes) | ||||
|                 } else { | ||||
|                     $$ = stmt.NewSwitch($3, $5.nodes) | ||||
|                 switch n := $5.(type) { | ||||
|                 case *stmt.Switch: | ||||
|                     n.Cond = $3 | ||||
|                 case *stmt.AltSwitch: | ||||
|                     n.Cond = $3 | ||||
|                 default: | ||||
|                     panic("unexpected node type") | ||||
|                 } | ||||
| 
 | ||||
|                 $$ = $5 | ||||
| 
 | ||||
|                 // save position | ||||
|                 yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $5.endToken)) | ||||
|                 yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokenNodePosition($1, $5)) | ||||
| 
 | ||||
|                 // save comments | ||||
|                 yylex.(*Parser).comments.AddFromToken($$, $1, comment.SwitchToken) | ||||
| @ -1399,10 +1405,63 @@ declare_statement: | ||||
| ; | ||||
| 
 | ||||
| switch_case_list: | ||||
|         '{' case_list '}'                               { $$ = &nodesWithEndToken{$2, $3} } | ||||
|     |   '{' ';' case_list '}'                           { $$ = &nodesWithEndToken{$3, $4} } | ||||
|     |   ':' case_list T_ENDSWITCH ';'                   { $$ = &nodesWithEndToken{$2, $4} } | ||||
|     |   ':' ';' case_list T_ENDSWITCH ';'               { $$ = &nodesWithEndToken{$3, $5} } | ||||
|         '{' case_list '}' | ||||
|             { | ||||
|                 caseList := stmt.NewCaseList($2) | ||||
|                 $$ = stmt.NewSwitch(nil, caseList) | ||||
| 
 | ||||
|                 // save position | ||||
|                 yylex.(*Parser).positions.AddPosition(caseList, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $3)) | ||||
|                 yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $3)) | ||||
| 
 | ||||
|                 // save comments | ||||
|                 yylex.(*Parser).comments.AddFromToken(caseList, $1, comment.OpenCurlyBracesToken) | ||||
|                 yylex.(*Parser).comments.AddFromToken(caseList, $3, comment.CloseCurlyBracesToken) | ||||
|             } | ||||
|     |   '{' ';' case_list '}' | ||||
|             { | ||||
|                 caseList := stmt.NewCaseList($3) | ||||
|                 $$ = stmt.NewSwitch(nil, caseList) | ||||
| 
 | ||||
|                 // save position | ||||
|                 yylex.(*Parser).positions.AddPosition(caseList, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $4)) | ||||
|                 yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $4)) | ||||
| 
 | ||||
|                 // save comments | ||||
|                 yylex.(*Parser).comments.AddFromToken(caseList, $1, comment.OpenCurlyBracesToken) | ||||
|                 yylex.(*Parser).comments.AddFromToken(caseList, $2, comment.SemiColonToken) | ||||
|                 yylex.(*Parser).comments.AddFromToken(caseList, $4, comment.CloseCurlyBracesToken) | ||||
|             } | ||||
|     |   ':' case_list T_ENDSWITCH ';' | ||||
|             { | ||||
|                 caseList := stmt.NewCaseList($2) | ||||
|                 $$ = stmt.NewAltSwitch(nil, caseList) | ||||
| 
 | ||||
|                 // save position | ||||
|                 yylex.(*Parser).positions.AddPosition(caseList, yylex.(*Parser).positionBuilder.NewNodeListPosition($2)) | ||||
|                 yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $4)) | ||||
| 
 | ||||
|                 // save comments | ||||
|                 yylex.(*Parser).comments.AddFromToken(caseList, $1, comment.ColonToken) | ||||
|                 yylex.(*Parser).comments.AddFromToken(caseList, $3, comment.EndswitchToken) | ||||
|                 yylex.(*Parser).comments.AddFromToken($$, $4, comment.SemiColonToken) | ||||
|             } | ||||
|     |   ':' ';' case_list T_ENDSWITCH ';' | ||||
|             { | ||||
| 
 | ||||
|                 caseList := stmt.NewCaseList($3) | ||||
|                 $$ = stmt.NewAltSwitch(nil, caseList) | ||||
| 
 | ||||
|                 // save position | ||||
|                 yylex.(*Parser).positions.AddPosition(caseList, yylex.(*Parser).positionBuilder.NewNodeListPosition($3)) | ||||
|                 yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $5)) | ||||
| 
 | ||||
|                 // save comments | ||||
|                 yylex.(*Parser).comments.AddFromToken(caseList, $1, comment.ColonToken) | ||||
|                 yylex.(*Parser).comments.AddFromToken(caseList, $2, comment.SemiColonToken) | ||||
|                 yylex.(*Parser).comments.AddFromToken(caseList, $4, comment.EndswitchToken) | ||||
|                 yylex.(*Parser).comments.AddFromToken($$, $5, comment.SemiColonToken) | ||||
|             } | ||||
| ; | ||||
| 
 | ||||
| case_list: | ||||
|  | ||||
| @ -1381,63 +1381,71 @@ func TestPhp7(t *testing.T) { | ||||
| 			}, | ||||
| 			&stmt.AltSwitch{ | ||||
| 				Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 				Cases: []node.Node{ | ||||
| 					&stmt.Case{ | ||||
| 						Cond:  &scalar.Lnumber{Value: "1"}, | ||||
| 						Stmts: []node.Node{}, | ||||
| 					}, | ||||
| 					&stmt.Default{ | ||||
| 						Stmts: []node.Node{}, | ||||
| 					}, | ||||
| 					&stmt.Case{ | ||||
| 						Cond:  &scalar.Lnumber{Value: "2"}, | ||||
| 						Stmts: []node.Node{}, | ||||
| 				CaseList: &stmt.CaseList{ | ||||
| 					Cases: []node.Node{ | ||||
| 						&stmt.Case{ | ||||
| 							Cond:  &scalar.Lnumber{Value: "1"}, | ||||
| 							Stmts: []node.Node{}, | ||||
| 						}, | ||||
| 						&stmt.Default{ | ||||
| 							Stmts: []node.Node{}, | ||||
| 						}, | ||||
| 						&stmt.Case{ | ||||
| 							Cond:  &scalar.Lnumber{Value: "2"}, | ||||
| 							Stmts: []node.Node{}, | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			&stmt.AltSwitch{ | ||||
| 				Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 				Cases: []node.Node{ | ||||
| 					&stmt.Case{ | ||||
| 						Cond:  &scalar.Lnumber{Value: "1"}, | ||||
| 						Stmts: []node.Node{}, | ||||
| 					}, | ||||
| 					&stmt.Case{ | ||||
| 						Cond:  &scalar.Lnumber{Value: "2"}, | ||||
| 						Stmts: []node.Node{}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			&stmt.Switch{ | ||||
| 				Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 				Cases: []node.Node{ | ||||
| 					&stmt.Case{ | ||||
| 						Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 						Stmts: []node.Node{ | ||||
| 							&stmt.Break{}, | ||||
| 				CaseList: &stmt.CaseList{ | ||||
| 					Cases: []node.Node{ | ||||
| 						&stmt.Case{ | ||||
| 							Cond:  &scalar.Lnumber{Value: "1"}, | ||||
| 							Stmts: []node.Node{}, | ||||
| 						}, | ||||
| 					}, | ||||
| 					&stmt.Case{ | ||||
| 						Cond: &scalar.Lnumber{Value: "2"}, | ||||
| 						Stmts: []node.Node{ | ||||
| 							&stmt.Break{}, | ||||
| 						&stmt.Case{ | ||||
| 							Cond:  &scalar.Lnumber{Value: "2"}, | ||||
| 							Stmts: []node.Node{}, | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			&stmt.Switch{ | ||||
| 				Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 				Cases: []node.Node{ | ||||
| 					&stmt.Case{ | ||||
| 						Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 						Stmts: []node.Node{ | ||||
| 							&stmt.Break{}, | ||||
| 				CaseList: &stmt.CaseList{ | ||||
| 					Cases: []node.Node{ | ||||
| 						&stmt.Case{ | ||||
| 							Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 							Stmts: []node.Node{ | ||||
| 								&stmt.Break{}, | ||||
| 							}, | ||||
| 						}, | ||||
| 						&stmt.Case{ | ||||
| 							Cond: &scalar.Lnumber{Value: "2"}, | ||||
| 							Stmts: []node.Node{ | ||||
| 								&stmt.Break{}, | ||||
| 							}, | ||||
| 						}, | ||||
| 					}, | ||||
| 					&stmt.Case{ | ||||
| 						Cond: &scalar.Lnumber{Value: "2"}, | ||||
| 						Stmts: []node.Node{ | ||||
| 							&stmt.Break{}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			&stmt.Switch{ | ||||
| 				Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 				CaseList: &stmt.CaseList{ | ||||
| 					Cases: []node.Node{ | ||||
| 						&stmt.Case{ | ||||
| 							Cond: &scalar.Lnumber{Value: "1"}, | ||||
| 							Stmts: []node.Node{ | ||||
| 								&stmt.Break{}, | ||||
| 							}, | ||||
| 						}, | ||||
| 						&stmt.Case{ | ||||
| 							Cond: &scalar.Lnumber{Value: "2"}, | ||||
| 							Stmts: []node.Node{ | ||||
| 								&stmt.Break{}, | ||||
| 							}, | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
|  | ||||
| @ -1428,7 +1428,7 @@ func (p *Printer) printStmtAltSwitch(n node.Node) { | ||||
| 	p.Print(nn.Cond) | ||||
| 	io.WriteString(p.w, ") :\n") | ||||
| 
 | ||||
| 	s := nn.Cases | ||||
| 	s := nn.CaseList.Cases | ||||
| 	p.printNodes(s) | ||||
| 
 | ||||
| 	io.WriteString(p.w, "\n") | ||||
| @ -2009,7 +2009,7 @@ func (p *Printer) printStmtSwitch(n node.Node) { | ||||
| 	io.WriteString(p.w, ")") | ||||
| 
 | ||||
| 	io.WriteString(p.w, " {\n") | ||||
| 	p.printNodes(nn.Cases) | ||||
| 	p.printNodes(nn.CaseList.Cases) | ||||
| 	io.WriteString(p.w, "\n") | ||||
| 	p.printIndent() | ||||
| 	io.WriteString(p.w, "}") | ||||
|  | ||||
| @ -2284,17 +2284,19 @@ func TestPrintStmtAltSwitch(t *testing.T) { | ||||
| 		Stmts: []node.Node{ | ||||
| 			&stmt.AltSwitch{ | ||||
| 				Cond: &expr.Variable{VarName: &node.Identifier{Value: "var"}}, | ||||
| 				Cases: []node.Node{ | ||||
| 					&stmt.Case{ | ||||
| 						Cond: &scalar.String{Value: "'a'"}, | ||||
| 						Stmts: []node.Node{ | ||||
| 							&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}}, | ||||
| 				CaseList: &stmt.CaseList{ | ||||
| 					Cases: []node.Node{ | ||||
| 						&stmt.Case{ | ||||
| 							Cond: &scalar.String{Value: "'a'"}, | ||||
| 							Stmts: []node.Node{ | ||||
| 								&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}}, | ||||
| 							}, | ||||
| 						}, | ||||
| 					}, | ||||
| 					&stmt.Case{ | ||||
| 						Cond: &scalar.String{Value: "'b'"}, | ||||
| 						Stmts: []node.Node{ | ||||
| 							&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}}, | ||||
| 						&stmt.Case{ | ||||
| 							Cond: &scalar.String{Value: "'b'"}, | ||||
| 							Stmts: []node.Node{ | ||||
| 								&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}}, | ||||
| 							}, | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
| @ -3649,17 +3651,19 @@ func TestPrintStmtSwitch(t *testing.T) { | ||||
| 		Stmts: []node.Node{ | ||||
| 			&stmt.Switch{ | ||||
| 				Cond: &expr.Variable{VarName: &node.Identifier{Value: "var"}}, | ||||
| 				Cases: []node.Node{ | ||||
| 					&stmt.Case{ | ||||
| 						Cond: &scalar.String{Value: "'a'"}, | ||||
| 						Stmts: []node.Node{ | ||||
| 							&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}}, | ||||
| 				CaseList: &stmt.CaseList{ | ||||
| 					Cases: []node.Node{ | ||||
| 						&stmt.Case{ | ||||
| 							Cond: &scalar.String{Value: "'a'"}, | ||||
| 							Stmts: []node.Node{ | ||||
| 								&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}}, | ||||
| 							}, | ||||
| 						}, | ||||
| 					}, | ||||
| 					&stmt.Case{ | ||||
| 						Cond: &scalar.String{Value: "'b'"}, | ||||
| 						Stmts: []node.Node{ | ||||
| 							&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}}, | ||||
| 						&stmt.Case{ | ||||
| 							Cond: &scalar.String{Value: "'b'"}, | ||||
| 							Stmts: []node.Node{ | ||||
| 								&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}}, | ||||
| 							}, | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user