[refactoring] update ast structure of "ClassMethod", "Function", "ArrowFunction" and "Closure" nodes

This commit is contained in:
Vadym Slizov 2020-11-22 14:26:24 +02:00
parent 4c54c56af5
commit 2d6ae3a9a2
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
8 changed files with 211 additions and 256 deletions

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -1378,26 +1378,26 @@ is_variadic:
unticked_function_declaration_statement:
function is_reference T_STRING '(' parameter_list ')' '{' inner_statement_list '}'
{
name := &ast.Identifier{
$$ = &ast.StmtFunction{
Node: ast.Node{
Position: position.NewTokensPosition($1, $9),
},
FunctionTkn: $1,
AmpersandTkn: $2,
FunctionName: &ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($3),
},
IdentifierTkn: $3,
Value: $3.Value,
},
OpenParenthesisTkn: $4,
Params: $5,
CloseParenthesisTkn: $6,
OpenCurlyBracketTkn: $7,
Stmts: $8,
CloseCurlyBracketTkn: $9,
}
$$ = &ast.StmtFunction{ast.Node{}, $2 != nil, name, $5, nil, $8}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $9)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
if $2 != nil {
yylex.(*Parser).setFreeFloating($$, token.Function, $2.SkippedTokens)
}
yylex.(*Parser).setFreeFloating($$, token.ParamList, $6.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Params, $7.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Stmts, $9.SkippedTokens)
}
;
@ -2481,37 +2481,30 @@ class_statement:
}
| method_modifiers function is_reference T_STRING '(' parameter_list ')' method_body
{
name := &ast.Identifier{
pos := position.NewTokenNodePosition($2, $8)
if $1 != nil {
$$.GetNode().Position = position.NewNodeListNodePosition($1, $8)
}
$$ = &ast.StmtClassMethod{
Node: ast.Node{
Position: pos,
},
Modifiers: $1,
FunctionTkn: $2,
AmpersandTkn: $3,
MethodName: &ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($4),
},
IdentifierTkn: $4,
Value: $4.Value,
},
OpenParenthesisTkn: $5,
Params: $6,
CloseParenthesisTkn: $7,
Stmt: $8,
}
$$ = &ast.StmtClassMethod{ast.Node{}, $3 != nil, name, $1, $6, nil, $8}
// save position
if $1 == nil {
$$.GetNode().Position = position.NewTokenNodePosition($2, $8)
} else {
$$.GetNode().Position = position.NewNodeListNodePosition($1, $8)
}
// save comments
if len($1) > 0 {
yylex.(*Parser).MoveFreeFloating($1[0], $$)
yylex.(*Parser).setFreeFloating($$, token.ModifierList, $2.SkippedTokens)
} else {
yylex.(*Parser).setFreeFloating($$, token.Start, $2.SkippedTokens)
}
if $3 == nil {
yylex.(*Parser).setFreeFloating($$, token.Function, $4.SkippedTokens)
} else {
yylex.(*Parser).setFreeFloating($$, token.Function, $3.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Ampersand, $4.SkippedTokens)
}
yylex.(*Parser).setFreeFloating($$, token.Name, $5.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.ParameterList, $7.SkippedTokens)
}
;
@ -3907,51 +3900,37 @@ expr_without_variable:
}
| function is_reference '(' parameter_list ')' lexical_vars '{' inner_statement_list '}'
{
$$ = &ast.ExprClosure{ast.Node{}, $2 != nil, false, $4, $6, nil, $8}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $9)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
if $2 == nil {
yylex.(*Parser).setFreeFloating($$, token.Function, $3.SkippedTokens)
} else {
yylex.(*Parser).setFreeFloating($$, token.Function, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Ampersand, $3.SkippedTokens)
}
yylex.(*Parser).setFreeFloating($$, token.ParameterList, $5.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.LexicalVars, $7.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Stmts, $9.SkippedTokens)
// normalize
if $6 == nil {
yylex.(*Parser).setFreeFloatingTokens($$, token.Params, $$.GetNode().Tokens[token.LexicalVars]); delete($$.GetNode().Tokens, token.LexicalVars)
$$ = &ast.ExprClosure{
Node: ast.Node{
Position: position.NewTokensPosition($1, $9),
},
FunctionTkn: $1,
AmpersandTkn: $2,
OpenParenthesisTkn: $3,
Params: $4,
CloseParenthesisTkn: $5,
ClosureUse: $6,
OpenCurlyBracketTkn: $7,
Stmts: $8,
CloseCurlyBracketTkn: $9,
}
}
| T_STATIC function is_reference '(' parameter_list ')' lexical_vars '{' inner_statement_list '}'
{
$$ = &ast.ExprClosure{ast.Node{}, $3 != nil, true, $5, $7, nil, $9}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $10)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Static, $2.SkippedTokens)
if $3 == nil {
yylex.(*Parser).setFreeFloating($$, token.Function, $4.SkippedTokens)
} else {
yylex.(*Parser).setFreeFloating($$, token.Function, $3.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Ampersand, $4.SkippedTokens)
}
yylex.(*Parser).setFreeFloating($$, token.ParameterList, $6.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.LexicalVars, $8.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Stmts, $10.SkippedTokens)
// normalize
if $7 == nil {
yylex.(*Parser).setFreeFloatingTokens($$, token.Params, $$.GetNode().Tokens[token.LexicalVars]); delete($$.GetNode().Tokens, token.LexicalVars)
$$ = &ast.ExprClosure{
Node: ast.Node{
Position: position.NewTokensPosition($1, $10),
},
StaticTkn: $1,
FunctionTkn: $2,
AmpersandTkn: $3,
OpenParenthesisTkn: $4,
Params: $5,
CloseParenthesisTkn: $6,
ClosureUse: $7,
OpenCurlyBracketTkn: $8,
Stmts: $9,
CloseCurlyBracketTkn: $10,
}
}
;

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -1214,31 +1214,27 @@ unset_variable:
function_declaration_statement:
T_FUNCTION returns_ref T_STRING backup_doc_comment '(' parameter_list ')' return_type '{' inner_statement_list '}'
{
name := &ast.Identifier{
$$ = &ast.StmtFunction{
Node: ast.Node{
Position: position.NewTokensPosition($1, $11),
},
FunctionTkn: $1,
AmpersandTkn: $2,
FunctionName: &ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($3),
},
IdentifierTkn: $3,
Value: $3.Value,
}
$$ = &ast.StmtFunction{ast.Node{}, $2 != nil, name, $6, $8, $10}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $11)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
if $2 != nil {
yylex.(*Parser).setFreeFloating($$, token.Function, $2.SkippedTokens)
}
yylex.(*Parser).setFreeFloating($$, token.ParamList, $7.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.ReturnType, $9.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Stmts, $11.SkippedTokens)
// normalize
if $8 == nil {
yylex.(*Parser).setFreeFloatingTokens($$, token.Params, $$.GetNode().Tokens[token.ReturnType]); delete($$.GetNode().Tokens, token.ReturnType)
},
OpenParenthesisTkn: $5,
Params: $6,
CloseParenthesisTkn: $7,
ColonTkn: $8.(*ast.ReturnType).ColonTkn,
ReturnType: $8.(*ast.ReturnType).Type,
OpenCurlyBracketTkn: $9,
Stmts: $10,
CloseCurlyBracketTkn: $11,
}
}
;
@ -2025,10 +2021,10 @@ return_type:
}
| ':' type_expr
{
$$ = $2;
// save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, append($1.SkippedTokens, $$.GetNode().Tokens[token.Start]...))
$$ = &ast.ReturnType{
ColonTkn: $1,
Type: $2,
}
}
;
@ -2234,36 +2230,32 @@ class_statement:
}
| method_modifiers T_FUNCTION returns_ref identifier backup_doc_comment '(' parameter_list ')' return_type method_body
{
name := &ast.Identifier{
pos := position.NewTokenNodePosition($2, $10)
if $1 != nil {
$$.GetNode().Position = position.NewNodeListNodePosition($1, $10)
}
$$ = &ast.StmtClassMethod{
Node: ast.Node{
Position: pos,
},
Modifiers: $1,
FunctionTkn: $2,
AmpersandTkn: $3,
MethodName: &ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($4),
},
IdentifierTkn: $4,
Value: $4.Value,
},
OpenParenthesisTkn: $6,
Params: $7,
CloseParenthesisTkn: $8,
ColonTkn: $9.(*ast.ReturnType).ColonTkn,
ReturnType: $9.(*ast.ReturnType).Type,
Stmt: $10,
}
$$ = &ast.StmtClassMethod{ast.Node{}, $3 != nil, name, $1, $7, $9, $10}
// save position
if $1 == nil {
$$.GetNode().Position = position.NewTokenNodePosition($2, $10)
} else {
$$.GetNode().Position = position.NewNodeListNodePosition($1, $10)
}
// save comments
if len($1) > 0 {
yylex.(*Parser).MoveFreeFloating($1[0], $$)
yylex.(*Parser).setFreeFloating($$, token.ModifierList, $2.SkippedTokens)
} else {
yylex.(*Parser).setFreeFloating($$, token.Start, $2.SkippedTokens)
}
if $3 == nil {
yylex.(*Parser).setFreeFloating($$, token.Function, $4.SkippedTokens)
} else {
yylex.(*Parser).setFreeFloating($$, token.Function, $3.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Ampersand, $4.SkippedTokens)
}
yylex.(*Parser).setFreeFloating($$, token.ParameterList, $8.SkippedTokens)
}
;
@ -3624,74 +3616,55 @@ expr_without_variable:
}
| T_STATIC inline_function
{
$$ = $2;
switch n := $$.(type) {
switch n := $2.(type) {
case *ast.ExprClosure :
n.Static = true;
n.Position = position.NewTokenNodePosition($1, $2)
n.StaticTkn = $1;
case *ast.ExprArrowFunction :
n.Static = true;
n.Position = position.NewTokenNodePosition($1, $2)
n.StaticTkn = $1;
};
// save position
$$.GetNode().Position = position.NewTokenNodePosition($1, $2)
// save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Static, $$.GetNode().Tokens[token.Start]); delete($$.GetNode().Tokens, token.Start)
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens);
$$ = $2
}
;
inline_function:
T_FUNCTION returns_ref backup_doc_comment '(' parameter_list ')' lexical_vars return_type '{' inner_statement_list '}'
{
$$ = &ast.ExprClosure{ast.Node{}, $2 != nil, false, $5, $7, $8, $10}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $11)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
if $2 == nil {
yylex.(*Parser).setFreeFloating($$, token.Function, $4.SkippedTokens)
} else {
yylex.(*Parser).setFreeFloating($$, token.Function, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Ampersand, $4.SkippedTokens)
}
yylex.(*Parser).setFreeFloating($$, token.ParameterList, $6.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.ReturnType, $9.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Stmts, $11.SkippedTokens)
// normalize
if $8 == nil {
yylex.(*Parser).setFreeFloatingTokens($$, token.LexicalVars, $$.GetNode().Tokens[token.ReturnType]); delete($$.GetNode().Tokens, token.ReturnType)
}
if $7 == nil {
yylex.(*Parser).setFreeFloatingTokens($$, token.Params, $$.GetNode().Tokens[token.LexicalVarList]); delete($$.GetNode().Tokens, token.LexicalVarList)
$$ = &ast.ExprClosure{
Node: ast.Node{
Position: position.NewTokensPosition($1, $11),
},
FunctionTkn: $1,
AmpersandTkn: $2,
OpenParenthesisTkn: $4,
Params: $5,
CloseParenthesisTkn: $6,
ClosureUse: $7,
ColonTkn: $8.(*ast.ReturnType).ColonTkn,
ReturnType: $8.(*ast.ReturnType).Type,
OpenCurlyBracketTkn: $9,
Stmts: $10,
CloseCurlyBracketTkn: $11,
}
}
| T_FN returns_ref '(' parameter_list ')' return_type backup_doc_comment T_DOUBLE_ARROW expr
{
$$ = &ast.ExprArrowFunction{ast.Node{}, $2 != nil, false, $4, $6, $9}
// save position
$$.GetNode().Position = position.NewTokenNodePosition($1, $9)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
if $2 == nil {
yylex.(*Parser).setFreeFloating($$, token.Function, $3.SkippedTokens)
} else {
yylex.(*Parser).setFreeFloating($$, token.Function, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Ampersand, $3.SkippedTokens)
};
yylex.(*Parser).setFreeFloating($$, token.ParameterList, $5.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.ReturnType, $8.SkippedTokens)
// normalize
if $6 == nil {
yylex.(*Parser).setFreeFloatingTokens($$, token.Params, $$.GetNode().Tokens[token.ReturnType]); delete($$.GetNode().Tokens, token.ReturnType)
};
$$ = &ast.ExprArrowFunction{
Node: ast.Node{
Position: position.NewTokenNodePosition($1, $9),
},
FnTkn: $1,
AmpersandTkn: $2,
OpenParenthesisTkn: $3,
Params: $4,
CloseParenthesisTkn: $5,
ColonTkn: $6.(*ast.ReturnType).ColonTkn,
ReturnType: $6.(*ast.ReturnType).Type,
DoubleArrowTkn: $8,
Expr: $9,
}
}
;

