glimr/loom/lexer

Template Lexer

Converts template source text into a stream of tokens. Recognizes variables, directives, components, and plain text for the parser to process.

Types

Represents an attribute on a component tag. Can be a string literal, an expression to evaluate, a boolean attribute with no value, or conditional class/style expressions.

pub type ComponentAttr {
  StringAttr(name: String, value: String)
  ExprAttr(name: String, value: String)
  BoolAttr(name: String)
  ClassAttr(value: String)
  StyleAttr(value: String)
  LmIf(condition: String)
  LmElseIf(condition: String)
  LmElse
  LmFor(
    collection: String,
    items: List(String),
    loop_var: option.Option(String),
  )
}

Constructors

  • StringAttr(name: String, value: String)
  • ExprAttr(name: String, value: String)
  • BoolAttr(name: String)
  • ClassAttr(value: String)
  • StyleAttr(value: String)
  • LmIf(condition: String)
  • LmElseIf(condition: String)
  • LmElse
  • LmFor(
      collection: String,
      items: List(String),
      loop_var: option.Option(String),
    )

Errors that can occur during lexical analysis. Includes unterminated constructs, invalid names, and malformed directive syntax.

pub type LexerError {
  UnterminatedVariable(position: Int)
  InvalidVariableName(name: String, position: Int)
  UnterminatedDirective(position: Int)
  InvalidDirective(directive: String, position: Int)
  InvalidLmForSyntax(content: String, position: Int)
  UnterminatedComponent(position: Int)
  InvalidComponentSyntax(content: String, position: Int)
}

Constructors

  • UnterminatedVariable(position: Int)
  • InvalidVariableName(name: String, position: Int)
  • UnterminatedDirective(position: Int)
  • InvalidDirective(directive: String, position: Int)
  • InvalidLmForSyntax(content: String, position: Int)
  • UnterminatedComponent(position: Int)
  • InvalidComponentSyntax(content: String, position: Int)

Represents a token produced by the lexer. Each variant corresponds to a template syntax element like variables, directives, or component tags.

pub type Token {
  Text(String)
  Variable(name: String)
  RawVariable(name: String)
  Slot(name: option.Option(String))
  SlotDef(name: option.Option(String))
  SlotDefEnd
  Attributes
  Component(
    name: String,
    attributes: List(ComponentAttr),
    self_closing: Bool,
  )
  ComponentEnd(name: String)
  Element(
    tag: String,
    attributes: List(ComponentAttr),
    self_closing: Bool,
  )
  ElementEnd(tag: String)
}

Constructors

  • Text(String)
  • Variable(name: String)
  • RawVariable(name: String)
  • Slot(name: option.Option(String))
  • SlotDef(name: option.Option(String))
  • SlotDefEnd
  • Attributes
  • Component(
      name: String,
      attributes: List(ComponentAttr),
      self_closing: Bool,
    )
  • ComponentEnd(name: String)
  • Element(
      tag: String,
      attributes: List(ComponentAttr),
      self_closing: Bool,
    )
  • ElementEnd(tag: String)

Values

pub fn tokenize(input: String) -> Result(List(Token), LexerError)

Tokenizes template source into a list of tokens. Scans the input character by character, recognizing template syntax and returning errors for malformed constructs.

Search Document