Creates a new Parser for the given template source string.
@eryx/template/parser Module
JSON
Summary
Exported Class
API Reference
Exported Class
Parser
Properties
Parser.new
Parameters
The template source to parse.
Optional file path of the template, used for resolving {% include %} paths.
Returns
A new parser instance.
Parser:cur
Parser:tryConsume
Parser:consume
Parser:skipWhitespace
Parser:pos
Returns the (line, col) of the current cursor position by scanning
from the start of the source string.
O(n) in the number of characters consumed so far. Only called when creating AST nodes or when an error is about to be raised, so the cost is negligible in practice.
Parser:consumeName
Parser:consumeExpr
Consumes an expression: either a numeric literal (integer or decimal)
or a dotted identifier path (e.g. user.profile.name), optionally
followed by one or more pipe-separated filter names.
Returns
The parsed expression node.
Parser:consumeCondition
Consumes a full condition chain used in {% if %} / {% elseif %}.
Each link is an expression optionally compared to another via a
relational operator (==, ~=, <, >, <=, >=), and links
are joined by and or or.
Returns
The parsed condition chain.
Parser:consumeStmt
Consumes a statement block opened by {%.
Dispatches to the appropriate handler based on the keyword:
if, elseif, else, for, end, macro, use, slot,
fill, or include.
Returns
The parsed statement node.
Parser:consumeComment
Consumes a comment block ({# ... #}).
Advances the cursor past the closing #} and returns nil (no AST
node is produced).
Parser:consumeUntil
Parses nodes until a node whose type is in match is encountered.
Returns the collected body nodes and the terminating node. Errors if EOF is reached before finding a match.
Parameters
An array of node type strings to stop at.
Returns
Types
TCondition
A single link in a condition chain.
lhs is always present; cond/rhs are present for comparisons;
chain links to the next condition via "and" or "or".
TemplateError
A structured error thrown by the parser and evaluator.
Contains the human-readable message and the source location
(line, col) where the error occurred, plus the optional filepath
of the template file that produced the error.
AstEof
End-of-file sentinel.
AstLiteral
Raw text between template delimiters.
AstExpression
An interpolated expression: {{ value }} or {{ value | filter }}.
value is either an array of path segments (e.g. {"user", "name"})
for dotted variable access, a number literal, or a double-quoted
string literal (with \n, \t, \r, \\, \" escapes).
Path values are arrays of segments: strings for .key access,
or { dyn: AstExpression } tables for [expr] subscript access.
AstStatementEnd
Marks the end of a control-flow block ({% end %}).
AstStatementElse
The fallback branch of an if/elseif chain ({% else %}).
AstStatementElseIf
An {% elseif cond then %} branch, with its own condition and body.
AstStatementIf
The opening {% if cond then %} of a conditional chain.
AstStatementForTable
A key/value for-in loop: {% for k, v in expr do %}.
AstStatementForNumber
A numeric for loop: {% for i = start, end do %}.
AstMacro
A macro definition: {% macro name(arg1, arg2) %} body {% end %}.
Declares a reusable template block. The macro is registered in the evaluation context and can be invoked via AstMacroCall.
AstMacroCall
A macro invocation: {% use name(expr, ...) %} body {% end %}.
Calls a previously defined macro. Positional arguments are evaluated
and bound to the macro's parameter names. The body between {% use %}
and {% end %} is rendered and made available to the macro as the
"default" slot.
AstSlot
A slot placeholder inside a macro body: {% slot name %} default {% end %}.
When the macro is called, the slot is replaced by the caller's
corresponding {% fill name %} content. If no fill is provided, the
slot's own body is rendered as a default. The name "default" is
reserved for the caller's top-level body.
AstSlotAssign
A slot fill inside a macro call: {% fill name %} content {% end %}.
Provides content for the named AstSlot in the invoked macro.
AstIncluded
An inlined template file: {% include "path" %}.
At parse time the referenced file is read and recursively parsed.
The resolved AST is stored in body. The path is resolved relative
to the directory of the file currently being parsed.
AstNode
Union of all possible AST node types the parser can produce.