[refactoring] update ast structure of "Try", "Catch" and "Finally" nodes

This commit is contained in:
Vadym Slizov 2020-09-14 20:15:50 +03:00
parent 33af1df9c4
commit 80aa328386
7 changed files with 148 additions and 121 deletions

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -1150,19 +1150,20 @@ unticked_statement:
} }
| T_TRY '{' inner_statement_list '}' catch_statement finally_statement | T_TRY '{' inner_statement_list '}' catch_statement finally_statement
{ {
$$ = &ast.StmtTry{ast.Node{}, $3, $5, $6} $$ = &ast.StmtTry{
TryTkn: $1,
OpenCurlyBracket: $2,
Stmts: $3,
CloseCurlyBracket: $4,
Catches: $5,
Finally: $6,
}
// save position
if $6 == nil { if $6 == nil {
$$.GetNode().Position = position.NewTokenNodeListPosition($1, $5) $$.GetNode().Position = position.NewTokenNodeListPosition($1, $5)
} else { } else {
$$.GetNode().Position = position.NewTokenNodePosition($1, $6) $$.GetNode().Position = position.NewTokenNodePosition($1, $6)
} }
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Try, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Stmts, $4.SkippedTokens)
} }
| T_THROW expr ';' | T_THROW expr ';'
{ {
@ -1202,21 +1203,27 @@ catch_statement:
{ {
identifier := &ast.Identifier{ast.Node{}, $4.Value} identifier := &ast.Identifier{ast.Node{}, $4.Value}
variable := &ast.ExprVariable{ast.Node{}, identifier} variable := &ast.ExprVariable{ast.Node{}, identifier}
catchNode := &ast.StmtCatch{ast.Node{}, []ast.Vertex{$3}, variable, $7} catch := &ast.StmtCatch{
$$ = append([]ast.Vertex{catchNode}, $9...) Node: ast.Node{
Position: position.NewTokensPosition($1, $8),
},
CatchTkn: $1,
OpenParenthesisTkn: $2,
Types: []ast.Vertex{$3},
Var: variable,
CloseParenthesisTkn: $5,
OpenCurlyBracketTkn: $6,
Stmts: $7,
CloseCurlyBracketTkn: $8,
}
$$ = append([]ast.Vertex{catch}, $9...)
// save position // save position
identifier.GetNode().Position = position.NewTokenPosition($4) identifier.GetNode().Position = position.NewTokenPosition($4)
variable.GetNode().Position = position.NewTokenPosition($4) variable.GetNode().Position = position.NewTokenPosition($4)
catchNode.GetNode().Position = position.NewTokensPosition($1, $8)
// save comments // save comments
yylex.(*Parser).setFreeFloating(catchNode, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating(catchNode, token.Catch, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating(variable, token.Start, $4.SkippedTokens) yylex.(*Parser).setFreeFloating(variable, token.Start, $4.SkippedTokens)
yylex.(*Parser).setFreeFloating(catchNode, token.Var, $5.SkippedTokens)
yylex.(*Parser).setFreeFloating(catchNode, token.Cond, $6.SkippedTokens)
yylex.(*Parser).setFreeFloating(catchNode, token.Stmts, $8.SkippedTokens)
} }
; ;
@ -1227,15 +1234,15 @@ finally_statement:
} }
| T_FINALLY '{' inner_statement_list '}' | T_FINALLY '{' inner_statement_list '}'
{ {
$$ = &ast.StmtFinally{ast.Node{}, $3} $$ = &ast.StmtFinally{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $4),
$$.GetNode().Position = position.NewTokensPosition($1, $4) },
FinallyTkn: $1,
// save comments OpenCurlyBracketTkn: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) Stmts: $3,
yylex.(*Parser).setFreeFloating($$, token.Finally, $2.SkippedTokens) CloseCurlyBracketTkn: $4,
yylex.(*Parser).setFreeFloating($$, token.Stmts, $4.SkippedTokens) }
} }
; ;
@ -1266,20 +1273,26 @@ additional_catch:
{ {
identifier := &ast.Identifier{ast.Node{}, $4.Value} identifier := &ast.Identifier{ast.Node{}, $4.Value}
variable := &ast.ExprVariable{ast.Node{}, identifier} variable := &ast.ExprVariable{ast.Node{}, identifier}
$$ = &ast.StmtCatch{ast.Node{}, []ast.Vertex{$3}, variable, $7} $$ = &ast.StmtCatch{
Node: ast.Node{
Position: position.NewTokensPosition($1, $8),
},
CatchTkn: $1,
OpenParenthesisTkn: $2,
Types: []ast.Vertex{$3},
Var: variable,
CloseParenthesisTkn: $5,
OpenCurlyBracketTkn: $6,
Stmts: $7,
CloseCurlyBracketTkn: $8,
}
// save position // save position
identifier.GetNode().Position = position.NewTokenPosition($4) identifier.GetNode().Position = position.NewTokenPosition($4)
variable.GetNode().Position = position.NewTokenPosition($4) variable.GetNode().Position = position.NewTokenPosition($4)
$$.GetNode().Position = position.NewTokensPosition($1, $8)
// save comments // save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Catch, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating(variable, token.Start, $4.SkippedTokens) yylex.(*Parser).setFreeFloating(variable, token.Start, $4.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Var, $5.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Cond, $6.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Stmts, $8.SkippedTokens)
} }
; ;

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -249,7 +249,7 @@ import (
%type <node> exit_expr scalar lexical_var function_call member_name property_name %type <node> exit_expr scalar lexical_var function_call member_name property_name
%type <node> variable_class_name dereferencable_scalar constant dereferencable %type <node> variable_class_name dereferencable_scalar constant dereferencable
%type <node> callable_expr callable_variable static_member new_variable %type <node> callable_expr callable_variable static_member new_variable
%type <node> encaps_var encaps_var_offset echo_expr_list %type <node> encaps_var encaps_var_offset echo_expr_list catch_name_list
%type <node> if_stmt const_list %type <node> if_stmt const_list
%type <node> alt_if_stmt %type <node> alt_if_stmt
%type <node> if_stmt_without_else %type <node> if_stmt_without_else
@ -275,7 +275,7 @@ import (
%type <node> foreach_variable %type <node> foreach_variable
%type <list> encaps_list backticks_expr namespace_name catch_name_list catch_list class_const_list %type <list> encaps_list backticks_expr namespace_name catch_list class_const_list
%type <list> for_exprs non_empty_for_exprs %type <list> for_exprs non_empty_for_exprs
%type <list> unprefixed_use_declarations inline_use_declarations property_list %type <list> unprefixed_use_declarations inline_use_declarations property_list
%type <list> case_list trait_adaptation_list %type <list> case_list trait_adaptation_list
@ -1060,18 +1060,20 @@ statement:
} }
| T_TRY '{' inner_statement_list '}' catch_list finally_statement | T_TRY '{' inner_statement_list '}' catch_list finally_statement
{ {
if $6 == nil { $$ = &ast.StmtTry{
$$ = &ast.StmtTry{ast.Node{}, $3, $5, $6} TryTkn: $1,
$$.GetNode().Position = position.NewTokenNodeListPosition($1, $5) OpenCurlyBracket: $2,
} else { Stmts: $3,
$$ = &ast.StmtTry{ast.Node{}, $3, $5, $6} CloseCurlyBracket: $4,
$$.GetNode().Position = position.NewTokenNodePosition($1, $6) Catches: $5,
Finally: $6,
} }
// save comments if $6 == nil {
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) $$.GetNode().Position = position.NewTokenNodeListPosition($1, $5)
yylex.(*Parser).setFreeFloating($$, token.Try, $2.SkippedTokens) } else {
yylex.(*Parser).setFreeFloating($$, token.Stmts, $4.SkippedTokens) $$.GetNode().Position = position.NewTokenNodePosition($1, $6)
}
} }
| T_THROW expr ';' | T_THROW expr ';'
{ {
@ -1123,36 +1125,40 @@ catch_list:
{ {
identifier := &ast.Identifier{ast.Node{}, $5.Value} identifier := &ast.Identifier{ast.Node{}, $5.Value}
variable := &ast.ExprVariable{ast.Node{}, identifier} variable := &ast.ExprVariable{ast.Node{}, identifier}
catch := &ast.StmtCatch{ast.Node{}, $4, variable, $8}
catch := $4.(*ast.StmtCatch)
catch.CatchTkn = $2
catch.OpenParenthesisTkn = $3
catch.Var = variable
catch.CloseParenthesisTkn = $6
catch.OpenCurlyBracketTkn = $7
catch.Stmts = $8
catch.CloseCurlyBracketTkn = $9
catch.GetNode().Position = position.NewTokensPosition($2, $9)
$$ = append($1, catch) $$ = append($1, catch)
// save position // save position
identifier.GetNode().Position = position.NewTokenPosition($5) identifier.GetNode().Position = position.NewTokenPosition($5)
variable.GetNode().Position = position.NewTokenPosition($5) variable.GetNode().Position = position.NewTokenPosition($5)
catch.GetNode().Position = position.NewTokensPosition($2, $9)
// save comments // save comments
yylex.(*Parser).setFreeFloating(catch, token.Start, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating(catch, token.Catch, $3.SkippedTokens)
yylex.(*Parser).setFreeFloating(variable, token.Start, $5.SkippedTokens) yylex.(*Parser).setFreeFloating(variable, token.Start, $5.SkippedTokens)
yylex.(*Parser).setFreeFloating(catch, token.Var, $6.SkippedTokens)
yylex.(*Parser).setFreeFloating(catch, token.Cond, $7.SkippedTokens)
yylex.(*Parser).setFreeFloating(catch, token.Stmts, $9.SkippedTokens)
} }
; ;
catch_name_list: catch_name_list:
name name
{ {
$$ = []ast.Vertex{$1} $$ = &ast.StmtCatch{
Types: []ast.Vertex{$1},
}
} }
| catch_name_list '|' name | catch_name_list '|' name
{ {
switch n := lastNode($1).(type) { $1.(*ast.StmtCatch).SeparatorTkns = append($1.(*ast.StmtCatch).SeparatorTkns, $2)
case *ast.NameName: n.ListSeparatorTkn = $2 $1.(*ast.StmtCatch).Types = append($1.(*ast.StmtCatch).Types, $3)
case *ast.NameFullyQualified: n.ListSeparatorTkn = $2
case *ast.NameRelative: n.ListSeparatorTkn = $2 $$ = $1
}
$$ = append($1, $3)
} }
; ;
@ -1163,15 +1169,15 @@ finally_statement:
} }
| T_FINALLY '{' inner_statement_list '}' | T_FINALLY '{' inner_statement_list '}'
{ {
$$ = &ast.StmtFinally{ast.Node{}, $3} $$ = &ast.StmtFinally{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $4),
$$.GetNode().Position = position.NewTokensPosition($1, $4) },
FinallyTkn: $1,
// save comments OpenCurlyBracketTkn: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) Stmts: $3,
yylex.(*Parser).setFreeFloating($$, token.Finally, $2.SkippedTokens) CloseCurlyBracketTkn: $4,
yylex.(*Parser).setFreeFloating($$, token.Stmts, $4.SkippedTokens) }
} }
; ;

