Module
tuck/lexer
src/tuck/lexer.pookaReading a template is mostly waiting for a curly brace.
The lexer walks the source one character at a time. Outside tags it
collects plain text; inside {{ … }} and {% … %} it reads names,
strings, numbers and symbols. {# … #} comments disappear, and
{% raw %} … {% endraw %} becomes text exactly as written.
A - just inside a delimiter, as in {%- or -}}, trims the whitespace
on that side of the tag, so a template can stay readable without
scattering blank lines through its output.
API reference
class Token
One piece of a template.
class Text(value: String) is Token
Text outside any tag, printed as it is.
value: String
String
fn(String) -> tuck/lexer.Textclass OpenPrint(trim: Bool) is Token
{{, or {{- when trim is set.
trim: Bool
Bool
fn(Bool) -> tuck/lexer.OpenPrintclass ClosePrint(trim: Bool) is Token
}}, or -}} when trim is set.
trim: Bool
Bool
fn(Bool) -> tuck/lexer.ClosePrintclass OpenTag(trim: Bool) is Token
{%, or {%- when trim is set.
trim: Bool
Bool
fn(Bool) -> tuck/lexer.OpenTagclass CloseTag(trim: Bool) is Token
%}, or -%} when trim is set.
trim: Bool
Bool
fn(Bool) -> tuck/lexer.CloseTagclass Name(value: String) is Token
A name, such as user, if or upper.
value: String
String
fn(String) -> tuck/lexer.Nameclass Str(value: String) is Token
A quoted string, with its escapes already read.
value: String
String
fn(String) -> tuck/lexer.Strclass Integer(value: Int) is Token
A whole number.
value: Int
Int
fn(Int) -> tuck/lexer.Integerclass Decimal(value: Float) is Token
A number with a decimal point.
value: Float
Float
fn(Float) -> tuck/lexer.Decimalclass Symbol(value: String) is Token
Punctuation and operators: . , | ( ) [ ] = == != <
<= > >=.
value: String
String
fn(String) -> tuck/lexer.Symbolclass Lexeme(token: Token, at: Pos)
A token and where it starts.
token: Token
tuck/lexer.Tokenat: Pos
tuck/error.Pos
fn(tuck/lexer.Token, tuck/error.Pos) -> tuck/lexer.Lexemelex(template: String, source: String) -> Result(List(Lexeme), TuckError)
fn(String, String) -> Result(List(tuck/lexer.Lexeme), tuck/error.TuckError)Read a template into lexemes, or say where it stops making sense.
import tuck/lexer
let assert Ok(lexemes) = lexer.lex("page", "Hi {{ name }}")
assert list.length(lexemes) == 4