[refactoring] update ast structure of "Class" and "Trait" nodes

This commit is contained in:
Vadym Slizov 2020-11-22 02:03:39 +02:00
parent 3bda40e8ce
commit fe2e097d8f
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
7 changed files with 130 additions and 114 deletions

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -1408,33 +1408,38 @@ unticked_function_declaration_statement:
unticked_class_declaration_statement:
class_entry_type T_STRING extends_from implements_list '{' class_statement_list '}'
{
name := &ast.Identifier{
switch n := $1.(type) {
case *ast.StmtClass :
n.Position = position.NewNodeTokenPosition($1, $7)
n.ClassName = &ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($2),
},
IdentifierTkn: $2,
Value: $2.Value,
}
switch n := $1.(type) {
case *ast.StmtClass :
n.ClassName = name
n.Stmts = $6
n.Extends = $3
n.Implements = $4
case *ast.StmtTrait :
// TODO: is it possible that trait extend or implement
n.TraitName = name
n.OpenCurlyBracket = $5
n.Stmts = $6
n.CloseCurlyBracket = $7
case *ast.StmtTrait :
n.Position = position.NewNodeTokenPosition($1, $7)
n.TraitName = &ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($2),
},
IdentifierTkn: $2,
Value: $2.Value,
}
n.Extends = $3
n.Implements = $4
n.OpenCurlyBracket = $5
n.Stmts = $6
n.CloseCurlyBracket = $7
}
$$ = $1
// save position
$$.GetNode().Position = position.NewNodeTokenPosition($1, $7)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Name, $5.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Stmts, $7.SkippedTokens)
}
| interface_entry T_STRING interface_extends_list '{' class_statement_list '}'
{
@ -1461,59 +1466,57 @@ unticked_class_declaration_statement:
class_entry_type:
T_CLASS
{
$$ = &ast.StmtClass{ast.Node{}, nil, nil, nil, nil, nil, nil}
// save position
$$.GetNode().Position = position.NewTokenPosition($1)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = &ast.StmtClass{
Node: ast.Node{
Position: position.NewTokenPosition($1),
},
ClassTkn: $1,
}
}
| T_ABSTRACT T_CLASS
{
classModifier := &ast.Identifier{
$$ = &ast.StmtClass{
Node: ast.Node{
Position: position.NewTokensPosition($1, $2),
},
Modifiers: []ast.Vertex{
&ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($1),
},
IdentifierTkn: $1,
Value: $1.Value,
},
},
ClassTkn: $2,
}
$$ = &ast.StmtClass{ast.Node{}, nil, []ast.Vertex{classModifier}, nil, nil, nil, nil}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $2)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.ModifierList, $2.SkippedTokens)
}
| T_TRAIT
{
$$ = &ast.Identifier{
$$ = &ast.StmtTrait{
Node: ast.Node{
Position: position.NewTokenPosition($1),
},
IdentifierTkn: $1,
Value: $1.Value,
TraitTkn: $1,
}
}
| T_FINAL T_CLASS
{
classModifier := &ast.Identifier{
$$ = &ast.StmtClass{
Node: ast.Node{
Position: position.NewTokensPosition($1, $2),
},
Modifiers: []ast.Vertex{
&ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($1),
},
IdentifierTkn: $1,
Value: $1.Value,
},
},
ClassTkn: $2,
}
$$ = &ast.StmtClass{ast.Node{}, nil, []ast.Vertex{classModifier}, nil, nil, nil, nil}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $2)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.ModifierList, $2.SkippedTokens)
}
;

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -1271,40 +1271,46 @@ is_variadic:
class_declaration_statement:
class_modifiers T_CLASS T_STRING extends_from implements_list backup_doc_comment '{' class_statement_list '}'
{
name := &ast.Identifier{
$$ = &ast.StmtClass{
Node: ast.Node{
Position: position.NewOptionalListTokensPosition($1, $2, $9),
},
Modifiers: $1,
ClassTkn: $2,
ClassName: &ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($3),
},
IdentifierTkn: $3,
Value: $3.Value,
},
Extends: $4,
Implements: $5,
OpenCurlyBracket: $7,
Stmts: $8,
CloseCurlyBracket: $9,
}
$$ = &ast.StmtClass{ast.Node{}, name, $1, nil, $4, $5, $8}
// save position
$$.GetNode().Position = position.NewOptionalListTokensPosition($1, $2, $9)
// save comments
yylex.(*Parser).MoveFreeFloating($1[0], $$)
yylex.(*Parser).setFreeFloating($$, token.ModifierList, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Stmts, $9.SkippedTokens)
}
| T_CLASS T_STRING extends_from implements_list backup_doc_comment '{' class_statement_list '}'
{
name := &ast.Identifier{
$$ = &ast.StmtClass{
Node: ast.Node{
Position: position.NewTokensPosition($1, $8),
},
ClassTkn: $1,
ClassName: &ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($2),
},
IdentifierTkn: $2,
Value: $2.Value,
},
Extends: $3,
Implements: $4,
OpenCurlyBracket: $6,
Stmts: $7,
CloseCurlyBracket: $8,
}
$$ = &ast.StmtClass{ast.Node{}, name, nil, nil, $3, $4, $7}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $8)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Stmts, $8.SkippedTokens)
}
;
@ -1345,22 +1351,22 @@ class_modifier:
trait_declaration_statement:
T_TRAIT T_STRING backup_doc_comment '{' class_statement_list '}'
{
name := &ast.Identifier{
$$ = &ast.StmtTrait{
Node: ast.Node{
Position: position.NewTokensPosition($1, $6),
},
TraitTkn: $1,
TraitName: &ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($2),
},
IdentifierTkn: $2,
Value: $2.Value,
},
OpenCurlyBracket: $4,
Stmts: $5,
CloseCurlyBracket: $6,
}
$$ = &ast.StmtTrait{ast.Node{}, name, $5}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $6)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Name, $4.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Stmts, $6.SkippedTokens)
}
;
@ -2756,19 +2762,18 @@ non_empty_for_exprs:
anonymous_class:
T_CLASS ctor_arguments extends_from implements_list backup_doc_comment '{' class_statement_list '}'
{
if $2 != nil {
$$ = &ast.StmtClass{ast.Node{}, nil, nil, $2.(*ast.ArgumentList), $3, $4, $7}
} else {
$$ = &ast.StmtClass{ast.Node{}, nil, nil, nil, $3, $4, $7}
$$ = &ast.StmtClass{
Node: ast.Node{
Position: position.NewTokensPosition($1, $8),
},
ClassTkn: $1,
ArgumentList: $2,
Extends: $3,
Implements: $4,
OpenCurlyBracket: $6,
Stmts: $7,
CloseCurlyBracket: $8,
}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $8)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Name, $6.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Stmts, $8.SkippedTokens)
}
;

