hare::unparse
hare::unparse provides an unparser for Hare. All functions take in some part of the AST and write formatted Hare source code to an io::handle.
Index
Types
type context = struct {
out: io::handle,
stack: nullable *stack,
linelen: size,
indent: size,
};
type stack = struct {
cur: (*ast::decl | *ast::expr | *ast::_type | *ast::import),
up: nullable *stack,
extra: nullable *opaque,
};
type synfunc = fn(ctx: *context, s: str, kind: synkind) (size | io::error);
type synkind = enum {
IDENT,
COMMENT,
CONSTANT,
FUNCTION,
GLOBAL,
TYPEDEF,
IMPORT_ALIAS,
SECONDARY,
KEYWORD,
TYPE,
ATTRIBUTE,
OPERATOR,
PUNCTUATION,
RUNE_STRING,
NUMBER,
LABEL,
};
Functions
fn _type(out: io::handle, syn: *synfunc, t: *ast::_type) (size | io::error);
fn builtin_type(b: ast::builtin_type) str;
fn decl(out: io::handle, syn: *synfunc, d: *ast::decl) (size | io::error);
fn expr(out: io::handle, syn: *synfunc, e: *ast::expr) (size | io::error);
fn ident(out: io::handle, id: ast::ident) (size | io::error);
fn identstr(id: ast::ident) str;
fn import(out: io::handle, syn: *synfunc, import: *ast::import) (size | io::error);
fn subunit(out: io::handle, syn: *synfunc, s: ast::subunit) (size | io::error);
fn syn_nowrap(ctx: *context, s: str, kind: synkind) (size | io::error);
fn syn_wrap(ctx: *context, s: str, kind: synkind) (size | io::error);
Types
type context
type context = struct {
out: io::handle,
stack: nullable *stack,
linelen: size,
indent: size,
};
Context about the unparsing state supplied to a synfunc. The linelen and indent fields may be mutated.
type stack
type stack = struct {
cur: (*ast::decl | *ast::expr | *ast::_type | *ast::import),
up: nullable *stack,
extra: nullable *opaque,
};
A linked list of AST nodes currently being unparsed.
type synfunc
type synfunc = fn(ctx: *context, s: str, kind: synkind) (size | io::error);
A user-supplied function which writes unparsed Hare source code to a handle, optionally including extra stylistic features. The function is expected to write to, at the minimum, write the provided string to ctx.out, and update ctx.linelen based on how much data was written.
syn_nowrap and syn_wrap are provided for when no additional styling is desired.
type synkind
type synkind = enum {
IDENT,
COMMENT,
CONSTANT,
FUNCTION,
GLOBAL,
TYPEDEF,
IMPORT_ALIAS,
SECONDARY,
KEYWORD,
TYPE,
ATTRIBUTE,
OPERATOR,
PUNCTUATION,
RUNE_STRING,
NUMBER,
LABEL,
};
The kind of thing being unparsed.
Functions
fn _type
fn _type(out: io::handle, syn: *synfunc, t: *ast::_type) (size | io::error);
Unparses a hare::ast::_type.
fn builtin_type
fn builtin_type(b: ast::builtin_type) str;
Returns a builtin type as a string.
fn decl
fn decl(out: io::handle, syn: *synfunc, d: *ast::decl) (size | io::error);
Unparses a hare::ast::decl.
fn expr
fn expr(out: io::handle, syn: *synfunc, e: *ast::expr) (size | io::error);
Unparses a hare::ast::expr.
fn ident
fn ident(out: io::handle, id: ast::ident) (size | io::error);
Unparses an identifier.
fn identstr
fn identstr(id: ast::ident) str;
Unparses an identifier into a string. The caller must free the return value.
fn import
fn import(out: io::handle, syn: *synfunc, import: *ast::import) (size | io::error);
Unparses a hare::ast::import.
fn subunit
fn subunit(out: io::handle, syn: *synfunc, s: ast::subunit) (size | io::error);
Unparses a hare::ast::subunit.
fn syn_nowrap
fn syn_nowrap(ctx: *context, s: str, kind: synkind) (size | io::error);
A synfunc implementation which unparses without additional styling, and without wrapping any long lines.
fn syn_wrap
fn syn_wrap(ctx: *context, s: str, kind: synkind) (size | io::error);
A synfunc implementation which unparses without additional styling, but which wraps some long lines at 80 columns, in accordance with the style guide.