Syntax.Codec
Codecs for the versioned syntax artifacts.
Decoding turns the compiler’s exports into the typed Syntax vocabularies, rejecting wrong schema tags, malformed shapes, and spans that invert or reach past the embedded source with one structured error; encoding is the exact inverse, re-emitting identical bytes.
The printer here matches the compiler’s export format precisely: two-space indent, one element per line, a space after each key’s colon, [] and {} for empty collections, insertion-order object keys, and the shared escape discipline (named escapes plus lowercase \u00xx for other control characters). Field order is part of the schema: envelope keys are in declaration order, node objects are alphabetical, and optional fields are omitted rather than null.
Types
CodecError
type CodecError = CodecError { path: String, reason: String }
A structured decode refusal: the path of the offending value inside the document and the reason it was rejected.
TokensDoc
type TokensDoc = TokensDoc {
schema: String,
compiler: String,
source: SourceFile,
raw: List(Token),
parse: List(Token),
trivia: List(Trivia)
}
A decoded prism-syntax-tokens-v1 document: the envelope identity, the embedded source, the raw and post-layout token streams, and the trivia events, all in stream order.
SurfaceDoc
type SurfaceDoc = SurfaceDoc {
schema: String,
compiler: String,
source: SourceFile,
items: List(Item)
}
A decoded prism-surface-syntax-v1 document: the envelope identity, the embedded source, and the ordered item list of the parsed file.
Effects
Decode
effect Decode
never fail_decode(CodecError) : a
Functions and Values
tokens_schema
tokens_schema : () -> String
The schema tag of the token-stream artifact.
codec_error_message
codec_error_message : (Syntax.Codec.CodecError) -> String
Render a codec error as one line.
decode_tokens
decode_tokens : (String) -> Result(Syntax.Codec.TokensDoc, Syntax.Codec.CodecError)
Decode a prism-syntax-tokens-v1 document from its exact bytes. A wrong or missing schema tag, malformed JSON, missing field, or invalid span is a structured CodecError, never a partial document.
canonical_json
canonical_json : (Json.Json) -> String
Render a JSON value in the export layout the encoders here use, for tools that derive a document from an artifact instead of re-encoding a decoded one.
encode_tokens
encode_tokens : (Syntax.Codec.TokensDoc) -> String
Encode a token document back to the exact artifact bytes the compiler emits: encode_tokens after decode_tokens is byte-identity on every well-formed export.
surface_schema
surface_schema : () -> String
The schema tag of the surface-syntax artifact.
encode_surface
encode_surface : (Syntax.Codec.SurfaceDoc) -> String
Encode a surface document back to the exact artifact bytes the compiler emits: encode_surface after decode_surface is byte-identity on every well-formed export.
decode_surface
decode_surface : (String) -> Result(Syntax.Codec.SurfaceDoc, Syntax.Codec.CodecError)
Decode a prism-surface-syntax-v1 document from its exact bytes. A wrong or missing schema tag, malformed JSON, missing field, or invalid span is a structured CodecError, never a partial document.
decode_diagnostics
decode_diagnostics : (String) -> Result(Syntax.Diagnostic.DiagnosticsDoc, Syntax.Codec.CodecError)
Decode a prism-syntax-diagnostics-v1 document. Total: every malformed input lands on a CodecError naming the JSON path that refused.
encode_diagnostics
encode_diagnostics : (Syntax.Diagnostic.DiagnosticsDoc) -> String
Encode a diagnostics document to the canonical artifact bytes, the exact inverse of decode_diagnostics on every Rust-produced document.
run_decode
run_decode : forall e0 a. (() -> a ! {Syntax.Codec.Decode, e0}) -> Result(a, Syntax.Codec.CodecError) ! {e0}
Run a decoding computation, turning a fail_decode into Err and a completed decode into Ok.
decode_resolved
decode_resolved : (String) -> Result(Syntax.Resolved.ResolvedDoc, Syntax.Codec.CodecError)
Decode a prism-resolved-syntax-v1 document. Total: every malformed input lands on a CodecError naming the JSON path that refused. The node tree decodes recursively; a child list is bounded by the input’s own nesting.
encode_resolved
encode_resolved : (Syntax.Resolved.ResolvedDoc) -> String