template/parse
parse
(parse [tokens : int] :)
consume a token chain and return the root node.
| tokens | token chain handle returned by `lex` |
A handle to the first node, or 0 if the template was empty. Walk the chain via node-next; free with nodes-free.
(let [tk (lex "<%= name %>") nd (parse tk)] (node-tag nd)) ; => 1
Since: 0.1.0
node-tag
(node-tag [n : int] :)
read a node's tag.
| n | node handle |
0 = Literal, 1 = Emit, 2 = IfNode, 3 = ForNode, 4 = LetNode, -1 if n is 0.
(node-tag (parse (lex "<%= x %>"))) ; => 1
Since: 0.1.0
node-name
(node-name [n : int] :)
read a node's name slot.
| n | node handle |
The variable name for Emit/If/For/Let nodes; "" for Literal or null nodes.
(node-name (parse (lex "<%= title %>"))) ; => "title"
Since: 0.1.0
node-value
(node-value [n : int] :)
read a node's value slot.
| n | node handle |
For Literal nodes: the emitted text. For ForNode: the collection variable name. For LetNode: the literal value, or "@key" for an env-ref binding. "" for Emit/If or null nodes.
(node-value (parse (lex "hello"))) ; => "hello"
Since: 0.1.0
node-then
(node-then [n : int] :)
read the then-body of an If/For/Let node.
| n | node handle |
The first child node of the then-branch, or 0 if empty/not applicable.
(node-then (parse (lex "<% if x %>y<% end %>"))) ; => Literal "y"
Since: 0.1.0
node-else
(node-else [n : int] :)
read the else-body of an If node.
| n | node handle |
The first child node of the else-branch, or 0 if no else clause.
(node-else (parse (lex "<% if x %>a<% else %>b<% end %>"))) ; => Literal "b"
Since: 0.1.0
node-next
(node-next [n : int] :)
read the next sibling node in the chain.
| n | node handle |
The next sibling node, or 0 if this was the last in its body.
(node-next first-node)
Since: 0.1.0
nodes-free
(nodes-free [n : int] :)
recursively release a node chain and its children.
| n | node chain head (may be 0) |
(nodes-free (parse (lex "<%= x %>")))
Since: 0.1.0