Skip to main content

Module emit_pretty

Module emit_pretty 

Source
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:

  • STRING nodes contribute literal token bytes directly.
  • SYMBOL and FIELD nodes recurse into the schema’s children, matching by edge kind (which is the tree-sitter field name).
  • SEQ emits its members in order.
  • CHOICE picks the alternative whose head SYMBOL matches an actual child kind, or whose terminals appear in the rendered prefix; falls back to the first non-BLANK alternative when no alternative matches.
  • REPEAT and REPEAT1 emit their content once per matching child edge in declared order.
  • OPTIONAL emits its content iff a corresponding child edge or constraint is populated.
  • PATTERN is a regex placeholder for variable-text terminals (identifiers, numbers, quoted strings). The walker emits a literal-value constraint 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§

FormatPolicy
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.