[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
{
$$ = &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 {
$$.GetNode().Position = position.NewTokenNodeListPosition($1, $5)
} else {
$$.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 ';'
{
@ -1202,21 +1203,27 @@ catch_statement:
{
identifier := &ast.Identifier{ast.Node{}, $4.Value}
variable := &ast.ExprVariable{ast.Node{}, identifier}
catchNode := &ast.StmtCatch{ast.Node{}, []ast.Vertex{$3}, variable, $7}
$$ = append([]ast.Vertex{catchNode}, $9...)
catch := &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,
}
$$ = append([]ast.Vertex{catch}, $9...)
// save position
identifier.GetNode().Position = position.NewTokenPosition($4)
variable.GetNode().Position = position.NewTokenPosition($4)
catchNode.GetNode().Position = position.NewTokensPosition($1, $8)
// 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(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 '}'
{
$$ = &ast.StmtFinally{ast.Node{}, $3}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Finally, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Stmts, $4.SkippedTokens)
$$ = &ast.StmtFinally{
Node: ast.Node{
Position: position.NewTokensPosition($1, $4),
},
FinallyTkn: $1,
OpenCurlyBracketTkn: $2,
Stmts: $3,
CloseCurlyBracketTkn: $4,
}
}
;
@ -1266,20 +1273,26 @@ additional_catch:
{
identifier := &ast.Identifier{ast.Node{}, $4.Value}
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
identifier.GetNode().Position = position.NewTokenPosition($4)
variable.GetNode().Position = position.NewTokenPosition($4)
$$.GetNode().Position = position.NewTokensPosition($1, $8)
// 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($$, 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> variable_class_name dereferencable_scalar constant dereferencable
%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> alt_if_stmt
%type <node> if_stmt_without_else
@ -275,7 +275,7 @@ import (
%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> unprefixed_use_declarations inline_use_declarations property_list
%type <list> case_list trait_adaptation_list
@ -1060,18 +1060,20 @@ statement:
}
| T_TRY '{' inner_statement_list '}' catch_list finally_statement
{
if $6 == nil {
$$ = &ast.StmtTry{ast.Node{}, $3, $5, $6}
$$.GetNode().Position = position.NewTokenNodeListPosition($1, $5)
} else {
$$ = &ast.StmtTry{ast.Node{}, $3, $5, $6}
$$.GetNode().Position = position.NewTokenNodePosition($1, $6)
$$ = &ast.StmtTry{
TryTkn: $1,
OpenCurlyBracket: $2,
Stmts: $3,
CloseCurlyBracket: $4,
Catches: $5,
Finally: $6,
}
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Try, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Stmts, $4.SkippedTokens)
if $6 == nil {
$$.GetNode().Position = position.NewTokenNodeListPosition($1, $5)
} else {
$$.GetNode().Position = position.NewTokenNodePosition($1, $6)
}
}
| T_THROW expr ';'
{
@ -1123,36 +1125,40 @@ catch_list:
{
identifier := &ast.Identifier{ast.Node{}, $5.Value}
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)
// save position
identifier.GetNode().Position = position.NewTokenPosition($5)
variable.GetNode().Position = position.NewTokenPosition($5)
catch.GetNode().Position = position.NewTokensPosition($2, $9)
// 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(catch, token.Var, $6.SkippedTokens)
yylex.(*Parser).setFreeFloating(catch, token.Cond, $7.SkippedTokens)
yylex.(*Parser).setFreeFloating(catch, token.Stmts, $9.SkippedTokens)
}
;
catch_name_list:
name
{
$$ = []ast.Vertex{$1}
$$ = &ast.StmtCatch{
Types: []ast.Vertex{$1},
}
}
| catch_name_list '|' name
{
switch n := lastNode($1).(type) {
case *ast.NameName: n.ListSeparatorTkn = $2
case *ast.NameFullyQualified: n.ListSeparatorTkn = $2
case *ast.NameRelative: n.ListSeparatorTkn = $2
}
$$ = append($1, $3)
$1.(*ast.StmtCatch).SeparatorTkns = append($1.(*ast.StmtCatch).SeparatorTkns, $2)
$1.(*ast.StmtCatch).Types = append($1.(*ast.StmtCatch).Types, $3)
$$ = $1
}
;
@ -1163,15 +1169,15 @@ finally_statement:
}
| T_FINALLY '{' inner_statement_list '}'
{
$$ = &ast.StmtFinally{ast.Node{}, $3}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Finally, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Stmts, $4.SkippedTokens)
$$ = &ast.StmtFinally{
Node: ast.Node{
Position: position.NewTokensPosition($1, $4),
},
FinallyTkn: $1,
OpenCurlyBracketTkn: $2,
Stmts: $3,
CloseCurlyBracketTkn: $4,
}
}
;

View File

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

View File

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