[refactoring] update ast structure of "StmtList" nodes

This commit is contained in:
Vadym Slizov 2020-09-02 22:58:19 +03:00
parent c63213630a
commit 954208510e
9 changed files with 133 additions and 73 deletions

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -844,14 +844,14 @@ statement:
unticked_statement: unticked_statement:
'{' inner_statement_list '}' '{' inner_statement_list '}'
{ {
$$ = &ast.StmtStmtList{ast.Node{}, $2} $$ = &ast.StmtStmtList{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $3),
$$.GetNode().Position = position.NewTokensPosition($1, $3) },
OpenCurlyBracket: $1,
// save comments Stmts: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) CloseCurlyBracket: $3,
yylex.(*Parser).setFreeFloating($$, token.Stmts, $3.SkippedTokens) }
} }
| T_IF parenthesis_expr statement elseif_list else_single | T_IF parenthesis_expr statement elseif_list else_single
{ {
@ -871,12 +871,16 @@ unticked_statement:
} }
| T_IF parenthesis_expr ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';' | T_IF parenthesis_expr ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';'
{ {
stmts := &ast.StmtStmtList{ast.Node{}, $4} stmts := &ast.StmtStmtList{
Node: ast.Node{
Position: position.NewNodeListPosition($4),
},
Stmts: $4,
}
stmtsBrackets := &ast.ParserBrackets{ast.Node{}, stmts} stmtsBrackets := &ast.ParserBrackets{ast.Node{}, stmts}
$$ = &ast.StmtAltIf{ast.Node{}, $2, stmtsBrackets, $5, $6} $$ = &ast.StmtAltIf{ast.Node{}, $2, stmtsBrackets, $5, $6}
// save position // save position
stmts.GetNode().Position = position.NewNodeListPosition($4)
stmtsBrackets.GetNode().Position = position.NewTokenNodeListPosition($3, $4) stmtsBrackets.GetNode().Position = position.NewTokenNodeListPosition($3, $4)
$$.GetNode().Position = position.NewTokensPosition($1, $8) $$.GetNode().Position = position.NewTokensPosition($1, $8)
@ -1673,11 +1677,15 @@ for_statement:
} }
| ':' inner_statement_list T_ENDFOR ';' | ':' inner_statement_list T_ENDFOR ';'
{ {
stmtList := &ast.StmtStmtList{ast.Node{}, $2} stmtList := &ast.StmtStmtList{
Node: ast.Node{
Position: position.NewNodeListPosition($2),
},
Stmts: $2,
}
$$ = &ast.StmtAltFor{ast.Node{}, nil, nil, nil, stmtList} $$ = &ast.StmtAltFor{ast.Node{}, nil, nil, nil, stmtList}
// save position // save position
stmtList.GetNode().Position = position.NewNodeListPosition($2)
$$.GetNode().Position = position.NewTokensPosition($1, $4) $$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments // save comments
@ -1698,11 +1706,15 @@ foreach_statement:
} }
| ':' inner_statement_list T_ENDFOREACH ';' | ':' inner_statement_list T_ENDFOREACH ';'
{ {
stmtList := &ast.StmtStmtList{ast.Node{}, $2} stmtList := &ast.StmtStmtList{
Node: ast.Node{
Position: position.NewNodeListPosition($2),
},
Stmts: $2,
}
$$ = &ast.StmtAltForeach{ast.Node{}, nil, nil, nil, stmtList} $$ = &ast.StmtAltForeach{ast.Node{}, nil, nil, nil, stmtList}
// save position // save position
stmtList.GetNode().Position = position.NewNodeListPosition($2)
$$.GetNode().Position = position.NewTokensPosition($1, $4) $$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments // save comments
@ -1724,11 +1736,15 @@ declare_statement:
} }
| ':' inner_statement_list T_ENDDECLARE ';' | ':' inner_statement_list T_ENDDECLARE ';'
{ {
stmtList := &ast.StmtStmtList{ast.Node{}, $2} stmtList := &ast.StmtStmtList{
Node: ast.Node{
Position: position.NewNodeListPosition($2),
},
Stmts: $2,
}
$$ = &ast.StmtDeclare{ast.Node{}, true, nil, stmtList} $$ = &ast.StmtDeclare{ast.Node{}, true, nil, stmtList}
// save position // save position
stmtList.GetNode().Position = position.NewNodeListPosition($2)
$$.GetNode().Position = position.NewTokensPosition($1, $4) $$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments // save comments
@ -1902,11 +1918,15 @@ while_statement:
} }
| ':' inner_statement_list T_ENDWHILE ';' | ':' inner_statement_list T_ENDWHILE ';'
{ {
stmtList := &ast.StmtStmtList{ast.Node{}, $2} stmtList := &ast.StmtStmtList{
Node: ast.Node{
Position: position.NewNodeListPosition($2),
},
Stmts: $2,
}
$$ = &ast.StmtAltWhile{ast.Node{}, nil, stmtList} $$ = &ast.StmtAltWhile{ast.Node{}, nil, stmtList}
// save position // save position
stmtList.GetNode().Position = position.NewNodeListPosition($2)
$$.GetNode().Position = position.NewTokensPosition($1, $4) $$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments // save comments
@ -1945,13 +1965,17 @@ new_elseif_list:
} }
| new_elseif_list T_ELSEIF parenthesis_expr ':' inner_statement_list | new_elseif_list T_ELSEIF parenthesis_expr ':' inner_statement_list
{ {
stmts := &ast.StmtStmtList{ast.Node{}, $5} stmts := &ast.StmtStmtList{
Node: ast.Node{
Position: position.NewNodeListPosition($5),
},
Stmts: $5,
}
stmtsBrackets := &ast.ParserBrackets{ast.Node{}, stmts} stmtsBrackets := &ast.ParserBrackets{ast.Node{}, stmts}
_elseIf := &ast.StmtAltElseIf{ast.Node{}, $3, stmtsBrackets} _elseIf := &ast.StmtAltElseIf{ast.Node{}, $3, stmtsBrackets}
$$ = append($1, _elseIf) $$ = append($1, _elseIf)
// save position // save position
stmts.GetNode().Position = position.NewNodeListPosition($5)
stmtsBrackets.GetNode().Position = position.NewTokenNodeListPosition($4, $5) stmtsBrackets.GetNode().Position = position.NewTokenNodeListPosition($4, $5)
_elseIf.GetNode().Position = position.NewTokenNodeListPosition($2, $5) _elseIf.GetNode().Position = position.NewTokenNodeListPosition($2, $5)
@ -1987,12 +2011,16 @@ new_else_single:
} }
| T_ELSE ':' inner_statement_list | T_ELSE ':' inner_statement_list
{ {
stmts := &ast.StmtStmtList{ast.Node{}, $3} stmts := &ast.StmtStmtList{
Node: ast.Node{
Position: position.NewNodeListPosition($3),
},
Stmts: $3,
}
stmtsBrackets := &ast.ParserBrackets{ast.Node{}, stmts} stmtsBrackets := &ast.ParserBrackets{ast.Node{}, stmts}
$$ = &ast.StmtAltElse{ast.Node{}, stmtsBrackets} $$ = &ast.StmtAltElse{ast.Node{}, stmtsBrackets}
// save position // save position
stmts.GetNode().Position = position.NewNodeListPosition($3)
stmtsBrackets.GetNode().Position = position.NewTokenNodeListPosition($2, $3) stmtsBrackets.GetNode().Position = position.NewTokenNodeListPosition($2, $3)
$$.GetNode().Position = position.NewTokenNodeListPosition($1, $3) $$.GetNode().Position = position.NewTokenNodeListPosition($1, $3)
@ -2626,14 +2654,14 @@ method_body:
} }
| '{' inner_statement_list '}' | '{' inner_statement_list '}'
{ {
$$ = &ast.StmtStmtList{ast.Node{}, $2} $$ = &ast.StmtStmtList{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $3),
$$.GetNode().Position = position.NewTokensPosition($1, $3) },
OpenCurlyBracket: $1,
// save comments Stmts: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) CloseCurlyBracket: $3,
yylex.(*Parser).setFreeFloating($$, token.Stmts, $3.SkippedTokens) }
} }
; ;

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -851,14 +851,14 @@ inner_statement:
statement: statement:
'{' inner_statement_list '}' '{' inner_statement_list '}'
{ {
$$ = &ast.StmtStmtList{ast.Node{}, $2} $$ = &ast.StmtStmtList{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $3),
$$.GetNode().Position = position.NewTokensPosition($1, $3) },
OpenCurlyBracket: $1,
// save comments Stmts: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) CloseCurlyBracket: $3,
yylex.(*Parser).setFreeFloating($$, token.Stmts, $3.SkippedTokens) }
} }
| if_stmt | if_stmt
{ {
@ -1535,11 +1535,15 @@ for_statement:
} }
| ':' inner_statement_list T_ENDFOR ';' | ':' inner_statement_list T_ENDFOR ';'
{ {
stmtList := &ast.StmtStmtList{ast.Node{}, $2} stmtList := &ast.StmtStmtList{
Node: ast.Node{
Position: position.NewNodeListPosition($2),
},
Stmts: $2,
}
$$ = &ast.StmtAltFor{ast.Node{}, nil, nil, nil, stmtList} $$ = &ast.StmtAltFor{ast.Node{}, nil, nil, nil, stmtList}
// save position // save position
stmtList.GetNode().Position = position.NewNodeListPosition($2)
$$.GetNode().Position = position.NewTokensPosition($1, $4) $$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments // save comments
@ -1560,11 +1564,15 @@ foreach_statement:
} }
| ':' inner_statement_list T_ENDFOREACH ';' | ':' inner_statement_list T_ENDFOREACH ';'
{ {
stmtList := &ast.StmtStmtList{ast.Node{}, $2} stmtList := &ast.StmtStmtList{
Node: ast.Node{
Position: position.NewNodeListPosition($2),
},
Stmts: $2,
}
$$ = &ast.StmtAltForeach{ast.Node{}, nil, nil, nil, stmtList} $$ = &ast.StmtAltForeach{ast.Node{}, nil, nil, nil, stmtList}
// save position // save position
stmtList.GetNode().Position = position.NewNodeListPosition($2)
$$.GetNode().Position = position.NewTokensPosition($1, $4) $$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments // save comments
@ -1585,11 +1593,15 @@ declare_statement:
} }
| ':' inner_statement_list T_ENDDECLARE ';' | ':' inner_statement_list T_ENDDECLARE ';'
{ {
stmtList := &ast.StmtStmtList{ast.Node{}, $2} stmtList := &ast.StmtStmtList{
Node: ast.Node{
Position: position.NewNodeListPosition($2),
},
Stmts: $2,
}
$$ = &ast.StmtDeclare{ast.Node{}, true, nil, stmtList} $$ = &ast.StmtDeclare{ast.Node{}, true, nil, stmtList}
// save position // save position
stmtList.GetNode().Position = position.NewNodeListPosition($2)
$$.GetNode().Position = position.NewTokensPosition($1, $4) $$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments // save comments
@ -1716,11 +1728,15 @@ while_statement:
} }
| ':' inner_statement_list T_ENDWHILE ';' | ':' inner_statement_list T_ENDWHILE ';'
{ {
stmtList := &ast.StmtStmtList{ast.Node{}, $2} stmtList := &ast.StmtStmtList{
Node: ast.Node{
Position: position.NewNodeListPosition($2),
},
Stmts: $2,
}
$$ = &ast.StmtAltWhile{ast.Node{}, nil, stmtList} $$ = &ast.StmtAltWhile{ast.Node{}, nil, stmtList}
// save position // save position
stmtList.GetNode().Position = position.NewNodeListPosition($2)
$$.GetNode().Position = position.NewTokensPosition($1, $4) $$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments // save comments
@ -1791,13 +1807,17 @@ alt_if_stmt_without_else:
T_IF '(' expr ')' ':' inner_statement_list T_IF '(' expr ')' ':' inner_statement_list
{ {
exprBrackets := &ast.ParserBrackets{ast.Node{}, $3} exprBrackets := &ast.ParserBrackets{ast.Node{}, $3}
stmts := &ast.StmtStmtList{ast.Node{}, $6} stmts := &ast.StmtStmtList{
Node: ast.Node{
Position: position.NewNodeListPosition($6),
},
Stmts: $6,
}
stmtsBrackets := &ast.ParserBrackets{ast.Node{}, stmts} stmtsBrackets := &ast.ParserBrackets{ast.Node{}, stmts}
$$ = &ast.StmtAltIf{ast.Node{}, exprBrackets, stmtsBrackets, nil, nil} $$ = &ast.StmtAltIf{ast.Node{}, exprBrackets, stmtsBrackets, nil, nil}
// save position // save position
exprBrackets.GetNode().Position = position.NewTokensPosition($2, $4) exprBrackets.GetNode().Position = position.NewTokensPosition($2, $4)
stmts.GetNode().Position = position.NewNodeListPosition($6)
stmtsBrackets.GetNode().Position = position.NewTokenNodeListPosition($5, $6) stmtsBrackets.GetNode().Position = position.NewTokenNodeListPosition($5, $6)
$$.GetNode().Position = position.NewTokenNodeListPosition($1, $6) $$.GetNode().Position = position.NewTokenNodeListPosition($1, $6)
@ -1810,7 +1830,12 @@ alt_if_stmt_without_else:
| alt_if_stmt_without_else T_ELSEIF '(' expr ')' ':' inner_statement_list | alt_if_stmt_without_else T_ELSEIF '(' expr ')' ':' inner_statement_list
{ {
exprBrackets := &ast.ParserBrackets{ast.Node{}, $4} exprBrackets := &ast.ParserBrackets{ast.Node{}, $4}
stmts := &ast.StmtStmtList{ast.Node{}, $7} stmts := &ast.StmtStmtList{
Node: ast.Node{
Position: position.NewNodeListPosition($7),
},
Stmts: $7,
}
stmtsBrackets := &ast.ParserBrackets{ast.Node{}, stmts} stmtsBrackets := &ast.ParserBrackets{ast.Node{}, stmts}
_elseIf := &ast.StmtAltElseIf{ast.Node{}, exprBrackets, stmtsBrackets} _elseIf := &ast.StmtAltElseIf{ast.Node{}, exprBrackets, stmtsBrackets}
$1.(*ast.StmtAltIf).ElseIf = append($1.(*ast.StmtAltIf).ElseIf, _elseIf) $1.(*ast.StmtAltIf).ElseIf = append($1.(*ast.StmtAltIf).ElseIf, _elseIf)
@ -1819,7 +1844,6 @@ alt_if_stmt_without_else:
// save position // save position
exprBrackets.GetNode().Position = position.NewTokensPosition($3, $5) exprBrackets.GetNode().Position = position.NewTokensPosition($3, $5)
stmts.GetNode().Position = position.NewNodeListPosition($7)
stmtsBrackets.GetNode().Position = position.NewTokenNodeListPosition($6, $7) stmtsBrackets.GetNode().Position = position.NewTokenNodeListPosition($6, $7)
_elseIf.GetNode().Position = position.NewTokenNodeListPosition($2, $7) _elseIf.GetNode().Position = position.NewTokenNodeListPosition($2, $7)
@ -1850,7 +1874,12 @@ alt_if_stmt:
} }
| alt_if_stmt_without_else T_ELSE ':' inner_statement_list T_ENDIF ';' | alt_if_stmt_without_else T_ELSE ':' inner_statement_list T_ENDIF ';'
{ {
stmts := &ast.StmtStmtList{ast.Node{}, $4} stmts := &ast.StmtStmtList{
Node: ast.Node{
Position: position.NewNodeListPosition($4),
},
Stmts: $4,
}
stmtsBrackets := &ast.ParserBrackets{ast.Node{}, stmts} stmtsBrackets := &ast.ParserBrackets{ast.Node{}, stmts}
_else := &ast.StmtAltElse{ast.Node{}, stmtsBrackets} _else := &ast.StmtAltElse{ast.Node{}, stmtsBrackets}
$1.(*ast.StmtAltIf).Else = _else $1.(*ast.StmtAltIf).Else = _else
@ -1858,7 +1887,6 @@ alt_if_stmt:
$$ = $1 $$ = $1
// save position // save position
stmts.GetNode().Position = position.NewNodeListPosition($4)
stmtsBrackets.GetNode().Position = position.NewTokensPosition($3, $5) stmtsBrackets.GetNode().Position = position.NewTokensPosition($3, $5)
_else.GetNode().Position = position.NewTokenNodeListPosition($2, $4) _else.GetNode().Position = position.NewTokenNodeListPosition($2, $4)
$$.GetNode().Position = position.NewNodeTokenPosition($1, $6) $$.GetNode().Position = position.NewNodeTokenPosition($1, $6)
@ -2446,14 +2474,14 @@ method_body:
} }
| '{' inner_statement_list '}' | '{' inner_statement_list '}'
{ {
$$ = &ast.StmtStmtList{ast.Node{}, $2} $$ = &ast.StmtStmtList{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $3),
$$.GetNode().Position = position.NewTokensPosition($1, $3) },
OpenCurlyBracket: $1,
// save comments Stmts: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) CloseCurlyBracket: $3,
yylex.(*Parser).setFreeFloating($$, token.Stmts, $3.SkippedTokens) }
} }
; ;

View File

@ -691,7 +691,9 @@ func (n *StmtStaticVar) Accept(v NodeVisitor) {
// StmtStmtList node // StmtStmtList node
type StmtStmtList struct { type StmtStmtList struct {
Node Node
Stmts []Vertex OpenCurlyBracket *token.Token
Stmts []Vertex
CloseCurlyBracket *token.Token
} }
func (n *StmtStmtList) Accept(v NodeVisitor) { func (n *StmtStmtList) Accept(v NodeVisitor) {

View File

@ -82,3 +82,8 @@ func (v *FilterTokens) StmtConstant(n *ast.StmtConstant) {
n.EqualTkn = nil n.EqualTkn = nil
n.CommaTkn = nil n.CommaTkn = nil
} }
func (v *FilterTokens) StmtStmtList(n *ast.StmtStmtList) {
n.OpenCurlyBracket = nil
n.CloseCurlyBracket = nil
}

View File

@ -2530,6 +2530,7 @@ func (p *Printer) printStmtDo(n ast.Vertex) {
} }
p.Print(nn.Stmt) p.Print(nn.Stmt)
p.printFreeFloating(nn, token.Stmts) p.printFreeFloating(nn, token.Stmts)
io.WriteString(p.w, "while") io.WriteString(p.w, "while")
@ -3001,16 +3002,10 @@ func (p *Printer) printStmtStatic(n ast.Vertex) {
p.printFreeFloating(nn, token.End) p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtStmtList(n ast.Vertex) { func (p *Printer) printStmtStmtList(n *ast.StmtStmtList) {
nn := n.(*ast.StmtStmtList) p.printToken(n.OpenCurlyBracket, "{")
p.printFreeFloating(nn, token.Start) p.printNodes(n.Stmts)
p.printToken(n.CloseCurlyBracket, "}")
io.WriteString(p.w, "{")
p.printNodes(nn.Stmts)
p.printFreeFloating(nn, token.Stmts)
io.WriteString(p.w, "}")
p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtSwitch(n ast.Vertex) { func (p *Printer) printStmtSwitch(n ast.Vertex) {

View File

@ -1211,7 +1211,8 @@ func TestParseAndPrintPhp5StaticVar(t *testing.T) {
} }
func TestParseAndPrintPhp5StmtList(t *testing.T) { func TestParseAndPrintPhp5StmtList(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
{ {
; ;
} }

View File

@ -1344,7 +1344,8 @@ func TestParseAndPrintStaticVar(t *testing.T) {
} }
func TestParseAndPrintStmtList(t *testing.T) { func TestParseAndPrintStmtList(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
{ {
; ;
} }