README

#justjavac/template_codegen/ast

Shared template AST and source metadata.

#
SourcePos

pub(all) struct SourcePos {
offset : Int
line : Int
column : Int
} derive(Eq, ToJson,
Debug
)

A 1-based source position plus the underlying string offset.

offset is the raw string offset used by scanners, while line and column are intended for human-readable diagnostics.

#
SourceSpan

pub(all) struct SourceSpan {
file : String
start : SourcePos
end : SourcePos
} derive(Eq, ToJson,
Debug
)

A half-open source span in one file.

The start position points at the first character in the range and end points just after the range. This keeps parser and codegen diagnostics easy to map back to the original template or MoonBit source.

#
Template

pub(all) struct Template {
path : String
nodes : Array[TemplateNode]
} derive(Eq, ToJson,
Debug
)

A parsed template document.

path identifies the source template for diagnostics, and nodes stores the parsed items in render order.

#
TemplateBinding

pub(all) struct TemplateBinding {
source_path : String
template_path : String
struct_name : String
field_names : Array[String]
span : SourceSpan
} derive(Eq, ToJson,
Debug
)

A user struct bound to a template path by #tpl.path.

Codegen uses the binding to connect a MoonBit struct with a template file. field_names records the struct fields that may be referenced without self. inside templates; generated code prefixes them automatically.

#
TemplateNode

pub(all) enum TemplateNode {
Text(String, SourceSpan)
EscapedExpr(String, SourceSpan)
RawExpr(String, SourceSpan)
Statement(String, SourceSpan)
Include(String, SourceSpan)
Comment(String, SourceSpan)
} derive(Eq, ToJson,
Debug
)

One parsed template item.

Nodes preserve source spans so downstream tools can report precise errors. Escaped expressions correspond to <%= ... %>, raw expressions correspond to <%- ... %>, statements correspond to <% ... %>, includes correspond to <%~ "path" %>, and comments correspond to <%# ... %>.

#
comment_node

fn comment_node(text : String, span : SourceSpan) -> TemplateNode

Creates a template comment node.

Comments are preserved in the AST for tooling, but they do not emit output.

#
escaped_expr_node

fn escaped_expr_node(expr : String, span : SourceSpan) -> TemplateNode

Creates an escaped expression output node.

Generated renderers evaluate the expression, format it with Show, and HTML-escape the formatted value before writing it.

#
include_node

fn include_node(path : String, span : SourceSpan) -> TemplateNode

Creates a static include node.

File-based codegen expands include nodes before emitting MoonBit source.

#
pos

fn pos(offset~ : Int, line~ : Int, column~ : Int) -> SourcePos

Creates a source position for diagnostics and source spans.

#
raw_expr_node

fn raw_expr_node(expr : String, span : SourceSpan) -> TemplateNode

Creates a raw expression output node.

Generated renderers evaluate the expression and write the formatted value without HTML escaping, so callers should use it only for trusted content.

#
span

fn span(file~ : String, start~ : SourcePos, end~ : SourcePos) -> SourceSpan

Creates a half-open source span for a single file.

#
statement_node

fn statement_node(code : String, span : SourceSpan) -> TemplateNode

Creates a MoonBit statement node.

Statement nodes are copied into the generated renderer after template field references and short filters have been qualified by codegen.

#
template

fn template(path~ : String, nodes~ : Array[TemplateNode]) -> Template

Creates a parsed template document from a path and node sequence.

#
template_binding

fn template_binding(source_path~ : String, template_path~ : String, struct_name~ : String, field_names? : Array[String], span~ : SourceSpan) -> TemplateBinding

Creates a template binding discovered in MoonBit source.

This constructor is useful for tests and custom scanners that want to feed bindings into the code generator without parsing files again.

#
text_node

fn text_node(text : String, span : SourceSpan) -> TemplateNode

Creates a plain text node.

Text nodes are emitted directly as string writes by the generator.

Source Files

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io