View File

@ -238,12 +238,15 @@ func (n *StmtCatch) Accept(v NodeVisitor) {
// StmtClass node
type StmtClass struct {
Node
ClassName Vertex
Modifiers []Vertex
ArgumentList *ArgumentList
ClassTkn *token.Token
ClassName Vertex
ArgumentList Vertex
Extends *StmtClassExtends
Implements *StmtClassImplements
OpenCurlyBracket *token.Token
Stmts []Vertex
CloseCurlyBracket *token.Token
}
func (n *StmtClass) Accept(v NodeVisitor) {
@ -741,8 +744,13 @@ func (n *StmtThrow) Accept(v NodeVisitor) {
// StmtTrait node
type StmtTrait struct {
Node
TraitTkn *token.Token
TraitName Vertex
Extends *StmtClassExtends
Implements *StmtClassImplements
OpenCurlyBracket *token.Token
Stmts []Vertex
CloseCurlyBracket *token.Token
}
func (n *StmtTrait) Accept(v NodeVisitor) {

View File

@ -1476,7 +1476,7 @@ func (p *PrettyPrinter) printStmtClass(n ast.Vertex) {
if nn.ArgumentList != nil {
io.WriteString(p.w, "(")
p.joinPrint(", ", nn.ArgumentList.Arguments)
p.joinPrint(", ", nn.ArgumentList.(*ast.ArgumentList).Arguments)
io.WriteString(p.w, ")")
}

View File

@ -2097,7 +2097,7 @@ func (p *Printer) printStmtClass(n ast.Vertex) {
if nn.ArgumentList != nil {
p.printFreeFloatingOrDefault(nn.ArgumentList, token.Start, "(")
p.joinPrint(",", nn.ArgumentList.Arguments)
p.joinPrint(",", nn.ArgumentList.(*ast.ArgumentList).Arguments)
p.printFreeFloatingOrDefault(nn.ArgumentList, token.End, ")")
}