glimr/loom/parser
Template Parser
Converts a stream of lexer tokens into an abstract syntax tree. Handles nested structures like conditionals, loops, components, and slot definitions.
Types
Represents a node in the template AST. Each variant maps to a different template construct like text, variables, control flow, or components.
pub type Node {
TextNode(String)
VariableNode(name: String)
RawVariableNode(name: String)
SlotNode(name: option.Option(String), fallback: List(Node))
SlotDefNode(name: option.Option(String), children: List(Node))
AttributesNode(base_attributes: List(lexer.ComponentAttr))
IfNode(branches: List(#(option.Option(String), List(Node))))
EachNode(
collection: String,
items: List(String),
loop_var: option.Option(String),
body: List(Node),
)
ComponentNode(
name: String,
attributes: List(lexer.ComponentAttr),
children: List(Node),
)
ElementNode(
tag: String,
attributes: List(lexer.ComponentAttr),
children: List(Node),
)
}
Constructors
-
TextNode(String) -
VariableNode(name: String) -
RawVariableNode(name: String) -
SlotNode(name: option.Option(String), fallback: List(Node))SlotNode outputs slot content with optional fallback. Used in component templates:
, fallback -
SlotDefNode(name: option.Option(String), children: List(Node))SlotDefNode defines content to pass to a slot when using a component. Used inside
: content -
AttributesNode(base_attributes: List(lexer.ComponentAttr))AttributesNode holds optional base attributes that will be merged with props.attributes
-
IfNode(branches: List(#(option.Option(String), List(Node)))) -
EachNode( collection: String, items: List(String), loop_var: option.Option(String), body: List(Node), ) -
ComponentNode( name: String, attributes: List(lexer.ComponentAttr), children: List(Node), ) -
ElementNode( tag: String, attributes: List(lexer.ComponentAttr), children: List(Node), )
Errors that can occur during template parsing. Includes unexpected closing tags, unclosed blocks, and invalid token sequences.
pub type ParserError {
UnexpectedLmElse
UnexpectedLmElseIf
LmElseAfterLmElse
LmElseIfAfterLmElse
UnexpectedComponentEnd(name: String)
UnexpectedElementEnd(tag: String)
UnexpectedSlotDefEnd
UnclosedComponent(name: String)
UnclosedElement(tag: String)
UnclosedSlot(name: option.Option(String))
UnexpectedToken(token: lexer.Token)
}
Constructors
-
UnexpectedLmElse -
UnexpectedLmElseIf -
LmElseAfterLmElse -
LmElseIfAfterLmElse -
UnexpectedComponentEnd(name: String) -
UnexpectedElementEnd(tag: String) -
UnexpectedSlotDefEnd -
UnclosedComponent(name: String) -
UnclosedElement(tag: String) -
UnclosedSlot(name: option.Option(String)) -
UnexpectedToken(token: lexer.Token)
Values
pub fn parse(
tokens: List(lexer.Token),
) -> Result(Template, ParserError)
Parses a list of tokens into a Template AST. Recursively parses all nodes including nested structures like conditionals, loops, and component hierarchies.