Skip to content

Commit

Permalink
Rename NodeFilter into Selector and revise the node traversal API for…
Browse files Browse the repository at this point in the history
… generated wrapper classes.
  • Loading branch information
inspirer committed Feb 17, 2017
1 parent 3db50b1 commit 7d6ba83
Show file tree
Hide file tree
Showing 10 changed files with 444 additions and 368 deletions.
19 changes: 13 additions & 6 deletions tm-go/parsers/test/ast/ast.go
Expand Up @@ -4,18 +4,24 @@ package ast

import (
"github.com/inspirer/textmapper/tm-go/parsers/test"
"github.com/inspirer/textmapper/tm-go/parsers/test/filter"
"github.com/inspirer/textmapper/tm-go/parsers/test/selector"
)

type Node interface {
Type() test.NodeType
Child(filter ...filter.NodeFilter) Node
Children(filter ...filter.NodeFilter) []Node
// Child returns the first child node that matches the selector.
Child(sel selector.Selector) Node
Children(sel selector.Selector) []Node
// Next returns the first element among the following siblings that matches the selector.
Next(sel selector.Selector) Node
// NextAll returns all following siblings of the node that match the selector.
NextAll(sel selector.Selector) []Node
}

// Interfaces.

type TestNode interface {
Node
testNodeNode()
}

Expand All @@ -31,6 +37,7 @@ func (Test) testNodeNode() {}
func (Token) testNodeNode() {}

type Declaration interface {
TestNode
declarationNode()
}

Expand All @@ -48,7 +55,7 @@ type Block struct {
}

func (n Block) Declaration() []Declaration {
nodes := n.Children(filter.Declaration)
nodes := n.Children(selector.Declaration)
var result []Declaration = make([]Declaration, 0, len(nodes))
for _, node := range nodes {
result = append(result, ToTestNode(node).(Declaration))
Expand All @@ -61,7 +68,7 @@ type Decl1 struct {
}

func (n Decl1) Identifier() []Token {
nodes := n.Children(filter.Identifier)
nodes := n.Children(selector.Identifier)
var result []Token = make([]Token, 0, len(nodes))
for _, node := range nodes {
result = append(result, Token{node})
Expand All @@ -78,7 +85,7 @@ type Test struct {
}

func (n Test) Declaration() []Declaration {
nodes := n.Children(filter.Declaration)
nodes := n.Children(selector.Declaration)
var result []Declaration = make([]Declaration, 0, len(nodes))
for _, node := range nodes {
result = append(result, ToTestNode(node).(Declaration))
Expand Down
6 changes: 3 additions & 3 deletions tm-go/parsers/test/ast/node.go
Expand Up @@ -2,7 +2,7 @@ package ast

import (
"github.com/inspirer/textmapper/tm-go/parsers/test"
"github.com/inspirer/textmapper/tm-go/parsers/test/filter"
"github.com/inspirer/textmapper/tm-go/parsers/test/selector"
)

type NodeImpl struct {
Expand All @@ -17,14 +17,14 @@ func (n *NodeImpl) Type() test.NodeType {
return n.tp
}

func (n *NodeImpl) Child(t ...filter.NodeFilter) Node {
func (n *NodeImpl) Child(sel selector.Selector) Node {
//index := 0
//for _, child := range n.Children {
//
//}
return nil
}

func (n *NodeImpl) Children(t ...filter.NodeFilter) []Node {
func (n *NodeImpl) Children(sel selector.Selector) []Node {
return nil
}
@@ -1,12 +1,12 @@
// generated by Textmapper; DO NOT EDIT

package filter
package selector

import (
"github.com/inspirer/textmapper/tm-go/parsers/test"
)

type NodeFilter func(nt test.NodeType) bool
type Selector func(nt test.NodeType) bool

var (
Block = func(t test.NodeType) bool { return t == test.Block }
Expand All @@ -18,7 +18,7 @@ var (
TokenSet = OneOf(test.TokenSet...)
)

func OneOf(types ...test.NodeType) NodeFilter {
func OneOf(types ...test.NodeType) Selector {
if len(types) == 0 {
return func(test.NodeType) bool { return false }
}
Expand Down

0 comments on commit 7d6ba83

Please sign in to comment.