[refactoring] update ast structure of "Encapsed", "EncapsedStringPart" and "Heredoc" nodes

This commit is contained in:
Vadym Slizov 2020-11-20 23:36:45 +02:00
parent e3ad9747f4
commit e6a23dfa3a
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
8 changed files with 148 additions and 113 deletions

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -4467,11 +4467,15 @@ backticks_expr:
}
| T_ENCAPSED_AND_WHITESPACE
{
part := &ast.ScalarEncapsedStringPart{ast.Node{}, $1.Value}
$$ = []ast.Vertex{part}
// save position
part.GetNode().Position = position.NewTokenPosition($1)
$$ = []ast.Vertex{
&ast.ScalarEncapsedStringPart{
Node: ast.Node{
Position: position.NewTokenPosition($1),
},
EncapsedStrTkn: $1,
Value: $1.Value,
},
}
}
| encaps_list
{
@ -4593,25 +4597,32 @@ common_scalar:
}
| T_START_HEREDOC T_ENCAPSED_AND_WHITESPACE T_END_HEREDOC
{
encapsed := &ast.ScalarEncapsedStringPart{ast.Node{}, $2.Value}
$$ = &ast.ScalarHeredoc{ast.Node{}, $1.Value, []ast.Vertex{encapsed}}
// save position
encapsed.GetNode().Position = position.NewTokenPosition($2)
$$.GetNode().Position = position.NewTokensPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = &ast.ScalarHeredoc{
Node: ast.Node{
Position: position.NewTokensPosition($1, $3),
},
OpenHeredocTkn: $1,
Parts: []ast.Vertex{
&ast.ScalarEncapsedStringPart{
Node: ast.Node{
Position: position.NewTokenPosition($2),
},
EncapsedStrTkn: $2,
Value: $2.Value,
},
},
CloseHeredocTkn: $3,
}
}
| T_START_HEREDOC T_END_HEREDOC
{
$$ = &ast.ScalarHeredoc{ast.Node{}, $1.Value, nil}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $2)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = &ast.ScalarHeredoc{
Node: ast.Node{
Position: position.NewTokensPosition($1, $2),
},
OpenHeredocTkn: $1,
CloseHeredocTkn: $2,
}
}
;
@ -5180,23 +5191,25 @@ scalar:
}
| '"' encaps_list '"'
{
$$ = &ast.ScalarEncapsed{ast.Node{}, $2}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = &ast.ScalarEncapsed{
Node: ast.Node{
Position: position.NewTokensPosition($1, $3),
},
OpenQoteTkn: $1,
Parts: $2,
CloseQoteTkn: $1,
}
}
| T_START_HEREDOC encaps_list T_END_HEREDOC
{
$$ = &ast.ScalarHeredoc{ast.Node{}, $1.Value, $2}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = &ast.ScalarHeredoc{
Node: ast.Node{
Position: position.NewTokensPosition($1, $3),
},
OpenHeredocTkn: $1,
Parts: $2,
CloseHeredocTkn: $3,
}
}
| T_CLASS_C
{
@ -5976,14 +5989,16 @@ encaps_list:
}
| encaps_list T_ENCAPSED_AND_WHITESPACE
{
encapsed := &ast.ScalarEncapsedStringPart{ast.Node{}, $2.Value}
$$ = append($1, encapsed)
// save position
encapsed.GetNode().Position = position.NewTokenPosition($2)
// save comments
yylex.(*Parser).setFreeFloating(encapsed, token.Start, $2.SkippedTokens)
$$ = append(
$1,
&ast.ScalarEncapsedStringPart{
Node: ast.Node{
Position: position.NewTokenPosition($2),
},
EncapsedStrTkn: $2,
Value: $2.Value,
},
)
}
| encaps_var
{
@ -5991,14 +6006,16 @@ encaps_list:
}
| T_ENCAPSED_AND_WHITESPACE encaps_var
{
encapsed := &ast.ScalarEncapsedStringPart{ast.Node{}, $1.Value}
$$ = []ast.Vertex{encapsed, $2}
// save position
encapsed.GetNode().Position = position.NewTokenPosition($1)
// save comments
yylex.(*Parser).setFreeFloating(encapsed, token.Start, $1.SkippedTokens)
$$ = []ast.Vertex{
&ast.ScalarEncapsedStringPart{
Node: ast.Node{
Position: position.NewTokenPosition($1),
},
EncapsedStrTkn: $1,
Value: $1.Value,
},
$2,
}
}
;

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -3878,11 +3878,15 @@ backticks_expr:
}
| T_ENCAPSED_AND_WHITESPACE
{
part := &ast.ScalarEncapsedStringPart{ast.Node{}, $1.Value}
$$ = []ast.Vertex{part}
// save position
part.GetNode().Position = position.NewTokenPosition($1)
$$ = []ast.Vertex{
&ast.ScalarEncapsedStringPart{
Node: ast.Node{
Position: position.NewTokenPosition($1),
},
EncapsedStrTkn: $1,
Value: $1.Value,
},
}
}
| encaps_list
{
@ -4040,45 +4044,54 @@ scalar:
}
| T_START_HEREDOC T_ENCAPSED_AND_WHITESPACE T_END_HEREDOC
{
encapsed := &ast.ScalarEncapsedStringPart{ast.Node{}, $2.Value}
$$ = &ast.ScalarHeredoc{ast.Node{}, $1.Value, []ast.Vertex{encapsed}}
// save position
encapsed.GetNode().Position = position.NewTokenPosition($2)
$$.GetNode().Position = position.NewTokensPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = &ast.ScalarHeredoc{
Node: ast.Node{
Position: position.NewTokensPosition($1, $3),
},
OpenHeredocTkn: $1,
Parts: []ast.Vertex{
&ast.ScalarEncapsedStringPart{
Node: ast.Node{
Position: position.NewTokenPosition($2),
},
EncapsedStrTkn: $2,
Value: $2.Value,
},
},
CloseHeredocTkn: $3,
}
}
| T_START_HEREDOC T_END_HEREDOC
{
$$ = &ast.ScalarHeredoc{ast.Node{}, $1.Value, nil}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $2)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = &ast.ScalarHeredoc{
Node: ast.Node{
Position: position.NewTokensPosition($1, $2),
},
OpenHeredocTkn: $1,
CloseHeredocTkn: $2,
}
}
| '"' encaps_list '"'
{
$$ = &ast.ScalarEncapsed{ast.Node{}, $2}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = &ast.ScalarEncapsed{
Node: ast.Node{
Position: position.NewTokensPosition($1, $3),
},
OpenQoteTkn: $1,
Parts: $2,
CloseQoteTkn: $1,
}
}
| T_START_HEREDOC encaps_list T_END_HEREDOC
{
$$ = &ast.ScalarHeredoc{ast.Node{}, $1.Value, $2}
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = &ast.ScalarHeredoc{
Node: ast.Node{
Position: position.NewTokensPosition($1, $3),
},
OpenHeredocTkn: $1,
Parts: $2,
CloseHeredocTkn: $3,
}
}
| dereferencable_scalar
{
@ -4615,14 +4628,16 @@ encaps_list:
}
| encaps_list T_ENCAPSED_AND_WHITESPACE
{
encapsed := &ast.ScalarEncapsedStringPart{ast.Node{}, $2.Value}
$$ = append($1, encapsed)
// save position
encapsed.GetNode().Position = position.NewTokenPosition($2)
// save comments
yylex.(*Parser).setFreeFloating(encapsed, token.Start, $2.SkippedTokens)
$$ = append(
$1,
&ast.ScalarEncapsedStringPart{
Node: ast.Node{
Position: position.NewTokenPosition($2),
},
EncapsedStrTkn: $2,
Value: $2.Value,
},
)
}
| encaps_var
{
@ -4630,14 +4645,16 @@ encaps_list:
}
| T_ENCAPSED_AND_WHITESPACE encaps_var
{
encapsed := &ast.ScalarEncapsedStringPart{ast.Node{}, $1.Value}
$$ = []ast.Vertex{encapsed, $2}
// save position
encapsed.GetNode().Position = position.NewTokenPosition($1)
// save comments
yylex.(*Parser).setFreeFloating(encapsed, token.Start, $1.SkippedTokens)
$$ = []ast.Vertex{
&ast.ScalarEncapsedStringPart{
Node: ast.Node{
Position: position.NewTokenPosition($1),
},
EncapsedStrTkn: $1,
Value: $1.Value,
},
$2,
}
}
;