View File

@ -203,9 +203,15 @@ func (n *StmtCase) Accept(v NodeVisitor) {
// StmtCatch node // StmtCatch node
type StmtCatch struct { type StmtCatch struct {
Node Node
Types []Vertex CatchTkn *token.Token
Var Vertex OpenParenthesisTkn *token.Token
Stmts []Vertex Types []Vertex
SeparatorTkns []*token.Token
Var Vertex
CloseParenthesisTkn *token.Token
OpenCurlyBracketTkn *token.Token
Stmts []Vertex
CloseCurlyBracketTkn *token.Token
} }
func (n *StmtCatch) Accept(v NodeVisitor) { func (n *StmtCatch) Accept(v NodeVisitor) {
@ -415,7 +421,10 @@ func (n *StmtExpression) Accept(v NodeVisitor) {
// StmtFinally node // StmtFinally node
type StmtFinally struct { type StmtFinally struct {
Node Node
Stmts []Vertex FinallyTkn *token.Token
OpenCurlyBracketTkn *token.Token
Stmts []Vertex
CloseCurlyBracketTkn *token.Token
} }
func (n *StmtFinally) Accept(v NodeVisitor) { func (n *StmtFinally) Accept(v NodeVisitor) {
@ -776,9 +785,12 @@ func (n *StmtTraitUsePrecedence) Accept(v NodeVisitor) {
// StmtTry node // StmtTry node
type StmtTry struct { type StmtTry struct {
Node Node
Stmts []Vertex TryTkn *token.Token
Catches []Vertex OpenCurlyBracket *token.Token
Finally Vertex Stmts []Vertex
CloseCurlyBracket *token.Token
Catches []Vertex
Finally Vertex
} }
func (n *StmtTry) Accept(v NodeVisitor) { func (n *StmtTry) Accept(v NodeVisitor) {

View File

@ -239,3 +239,24 @@ func (v *FilterTokens) StmtDeclare(n *ast.StmtDeclare) {
func (v *FilterTokens) StmtNop(n *ast.StmtNop) { func (v *FilterTokens) StmtNop(n *ast.StmtNop) {
n.SemiColonTkn = nil n.SemiColonTkn = nil
} }
func (v *FilterTokens) StmtTry(n *ast.StmtTry) {
n.TryTkn = nil
n.OpenCurlyBracket = nil
n.CloseCurlyBracket = nil
}
func (v *FilterTokens) StmtCatch(n *ast.StmtCatch) {
n.CatchTkn = nil
n.OpenParenthesisTkn = nil
n.SeparatorTkns = nil
n.CloseParenthesisTkn = nil
n.OpenCurlyBracketTkn = nil
n.CloseCurlyBracketTkn = nil
}
func (v *FilterTokens) StmtFinally(n *ast.StmtFinally) {
n.FinallyTkn = nil
n.OpenCurlyBracketTkn = nil
n.CloseCurlyBracketTkn = nil
}

View File

@ -2009,26 +2009,15 @@ func (p *Printer) printStmtCase(n *ast.StmtCase) {
p.printNodes(n.Stmts) p.printNodes(n.Stmts)
} }
func (p *Printer) printStmtCatch(n ast.Vertex) { func (p *Printer) printStmtCatch(n *ast.StmtCatch) {
nn := n.(*ast.StmtCatch) p.printToken(n.CatchTkn, "catch")
p.printFreeFloating(nn, token.Start) p.printToken(n.OpenParenthesisTkn, "(")
p.printSeparatedList(n.Types, n.SeparatorTkns, "|")
p.write([]byte("catch")) p.Print(n.Var)
p.printFreeFloating(nn, token.Catch) p.printToken(n.CloseParenthesisTkn, ")")
p.write([]byte("(")) p.printToken(n.OpenCurlyBracketTkn, "{")
p.printNodes(n.Stmts)
p.joinPrintRefactored("|", nn.Types) p.printToken(n.CloseCurlyBracketTkn, "}")
p.Print(nn.Var)
p.printFreeFloating(nn, token.Var)
p.write([]byte(")"))
p.printFreeFloating(nn, token.Cond)
p.write([]byte("{"))
p.printNodes(nn.Stmts)
p.printFreeFloating(nn, token.Stmts)
p.write([]byte("}"))
p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtClassMethod(n ast.Vertex) { func (p *Printer) printStmtClassMethod(n ast.Vertex) {
@ -2296,18 +2285,11 @@ func (p *Printer) printStmtExpression(n ast.Vertex) {
p.printFreeFloating(nn, token.End) p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtFinally(n ast.Vertex) { func (p *Printer) printStmtFinally(n *ast.StmtFinally) {
nn := n.(*ast.StmtFinally) p.printToken(n.FinallyTkn, "finally")
p.printFreeFloating(nn, token.Start) p.printToken(n.OpenCurlyBracketTkn, "{")
p.printNodes(n.Stmts)
p.write([]byte("finally")) p.printToken(n.CloseCurlyBracketTkn, "}")
p.printFreeFloating(nn, token.Finally)
p.write([]byte("{"))
p.printNodes(nn.Stmts)
p.printFreeFloating(nn, token.Stmts)
p.write([]byte("}"))
p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtFor(n *ast.StmtFor) { func (p *Printer) printStmtFor(n *ast.StmtFor) {
@ -2828,26 +2810,19 @@ func (p *Printer) printStmtTrait(n ast.Vertex) {
p.printFreeFloating(nn, token.End) p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtTry(n ast.Vertex) { func (p *Printer) printStmtTry(n *ast.StmtTry) {
nn := n.(*ast.StmtTry) p.printToken(n.TryTkn, "try")
p.printFreeFloating(nn, token.Start) p.printToken(n.OpenCurlyBracket, "{")
p.printNodes(n.Stmts)
p.printToken(n.CloseCurlyBracket, "}")
p.write([]byte("try")) if n.Catches != nil {
p.printFreeFloating(nn, token.Try) p.printNodes(n.Catches)
p.write([]byte("{"))
p.printNodes(nn.Stmts)
p.printFreeFloating(nn, token.Stmts)
p.write([]byte("}"))
if nn.Catches != nil {
p.printNodes(nn.Catches)
} }
if nn.Finally != nil { if n.Finally != nil {
p.Print(nn.Finally) p.Print(n.Finally)
} }
p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtUnset(n *ast.StmtUnset) { func (p *Printer) printStmtUnset(n *ast.StmtUnset) {