std.syntax
The syntax tree as values: what a program can write, as enums and structs a comptime
function or a macro reads, builds and writes back. The main types are Expr, Stmt, Decl,
Type and Pattern, with Node for any of them and Span for where one stands.
parseExpr(s), parseStmts(s), parseDecls(s) read source; toSource() writes a node as the
formatter would; children() and walk(f) go over a tree; Ident.fresh(hint) names what a
quote binds; error(at, msg) ends a macro's expansion with an error. The enums are
@NonExhaustive: a match over them outside std writes a
_, and a form the compiler has that this module has not - the rounds of a comptime for
over fields and variants among them - is refused where it is read (LYR-CT0005).
A node built here, not read, has a Span of nothing: file "", line and column 0.
Examples
import std.io { println };
import std.syntax { parseExpr };
fn main(): void {
println(comptime parseExpr("1+2 * 3").toSource());
}
1 + 2 * 3
Types
"" and 0 where the node was built, not read.u8, i32, ... or none), a float with its, a string, a byte string, a character, a bool, null.xs...).name = value, or name alone (isShorthand, the value the read of the name); an attribute's one positional argument has the name "".((k, v)) => ...), whether it is a place (&n), whether it is a trailing block's it.catch clause: the binding's name, its type (catch (e: T)) or its set (catch (e in [A, B])), the body - written bare after a try expression when isExpr."" where positional, none when none is named; then the module a quote wrote the name in, or null), ?T, T[] and T[N], a tuple with its labels ("" where none), a function type with its thrown set and which parameters take a place, a throwing type.import a.b;, import a.b { x, y as z } (names with renames by index, "" where a name keeps its own), import a.b as c;.Functions
pub fn parseExpr(s: string): Expr
Parses the expression a text holds, as the compiler parses it. A text that is no expression is
an error at the call (LYR-CT0001, with the parser's message).
pub fn parseStmts(s: string): Stmt[]
Parses the statements a text holds, as a block body would.
pub fn parseDecls(s: string): Decl[]
Parses the declarations a text holds, as a module would.
pub fn error(at: Span, msg: string): never
Reports a macro's own error and ends the expansion: the message at at, a node of the site -
error(e.span(), "...") - and a note at the site's !.