View File

@ -126,7 +126,9 @@ func (n *ScalarDnumber) Accept(v NodeVisitor) {
// ScalarEncapsed node
type ScalarEncapsed struct {
Node
OpenQoteTkn *token.Token
Parts []Vertex
CloseQoteTkn *token.Token
}
func (n *ScalarEncapsed) Accept(v NodeVisitor) {
@ -136,6 +138,7 @@ func (n *ScalarEncapsed) Accept(v NodeVisitor) {
// ScalarEncapsedStringPart node
type ScalarEncapsedStringPart struct {
Node
EncapsedStrTkn *token.Token
Value []byte
}
@ -146,8 +149,9 @@ func (n *ScalarEncapsedStringPart) Accept(v NodeVisitor) {
// ScalarHeredoc node
type ScalarHeredoc struct {
Node
Label []byte
OpenHeredocTkn *token.Token
Parts []Vertex
CloseHeredocTkn *token.Token
}
func (n *ScalarHeredoc) Accept(v NodeVisitor) {

View File

@ -1224,9 +1224,6 @@ func (v *Dump) ScalarHeredoc(n *ast.ScalarHeredoc) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.ScalarHeredoc{\n")
v.printNode(n.GetNode())
v.printIndent(v.indent)
v.print(fmt.Sprintf("Label: %q,\n", n.Label))
}
func (v *Dump) ScalarLnumber(n *ast.ScalarLnumber) {

View File

@ -558,7 +558,7 @@ func (p *PrettyPrinter) printScalarEncapsed(n ast.Vertex) {
func (p *PrettyPrinter) printScalarHeredoc(n ast.Vertex) {
nn := n.(*ast.ScalarHeredoc)
io.WriteString(p.w, string(nn.Label))
io.WriteString(p.w, string(nn.OpenHeredocTkn.Value))
for _, part := range nn.Parts {
switch part.(type) {
@ -571,7 +571,7 @@ func (p *PrettyPrinter) printScalarHeredoc(n ast.Vertex) {
}
}
io.WriteString(p.w, strings.Trim(string(nn.Label), "<\"'\n"))
io.WriteString(p.w, strings.Trim(string(nn.OpenHeredocTkn.Value), "<\"'\n"))
}
func (p *PrettyPrinter) printScalarMagicConstant(n ast.Vertex) {

View File

@ -687,7 +687,7 @@ func (p *Printer) printScalarHeredoc(n ast.Vertex) {
p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart)
p.bufStart = ""
p.write(nn.Label)
p.write(nn.OpenHeredocTkn.Value)
for _, part := range nn.Parts {
switch part.(type) {
@ -710,7 +710,7 @@ func (p *Printer) printScalarHeredoc(n ast.Vertex) {
}
}
p.write([]byte(strings.Trim(string(nn.Label), "<\"'\n")))
p.write([]byte(strings.Trim(string(nn.OpenHeredocTkn.Value), "<\"'\n")))
p.printFreeFloating(nn, token.End)
}