Skip to content

Commit

Permalink
add ast and parse.go stubs based on text/template parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoiron committed May 11, 2014
1 parent c461e11 commit 3d59271
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
62 changes: 62 additions & 0 deletions neo/ast.go
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()
}
15 changes: 15 additions & 0 deletions neo/parse.go
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.
}

0 comments on commit 3d59271

Please sign in to comment.