#70: [php7] inherit meta.Data at stmt.Expression and stmt.Label

This commit is contained in:
z7zmey 2018-12-25 11:56:32 +02:00
parent 86afa319f3
commit f406a441a9
7 changed files with 441 additions and 429 deletions

View File

@ -13,6 +13,14 @@ import (
"github.com/z7zmey/php-parser/scanner"
)
var inheritMetaFilter = meta.AndFilter(
meta.TokenNameFilter(meta.NodeStart),
meta.OrFilter(
meta.TypeFilter(meta.CommentType, meta.WhiteSpaceType),
meta.ValueFilter("<?php", "<?"),
),
)
func (lval *yySymType) Token(t *scanner.Token) {
lval.token = t
}

File diff suppressed because it is too large Load Diff

View File

@ -396,7 +396,7 @@ name:
$$.SetPosition(yylex.(*Parser).positionBuilder.NewNodeListPosition($1))
// save comments
$1[0].GetMeta().Cut(meta.TokenNameFilter(meta.NodeStart)).AppendTo($$.GetMeta())
$1[0].GetMeta().Cut(inheritMetaFilter).PrependTo($$.GetMeta())
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
}
@ -647,7 +647,7 @@ group_use_declaration:
}
$6.Meta.SetTokenName(meta.CloseCurlyBracesToken).AppendTo($$.GetMeta())
$1[0].GetMeta().Cut(meta.TokenNameFilter(meta.NodeStart)).AppendTo(name.GetMeta())
$1[0].GetMeta().Cut(inheritMetaFilter).PrependTo(name.GetMeta())
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
}
@ -671,7 +671,7 @@ group_use_declaration:
}
$7.Meta.SetTokenName(meta.CloseCurlyBracesToken).AppendTo($$.GetMeta())
$2[0].GetMeta().Cut(meta.TokenNameFilter(meta.NodeStart)).AppendTo(name.GetMeta())
$2[0].GetMeta().Cut(inheritMetaFilter).PrependTo(name.GetMeta())
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
}
@ -696,7 +696,7 @@ mixed_group_use_declaration:
}
$6.Meta.SetTokenName(meta.CloseCurlyBracesToken).AppendTo($$.GetMeta())
$1[0].GetMeta().Cut(meta.TokenNameFilter(meta.NodeStart)).AppendTo(name.GetMeta())
$1[0].GetMeta().Cut(inheritMetaFilter).PrependTo(name.GetMeta())
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
}
@ -720,7 +720,7 @@ mixed_group_use_declaration:
}
$7.Meta.SetTokenName(meta.CloseCurlyBracesToken).AppendTo($$.GetMeta())
$2[0].GetMeta().Cut(meta.TokenNameFilter(meta.NodeStart)).AppendTo(name.GetMeta())
$2[0].GetMeta().Cut(inheritMetaFilter).PrependTo(name.GetMeta())
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
}
@ -796,7 +796,7 @@ inline_use_declaration:
{
$$ = $1
$1.(*stmt.Use).Use.GetMeta().Cut(meta.TokenNameFilter(meta.NodeStart)).AppendTo($$.GetMeta())
$1.(*stmt.Use).Use.GetMeta().Cut(inheritMetaFilter).PrependTo($$.GetMeta())
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
}
@ -804,7 +804,7 @@ inline_use_declaration:
{
$$ = $2.(*stmt.Use).SetUseType($1)
$1.GetMeta().Cut(meta.TokenNameFilter(meta.NodeStart)).AppendTo($$.GetMeta())
$1.GetMeta().Cut(inheritMetaFilter).PrependTo($$.GetMeta())
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
}
@ -820,7 +820,7 @@ unprefixed_use_declaration:
name.SetPosition(yylex.(*Parser).positionBuilder.NewNodeListPosition($1))
$$.SetPosition(yylex.(*Parser).positionBuilder.NewNodeListPosition($1))
$1[0].GetMeta().Cut(meta.TokenNameFilter(meta.NodeStart)).AppendTo(name.GetMeta())
$1[0].GetMeta().Cut(inheritMetaFilter).PrependTo(name.GetMeta())
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
}
@ -839,7 +839,7 @@ unprefixed_use_declaration:
$2.Meta.SetTokenName(meta.AsToken).AppendTo($$.GetMeta())
$3.Meta.SetTokenName(meta.NodeStart).AppendTo(alias.GetMeta())
$1[0].GetMeta().Cut(meta.TokenNameFilter(meta.NodeStart)).AppendTo(name.GetMeta())
$1[0].GetMeta().Cut(inheritMetaFilter).PrependTo(name.GetMeta())
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
}
@ -850,7 +850,7 @@ use_declaration:
{
$$ = $1
$1.(*stmt.Use).Use.GetMeta().Cut(meta.TokenNameFilter(meta.NodeStart)).AppendTo($$.GetMeta())
$1.(*stmt.Use).Use.GetMeta().Cut(inheritMetaFilter).PrependTo($$.GetMeta())
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
}
@ -1178,6 +1178,8 @@ statement:
$2.Meta.SetTokenName(meta.SemiColonToken).AppendTo($$.GetMeta())
yylex.(*Parser).appendMetaToken($$, $2, meta.SemiColonToken)
$1.GetMeta().Cut(inheritMetaFilter).PrependTo($$.GetMeta())
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
}
| T_UNSET '(' unset_variables possible_comma ')' ';'
@ -1337,7 +1339,7 @@ statement:
$$.SetPosition(yylex.(*Parser).positionBuilder.NewTokensPosition($1, $2))
// save comments
$1.Meta.SetTokenName(meta.NodeStart).AppendTo(label.GetMeta())
$1.Meta.SetTokenName(meta.NodeStart).AppendTo($$.GetMeta())
$2.Meta.SetTokenName(meta.ColonToken).AppendTo($$.GetMeta())
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)

