-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ast and parse.go stubs based on text/template parsing
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package jigo | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
) | ||
|
||
type NodeType int | ||
|
||
func (t NodeType) Type() NodeType { | ||
return t | ||
} | ||
|
||
type Node interface { | ||
Type() NodeType | ||
String() string | ||
// Copy does a deep copy of the Node and all its components. | ||
Copy() Node | ||
Position() Pos // byte position of start of node in full original input string | ||
} | ||
|
||
const ( | ||
NodeList NodeType = iota | ||
) | ||
|
||
// ListNode holds a sequence of nodes. | ||
type ListNode struct { | ||
NodeType | ||
Pos | ||
Nodes []Node // The element nodes in lexical order. | ||
} | ||
|
||
func newList(pos Pos) *ListNode { | ||
return &ListNode{NodeType: NodeList, Pos: pos} | ||
} | ||
|
||
func (l *ListNode) append(n Node) { | ||
l.Nodes = append(l.Nodes, n) | ||
} | ||
|
||
func (l *ListNode) String() string { | ||
b := new(bytes.Buffer) | ||
for _, n := range l.Nodes { | ||
fmt.Fprint(b, n) | ||
} | ||
return b.String() | ||
} | ||
|
||
func (l *ListNode) CopyList() *ListNode { | ||
if l == nil { | ||
return l | ||
} | ||
n := newList(l.Pos) | ||
for _, elem := range l.Nodes { | ||
n.append(elem.Copy()) | ||
} | ||
return n | ||
} | ||
|
||
func (l *ListNode) Copy() Node { | ||
return l.CopyList() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package jigo | ||
|
||
// Tree is the representation of a single parsed template. | ||
type Tree struct { | ||
Name string // name of the template represented by the tree. | ||
ParseName string // name of the top-level template during parsing, for error messages. | ||
Root *ListNode // top-level root of the tree. | ||
text string // text parsed to create the template (or its parent) | ||
// Parsing only; cleared after parse. | ||
funcs []map[string]interface{} | ||
lex *lexer | ||
token [3]item // three-token lookahead for parser. | ||
peekCount int | ||
vars []string // variables defined at the moment. | ||
} |