Documentation
¶
Index ¶
- type Node
- func (n *Node) Add(t int, v string) *Node
- func (n *Node) Append(u *Node)
- func (n *Node) Copy() *Node
- func (n *Node) Cut() *Node
- func (n *Node) Init()
- func (s Node) Log()
- func (s Node) MarshalJSON() ([]byte, error)
- func (n *Node) Morph(c *Node)
- func (n *Node) Nodes() []*Node
- func (s Node) Print()
- func (s Node) Println()
- func (n *Node) Refs() string
- func (s Node) String() string
- func (n *Node) Take(from *Node)
- func (n *Node) WalkDeepPre(do func(n *Node))
- func (n *Node) WalkLevels(do func(n *Node))
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Node ¶
type Node struct {
T int `json:"T"` // type
V string `json:",omitempty"` // value
P *Node `json:"-"` // up/parent
Count int `json:"-"` // node count
// contains filtered or unexported fields
}
Node is an implementation of a "node" from traditional rooted node tree data structure. A Node may have other nodes under then (which some call a "parent" (P)) or can be an "edge" or "leaf" with a value instead. While there is nothing preventing a Node from having both a value and other nodes under it, such use is unsupported by the MarshalJSON/UnmarshalJSON methods. All nodes have a specific integer type (T).
Example (Properties) ¶
package main
import (
"fmt"
"github.com/rwxrob/pegn/ast"
)
func main() {
// Nodes have these properties updating every time
// their state is changed so that queries need not
// do the checks again later.
// initial state
n := new(ast.Node)
fmt.Printf("n: %v %q %v\n", n.P == nil, n.V, n.Count)
u := n.Add(1, "")
fmt.Printf("n: %v %q %v\n", n.P == nil, n.V, n.Count)
fmt.Printf("u: %v %q %v\n", u.P == nil, u.V, u.Count)
// make an edge node
u.V = "something"
// break edge by forcing it to have nodes and a value (discouraged)
u.Add(9001, "muhaha")
fmt.Printf("u: %v %q %v\n", u.P == nil, u.V, u.Count)
}
Output: n: true "" 0 n: true "" 1 u: false "" 0 u: false "something" 1
func (*Node) Add ¶
Add creates a new Node with type and value under and returns. It also updates Count.
func (*Node) Copy ¶
Copy returns a duplicate of the Node and all its relations. Values are copied using simple assignment. Copy is useful for preserving state in order to revert a Node or to allow independent processing with concurrency on individual copies. Note that Node[<ref>] types will not produce deep copies of values.
Example ¶
package main
import (
"fmt"
"github.com/rwxrob/pegn/ast"
)
func main() {
n := new(ast.Node)
n.Add(2, "some")
c := n.Copy()
c.Add(3, "new").Add(4, "deep")
// log.Println("Original -------------------------")
// n.LogRefs()
// log.Println("Copy -----------------------------")
// c.LogRefs()
fmt.Println(&n != &c)
n.Println()
c.Println()
}
Output: true {"T":0,"N":[{"T":2,"V":"some"}]} {"T":0,"N":[{"T":2,"V":"some"},{"T":3,"V":"new","N":[{"T":4,"V":"deep"}]}]}
func (*Node) Cut ¶
Cut removes a Node from under the one above it and returns.
Example (First) ¶
package main
import (
"github.com/rwxrob/pegn/ast"
)
func main() {
n := new(ast.Node)
c := n.Add(1, "")
n.Add(2, "")
n.Add(3, "")
n.Println()
x := c.Cut()
n.Println()
x.Println()
}
Output: {"T":0,"N":[{"T":1},{"T":2},{"T":3}]} {"T":0,"N":[{"T":2},{"T":3}]} {"T":1}
Example (Last) ¶
package main
import (
"github.com/rwxrob/pegn/ast"
)
func main() {
n := new(ast.Node)
n.Add(1, "")
n.Add(2, "")
c := n.Add(3, "")
n.Println()
x := c.Cut()
n.Println()
x.Println()
}
Output: {"T":0,"N":[{"T":1},{"T":2},{"T":3}]} {"T":0,"N":[{"T":1},{"T":2}]} {"T":3}
Example (Middle) ¶
package main
import (
"fmt"
"github.com/rwxrob/pegn/ast"
)
func main() {
n := new(ast.Node)
n.Add(1, "")
c := n.Add(2, "")
n.Add(3, "")
n.Println()
fmt.Println(n.Count)
x := c.Cut()
n.Println()
fmt.Println(n.Count)
x.Println()
}
Output: {"T":0,"N":[{"T":1},{"T":2},{"T":3}]} 3 {"T":0,"N":[{"T":1},{"T":3}]} 2 {"T":2}
func (*Node) Init ¶
func (n *Node) Init()
Init resets the node to its empty/zero state as if just created for the first time.
Example ¶
package main
import (
"github.com/rwxrob/pegn/ast"
)
func main() {
// create and print a brand new one
n := new(ast.Node)
n.Println()
// add something to it
n.V = `something`
n.Println()
// initialize it back to empty zero value
n.Init()
n.Println()
}
Output: {"T":0} {"T":0,"V":"something"} {"T":0}
func (Node) MarshalJSON ¶
MarshalJSON fulfills the json.Marshaler interface by first creating a copy of itself with the Nodes has N and then marshaling with an encoder that has had SetEscapeHTML set to false trims the extraneous newline added by json.Encoder.Encode. See String, Log, and Print as well.
func (*Node) Morph ¶
Morph initializes the node with Init and then sets it's value (V) and type (T) and all of its attachment references to those of the Node passed thereby preserving the Node reference of this method's receiver.
Example ¶
package main
import (
"github.com/rwxrob/pegn/ast"
)
func main() {
n := new(ast.Node)
n.Add(2, "some")
m := new(ast.Node)
m.Morph(n)
n.Println()
m.Println()
}
Output: {"T":0,"N":[{"T":2,"V":"some"}]} {"T":0,"N":[{"T":2,"V":"some"}]}
func (*Node) Nodes ¶
Nodes returns all the nodes under this Node. Prefer checking Count when the values are not needed.
Example ¶
package main
import (
"fmt"
"github.com/rwxrob/pegn/ast"
)
func main() {
n := new(ast.Node)
n.Add(1, "")
n.Add(2, "")
fmt.Println(n.Nodes(), n.Count)
// and another added under it
m := n.Add(3, "")
m.Add(3, "")
m.Add(3, "")
fmt.Println(m.Nodes(), m.Count)
fmt.Println(n.Nodes(), n.Count)
}
Output: [{"T":1} {"T":2}] 2 [{"T":3} {"T":3}] 2 [{"T":1} {"T":2} {"T":3,"N":[{"T":3},{"T":3}]}] 3
func (*Node) Refs ¶
Refs returns the internal pointers as a string for visualization mostly during debugging. See LogRefs.
func (Node) String ¶
String returns the MarshalJSON version or the string "null" if an error occurred. An error is also logged with log.Print. No additional line return is added.
Example ¶
package main
import (
"github.com/rwxrob/pegn/ast"
)
func main() {
t := new(ast.Node)
t.V = `<foo>` // <> not be escaped by encoding/json
t.Print()
}
Output: {"T":0,"V":"<foo>"}
func (*Node) Take ¶
Take moves all nodes from another under itself.
Example ¶
package main
import (
"fmt"
"github.com/rwxrob/pegn/ast"
)
func main() {
// build up the first
n := new(ast.Node)
n.T = 10
n.Add(1, "")
n.Add(2, "")
n.Add(3, "")
n.Println()
fmt.Println(n.Count)
// now take them over
m := new(ast.Node)
m.T = 20
m.Println()
fmt.Println(m.Count)
m.Take(n)
m.Println()
fmt.Println(m.Count)
n.Println()
fmt.Println(n.Count)
}
Output: {"T":10,"N":[{"T":1},{"T":2},{"T":3}]} 3 {"T":20} 0 {"T":20,"N":[{"T":1},{"T":2},{"T":3}]} 3 {"T":10} 0
func (*Node) WalkDeepPre ¶
WalkDeepPre will pass each Node in the tree to the given function traversing in a synchronous, depth-first, preorder way. The function passed may be a closure containing variables, contexts, or a channel outside of its own scope to be updated for each visit. This method uses functional recursion which may have some limitations depending on the depth of node trees required.
Example ¶
package main
import (
"fmt"
"github.com/rwxrob/pegn/ast"
)
func main() {
n := new(ast.Node)
n.Add(1, "").Add(11, "")
n.Add(2, "").Add(22, "")
n.Add(3, "").Add(33, "")
n.WalkDeepPre(func(c *ast.Node) { fmt.Print(c.T, " ") })
}
Output: 0 1 11 2 22 3 33
func (*Node) WalkLevels ¶
WalkLevels will pass each Node in the tree to the given function traversing in a synchronous, breadth-first, leveler way. The function passed may be a closure containing variables, contexts, or a channel outside of its own scope to be updated for each visit. This method uses functional recursion which may have some limitations depending on the depth of node trees required.
Example ¶
package main
import (
"fmt"
"github.com/rwxrob/pegn/ast"
)
func main() {
n := new(ast.Node)
n.Add(1, "").Add(11, "")
n.Add(2, "").Add(22, "")
n.Add(3, "").Add(33, "")
n.WalkLevels(func(c *ast.Node) { fmt.Print(c.T, " ") })
}
Output: 0 1 2 3 11 22 33