Sign in

    moonbitstack/moonpug/syntax does not have a README file

    Arg

    pub(all) enum Arg {
    Pos(Expr)
    Named(String, Expr)
    Spread(Expr)
    SpreadNamed(Expr)
    }

    An argument: positional, named, or spread with * and **.

    Attr

    pub(all) struct Attr {
    name : String
    value : Expr?
    escape : Bool
    }

    An attribute of an element in the indentation syntax. None is a bare attribute such as checked.

    Block

    pub(all) struct Block {
    name : String
    body : Array[Node]
    scoped : Bool
    required : Bool
    placement : Placement
    }

    Element

    pub(all) struct Element {
    tag : Expr
    attrs : Array[Attr]
    splats : Array[Expr]
    self_close : Bool
    body : Array[Node]
    }

    Expr

    pub(all) enum Expr {
    Lit(
    Value
    )
    Name(String)
    List(Array[Expr])
    Tuple(Array[Expr])
    Dict(Array[(Expr, Expr)])
    Attr(Expr, String)
    Item(Expr, Expr)
    Slice(Expr?, Expr?, Expr?)
    Call(Expr, Array[Arg])
    Not(Expr)
    Neg(Expr)
    Pos(Expr)
    Binary(Op, Expr, Expr)
    Compare(Expr, Array[(Op, Expr)])
    Concat(Array[Expr])
    Cond(Expr, Expr, Expr?)
    Filter(Expr?, String, Array[Arg])
    Test(Expr, String, Array[Arg])
    Attributes(Array[Attr], Array[Expr])
    }

    The tree both syntaxes read into. Nothing after reading knows which syntax a template was written in.

    Lang

    pub(all) struct Lang {
    name : String
    aliases : Array[String]
    options : Options
    read : (String, Reading) -> Array[Node] raise
    Refused

    }

    A language a template or a {% block lang=… %} is read in: its name, the other names it answers to, how its brace tags treat whitespace, and the reader that turns its source into the tree. The three built in are registered the same way a user's own is.

    Lexer

    pub(all) struct Lexer {
    src : String
    template : String
    pos : Int
    line : Int
    line_start : Int
    }

    A cursor over source text that knows its line and column.

    Lexer::column

    fn Lexer::column(self : Lexer) -> Int

    Lexer::fail

    fn Lexer::fail(self : Lexer, reason : String) ->
    Refused

    Lexer::new

    fn Lexer::new(src : String, template : String, pos? : Int, line? : Int, line_start? : Int) -> Lexer

    Lexer::token

    The next token inside a tag. Whitespace between tokens is skipped; Eof at the end of the text.

    Macro

    pub(all) struct Macro {
    name : String
    params : Array[Param]
    body : Array[Node]
    pug : Bool
    }

    A macro, or a mixin of the indentation syntax: a mixin also receives the call's attributes as attributes and its block as block.

    Node

    pub(all) enum Node {
    Line(Int)
    Text(String)
    Out(Expr)
    Raw(Expr)
    For(target~ : Target, iter~ : Expr, cond~ : Expr?, recursive~ : Bool, each~ : Bool, body~ : Array[Node], otherwise~ : Array[Node])
    If(Array[(Expr, Array[Node])], Array[Node])
    Set(Target, Expr)
    SetBlock(Target, Expr?, Array[Node])
    MacroDef(Macro)
    CallBlock(Array[Param], Expr, Array[Node])
    FilterBlock(Expr, Array[Node])
    Extends(Expr)
    BlockDef(Block)
    Include(Expr, missing~ : Bool, context~ : Bool, raw~ : Bool, inline~ : Bool, block~ : Array[Node])
    Yield
    Import(Expr, String, context~ : Bool)
    FromImport(Expr, Array[(String, String?)], context~ : Bool)
    With(Array[(Target, Expr)], Array[Node])
    Autoescape(Expr, Array[Node])
    Do(Expr)
    Debug
    Show(Expr, Array[Node])
    Once(Expr?, Array[Node])
    TransOut(Trans)
    Break
    Continue
    Element(Element)
    Doctype(String)
    Comment(Array[Node])
    Case(Expr, Array[(Array[Expr], Array[Node])], Array[Node])
    }

    pub(all) enum Op {
    Add
    Sub
    Mul
    Div
    FloorDiv
    Mod
    Pow
    And
    Or
    Eq
    Ne
    Lt
    Gt
    Le
    Ge
    In
    NotIn
    } derive(Eq)

    Op::equal

    fn Op::equal(Op, Op) -> Bool

    Op::not_equal

    fn Op::not_equal(x : Op, y : Op) -> Bool

    Options

    pub(all) struct Options {
    trim_blocks : Bool
    lstrip_blocks : Bool
    keep_trailing_newline : Bool
    line_statement : String?
    line_comment : String?
    }

    How the brace syntax reads whitespace and line-oriented tags.

    Param

    pub(all) struct Param {
    name : String
    default : Expr?
    rest : Bool
    }

    A macro or call-block parameter.

    Parser

    pub(all) struct Parser {
    template : String
    tokens : Array[Token]
    at : Int
    body : (Parser, Array[String]) -> Array[Node] raise
    Refused

    }

    A token stream and the way its syntax reads a statement's body. The brace syntax reads on through the same stream; the indentation syntax reads the indented lines and swaps in the tokens of the closing line.

    Parser::current

    fn Parser::current(self : Parser) -> Token

    Parser::expect

    fn Parser::expect(self : Parser, tok : Tok) -> Unit raise
    Refused

    Parser::fail

    fn Parser::fail(self : Parser, reason : String) ->
    Refused

    Parser::next

    fn Parser::next(self : Parser) -> Token

    Parser::parse_expression

    fn Parser::parse_expression(self : Parser) -> Expr raise
    Refused

    Parser::parse_filter

    fn Parser::parse_filter(self : Parser, input : Expr?) -> Expr raise
    Refused

    |name(args), chained; input is None for a filter block's head.

    Parser::parse_signature

    fn Parser::parse_signature(self : Parser) -> Array[Param] raise
    Refused

    (a, b=1, ...rest); the rest parameter is the indentation syntax's.

    Parser::parse_statement

    fn Parser::parse_statement(self : Parser) -> Array[Node] raise
    Refused

    The statements both syntaxes share. The current token is the name after {%; on return it is the end of the tag.

    Parser::parse_tuple

    fn Parser::parse_tuple(self : Parser, simplified? : Bool, condexpr? : Bool, extra? : Array[String], parenthesised? : Bool) -> Expr raise
    Refused

    An expression, or several separated by commas as a tuple.

    Parser::statements

    fn Parser::statements(self : Parser, ends : Array[String], drop? : Bool) -> Array[Node] raise
    Refused

    The body of a statement: the end of the head, then nodes up to one of ends. With drop, the end tag's name is consumed.

    Placement

    pub(all) enum Placement {
    Replace
    Append
    Prepend
    } derive(Eq)

    Placement::equal

    fn Placement::equal(Placement, Placement) -> Bool

    Placement::not_equal

    fn Placement::not_equal(x : Placement, y : Placement) -> Bool

    Reading

    pub(all) struct Reading {
    lang : Lang
    force : Bool
    template : String
    line : Int
    options : Options
    langs : (String) -> Lang?
    }

    Where a reader stands: the language and whether it is forced, the template and the line the source starts on, the brace options in effect, and the languages a block may name.

    Reading::allows

    fn Reading::allows(self : Reading, inner : Lang) -> Bool

    Whether a block in inner is read inside this stretch. A forced stretch reads its own language and, outside HTML, HTML; any other block stays text, markers and all.

    Reading::block

    fn Reading::block(self : Reading, source : String, name : String, force : Bool?, line : Int) -> Array[Node] raise
    Refused

    A block in the language named name, read into the tree; a reader of a user's own language calls this for the blocks it finds.

    Reading::inside

    fn Reading::inside(self : Reading, lang : Lang, force : Bool?, line : Int) -> Reading

    The reading inside a block starting at line: its own language and options, and its own force when it gives one, else the enclosing stretch's. A block in the stretch's own language keeps its options; line prefixes carry through.

    Target

    pub(all) enum Target {
    Name(String)
    Tuple(Array[Target])
    Field(String, String)
    }

    Where an assignment or a loop binds.

    Tok

    pub(all) enum Tok {
    Data(String)
    VarBegin
    VarEnd
    BlockBegin
    BlockEnd
    Name(String)
    Str(String)
    Int(Int64)
    Float(Double)
    Op(String)
    Lang(String, Bool?, String)
    Eof
    } derive(Eq)

    One token. Template text and the delimiters of the brace syntax sit in the same stream as the tokens inside tags, as in Jinja's lexer.

    Tok::equal

    fn Tok::equal(Tok, Tok) -> Bool

    Tok::not_equal

    fn Tok::not_equal(x : Tok, y : Tok) -> Bool

    Token

    pub(all) struct Token {
    tok : Tok
    line : Int
    column : Int
    }

    Trans

    pub(all) struct Trans {
    context : String?
    singular : String
    plural : String?
    vars : Array[(String, Expr)]
    count : Expr?
    }

    A {% trans %}: message ids with %(name)s placeholders, the values those names take, the plural count, and the message context.

    html

    let html : Lang

    HTML: text with {{ }}, {% %} and {# #}.

    lex_expression

    fn lex_expression(src : String, template : String, line? : Int, column? : Int) -> Array[Token] raise
    Refused

    The tokens of a bare expression, as the indentation syntax and interpolation read them: everything to the end of the text.

    lex_template

    fn lex_template(source : String, options : Options, template : String, reading? : Reading, first_line? : Int) -> Array[Token] raise
    Refused

    The token stream of a template in the brace syntax: template text and tags in order, ending in Eof.

    markdown

    let markdown : Lang

    Markdown: the brace syntax, with a tag on a line of its own leaving no blank line behind.

    options

    let options : Options

    Brace options with nothing trimmed and no line prefixes.

    pug

    let pug : Lang

    Pug: elements by indentation, < lines passed through, {% %} lines as statements.

    read

    fn read(source : String, reading : Reading) -> Array[Node] raise
    Refused

    A source read in the reading's language.

    read_braces

    fn read_braces(source : String, reading : Reading) -> Array[Node] raise
    Refused

    A source in the brace syntax, read into the tree: the reader of html and md.

    read_indent

    fn read_indent(source : String, reading : Reading) -> Array[Node] raise
    Refused

    A source in the indentation syntax, read into the tree: the reader of pug.

    resolve

    fn resolve(template : String, path : String, line? : Int) -> String raise
    Refused

    A path the indentation syntax names, relative to the template naming it unless it starts with /; .pug is added when there is no extension.