#63: print valid code even without meta
This commit is contained in:
parent
f2e5271e2a
commit
02a77f7fb0
@ -2,6 +2,7 @@ package php5
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/z7zmey/php-parser/errors"
|
"github.com/z7zmey/php-parser/errors"
|
||||||
"github.com/z7zmey/php-parser/meta"
|
"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)
|
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) {
|
func (p *Parser) returnTokenToPool(yyDollar []yySymType, yyVAL *yySymType) {
|
||||||
for i := 1; i < len(yyDollar); i++ {
|
for i := 1; i < len(yyDollar); i++ {
|
||||||
if yyDollar[i].token != nil {
|
if yyDollar[i].token != nil {
|
||||||
|
1078
php5/php5.go
1078
php5/php5.go
File diff suppressed because it is too large
Load Diff
18
php5/php5.y
18
php5/php5.y
@ -293,6 +293,11 @@ start:
|
|||||||
top_statement_list:
|
top_statement_list:
|
||||||
top_statement_list top_statement
|
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 {
|
if $2 != nil {
|
||||||
$$ = append($1, $2)
|
$$ = append($1, $2)
|
||||||
}
|
}
|
||||||
@ -804,6 +809,11 @@ constant_declaration:
|
|||||||
inner_statement_list:
|
inner_statement_list:
|
||||||
inner_statement_list inner_statement
|
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 {
|
if $2 != nil {
|
||||||
$$ = append($1, $2)
|
$$ = append($1, $2)
|
||||||
}
|
}
|
||||||
@ -3117,7 +3127,7 @@ class_variable_declaration:
|
|||||||
|
|
||||||
// save comments
|
// save comments
|
||||||
$2.Meta.SetTokenName(meta.NodeEnd).AppendTo(lastNode($1).GetMeta())
|
$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).appendMeta(variable, &meta.Data{"$", meta.TokenType, nil, meta.NodeStart}, meta.NodeStart)
|
||||||
|
|
||||||
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
|
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
|
||||||
@ -3136,7 +3146,7 @@ class_variable_declaration:
|
|||||||
|
|
||||||
// save comments
|
// save comments
|
||||||
$2.Meta.SetTokenName(meta.NodeEnd).AppendTo(lastNode($1).GetMeta())
|
$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).appendMeta(variable, &meta.Data{"$", meta.TokenType, nil, meta.NodeStart}, meta.NodeStart)
|
||||||
$4.Meta.SetTokenName(meta.EqualToken).AppendTo(property.GetMeta())
|
$4.Meta.SetTokenName(meta.EqualToken).AppendTo(property.GetMeta())
|
||||||
|
|
||||||
@ -3155,7 +3165,7 @@ class_variable_declaration:
|
|||||||
property.SetPosition(yylex.(*Parser).positionBuilder.NewTokenPosition($1))
|
property.SetPosition(yylex.(*Parser).positionBuilder.NewTokenPosition($1))
|
||||||
|
|
||||||
// save comments
|
// 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).appendMeta(variable, &meta.Data{"$", meta.TokenType, nil, meta.NodeStart}, meta.NodeStart)
|
||||||
|
|
||||||
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
|
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
|
||||||
@ -3173,7 +3183,7 @@ class_variable_declaration:
|
|||||||
property.SetPosition(yylex.(*Parser).positionBuilder.NewTokenNodePosition($1, $3))
|
property.SetPosition(yylex.(*Parser).positionBuilder.NewTokenNodePosition($1, $3))
|
||||||
|
|
||||||
// save comments
|
// 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).appendMeta(variable, &meta.Data{"$", meta.TokenType, nil, meta.NodeStart}, meta.NodeStart)
|
||||||
$2.Meta.SetTokenName(meta.EqualToken).AppendTo(property.GetMeta())
|
$2.Meta.SetTokenName(meta.EqualToken).AppendTo(property.GetMeta())
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package php7
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/z7zmey/php-parser/position"
|
"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)
|
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) {
|
func (p *Parser) returnTokenToPool(yyDollar []yySymType, yyVAL *yySymType) {
|
||||||
for i := 1; i < len(yyDollar); i++ {
|
for i := 1; i < len(yyDollar); i++ {
|
||||||
if yyDollar[i].token != nil {
|
if yyDollar[i].token != nil {
|
||||||
|
856
php7/php7.go
856
php7/php7.go
File diff suppressed because it is too large
Load Diff
10
php7/php7.y
10
php7/php7.y
@ -343,6 +343,11 @@ identifier:
|
|||||||
top_statement_list:
|
top_statement_list:
|
||||||
top_statement_list top_statement
|
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 {
|
if $2 != nil {
|
||||||
$$ = append($1, $2)
|
$$ = append($1, $2)
|
||||||
}
|
}
|
||||||
@ -888,6 +893,11 @@ const_list:
|
|||||||
inner_statement_list:
|
inner_statement_list:
|
||||||
inner_statement_list inner_statement
|
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 {
|
if $2 != nil {
|
||||||
$$ = append($1, $2)
|
$$ = append($1, $2)
|
||||||
}
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1491,6 +1491,7 @@ func TestParseAndPrintComplexString3(t *testing.T) {
|
|||||||
src := `<?php
|
src := `<?php
|
||||||
"test ${foo}" ;
|
"test ${foo}" ;
|
||||||
"test ${foo[0]}";
|
"test ${foo[0]}";
|
||||||
|
"test ${foo::$bar}";
|
||||||
"test ${foo }" ;
|
"test ${foo }" ;
|
||||||
"test ${foo . 'bar' }" ;
|
"test ${foo . 'bar' }" ;
|
||||||
"test ${foo [ ] }" ;
|
"test ${foo [ ] }" ;
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user