Merge pull request #73 from z7zmey/issue-63

#63: print valid code even without meta
This commit is contained in:
Vadym Slizov 2019-02-01 23:51:43 +02:00 committed by GitHub
commit a7082117d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 1859 additions and 5463 deletions

View File

@ -2,6 +2,7 @@ package php5
import (
"io"
"strings"
"github.com/z7zmey/php-parser/errors"
"github.com/z7zmey/php-parser/meta"
@ -163,6 +164,73 @@ func (l *Parser) prependMetaToken(n node.Node, t *scanner.Token, tn meta.TokenNa
n.GetMeta().Unshift(m)
}
func (l *Parser) splitSemicolonTokenAndPhpCloseTag(htmlNode node.Node, prevNode node.Node) {
SemiColonTokenMeta := prevNode.GetMeta().Cut(meta.AndFilter(
meta.TokenNameFilter(meta.SemiColonToken),
meta.TypeFilter(meta.TokenType),
))
if len(*SemiColonTokenMeta) < 1 {
return
}
metaTokenValue := (*SemiColonTokenMeta)[0].Value
i := strings.Index(metaTokenValue, "?>")
if i < 0 {
SemiColonTokenMeta.AppendTo(prevNode.GetMeta())
} else {
if metaTokenValue[0] == ';' {
prevNode.GetMeta().Push(&meta.Data{
Value: metaTokenValue[0:1],
Type: meta.TokenType,
Position: nil,
TokenName: meta.SemiColonToken,
})
htmlNode.GetMeta().Push(&meta.Data{
Value: metaTokenValue[1:i],
Type: meta.WhiteSpaceType,
Position: nil,
TokenName: meta.NodeStart,
})
htmlNode.GetMeta().Push(&meta.Data{
Value: metaTokenValue[i : i+2],
Type: meta.TokenType,
Position: nil,
TokenName: meta.NodeStart,
})
if len(metaTokenValue) > i+2 {
htmlNode.GetMeta().Push(&meta.Data{
Value: metaTokenValue[i+2:],
Type: meta.WhiteSpaceType,
Position: nil,
TokenName: meta.NodeStart,
})
}
} else {
htmlNode.GetMeta().Push(&meta.Data{
Value: metaTokenValue[:2],
Type: meta.TokenType,
Position: nil,
TokenName: meta.NodeStart,
})
if len(metaTokenValue) > 2 {
htmlNode.GetMeta().Push(&meta.Data{
Value: metaTokenValue[2:],
Type: meta.WhiteSpaceType,
Position: nil,
TokenName: meta.NodeStart,
})
}
}
}
}
func (p *Parser) returnTokenToPool(yyDollar []yySymType, yyVAL *yySymType) {
for i := 1; i < len(yyDollar); i++ {
if yyDollar[i].token != nil {

File diff suppressed because it is too large Load Diff

View File

@ -293,6 +293,11 @@ start:
top_statement_list:
top_statement_list top_statement
{
if inlineHtmlNode, ok := $2.(*stmt.InlineHtml); ok && len($1) > 0 {
prevNode := lastNode($1)
yylex.(*Parser).splitSemicolonTokenAndPhpCloseTag(inlineHtmlNode, prevNode)
}
if $2 != nil {
$$ = append($1, $2)
}
@ -804,6 +809,11 @@ constant_declaration:
inner_statement_list:
inner_statement_list inner_statement
{
if inlineHtmlNode, ok := $2.(*stmt.InlineHtml); ok && len($1) > 0 {
prevNode := lastNode($1)
yylex.(*Parser).splitSemicolonTokenAndPhpCloseTag(inlineHtmlNode, prevNode)
}
if $2 != nil {
$$ = append($1, $2)
}
@ -3117,7 +3127,7 @@ class_variable_declaration:
// save comments
$2.Meta.SetTokenName(meta.NodeEnd).AppendTo(lastNode($1).GetMeta())
$3.Meta.SetTokenName(meta.NodeStart).AppendTo(variable.GetMeta())
$3.Meta.SetTokenName(meta.NodeStart).AppendTo(property.GetMeta())
yylex.(*Parser).appendMeta(variable, &meta.Data{"$", meta.TokenType, nil, meta.NodeStart}, meta.NodeStart)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
@ -3136,7 +3146,7 @@ class_variable_declaration:
// save comments
$2.Meta.SetTokenName(meta.NodeEnd).AppendTo(lastNode($1).GetMeta())
$3.Meta.SetTokenName(meta.NodeStart).AppendTo(variable.GetMeta())
$3.Meta.SetTokenName(meta.NodeStart).AppendTo(property.GetMeta())
yylex.(*Parser).appendMeta(variable, &meta.Data{"$", meta.TokenType, nil, meta.NodeStart}, meta.NodeStart)
$4.Meta.SetTokenName(meta.EqualToken).AppendTo(property.GetMeta())
@ -3155,7 +3165,7 @@ class_variable_declaration:
property.SetPosition(yylex.(*Parser).positionBuilder.NewTokenPosition($1))
// save comments
$1.Meta.SetTokenName(meta.NodeStart).AppendTo(variable.GetMeta())
$1.Meta.SetTokenName(meta.NodeStart).AppendTo(property.GetMeta())
yylex.(*Parser).appendMeta(variable, &meta.Data{"$", meta.TokenType, nil, meta.NodeStart}, meta.NodeStart)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
@ -3173,7 +3183,7 @@ class_variable_declaration:
property.SetPosition(yylex.(*Parser).positionBuilder.NewTokenNodePosition($1, $3))
// save comments
$1.Meta.SetTokenName(meta.NodeStart).AppendTo(variable.GetMeta())
$1.Meta.SetTokenName(meta.NodeStart).AppendTo(property.GetMeta())
yylex.(*Parser).appendMeta(variable, &meta.Data{"$", meta.TokenType, nil, meta.NodeStart}, meta.NodeStart)
$2.Meta.SetTokenName(meta.EqualToken).AppendTo(property.GetMeta())

View File

@ -2,6 +2,7 @@ package php7
import (
"io"
"strings"
"github.com/z7zmey/php-parser/position"
@ -155,6 +156,73 @@ func (l *Parser) prependMetaToken(n node.Node, t *scanner.Token, tn meta.TokenNa
n.GetMeta().Unshift(m)
}
func (l *Parser) splitSemicolonTokenAndPhpCloseTag(htmlNode node.Node, prevNode node.Node) {
SemiColonTokenMeta := prevNode.GetMeta().Cut(meta.AndFilter(
meta.TokenNameFilter(meta.SemiColonToken),
meta.TypeFilter(meta.TokenType),
))
if len(*SemiColonTokenMeta) < 1 {
return
}
metaTokenValue := (*SemiColonTokenMeta)[0].Value
i := strings.Index(metaTokenValue, "?>")
if i < 0 {
SemiColonTokenMeta.AppendTo(prevNode.GetMeta())
} else {
if metaTokenValue[0] == ';' {
prevNode.GetMeta().Push(&meta.Data{
Value: metaTokenValue[0:1],
Type: meta.TokenType,
Position: nil,
TokenName: meta.SemiColonToken,
})
htmlNode.GetMeta().Push(&meta.Data{
Value: metaTokenValue[1:i],
Type: meta.WhiteSpaceType,
Position: nil,
TokenName: meta.NodeStart,
})
htmlNode.GetMeta().Push(&meta.Data{
Value: metaTokenValue[i : i+2],
Type: meta.TokenType,
Position: nil,
TokenName: meta.NodeStart,
})
if len(metaTokenValue) > i+2 {
htmlNode.GetMeta().Push(&meta.Data{
Value: metaTokenValue[i+2:],
Type: meta.WhiteSpaceType,
Position: nil,
TokenName: meta.NodeStart,
})
}
} else {
htmlNode.GetMeta().Push(&meta.Data{
Value: metaTokenValue[:2],
Type: meta.TokenType,
Position: nil,
TokenName: meta.NodeStart,
})
if len(metaTokenValue) > 2 {
htmlNode.GetMeta().Push(&meta.Data{
Value: metaTokenValue[2:],
Type: meta.WhiteSpaceType,
Position: nil,
TokenName: meta.NodeStart,
})
}
}
}
}
func (p *Parser) returnTokenToPool(yyDollar []yySymType, yyVAL *yySymType) {
for i := 1; i < len(yyDollar); i++ {
if yyDollar[i].token != nil {

File diff suppressed because it is too large Load Diff

View File

@ -343,6 +343,11 @@ identifier:
top_statement_list:
top_statement_list top_statement
{
if inlineHtmlNode, ok := $2.(*stmt.InlineHtml); ok && len($1) > 0 {
prevNode := lastNode($1)
yylex.(*Parser).splitSemicolonTokenAndPhpCloseTag(inlineHtmlNode, prevNode)
}
if $2 != nil {
$$ = append($1, $2)
}
@ -888,6 +893,11 @@ const_list:
inner_statement_list:
inner_statement_list inner_statement
{
if inlineHtmlNode, ok := $2.(*stmt.InlineHtml); ok && len($1) > 0 {
prevNode := lastNode($1)
yylex.(*Parser).splitSemicolonTokenAndPhpCloseTag(inlineHtmlNode, prevNode)
}
if $2 != nil {
$$ = append($1, $2)
}

File diff suppressed because it is too large Load Diff

View File

@ -1491,6 +1491,7 @@ func TestParseAndPrintComplexString3(t *testing.T) {
src := `<?php
"test ${foo}" ;
"test ${foo[0]}";
"test ${foo::$bar}";
"test ${foo }" ;
"test ${foo . 'bar' }" ;
"test ${foo [ ] }" ;

File diff suppressed because it is too large Load Diff