View File

@ -138,14 +138,14 @@ func ExampleDumper() {
// | [*stmt.Expression]
// | "Position": Pos{Line: 8-8 Pos: 124-128}
// | "Meta":
// | "\n\t\t\t\t\t" before "NodeStart"
// | "// some comment\n" before "NodeStart"
// | "\t\t\t\t\t" before "NodeStart"
// | ";" before "SemiColonToken"
// | "Expr":
// | [*expr.Variable]
// | "Position": Pos{Line: 8-8 Pos: 124-127}
// | "Meta":
// | "\n\t\t\t\t\t" before "NodeStart"
// | "// some comment\n" before "NodeStart"
// | "\t\t\t\t\t" before "NodeStart"
// | "$" before "NodeStart"
// | "VarName":
// | [*node.Identifier]

View File

@ -424,6 +424,39 @@ func ExampleGoDumper() {
// },
// Meta: meta.Collection{
// &meta.Data{
// Type: meta.WhiteSpaceType,
// Position: &position.Position{
// StartLine: 6,
// EndLine: 7,
// StartPos: 97,
// EndPos: 102,
// },
// Value: "\n\t\t\t\t\t",
// TokenName: meta.NodeStart,
// },
// &meta.Data{
// Type: meta.CommentType,
// Position: &position.Position{
// StartLine: 7,
// EndLine: 7,
// StartPos: 103,
// EndPos: 118,
// },
// Value: "// some comment\n",
// TokenName: meta.NodeStart,
// },
// &meta.Data{
// Type: meta.WhiteSpaceType,
// Position: &position.Position{
// StartLine: 8,
// EndLine: 8,
// StartPos: 119,
// EndPos: 123,
// },
// Value: "\t\t\t\t\t",
// TokenName: meta.NodeStart,
// },
// &meta.Data{
// Type: meta.TokenType,
// Position: &position.Position{
// StartLine: 8,
@ -444,39 +477,6 @@ func ExampleGoDumper() {
// },
// Meta: meta.Collection{
// &meta.Data{
// Type: meta.WhiteSpaceType,
// Position: &position.Position{
// StartLine: 6,
// EndLine: 7,
// StartPos: 97,
// EndPos: 102,
// },
// Value: "\n\t\t\t\t\t",
// TokenName: meta.NodeStart,
// },
// &meta.Data{
// Type: meta.CommentType,
// Position: &position.Position{
// StartLine: 7,
// EndLine: 7,
// StartPos: 103,
// EndPos: 118,
// },
// Value: "// some comment\n",
// TokenName: meta.NodeStart,
// },
// &meta.Data{
// Type: meta.WhiteSpaceType,
// Position: &position.Position{
// StartLine: 8,
// EndLine: 8,
// StartPos: 119,
// EndPos: 123,
// },
// Value: "\t\t\t\t\t",
// TokenName: meta.NodeStart,
// },
// &meta.Data{
// Type: meta.TokenType,
// Position: nil,
// Value: "$",

View File

@ -37,5 +37,5 @@ func ExampleJsonDumper() {
nodes.Walk(dumper)
// Output:
// {"type":"*node.Root","position":{"startPos":10,"endPos":166,"startLine":3,"endLine":12},"Stmts":[{"type":"*stmt.Namespace","position":{"startPos":10,"endPos":166,"startLine":3,"endLine":12},"meta":[{"type":"*meta.TokenType","value":"<?php","tokenName":"NodeStart"},{"type":"*meta.WhiteSpaceType","value":"\n\n\t\t","tokenName":"NodeStart"},{"type":"*meta.WhiteSpaceType","value":" ","tokenName":"OpenCurlyBracesToken"},{"type":"*meta.WhiteSpaceType","value":"\n\t\t","tokenName":"CloseCurlyBracesToken"}],"NamespaceName":{"type":"*name.Name","position":{"startPos":20,"endPos":22,"startLine":3,"endLine":3},"Parts":[{"type":"*name.NamePart","position":{"startPos":20,"endPos":22,"startLine":3,"endLine":3},"meta":[{"type":"*meta.WhiteSpaceType","value":" ","tokenName":"NodeStart"}],"Value":"Foo"}]},"Stmts":[{"type":"*stmt.Class","position":{"startPos":29,"endPos":162,"startLine":4,"endLine":11},"namespacedName":"Foo\\Bar","meta":[{"type":"*meta.WhiteSpaceType","value":"\n\t\t\t","tokenName":"ClassToken"},{"type":"*meta.WhiteSpaceType","value":" ","tokenName":"OpenCurlyBracesToken"},{"type":"*meta.WhiteSpaceType","value":"\n\t\t\t","tokenName":"CloseCurlyBracesToken"}],"PhpDocComment":"","ClassName":{"type":"*node.Identifier","position":{"startPos":35,"endPos":37,"startLine":4,"endLine":4},"meta":[{"type":"*meta.WhiteSpaceType","value":" ","tokenName":"NodeStart"}],"Value":"Bar"},"Stmts":[{"type":"*stmt.ClassMethod","position":{"startPos":45,"endPos":157,"startLine":5,"endLine":10},"meta":[{"type":"*meta.WhiteSpaceType","value":" ","tokenName":"FunctionToken"}],"PhpDocComment":"","ReturnsRef":false,"MethodName":{"type":"*node.Identifier","position":{"startPos":61,"endPos":72,"startLine":5,"endLine":5},"meta":[{"type":"*meta.WhiteSpaceType","value":" ","tokenName":"NodeStart"}],"Value":"FunctionName"},"Modifiers":[{"type":"*node.Identifier","position":{"startPos":45,"endPos":50,"startLine":5,"endLine":5},"meta":[{"type":"*meta.WhiteSpaceType","value":"\n\t\t\t\t","tokenName":"NodeStart"}],"Value":"public"}],"Params":[{"type":"*node.Parameter","position":{"startPos":74,"endPos":89,"startLine":5,"endLine":5},"meta":[{"type":"*meta.WhiteSpaceType","value":" ","tokenName":"EqualToken"}],"ByRef":false,"Variadic":false,"VariableType":{"type":"*name.Name","position":{"startPos":74,"endPos":77,"startLine":5,"endLine":5},"namespacedName":"Foo\\Type","Parts":[{"type":"*name.NamePart","position":{"startPos":74,"endPos":77,"startLine":5,"endLine":5},"Value":"Type"}]},"Variable":{"type":"*expr.Variable","position":{"startPos":79,"endPos":82,"startLine":5,"endLine":5},"meta":[{"type":"*meta.WhiteSpaceType","value":" ","tokenName":"NodeStart"},{"type":"*meta.TokenType","value":"$","tokenName":"NodeStart"}],"VarName":{"type":"*node.Identifier","position":{"startPos":79,"endPos":82,"startLine":5,"endLine":5},"Value":"var"}},"DefaultValue":{"type":"*expr.ConstFetch","position":{"startPos":86,"endPos":89,"startLine":5,"endLine":5},"Constant":{"type":"*name.Name","position":{"startPos":86,"endPos":89,"startLine":5,"endLine":5},"namespacedName":"null","meta":[{"type":"*meta.WhiteSpaceType","value":" ","tokenName":"NodeStart"}],"Parts":[{"type":"*name.NamePart","position":{"startPos":86,"endPos":89,"startLine":5,"endLine":5},"Value":"null"}]}}}],"Stmt":{"type":"*stmt.StmtList","position":{"startPos":96,"endPos":157,"startLine":6,"endLine":10},"meta":[{"type":"*meta.WhiteSpaceType","value":"\n\t\t\t\t","tokenName":"NodeStart"},{"type":"*meta.WhiteSpaceType","value":"\n\t\t\t\t","tokenName":"CloseCurlyBracesToken"}],"Stmts":[{"type":"*stmt.Expression","position":{"startPos":147,"endPos":151,"startLine":9,"endLine":9},"meta":[{"type":"*meta.TokenType","value":";","tokenName":"SemiColonToken"}],"Expr":{"type":"*expr.Variable","position":{"startPos":147,"endPos":150,"startLine":9,"endLine":9},"meta":[{"type":"*meta.WhiteSpaceType","value":"\n\t\t\t\t\t","tokenName":"NodeStart"},{"type":"*meta.CommentType","value":"// some comment\n","tokenName":"NodeStart"},{"type":"*meta.WhiteSpaceType","value":"\t\t\t\t\t","tokenName":"NodeStart"},{"type":"*meta.CommentType","value":"// second comment\n","tokenName":"NodeStart"},{"type":"*meta.WhiteSpaceType","value":"\t\t\t\t\t","tokenName":"NodeStart"},{"type":"*meta.TokenType","value":"$","tokenName":"NodeStart"}],"VarName":{"type":"*node.Identifier","position":{"startPos":147,"endPos":150,"startLine":9,"endLine":9},"Value":"var"}}}]}}]}]}]}
// {"type":"*node.Root","position":{"startPos":10,"endPos":166,"startLine":3,"endLine":12},"Stmts":[{"type":"*stmt.Namespace","position":{"startPos":10,"endPos":166,"startLine":3,"endLine":12},"meta":[{"type":"*meta.TokenType","value":"<?php","tokenName":"NodeStart"},{"type":"*meta.WhiteSpaceType","value":"\n\n\t\t","tokenName":"NodeStart"},{"type":"*meta.WhiteSpaceType","value":" ","tokenName":"OpenCurlyBracesToken"},{"type":"*meta.WhiteSpaceType","value":"\n\t\t","tokenName":"CloseCurlyBracesToken"}],"NamespaceName":{"type":"*name.Name","position":{"startPos":20,"endPos":22,"startLine":3,"endLine":3},"Parts":[{"type":"*name.NamePart","position":{"startPos":20,"endPos":22,"startLine":3,"endLine":3},"meta":[{"type":"*meta.WhiteSpaceType","value":" ","tokenName":"NodeStart"}],"Value":"Foo"}]},"Stmts":[{"type":"*stmt.Class","position":{"startPos":29,"endPos":162,"startLine":4,"endLine":11},"namespacedName":"Foo\\Bar","meta":[{"type":"*meta.WhiteSpaceType","value":"\n\t\t\t","tokenName":"ClassToken"},{"type":"*meta.WhiteSpaceType","value":" ","tokenName":"OpenCurlyBracesToken"},{"type":"*meta.WhiteSpaceType","value":"\n\t\t\t","tokenName":"CloseCurlyBracesToken"}],"PhpDocComment":"","ClassName":{"type":"*node.Identifier","position":{"startPos":35,"endPos":37,"startLine":4,"endLine":4},"meta":[{"type":"*meta.WhiteSpaceType","value":" ","tokenName":"NodeStart"}],"Value":"Bar"},"Stmts":[{"type":"*stmt.ClassMethod","position":{"startPos":45,"endPos":157,"startLine":5,"endLine":10},"meta":[{"type":"*meta.WhiteSpaceType","value":" ","tokenName":"FunctionToken"}],"PhpDocComment":"","ReturnsRef":false,"MethodName":{"type":"*node.Identifier","position":{"startPos":61,"endPos":72,"startLine":5,"endLine":5},"meta":[{"type":"*meta.WhiteSpaceType","value":" ","tokenName":"NodeStart"}],"Value":"FunctionName"},"Modifiers":[{"type":"*node.Identifier","position":{"startPos":45,"endPos":50,"startLine":5,"endLine":5},"meta":[{"type":"*meta.WhiteSpaceType","value":"\n\t\t\t\t","tokenName":"NodeStart"}],"Value":"public"}],"Params":[{"type":"*node.Parameter","position":{"startPos":74,"endPos":89,"startLine":5,"endLine":5},"meta":[{"type":"*meta.WhiteSpaceType","value":" ","tokenName":"EqualToken"}],"ByRef":false,"Variadic":false,"VariableType":{"type":"*name.Name","position":{"startPos":74,"endPos":77,"startLine":5,"endLine":5},"namespacedName":"Foo\\Type","Parts":[{"type":"*name.NamePart","position":{"startPos":74,"endPos":77,"startLine":5,"endLine":5},"Value":"Type"}]},"Variable":{"type":"*expr.Variable","position":{"startPos":79,"endPos":82,"startLine":5,"endLine":5},"meta":[{"type":"*meta.WhiteSpaceType","value":" ","tokenName":"NodeStart"},{"type":"*meta.TokenType","value":"$","tokenName":"NodeStart"}],"VarName":{"type":"*node.Identifier","position":{"startPos":79,"endPos":82,"startLine":5,"endLine":5},"Value":"var"}},"DefaultValue":{"type":"*expr.ConstFetch","position":{"startPos":86,"endPos":89,"startLine":5,"endLine":5},"Constant":{"type":"*name.Name","position":{"startPos":86,"endPos":89,"startLine":5,"endLine":5},"namespacedName":"null","meta":[{"type":"*meta.WhiteSpaceType","value":" ","tokenName":"NodeStart"}],"Parts":[{"type":"*name.NamePart","position":{"startPos":86,"endPos":89,"startLine":5,"endLine":5},"Value":"null"}]}}}],"Stmt":{"type":"*stmt.StmtList","position":{"startPos":96,"endPos":157,"startLine":6,"endLine":10},"meta":[{"type":"*meta.WhiteSpaceType","value":"\n\t\t\t\t","tokenName":"NodeStart"},{"type":"*meta.WhiteSpaceType","value":"\n\t\t\t\t","tokenName":"CloseCurlyBracesToken"}],"Stmts":[{"type":"*stmt.Expression","position":{"startPos":147,"endPos":151,"startLine":9,"endLine":9},"meta":[{"type":"*meta.WhiteSpaceType","value":"\n\t\t\t\t\t","tokenName":"NodeStart"},{"type":"*meta.CommentType","value":"// some comment\n","tokenName":"NodeStart"},{"type":"*meta.WhiteSpaceType","value":"\t\t\t\t\t","tokenName":"NodeStart"},{"type":"*meta.CommentType","value":"// second comment\n","tokenName":"NodeStart"},{"type":"*meta.WhiteSpaceType","value":"\t\t\t\t\t","tokenName":"NodeStart"},{"type":"*meta.TokenType","value":";","tokenName":"SemiColonToken"}],"Expr":{"type":"*expr.Variable","position":{"startPos":147,"endPos":150,"startLine":9,"endLine":9},"meta":[{"type":"*meta.TokenType","value":"$","tokenName":"NodeStart"}],"VarName":{"type":"*node.Identifier","position":{"startPos":147,"endPos":150,"startLine":9,"endLine":9},"Value":"var"}}}]}}]}]}]}
}

View File

@ -354,6 +354,31 @@ func ExamplePrettyJsonDumper() {
// },
// "meta": [
// {
// "type": "*meta.WhiteSpaceType",
// "value": "\n\t\t\t\t\t",
// "tokenName": "NodeStart"
// },
// {
// "type": "*meta.CommentType",
// "value": "// some comment\n",
// "tokenName": "NodeStart"
// },
// {
// "type": "*meta.WhiteSpaceType",
// "value": "\t\t\t\t\t",
// "tokenName": "NodeStart"
// },
// {
// "type": "*meta.CommentType",
// "value": "// second comment\n",
// "tokenName": "NodeStart"
// },
// {
// "type": "*meta.WhiteSpaceType",
// "value": "\t\t\t\t\t",
// "tokenName": "NodeStart"
// },
// {
// "type": "*meta.TokenType",
// "value": ";",
// "tokenName": "SemiColonToken"
@ -369,31 +394,6 @@ func ExamplePrettyJsonDumper() {
// },
// "meta": [
// {
// "type": "*meta.WhiteSpaceType",
// "value": "\n\t\t\t\t\t",
// "tokenName": "NodeStart"
// },
// {
// "type": "*meta.CommentType",
// "value": "// some comment\n",
// "tokenName": "NodeStart"
// },
// {
// "type": "*meta.WhiteSpaceType",
// "value": "\t\t\t\t\t",
// "tokenName": "NodeStart"
// },
// {
// "type": "*meta.CommentType",
// "value": "// second comment\n",
// "tokenName": "NodeStart"
// },
// {
// "type": "*meta.WhiteSpaceType",
// "value": "\t\t\t\t\t",
// "tokenName": "NodeStart"
// },
// {
// "type": "*meta.TokenType",
// "value": "$",
// "tokenName": "NodeStart"