View File

@ -292,10 +292,14 @@ func (n *StmtClassImplements) Accept(v NodeVisitor) {
// StmtClassMethod node
type StmtClassMethod struct {
Node
ReturnsRef bool
MethodName Vertex
Modifiers []Vertex
FunctionTkn *token.Token
AmpersandTkn *token.Token
MethodName Vertex
OpenParenthesisTkn *token.Token
Params []Vertex
CloseParenthesisTkn *token.Token
ColonTkn *token.Token
ReturnType Vertex
Stmt Vertex
}
@ -501,11 +505,17 @@ func (n *StmtForeach) Accept(v NodeVisitor) {
// StmtFunction node
type StmtFunction struct {
Node
ReturnsRef bool
FunctionTkn *token.Token
AmpersandTkn *token.Token
FunctionName Vertex
OpenParenthesisTkn *token.Token
Params []Vertex
CloseParenthesisTkn *token.Token
ColonTkn *token.Token
ReturnType Vertex
OpenCurlyBracketTkn *token.Token
Stmts []Vertex
CloseCurlyBracketTkn *token.Token
}
func (n *StmtFunction) Accept(v NodeVisitor) {
@ -951,10 +961,15 @@ func (n *ExprArrayItem) Accept(v NodeVisitor) {
// ExprArrowFunction node
type ExprArrowFunction struct {
Node
ReturnsRef bool
Static bool
StaticTkn *token.Token
FnTkn *token.Token
AmpersandTkn *token.Token
OpenParenthesisTkn *token.Token
Params []Vertex
CloseParenthesisTkn *token.Token
ColonTkn *token.Token
ReturnType Vertex
DoubleArrowTkn *token.Token
Expr Vertex
}
@ -1006,12 +1021,18 @@ func (n *ExprClone) Accept(v NodeVisitor) {
// ExprClosure node
type ExprClosure struct {
Node
ReturnsRef bool
Static bool
StaticTkn *token.Token
FunctionTkn *token.Token
AmpersandTkn *token.Token
OpenParenthesisTkn *token.Token
Params []Vertex
CloseParenthesisTkn *token.Token
ClosureUse *ExprClosureUse
ColonTkn *token.Token
ReturnType Vertex
OpenCurlyBracketTkn *token.Token
Stmts []Vertex
CloseCurlyBracketTkn *token.Token
}
func (n *ExprClosure) Accept(v NodeVisitor) {
@ -1947,6 +1968,8 @@ func (n *NameNamePart) Accept(v NodeVisitor) {
v.NameNamePart(n)
}
// TODO: move to private section
type ParserBrackets struct {
Node
OpenBracketTkn *token.Token
@ -1967,3 +1990,13 @@ type ParserSeparatedList struct {
func (n *ParserSeparatedList) Accept(v NodeVisitor) {
v.ParserSeparatedList(n)
}
type ReturnType struct {
Node
ColonTkn *token.Token
Type Vertex
}
func (n *ReturnType) Accept(v NodeVisitor) {
// do nothing
}

View File

@ -288,11 +288,6 @@ func (v *Dump) StmtClassMethod(n *ast.StmtClassMethod) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtClassMethod{\n")
v.printNode(n.GetNode())
if n.ReturnsRef {
v.printIndent(v.indent)
v.print("ReturnsRef: true,\n")
}
}
func (v *Dump) StmtConstList(n *ast.StmtConstList) {
@ -397,11 +392,6 @@ func (v *Dump) StmtFunction(n *ast.StmtFunction) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtFunction{\n")
v.printNode(n.GetNode())
if n.ReturnsRef {
v.printIndent(v.indent)
v.print("ReturnsRef: true,\n")
}
}
func (v *Dump) StmtGlobal(n *ast.StmtGlobal) {
@ -641,16 +631,6 @@ func (v *Dump) ExprArrowFunction(n *ast.ExprArrowFunction) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.ExprArrowFunction{\n")
v.printNode(n.GetNode())
if n.ReturnsRef {
v.printIndent(v.indent)
v.print("ReturnsRef: true,\n")
}
if n.Static {
v.printIndent(v.indent)
v.print("Static: true,\n")
}
}
func (v *Dump) ExprBitwiseNot(n *ast.ExprBitwiseNot) {
@ -681,16 +661,6 @@ func (v *Dump) ExprClosure(n *ast.ExprClosure) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.ExprClosure{\n")
v.printNode(n.GetNode())
if n.ReturnsRef {
v.printIndent(v.indent)
v.print("ReturnsRef: true,\n")
}
if n.Static {
v.printIndent(v.indent)
v.print("Static: true,\n")
}
}
func (v *Dump) ExprClosureUse(n *ast.ExprClosureUse) {

View File

@ -1015,13 +1015,13 @@ func (p *PrettyPrinter) printExprClosureUse(n ast.Vertex) {
func (p *PrettyPrinter) printExprClosure(n ast.Vertex) {
nn := n.(*ast.ExprClosure)
if nn.Static {
if nn.StaticTkn != nil {
io.WriteString(p.w, "static ")
}
io.WriteString(p.w, "function ")
if nn.ReturnsRef {
if nn.AmpersandTkn != nil {
io.WriteString(p.w, "&")
}
@ -1432,7 +1432,7 @@ func (p *PrettyPrinter) printStmtClassMethod(n ast.Vertex) {
}
io.WriteString(p.w, "function ")
if nn.ReturnsRef {
if nn.AmpersandTkn != nil {
io.WriteString(p.w, "&")
}
@ -1785,7 +1785,7 @@ func (p *PrettyPrinter) printStmtFunction(n ast.Vertex) {
io.WriteString(p.w, "function ")
if nn.ReturnsRef {
if nn.AmpersandTkn != nil {
io.WriteString(p.w, "&")
}

View File

@ -1405,18 +1405,18 @@ func (p *Printer) printExprArrowFunction(n ast.Vertex) {
nn := n.(*ast.ExprArrowFunction)
p.printFreeFloating(nn, token.Start)
if nn.Static {
if nn.StaticTkn != nil {
p.write([]byte("static"))
}
p.printFreeFloating(nn, token.Static)
if nn.Static && n.GetNode().Tokens.IsEmpty() {
if nn.StaticTkn != nil && n.GetNode().Tokens.IsEmpty() {
p.write([]byte(" "))
}
p.write([]byte("fn"))
p.printFreeFloating(nn, token.Function)
if nn.ReturnsRef {
if nn.AmpersandTkn != nil {
p.write([]byte("&"))
}
p.printFreeFloating(nn, token.Ampersand)
@ -1496,18 +1496,18 @@ func (p *Printer) printExprClosure(n ast.Vertex) {
nn := n.(*ast.ExprClosure)
p.printFreeFloating(nn, token.Start)
if nn.Static {
if nn.StaticTkn != nil {
p.write([]byte("static"))
}
p.printFreeFloating(nn, token.Static)
if nn.Static && n.GetNode().Tokens.IsEmpty() {
if nn.StaticTkn != nil && n.GetNode().Tokens.IsEmpty() {
p.write([]byte(" "))
}
p.write([]byte("function"))
p.printFreeFloating(nn, token.Function)
if nn.ReturnsRef {
if nn.AmpersandTkn != nil {
p.write([]byte("&"))
}
p.printFreeFloating(nn, token.Ampersand)
@ -2040,7 +2040,7 @@ func (p *Printer) printStmtClassMethod(n ast.Vertex) {
p.write([]byte("function"))
p.printFreeFloating(nn, token.Function)
if nn.ReturnsRef {
if nn.AmpersandTkn != nil {
if nn.GetNode().Tokens.IsEmpty() {
p.write([]byte(" "))
}
@ -2384,7 +2384,7 @@ func (p *Printer) printStmtFunction(n ast.Vertex) {
p.write([]byte("function"))
p.printFreeFloating(nn, token.Function)
if nn.ReturnsRef {
if nn.AmpersandTkn != nil {
if nn.GetNode().Tokens.IsEmpty() {
p.write([]byte(" "))
}