astutil

package
v0.56.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 4, 2022 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package astutil implements methods to walk and dump a tree.

Index

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 CloneNode

func CloneNode(node ast.Node) ast.Node

CloneNode returns a deep copy of node.

func ClonePosition

func ClonePosition(pos *ast.Position) *ast.Position

ClonePosition returns a copy of position pos.

func CloneTree

func CloneTree(tree *ast.Tree) *ast.Tree

CloneTree returns a complete copy of tree.

func Dump

func Dump(w io.Writer, node ast.Node) (err error)

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

func Inspect(node ast.Node, f func(ast.Node) bool)

Inspect visits the tree by calling the function f on every node. For more information, see the documentation of the Walk function.

func Walk

func Walk(v Visitor, node ast.Node)

Walk visits a tree in depth. Initially it calls v.Visit (node), where node must not be nil. If the value w returned by v.Visit (node) is different from nil, Walk is called recursively using w as the Visitor on all children other than nil of the tree. Finally, call w.Visit (nil).

Types

type Visitor

type Visitor interface {
	Visit(node ast.Node) (w Visitor)
}

Visitor has a Visit method invoked for every node encountered by Walk.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL