template/token
src/template/token.tur
defn
lex
(lex [src : cstr] :)
tokenize a template source string.
Parameters
| src | NUL-terminated template text |
Returns
A handle to the first token cell, or 0 if the template is empty. Walk the chain via token-next; free with tokens-free.
Example
(let [t (lex "Hi <%= name %>!")] (token-tag t)) ; => 0 (TextChunk)
Since: 0.1.0
defn
token-tag
(token-tag [t : int] :)
read the tag of a token.
Parameters
| t | token cell handle |
Returns
0 = TextChunk, 1 = CodeBlock, 2 = ExprBlock, 3 = Comment, -1 if t is 0.
Example
(token-tag (lex "<%= x %>")) ; => 2 (ExprBlock first token? no, here it's the only token)
Since: 0.1.0
defn
token-text
(token-text [t : int] :)
read the NUL-terminated payload of a token.
Parameters
| t | token cell handle |
Returns
The token's text content (interior of <% %> for tag tokens, verbatim slice for TextChunk). The empty string if t is 0.
Example
(token-text (lex "<%= x %>")) ; => " x "
Since: 0.1.0
defn
token-len
(token-len [t : int] :)
payload length in bytes.
Parameters
| t | token cell handle |
Returns
Byte length of the token text, 0 if t is 0.
Example
(token-len (lex "hi")) ; => 2
Since: 0.1.0
defn
token-next
(token-next [t : int] :)
read the next token in the chain.
Parameters
| t | token cell handle |
Returns
The next token handle, or 0 if this was the last cell.
Example
(token-next first-tok)
Since: 0.1.0
defn
tokens-free
(tokens-free [t : int] :)
release every cell in a token chain.
Parameters
| t | head token cell handle (may be 0) |
Example
(tokens-free (lex "hello"))
Since: 0.1.0