Expand description
De-novo source emission from by-construction schemas. De-novo source emission from a by-construction schema.
AstParser::emit reconstructs source from byte-position fragments
that the parser stored on the schema during parse. That works for
edit pipelines (parse → transform → emit) but fails for schemas
built by hand (SchemaBuilder with no parse history): they carry
no start-byte, no interstitial-N, no literal-value, and the
reconstructor returns Err(EmitFailed { reason: "schema has no text fragments" }).
This module renders such schemas to source bytes by walking
tree-sitter’s grammar.json production rules. For each schema
vertex of kind K, the walker looks up K’s production in the
grammar and emits its body in order:
STRINGnodes contribute literal token bytes directly.SYMBOLandFIELDnodes recurse into the schema’s children, matching by edge kind (which is the tree-sitter field name).SEQemits its members in order.CHOICEpicks the alternative whose headSYMBOLmatches an actual child kind, or whose terminals appear in the rendered prefix; falls back to the first non-BLANKalternative when no alternative matches.REPEATandREPEAT1emit their content once per matching child edge in declared order.OPTIONALemits its content iff a corresponding child edge or constraint is populated.PATTERNis a regex placeholder for variable-text terminals (identifiers, numbers, quoted strings). The walker emits aliteral-valueconstraint when present and otherwise falls back to a placeholder derived from the regex shape.BLANK,TOKEN,IMMEDIATE_TOKEN,ALIAS,PREC*are handled transparently (the inner content is emitted; the wrapper is dropped).
Whitespace and indentation come from a FormatPolicy applied
during emission. The default policy inserts a single space between
adjacent tokens, a newline after ; / } / {, and tracks an
indent counter on { / } boundaries.
Output is syntactically valid for any grammar that ships
grammar.json. Idiomatic formatting (rustfmt-style spacing rules,
per-language conventions) is a polish layer that lives outside
this module.
Structs§
- Format
Policy - Whitespace and indentation policy applied during emission.
- Grammar
- A grammar’s production-rule table, deserialized from
grammar.json.
Enums§
- Production
- A single tree-sitter production rule.
Functions§
- emit_
pretty - Emit a by-construction schema to source bytes.