Documentation ¶
Overview ¶
Package astutil implements methods to walk and dump a tree.
Index ¶
- func CloneExpression(expr ast.Expression) ast.Expression
- func CloneNode(node ast.Node) ast.Node
- func ClonePosition(pos *ast.Position) *ast.Position
- func CloneTree(tree *ast.Tree) *ast.Tree
- func Dump(w io.Writer, node ast.Node) (err error)
- func Inspect(node ast.Node, f func(ast.Node) bool)
- func Walk(v Visitor, node ast.Node)
- type Visitor
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloneExpression ¶
func CloneExpression(expr ast.Expression) ast.Expression
CloneExpression returns a complete copy of expression expr.
func ClonePosition ¶
ClonePosition returns a copy of position pos.
func Dump ¶
Dump writes the tree dump on w. In the case where the tree is nil, the function stops execution by returning an error other than nil. In case the tree is not extended, the function ends its execution after writing the base tree, returning a non-nil error.
Example ¶
package main import ( "bytes" "fmt" "github.com/open2b/scriggo/ast" "github.com/open2b/scriggo/ast/astutil" "github.com/open2b/scriggo/internal/compiler" ) func main() { cases := []string{ "{{ (4 + 5) * value() }}", "{% var x = 10 %}", "{% if true %} some text, blah blah {% end %}", "{% for i in x %} some text, blah blah blah {% end %}", "{% for i in x %} some very very very very very very very very long text, blah blah blah {% end %}", `{{ render "ciao.txt" }}`, "{{5+6}}", `{% var x = 10 %}`, `{% y = 10 %}`, `{% y = (4 + 5) %}`, } for _, c := range cases { tree, _, err := compiler.ParseTemplateSource([]byte(c), ast.FormatHTML, false, false) if err != nil { panic(err) } var buf bytes.Buffer err = astutil.Dump(&buf, tree) if err != nil { panic(err) } got := buf.String() fmt.Print(got) } }
Output: Tree: "":1:1 │ Show (1:1) show (4 + 5) * value() │ │ BinaryOperator (1:12) (4 + 5) * value() │ │ │ BinaryOperator (1:7) 4 + 5 │ │ │ │ BasicLiteral (1:5) 4 │ │ │ │ BasicLiteral (1:9) 5 │ │ │ Call (1:19) value() Tree: "":1:1 │ Var (1:4) var x = 10 │ │ Identifier (1:8) x │ │ BasicLiteral (1:12) 10 Tree: "":1:1 │ If (1:4) true │ │ Identifier (1:7) true │ │ Block (-) block statement │ │ │ Text (1:14) " some text, blah blah " Tree: "":1:1 │ ForIn (1:4) 1:4 │ │ Identifier (1:8) i │ │ Identifier (1:13) x │ │ Text (1:17) " some text, blah blah blah " Tree: "":1:1 │ ForIn (1:4) 1:4 │ │ Identifier (1:8) i │ │ Identifier (1:13) x │ │ Text (1:17) " some very very very very very..." Tree: "":1:1 │ Show (1:1) show render "ciao.txt" │ │ Render (1:4) render "ciao.txt" Tree: "":1:1 │ Show (1:1) show 5 + 6 │ │ BinaryOperator (1:4) 5 + 6 │ │ │ BasicLiteral (1:3) 5 │ │ │ BasicLiteral (1:5) 6 Tree: "":1:1 │ Var (1:4) var x = 10 │ │ Identifier (1:8) x │ │ BasicLiteral (1:12) 10 Tree: "":1:1 │ Assignment (1:4) y = 10 │ │ Identifier (1:4) y │ │ BasicLiteral (1:8) 10 Tree: "":1:1 │ Assignment (1:4) y = 4 + 5 │ │ Identifier (1:4) y │ │ BinaryOperator (1:11) 4 + 5 │ │ │ BasicLiteral (1:9) 4 │ │ │ BasicLiteral (1:13) 5
func Inspect ¶
Inspect visits the tree by calling the function f on every node. For more information, see the documentation of the Walk function.
Types ¶
Click to show internal directories.
Click to hide internal directories.