#33 comment package has renamed to meta and parser now saves whitespaces

This commit is contained in:
z7zmey
2018-06-30 00:51:11 +03:00
parent 36d0cf4823
commit e90df8ef5f
205 changed files with 16485 additions and 16302 deletions

View File

@@ -7,9 +7,8 @@ import (
"reflect"
"strings"
"github.com/z7zmey/php-parser/comment"
"github.com/z7zmey/php-parser/meta"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/parser"
"github.com/z7zmey/php-parser/walker"
)
@@ -19,7 +18,6 @@ import (
type Dumper struct {
Writer io.Writer
Indent string
Comments parser.Comments
NsResolver *NamespaceResolver
}
@@ -39,10 +37,10 @@ func (d *Dumper) EnterNode(w walker.Walkable) bool {
}
}
if c := n.GetComments(); len(c) > 0 {
fmt.Fprintf(d.Writer, "%v\"Comments\":\n", d.Indent+" ")
for _, cc := range c {
fmt.Fprintf(d.Writer, "%v%q before %q\n", d.Indent+" ", cc, comment.TokenNames[cc.TokenName])
if mm := n.GetMeta(); len(mm) > 0 {
fmt.Fprintf(d.Writer, "%v\"Meta\":\n", d.Indent+" ")
for _, m := range mm {
fmt.Fprintf(d.Writer, "%v%q before %q\n", d.Indent+" ", m, meta.TokenNames[m.GetTokenName()])
}
}

View File

@@ -1,4 +1,3 @@
// Package visitor contains walker.visitor implementations
package visitor_test
import (
@@ -23,6 +22,7 @@ func ExampleDumper() {
}`
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.WithMeta()
php7parser.Parse()
nodes := php7parser.GetRootNode()
@@ -32,7 +32,6 @@ func ExampleDumper() {
dumper := &visitor.Dumper{
Writer: os.Stdout,
Indent: "| ",
Comments: php7parser.GetComments(),
NsResolver: nsResolver,
}
nodes.Walk(dumper)
@@ -43,38 +42,58 @@ func ExampleDumper() {
// | "Stmts":
// | [*stmt.Namespace]
// | "Position": Pos{Line: 3-11 Pos: 10-143}
// | "Meta":
// | "\n\n\t\t" before "NamespaceToken"
// | " " before "OpenCurlyBracesToken"
// | "\n\t\t" before "CloseCurlyBracesToken"
// | "NamespaceName":
// | [*name.Name]
// | "Position": Pos{Line: 3-3 Pos: 20-22}
// | "Parts":
// | [*name.NamePart]
// | "Position": Pos{Line: 3-3 Pos: 20-22}
// | "Meta":
// | " " before "StringToken"
// | "Value": "Foo"
// | "Stmts":
// | [*stmt.Class]
// | "Position": Pos{Line: 4-10 Pos: 29-139}
// | "NamespacedName": "Foo\\Bar"
// | "Meta":
// | "\n\t\t\t" before "ClassToken"
// | " " before "OpenCurlyBracesToken"
// | "\n\t\t\t" before "CloseCurlyBracesToken"
// | "PhpDocComment": ""
// | "ClassName":
// | [*node.Identifier]
// | "Position": Pos{Line: 4-4 Pos: 35-37}
// | "Meta":
// | " " before "StringToken"
// | "Value": "Bar"
// | "Stmts":
// | [*stmt.ClassMethod]
// | "Position": Pos{Line: 5-9 Pos: 45-134}
// | "Meta":
// | " " before "FunctionToken"
// | "ReturnsRef": false
// | "PhpDocComment": ""
// | "MethodName":
// | [*node.Identifier]
// | "Position": Pos{Line: 5-5 Pos: 61-72}
// | "Meta":
// | " " before "IdentifierToken"
// | "Value": "FunctionName"
// | "Modifiers":
// | [*node.Identifier]
// | "Position": Pos{Line: 5-5 Pos: 45-50}
// | "Meta":
// | "\n\t\t\t\t" before "PublicToken"
// | "Value": "public"
// | "Params":
// | [*node.Parameter]
// | "Position": Pos{Line: 5-5 Pos: 74-89}
// | "Meta":
// | " " before "EqualToken"
// | "Variadic": false
// | "ByRef": false
// | "VariableType":
@@ -88,6 +107,8 @@ func ExampleDumper() {
// | "Variable":
// | [*expr.Variable]
// | "Position": Pos{Line: 5-5 Pos: 79-82}
// | "Meta":
// | " " before "VariableToken"
// | "VarName":
// | [*node.Identifier]
// | "Position": Pos{Line: 5-5 Pos: 79-82}
@@ -102,18 +123,25 @@ func ExampleDumper() {
// | "Parts":
// | [*name.NamePart]
// | "Position": Pos{Line: 5-5 Pos: 86-89}
// | "Meta":
// | " " before "StringToken"
// | "Value": "null"
// | "Stmt":
// | [*stmt.StmtList]
// | "Position": Pos{Line: 6-9 Pos: 96-134}
// | "Meta":
// | "\n\t\t\t\t" before "OpenCurlyBracesToken"
// | "\n\t\t\t\t" before "CloseCurlyBracesToken"
// | "Stmts":
// | [*stmt.Expression]
// | "Position": Pos{Line: 8-8 Pos: 124-128}
// | "Expr":
// | [*expr.Variable]
// | "Position": Pos{Line: 8-8 Pos: 124-127}
// | "Comments":
// | "Meta":
// | "\n\t\t\t\t\t" before "VariableToken"
// | "// some comment\n" before "VariableToken"
// | "\n\t\t\t\t\t" before "VariableToken"
// | "VarName":
// | [*node.Identifier]
// | "Position": Pos{Line: 8-8 Pos: 124-127}

View File

@@ -7,6 +7,8 @@ import (
"reflect"
"strings"
"github.com/z7zmey/php-parser/meta"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/walker"
@@ -59,34 +61,39 @@ func (d *GoDumper) EnterNode(w walker.Walkable) bool {
fmt.Fprint(d.Writer, "},\n")
}
if cc := n.GetComments(); len(cc) > 0 {
if mm := n.GetMeta(); len(mm) > 0 {
printIndent(d.Writer, d.depth)
fmt.Fprint(d.Writer, "Comments: []*comment.Comment{\n")
fmt.Fprint(d.Writer, "Meta: []meta.Meta{\n")
d.depth++
for _, c := range cc {
for _, m := range mm {
printIndent(d.Writer, d.depth)
fmt.Fprint(d.Writer, "&comment.Comment{\n")
switch m.(type) {
case *meta.Comment:
fmt.Fprint(d.Writer, "&meta.Comment{\n")
case *meta.WhiteSpace:
fmt.Fprint(d.Writer, "&meta.WhiteSpace{\n")
}
d.depth++
printIndent(d.Writer, d.depth)
fmt.Fprint(d.Writer, "Position: &position.Position{\n")
d.depth++
printIndent(d.Writer, d.depth)
fmt.Fprintf(d.Writer, "StartLine: %d,\n", c.Position.StartLine)
fmt.Fprintf(d.Writer, "StartLine: %d,\n", m.GetPosition().StartLine)
printIndent(d.Writer, d.depth)
fmt.Fprintf(d.Writer, "EndLine: %d,\n", c.Position.EndLine)
fmt.Fprintf(d.Writer, "EndLine: %d,\n", m.GetPosition().EndLine)
printIndent(d.Writer, d.depth)
fmt.Fprintf(d.Writer, "StartPos: %d,\n", c.Position.StartPos)
fmt.Fprintf(d.Writer, "StartPos: %d,\n", m.GetPosition().StartPos)
printIndent(d.Writer, d.depth)
fmt.Fprintf(d.Writer, "EndPos: %d,\n", c.Position.EndPos)
fmt.Fprintf(d.Writer, "EndPos: %d,\n", m.GetPosition().EndPos)
d.depth--
printIndent(d.Writer, d.depth)
fmt.Fprint(d.Writer, "},\n")
printIndent(d.Writer, d.depth)
fmt.Fprintf(d.Writer, "Value: %q,\n", c.Value)
fmt.Fprintf(d.Writer, "Value: %q,\n", m.String())
printIndent(d.Writer, d.depth)
fmt.Fprintf(d.Writer, "TokenName: %q,\n", c.TokenName)
fmt.Fprintf(d.Writer, "TokenName: %d,\n", m.GetTokenName())
d.depth--
printIndent(d.Writer, d.depth)

View File

@@ -1,4 +1,3 @@
//Package visitor contains walker.visitor implementations
package visitor_test
import (
@@ -23,6 +22,7 @@ func ExampleGoDumper() {
}`
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.WithMeta()
php7parser.Parse()
nodes := php7parser.GetRootNode()
@@ -50,6 +50,38 @@ func ExampleGoDumper() {
// StartPos: 10,
// EndPos: 142,
// },
// Meta: []meta.Meta{
// &meta.WhiteSpace{
// Position: &position.Position{
// StartLine: 1,
// EndLine: 3,
// StartPos: 6,
// EndPos: 9,
// },
// Value: "\n\n\t\t",
// TokenName: 67,
// },
// &meta.WhiteSpace{
// Position: &position.Position{
// StartLine: 3,
// EndLine: 3,
// StartPos: 23,
// EndPos: 23,
// },
// Value: " ",
// TokenName: 133,
// },
// &meta.WhiteSpace{
// Position: &position.Position{
// StartLine: 10,
// EndLine: 11,
// StartPos: 139,
// EndPos: 141,
// },
// Value: "\n\t\t",
// TokenName: 134,
// },
// },
// NamespaceName: &name.Name{
// Position: &position.Position{
// StartLine: 3,
@@ -65,6 +97,18 @@ func ExampleGoDumper() {
// StartPos: 20,
// EndPos: 22,
// },
// Meta: []meta.Meta{
// &meta.WhiteSpace{
// Position: &position.Position{
// StartLine: 3,
// EndLine: 3,
// StartPos: 19,
// EndPos: 19,
// },
// Value: " ",
// TokenName: 7,
// },
// },
// Value: "Foo",
// },
// },
@@ -77,6 +121,38 @@ func ExampleGoDumper() {
// StartPos: 29,
// EndPos: 138,
// },
// Meta: []meta.Meta{
// &meta.WhiteSpace{
// Position: &position.Position{
// StartLine: 3,
// EndLine: 4,
// StartPos: 25,
// EndPos: 28,
// },
// Value: "\n\t\t\t",
// TokenName: 48,
// },
// &meta.WhiteSpace{
// Position: &position.Position{
// StartLine: 4,
// EndLine: 4,
// StartPos: 38,
// EndPos: 38,
// },
// Value: " ",
// TokenName: 133,
// },
// &meta.WhiteSpace{
// Position: &position.Position{
// StartLine: 9,
// EndLine: 10,
// StartPos: 134,
// EndPos: 137,
// },
// Value: "\n\t\t\t",
// TokenName: 134,
// },
// },
// PhpDocComment: "",
// ClassName: &node.Identifier{
// Position: &position.Position{
@@ -85,6 +161,18 @@ func ExampleGoDumper() {
// StartPos: 35,
// EndPos: 37,
// },
// Meta: []meta.Meta{
// &meta.WhiteSpace{
// Position: &position.Position{
// StartLine: 4,
// EndLine: 4,
// StartPos: 34,
// EndPos: 34,
// },
// Value: " ",
// TokenName: 7,
// },
// },
// Value: "Bar",
// },
// Stmts: []node.Node{
@@ -95,6 +183,18 @@ func ExampleGoDumper() {
// StartPos: 45,
// EndPos: 133,
// },
// Meta: []meta.Meta{
// &meta.WhiteSpace{
// Position: &position.Position{
// StartLine: 5,
// EndLine: 5,
// StartPos: 51,
// EndPos: 51,
// },
// Value: " ",
// TokenName: 34,
// },
// },
// ReturnsRef: false,
// PhpDocComment: "",
// MethodName: &node.Identifier{
@@ -104,6 +204,18 @@ func ExampleGoDumper() {
// StartPos: 61,
// EndPos: 72,
// },
// Meta: []meta.Meta{
// &meta.WhiteSpace{
// Position: &position.Position{
// StartLine: 5,
// EndLine: 5,
// StartPos: 60,
// EndPos: 60,
// },
// Value: " ",
// TokenName: 129,
// },
// },
// Value: "FunctionName",
// },
// Modifiers: []node.Node{
@@ -114,6 +226,18 @@ func ExampleGoDumper() {
// StartPos: 45,
// EndPos: 50,
// },
// Meta: []meta.Meta{
// &meta.WhiteSpace{
// Position: &position.Position{
// StartLine: 4,
// EndLine: 5,
// StartPos: 40,
// EndPos: 44,
// },
// Value: "\n\t\t\t\t",
// TokenName: 91,
// },
// },
// Value: "public",
// },
// },
@@ -125,6 +249,18 @@ func ExampleGoDumper() {
// StartPos: 74,
// EndPos: 89,
// },
// Meta: []meta.Meta{
// &meta.WhiteSpace{
// Position: &position.Position{
// StartLine: 5,
// EndLine: 5,
// StartPos: 83,
// EndPos: 83,
// },
// Value: " ",
// TokenName: 151,
// },
// },
// Variadic: false,
// ByRef: false,
// VariableType: &name.Name{
@@ -153,6 +289,18 @@ func ExampleGoDumper() {
// StartPos: 79,
// EndPos: 82,
// },
// Meta: []meta.Meta{
// &meta.WhiteSpace{
// Position: &position.Position{
// StartLine: 5,
// EndLine: 5,
// StartPos: 78,
// EndPos: 78,
// },
// Value: " ",
// TokenName: 9,
// },
// },
// VarName: &node.Identifier{
// Position: &position.Position{
// StartLine: 5,
@@ -185,6 +333,18 @@ func ExampleGoDumper() {
// StartPos: 86,
// EndPos: 89,
// },
// Meta: []meta.Meta{
// &meta.WhiteSpace{
// Position: &position.Position{
// StartLine: 5,
// EndLine: 5,
// StartPos: 85,
// EndPos: 85,
// },
// Value: " ",
// TokenName: 7,
// },
// },
// Value: "null",
// },
// },
@@ -199,6 +359,28 @@ func ExampleGoDumper() {
// StartPos: 96,
// EndPos: 133,
// },
// Meta: []meta.Meta{
// &meta.WhiteSpace{
// Position: &position.Position{
// StartLine: 5,
// EndLine: 6,
// StartPos: 91,
// EndPos: 95,
// },
// Value: "\n\t\t\t\t",
// TokenName: 133,
// },
// &meta.WhiteSpace{
// Position: &position.Position{
// StartLine: 8,
// EndLine: 9,
// StartPos: 128,
// EndPos: 132,
// },
// Value: "\n\t\t\t\t",
// TokenName: 134,
// },
// },
// Stmts: []node.Node{
// &stmt.Expression{
// Position: &position.Position{
@@ -214,8 +396,18 @@ func ExampleGoDumper() {
// StartPos: 123,
// EndPos: 126,
// },
// Comments: []*comment.Comment{
// &comment.Comment{
// Meta: []meta.Meta{
// &meta.WhiteSpace{
// Position: &position.Position{
// StartLine: 6,
// EndLine: 7,
// StartPos: 97,
// EndPos: 102,
// },
// Value: "\n\t\t\t\t\t",
// TokenName: 9,
// },
// &meta.Comment{
// Position: &position.Position{
// StartLine: 7,
// EndLine: 7,
@@ -223,7 +415,17 @@ func ExampleGoDumper() {
// EndPos: 117,
// },
// Value: "//some comment\n",
// TokenName: '\t',
// TokenName: 9,
// },
// &meta.WhiteSpace{
// Position: &position.Position{
// StartLine: 7,
// EndLine: 8,
// StartPos: 117,
// EndPos: 122,
// },
// Value: "\n\t\t\t\t\t",
// TokenName: 9,
// },
// },
// VarName: &node.Identifier{

View File

@@ -5,16 +5,17 @@ import (
"fmt"
"io"
"reflect"
"sort"
"github.com/z7zmey/php-parser/meta"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/parser"
"github.com/z7zmey/php-parser/walker"
)
type JsonDumper struct {
Writer io.Writer
Comments parser.Comments
NsResolver *NamespaceResolver
}
@@ -42,14 +43,19 @@ func (d *JsonDumper) EnterNode(w walker.Walkable) bool {
}
}
if c := n.GetComments(); len(c) > 0 {
fmt.Fprintf(d.Writer, ",%q:[", "comments")
if mm := n.GetMeta(); len(mm) > 0 {
fmt.Fprintf(d.Writer, ",%q:[", "meta")
for k, cc := range c {
if k == 0 {
fmt.Fprintf(d.Writer, "%q", cc)
} else {
fmt.Fprintf(d.Writer, ",%q", cc)
for k, m := range mm {
if k != 0 {
fmt.Fprint(d.Writer, ",")
}
switch m.(type) {
case *meta.Comment:
fmt.Fprintf(d.Writer, "{%q:%q,%q:%q,%q:%q}", "type", "*meta.Comment", "value", m.String(), "tokenName", meta.TokenNames[m.GetTokenName()])
case *meta.WhiteSpace:
fmt.Fprintf(d.Writer, "{%q:%q,%q:%q,%q:%q}", "type", "*meta.WhiteSpace", "value", m.String(), "tokenName", meta.TokenNames[m.GetTokenName()])
}
}
@@ -57,12 +63,20 @@ func (d *JsonDumper) EnterNode(w walker.Walkable) bool {
}
if a := n.Attributes(); len(a) > 0 {
for key, attr := range a {
var attributes []string
for key := range n.Attributes() {
attributes = append(attributes, key)
}
sort.Strings(attributes)
for _, attributeName := range attributes {
attr := a[attributeName]
switch attr.(type) {
case string:
fmt.Fprintf(d.Writer, ",\"%s\":%q", key, attr)
fmt.Fprintf(d.Writer, ",\"%s\":%q", attributeName, attr)
default:
fmt.Fprintf(d.Writer, ",\"%s\":%v", key, attr)
fmt.Fprintf(d.Writer, ",\"%s\":%v", attributeName, attr)
}
}
}

View File

@@ -1,4 +1,3 @@
//Package visitor contains walker.visitor implementations
package visitor_test
import (
@@ -24,6 +23,7 @@ func ExampleJsonDumper() {
}`
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.WithMeta()
php7parser.Parse()
nodes := php7parser.GetRootNode()
@@ -32,11 +32,10 @@ func ExampleJsonDumper() {
dumper := &visitor.JsonDumper{
Writer: os.Stdout,
Comments: php7parser.GetComments(),
NsResolver: nsResolver,
}
nodes.Walk(dumper)
// Unordered 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},"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},"Value":"Foo"}]},"Stmts":[{"type":"*stmt.Class","position":{"startPos":29,"endPos":162,"startLine":4,"endLine":11},"namespacedName":"Foo\\Bar","PhpDocComment":"","ClassName":{"type":"*node.Identifier","position":{"startPos":35,"endPos":37,"startLine":4,"endLine":4},"Value":"Bar"},"Stmts":[{"type":"*stmt.ClassMethod","position":{"startPos":45,"endPos":157,"startLine":5,"endLine":10},"ReturnsRef":false,"PhpDocComment":"","MethodName":{"type":"*node.Identifier","position":{"startPos":61,"endPos":72,"startLine":5,"endLine":5},"Value":"FunctionName"},"Modifiers":[{"type":"*node.Identifier","position":{"startPos":45,"endPos":50,"startLine":5,"endLine":5},"Value":"public"}],"Params":[{"type":"*node.Parameter","position":{"startPos":74,"endPos":89,"startLine":5,"endLine":5},"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},"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","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},"Stmts":[{"type":"*stmt.Expression","position":{"startPos":147,"endPos":151,"startLine":9,"endLine":9},"Expr":{"type":"*expr.Variable","position":{"startPos":147,"endPos":150,"startLine":9,"endLine":9},"comments":["// some comment\n","// second comment\n"],"VarName":{"type":"*node.Identifier","position":{"startPos":147,"endPos":150,"startLine":9,"endLine":9},"Value":"var"}}}]}}]}]}]}
// 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.WhiteSpace","value":"\n\n\t\t","tokenName":"NamespaceToken"},{"type":"*meta.WhiteSpace","value":" ","tokenName":"OpenCurlyBracesToken"},{"type":"*meta.WhiteSpace","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.WhiteSpace","value":" ","tokenName":"StringToken"}],"Value":"Foo"}]},"Stmts":[{"type":"*stmt.Class","position":{"startPos":29,"endPos":162,"startLine":4,"endLine":11},"namespacedName":"Foo\\Bar","meta":[{"type":"*meta.WhiteSpace","value":"\n\t\t\t","tokenName":"ClassToken"},{"type":"*meta.WhiteSpace","value":" ","tokenName":"OpenCurlyBracesToken"},{"type":"*meta.WhiteSpace","value":"\n\t\t\t","tokenName":"CloseCurlyBracesToken"}],"PhpDocComment":"","ClassName":{"type":"*node.Identifier","position":{"startPos":35,"endPos":37,"startLine":4,"endLine":4},"meta":[{"type":"*meta.WhiteSpace","value":" ","tokenName":"StringToken"}],"Value":"Bar"},"Stmts":[{"type":"*stmt.ClassMethod","position":{"startPos":45,"endPos":157,"startLine":5,"endLine":10},"meta":[{"type":"*meta.WhiteSpace","value":" ","tokenName":"FunctionToken"}],"PhpDocComment":"","ReturnsRef":false,"MethodName":{"type":"*node.Identifier","position":{"startPos":61,"endPos":72,"startLine":5,"endLine":5},"meta":[{"type":"*meta.WhiteSpace","value":" ","tokenName":"IdentifierToken"}],"Value":"FunctionName"},"Modifiers":[{"type":"*node.Identifier","position":{"startPos":45,"endPos":50,"startLine":5,"endLine":5},"meta":[{"type":"*meta.WhiteSpace","value":"\n\t\t\t\t","tokenName":"PublicToken"}],"Value":"public"}],"Params":[{"type":"*node.Parameter","position":{"startPos":74,"endPos":89,"startLine":5,"endLine":5},"meta":[{"type":"*meta.WhiteSpace","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.WhiteSpace","value":" ","tokenName":"VariableToken"}],"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","Parts":[{"type":"*name.NamePart","position":{"startPos":86,"endPos":89,"startLine":5,"endLine":5},"meta":[{"type":"*meta.WhiteSpace","value":" ","tokenName":"StringToken"}],"Value":"null"}]}}}],"Stmt":{"type":"*stmt.StmtList","position":{"startPos":96,"endPos":157,"startLine":6,"endLine":10},"meta":[{"type":"*meta.WhiteSpace","value":"\n\t\t\t\t","tokenName":"OpenCurlyBracesToken"},{"type":"*meta.WhiteSpace","value":"\n\t\t\t\t","tokenName":"CloseCurlyBracesToken"}],"Stmts":[{"type":"*stmt.Expression","position":{"startPos":147,"endPos":151,"startLine":9,"endLine":9},"Expr":{"type":"*expr.Variable","position":{"startPos":147,"endPos":150,"startLine":9,"endLine":9},"meta":[{"type":"*meta.WhiteSpace","value":"\n\t\t\t\t\t","tokenName":"VariableToken"},{"type":"*meta.Comment","value":"// some comment\n","tokenName":"VariableToken"},{"type":"*meta.WhiteSpace","value":"\n\t\t\t\t\t","tokenName":"VariableToken"},{"type":"*meta.Comment","value":"// second comment\n","tokenName":"VariableToken"},{"type":"*meta.WhiteSpace","value":"\n\t\t\t\t\t","tokenName":"VariableToken"}],"VarName":{"type":"*node.Identifier","position":{"startPos":147,"endPos":150,"startLine":9,"endLine":9},"Value":"var"}}}]}}]}]}]}
}

View File

@@ -1,4 +1,3 @@
// Package visitor contains walker.visitor implementations
package visitor_test
import (

View File

@@ -6,18 +6,27 @@ import (
"io"
"reflect"
"github.com/z7zmey/php-parser/meta"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/parser"
"github.com/z7zmey/php-parser/walker"
)
type PrettyJsonDumper struct {
Writer io.Writer
Comments parser.Comments
NsResolver *NamespaceResolver
depth int
isChildNode bool
Writer io.Writer
NsResolver *NamespaceResolver
depth int
isChildNode bool
isNotFirstNode bool
}
func NewPrettyJsonDumper(Writer io.Writer, NsResolver *NamespaceResolver) *PrettyJsonDumper {
return &PrettyJsonDumper{
Writer: Writer,
NsResolver: NsResolver,
depth: 0,
isChildNode: false,
isNotFirstNode: false,
}
}
func (d *PrettyJsonDumper) printIndent(w io.Writer) {
@@ -34,8 +43,12 @@ func (d *PrettyJsonDumper) EnterNode(w walker.Walkable) bool {
if d.isChildNode {
d.isChildNode = false
} else if d.isNotFirstNode {
fmt.Fprint(d.Writer, ",\n")
d.printIndent(d.Writer)
} else {
d.printIndent(d.Writer)
d.isNotFirstNode = true
}
fmt.Fprint(d.Writer, "{\n")
@@ -69,20 +82,33 @@ func (d *PrettyJsonDumper) EnterNode(w walker.Walkable) bool {
}
}
if c := n.GetComments(); len(c) > 0 {
if mm := n.GetMeta(); len(mm) > 0 {
fmt.Fprint(d.Writer, ",\n")
d.printIndent(d.Writer)
fmt.Fprint(d.Writer, "\"comments\": [\n")
fmt.Fprint(d.Writer, "\"meta\": [\n")
d.depth++
for k, cc := range c {
if k == 0 {
d.printIndent(d.Writer)
fmt.Fprintf(d.Writer, "%q", cc)
} else {
for k, m := range mm {
if k != 0 {
fmt.Fprint(d.Writer, ",\n")
d.printIndent(d.Writer)
fmt.Fprintf(d.Writer, "%q", cc)
}
d.printIndent(d.Writer)
fmt.Fprint(d.Writer, "{\n")
d.depth++
d.printIndent(d.Writer)
switch m.(type) {
case *meta.Comment:
fmt.Fprintf(d.Writer, "%q: %q,\n", "type", "*meta.Comment")
case *meta.WhiteSpace:
fmt.Fprintf(d.Writer, "%q: %q,\n", "type", "*meta.WhiteSpace")
}
d.printIndent(d.Writer)
fmt.Fprintf(d.Writer, "%q: %q,\n", "value", m.String())
d.printIndent(d.Writer)
fmt.Fprintf(d.Writer, "%q: %q\n", "tokenName", meta.TokenNames[m.GetTokenName()])
d.depth--
d.printIndent(d.Writer)
fmt.Fprint(d.Writer, "}")
}
d.depth--
fmt.Fprint(d.Writer, "\n")
@@ -130,6 +156,8 @@ func (d *PrettyJsonDumper) EnterChildList(key string, w walker.Walkable) {
d.printIndent(d.Writer)
fmt.Fprintf(d.Writer, "%q: [\n", key)
d.depth++
d.isNotFirstNode = false
}
func (d *PrettyJsonDumper) LeaveChildList(key string, w walker.Walkable) {

View File

@@ -1,4 +1,3 @@
//Package visitor contains walker.visitor implementations
package visitor_test
import (
@@ -21,20 +20,20 @@ func ExamplePrettyJsonDumper() {
$var;
}
}
}`
function foo() {}
}
`
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.WithMeta()
php7parser.Parse()
nodes := php7parser.GetRootNode()
nsResolver := visitor.NewNamespaceResolver()
nodes.Walk(nsResolver)
dumper := &visitor.PrettyJsonDumper{
Writer: os.Stdout,
Comments: php7parser.GetComments(),
NsResolver: nsResolver,
}
dumper := visitor.NewPrettyJsonDumper(os.Stdout, nsResolver)
nodes.Walk(dumper)
// Unordered output:
@@ -42,19 +41,36 @@ func ExamplePrettyJsonDumper() {
// "type": "*node.Root",
// "position": {
// "startPos": 10,
// "endPos": 166,
// "endPos": 188,
// "startLine": 3,
// "endLine": 12
// "endLine": 14
// },
// "Stmts": [
// {
// "type": "*stmt.Namespace",
// "position": {
// "startPos": 10,
// "endPos": 166,
// "endPos": 188,
// "startLine": 3,
// "endLine": 12
// "endLine": 14
// },
// "meta": [
// {
// "type": "*meta.WhiteSpace",
// "value": "\n\n\t\t",
// "tokenName": "NamespaceToken"
// },
// {
// "type": "*meta.WhiteSpace",
// "value": " ",
// "tokenName": "OpenCurlyBracesToken"
// },
// {
// "type": "*meta.WhiteSpace",
// "value": "\n\t\t",
// "tokenName": "CloseCurlyBracesToken"
// }
// ],
// "NamespaceName": {
// "type": "*name.Name",
// "position": {
@@ -72,6 +88,13 @@ func ExamplePrettyJsonDumper() {
// "startLine": 3,
// "endLine": 3
// },
// "meta": [
// {
// "type": "*meta.WhiteSpace",
// "value": " ",
// "tokenName": "StringToken"
// }
// ],
// "Value": "Foo"
// }
// ]
@@ -86,6 +109,23 @@ func ExamplePrettyJsonDumper() {
// "endLine": 11
// },
// "namespacedName": "Foo\\Bar",
// "meta": [
// {
// "type": "*meta.WhiteSpace",
// "value": "\n\t\t\t",
// "tokenName": "ClassToken"
// },
// {
// "type": "*meta.WhiteSpace",
// "value": " ",
// "tokenName": "OpenCurlyBracesToken"
// },
// {
// "type": "*meta.WhiteSpace",
// "value": "\n\t\t\t",
// "tokenName": "CloseCurlyBracesToken"
// }
// ],
// "PhpDocComment": "",
// "ClassName": {
// "type": "*node.Identifier",
@@ -95,6 +135,13 @@ func ExamplePrettyJsonDumper() {
// "startLine": 4,
// "endLine": 4
// },
// "meta": [
// {
// "type": "*meta.WhiteSpace",
// "value": " ",
// "tokenName": "StringToken"
// }
// ],
// "Value": "Bar"
// },
// "Stmts": [
@@ -106,6 +153,13 @@ func ExamplePrettyJsonDumper() {
// "startLine": 5,
// "endLine": 10
// },
// "meta": [
// {
// "type": "*meta.WhiteSpace",
// "value": " ",
// "tokenName": "FunctionToken"
// }
// ],
// "ReturnsRef": false,
// "PhpDocComment": "",
// "MethodName": {
@@ -116,6 +170,13 @@ func ExamplePrettyJsonDumper() {
// "startLine": 5,
// "endLine": 5
// },
// "meta": [
// {
// "type": "*meta.WhiteSpace",
// "value": " ",
// "tokenName": "IdentifierToken"
// }
// ],
// "Value": "FunctionName"
// },
// "Modifiers": [
@@ -127,6 +188,13 @@ func ExamplePrettyJsonDumper() {
// "startLine": 5,
// "endLine": 5
// },
// "meta": [
// {
// "type": "*meta.WhiteSpace",
// "value": "\n\t\t\t\t",
// "tokenName": "PublicToken"
// }
// ],
// "Value": "public"
// }
// ],
@@ -139,6 +207,13 @@ func ExamplePrettyJsonDumper() {
// "startLine": 5,
// "endLine": 5
// },
// "meta": [
// {
// "type": "*meta.WhiteSpace",
// "value": " ",
// "tokenName": "EqualToken"
// }
// ],
// "ByRef": false,
// "Variadic": false,
// "VariableType": {
@@ -171,6 +246,13 @@ func ExamplePrettyJsonDumper() {
// "startLine": 5,
// "endLine": 5
// },
// "meta": [
// {
// "type": "*meta.WhiteSpace",
// "value": " ",
// "tokenName": "VariableToken"
// }
// ],
// "VarName": {
// "type": "*node.Identifier",
// "position": {
@@ -208,6 +290,13 @@ func ExamplePrettyJsonDumper() {
// "startLine": 5,
// "endLine": 5
// },
// "meta": [
// {
// "type": "*meta.WhiteSpace",
// "value": " ",
// "tokenName": "StringToken"
// }
// ],
// "Value": "null"
// }
// ]
@@ -223,6 +312,18 @@ func ExamplePrettyJsonDumper() {
// "startLine": 6,
// "endLine": 10
// },
// "meta": [
// {
// "type": "*meta.WhiteSpace",
// "value": "\n\t\t\t\t",
// "tokenName": "OpenCurlyBracesToken"
// },
// {
// "type": "*meta.WhiteSpace",
// "value": "\n\t\t\t\t",
// "tokenName": "CloseCurlyBracesToken"
// }
// ],
// "Stmts": [
// {
// "type": "*stmt.Expression",
@@ -240,9 +341,32 @@ func ExamplePrettyJsonDumper() {
// "startLine": 9,
// "endLine": 9
// },
// "comments": [
// "// some comment\n",
// "// second comment\n"
// "meta": [
// {
// "type": "*meta.WhiteSpace",
// "value": "\n\t\t\t\t\t",
// "tokenName": "VariableToken"
// },
// {
// "type": "*meta.Comment",
// "value": "// some comment\n",
// "tokenName": "VariableToken"
// },
// {
// "type": "*meta.WhiteSpace",
// "value": "\n\t\t\t\t\t",
// "tokenName": "VariableToken"
// },
// {
// "type": "*meta.Comment",
// "value": "// second comment\n",
// "tokenName": "VariableToken"
// },
// {
// "type": "*meta.WhiteSpace",
// "value": "\n\t\t\t\t\t",
// "tokenName": "VariableToken"
// }
// ],
// "VarName": {
// "type": "*node.Identifier",
@@ -260,6 +384,50 @@ func ExamplePrettyJsonDumper() {
// }
// }
// ]
// },
// {
// "type": "*stmt.Function",
// "position": {
// "startPos": 168,
// "endPos": 184,
// "startLine": 13,
// "endLine": 13
// },
// "namespacedName": "Foo\\foo",
// "meta": [
// {
// "type": "*meta.WhiteSpace",
// "value": "\n\n\t\t\t",
// "tokenName": "FunctionToken"
// },
// {
// "type": "*meta.WhiteSpace",
// "value": " ",
// "tokenName": "OpenCurlyBracesToken"
// }
// ],
// "ReturnsRef": false,
// "PhpDocComment": "",
// "FunctionName": {
// "type": "*node.Identifier",
// "position": {
// "startPos": 177,
// "endPos": 179,
// "startLine": 13,
// "endLine": 13
// },
// "meta": [
// {
// "type": "*meta.WhiteSpace",
// "value": " ",
// "tokenName": "StringToken"
// }
// ],
// "Value": "foo"
// },
// "Stmts": [
//
// ]
// }
// ]
// }