[refactoring] update ast structure of "HaltCompiler" node

This commit is contained in:
Vadym Slizov 2020-08-24 21:41:06 +03:00
parent c1b3e6f5b2
commit 0285900fe5
9 changed files with 56 additions and 41 deletions

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -344,14 +344,15 @@ top_statement:
} }
| T_HALT_COMPILER '(' ')' ';' | T_HALT_COMPILER '(' ')' ';'
{ {
$$ = &ast.StmtHaltCompiler{ast.Node{}} $$ = &ast.StmtHaltCompiler{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $4),
$$.GetNode().Position = position.NewTokensPosition($1, $4) },
HaltCompilerTkn: $1,
// save comments OpenParenthesisTkn: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) CloseParenthesisTkn: $3,
yylex.(*Parser).setFreeFloatingTokens($$, token.End, append($2.SkippedTokens, append($3.SkippedTokens, $4.SkippedTokens...)...)) SemiColonTkn: $4,
}
} }
| T_NAMESPACE namespace_name ';' | T_NAMESPACE namespace_name ';'
{ {
@ -799,14 +800,15 @@ inner_statement:
} }
| T_HALT_COMPILER '(' ')' ';' | T_HALT_COMPILER '(' ')' ';'
{ {
$$ = &ast.StmtHaltCompiler{ast.Node{}} $$ = &ast.StmtHaltCompiler{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $4),
$$.GetNode().Position = position.NewTokensPosition($1, $4) },
HaltCompilerTkn: $1,
// save comments OpenParenthesisTkn: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) CloseParenthesisTkn: $3,
yylex.(*Parser).setFreeFloatingTokens($$, token.End, append($2.SkippedTokens, append($3.SkippedTokens, $4.SkippedTokens...)...)) SemiColonTkn: $4,
}
} }
; ;

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -434,14 +434,15 @@ top_statement:
} }
| T_HALT_COMPILER '(' ')' ';' | T_HALT_COMPILER '(' ')' ';'
{ {
$$ = &ast.StmtHaltCompiler{ast.Node{}} $$ = &ast.StmtHaltCompiler{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $4),
$$.GetNode().Position = position.NewTokensPosition($1, $4) },
HaltCompilerTkn: $1,
// save comments OpenParenthesisTkn: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) CloseParenthesisTkn: $3,
yylex.(*Parser).setFreeFloatingTokens($$, token.End, append($2.SkippedTokens, append($3.SkippedTokens, $4.SkippedTokens...)...)) SemiColonTkn: $4,
}
} }
| T_NAMESPACE namespace_name ';' | T_NAMESPACE namespace_name ';'
{ {
@ -838,14 +839,15 @@ inner_statement:
} }
| T_HALT_COMPILER '(' ')' ';' | T_HALT_COMPILER '(' ')' ';'
{ {
$$ = &ast.StmtHaltCompiler{ast.Node{}} $$ = &ast.StmtHaltCompiler{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $4),
$$.GetNode().Position = position.NewTokensPosition($1, $4) },
HaltCompilerTkn: $1,
// save comments OpenParenthesisTkn: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) CloseParenthesisTkn: $3,
yylex.(*Parser).setFreeFloatingTokens($$, token.End, append($2.SkippedTokens, append($3.SkippedTokens, $4.SkippedTokens...)...)) SemiColonTkn: $4,
}
} }
statement: statement:

View File

@ -539,6 +539,10 @@ func (n *StmtGoto) Accept(v NodeVisitor) {
// StmtHaltCompiler node // StmtHaltCompiler node
type StmtHaltCompiler struct { type StmtHaltCompiler struct {
Node Node
HaltCompilerTkn *token.Token
OpenParenthesisTkn *token.Token
CloseParenthesisTkn *token.Token
SemiColonTkn *token.Token
} }
func (n *StmtHaltCompiler) Accept(v NodeVisitor) { func (n *StmtHaltCompiler) Accept(v NodeVisitor) {

View File

@ -60,3 +60,10 @@ func (v *FilterTokens) StmtNamespace(n *ast.StmtNamespace) {
n.CloseCurlyBracket = nil n.CloseCurlyBracket = nil
n.SemiColonTkn = nil n.SemiColonTkn = nil
} }
func (v *FilterTokens) StmtHaltCompiler(n *ast.StmtHaltCompiler) {
n.HaltCompilerTkn = nil
n.OpenParenthesisTkn = nil
n.CloseParenthesisTkn = nil
n.SemiColonTkn = nil
}

View File

@ -2823,13 +2823,11 @@ func (p *Printer) printStmtGoto(n ast.Vertex) {
p.printFreeFloating(nn, token.End) p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtHaltCompiler(n ast.Vertex) { func (p *Printer) printStmtHaltCompiler(n *ast.StmtHaltCompiler) {
nn := n.(*ast.StmtHaltCompiler) p.printToken(n.HaltCompilerTkn, "__halt_compiler")
p.printFreeFloating(nn, token.Start) p.printToken(n.OpenParenthesisTkn, "(")
p.printToken(n.CloseParenthesisTkn, ")")
io.WriteString(p.w, "__halt_compiler") p.printToken(n.SemiColonTkn, ";")
p.printFreeFloatingOrDefault(nn, token.End, "();")
} }
func (p *Printer) printStmtIf(n ast.Vertex) { func (p *Printer) printStmtIf(n ast.Vertex) {

View File

@ -1049,7 +1049,8 @@ func TestParseAndPrintPhp5Goto(t *testing.T) {
} }
func TestParseAndPrintPhp5HaltCompiler(t *testing.T) { func TestParseAndPrintPhp5HaltCompiler(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
__halt_compiler ( ) ; __halt_compiler ( ) ;
this text is ignored by parser this text is ignored by parser
` `

View File

@ -1182,7 +1182,8 @@ func TestParseAndPrintGroupUse(t *testing.T) {
} }
func TestParseAndPrintHaltCompiler(t *testing.T) { func TestParseAndPrintHaltCompiler(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
__halt_compiler ( ) ; __halt_compiler ( ) ;
this text is ignored by parser this text is ignored by parser
` `