smithy-syntax

    Smithy syntax for MoonBit: lossless IDL parser, traversal, diagnostics and typed JSON AST codec

    smithy
    idl
    parser
    ast
    moonbit
    Download zip
    Author
    Version
    0.10.0
    License
    Apache-2.0
    Last updated
    16 hours ago
    Downloads
    337

    #moonrockz/smithy-syntax

    Smithy syntax for MoonBit: a lossless IDL scanner, a typed AST with spans, a CST that prints the source back byte for byte, a parser with recovery per statement, diagnostics in the style of Elm and Rust, and a typed JSON AST codec.

    #Status

    • Scanner: done.
    • AST: done.
    • CST: done.
    • Parser: done.
    • Diagnostics: done.
    • Traversal: done (traversal: NodeRef, walk, fold, reduce, accept, events, Tree, TreeCursor).
    • JSON AST: done (json_ast: typed records, decoder and encoder with exact numeric text round trips).

    #Example

    test {
    let r = @syntax.parse_idl(
    (
    #|$version: "2"
    #|namespace com.foo
    #|
    #|string Name
    #|
    ),
    name="a.smithy",
    )
    inspect(r.has_errors(), content="false")
    }

    #Installation

    moon add moonrockz/smithy-syntax

    #Packages

    PackageImport pathHolds
    spanmoonrockz/smithy-syntax/spanSpan, Position, SourceText
    diagnostic.../diagnosticSeverity, Label, Diagnostic, codes()
    scanner.../scannerTokenKind, Token, Trivia, Comment, TokenStream, tokenize, unescape, text_block_value
    ast.../astthe typed IDL tree
    cst.../cstIdlCst, StatementCst, StatementKind
    parser.../parserparse, parse_tokens, ParseResult, Version, max_nesting
    traversal.../traversalNodeRef, walk, fold, reduce, reduce_pruned, accept, EventReader, push_events, Tree, TreeCursor, NodePath, kind_table
    report.../reportrender_terminal, render_plain, render_short
    json_ast.../json_astJsonAst, shape records, parse, decode, encode, stringify
    rootmoonrockz/smithy-syntaxparse_idl, parse_json_ast, decode_json_ast, encode_json_ast, public type re-exports

    Dependencies point one way: span ← diagnostic ← scanner ← ast ← cst ← parser ← traversal and report ← root (traversal and report do not import each other). Each package has a README.mbt.md with checked examples. See the design in DESIGN.md, and the cookbook in docs/cookbook for task articles.

    The JSON AST package depends on core JSON and the IDL AST's simple-type enum. Its package guide defines the codec's preservation and validation rules. The semantic loader and IDL-to-JSON conversion are available in moonrockz/smithy.

    The facade also exports parse_tokens, codes and render_all_to. Use the scanner package for its pipeline records Piece and Step, and the parser package for Depth and ParseError. Property-test generators are internal.

    ShapeKind::keyword() and its Show implementation return IDL keywords. NumberLiteral::as_double() returns signed infinity on overflow. The exact number stays in NumberLiteral::value. Partial trees may contain duplicate member names; check has_errors() before relying on member uniqueness.

    AggregateType

    The AST.

    ApplyStatement

    The AST.

    Column

    Spans (UTF-16 unit offsets), positions and source text.

    Comment

    The scanner: tokens, trivia and lexical errors.

    CommentKind

    The scanner: tokens, trivia and lexical errors.

    Control

    The traversal: the node model, the cursor, the tree and the event types.

    ControlStatement

    The AST.

    Diagnostic

    Diagnostics: severity, labels and the problem record.

    DocComment

    The AST.

    EnterEvent

    The traversal: the node model, the cursor, the tree and the event types.

    EntityType

    The AST.

    EnumMember

    The AST.

    EnumType

    The AST.

    Event

    The traversal: the node model, the cursor, the tree and the event types.

    EventReader

    The traversal: the node model, the cursor, the tree and the event types.

    EventSource

    The traversal: the node model, the cursor, the tree and the event types.

    Handler

    The traversal: the node model, the cursor, the tree and the event types.

    IdlCst

    The CST: every token grouped per statement.

    IdlFile

    The AST.

    InlineShape

    The AST.

    JsonAst

    Typed Smithy JSON AST records. The IDL AST and JSON AST are separate representations; converting between them requires the semantic loader.

    JsonMember

    Typed Smithy JSON AST records. The IDL AST and JSON AST are separate representations; converting between them requires the semantic loader.

    JsonOperation

    Typed Smithy JSON AST records. The IDL AST and JSON AST are separate representations; converting between them requires the semantic loader.

    JsonReference

    Typed Smithy JSON AST records. The IDL AST and JSON AST are separate representations; converting between them requires the semantic loader.

    JsonResource

    Typed Smithy JSON AST records. The IDL AST and JSON AST are separate representations; converting between them requires the semantic loader.

    JsonService

    Typed Smithy JSON AST records. The IDL AST and JSON AST are separate representations; converting between them requires the semantic loader.

    JsonShape

    Typed Smithy JSON AST records. The IDL AST and JSON AST are separate representations; converting between them requires the semantic loader.

    JsonShapeKind

    Typed Smithy JSON AST records. The IDL AST and JSON AST are separate representations; converting between them requires the semantic loader.

    Keyword

    The AST.

    Label

    Diagnostics: severity, labels and the problem record.

    LeaveEvent

    The traversal: the node model, the cursor, the tree and the event types.

    Length

    Spans (UTF-16 unit offsets), positions and source text.

    Line

    Spans (UTF-16 unit offsets), positions and source text.

    Member

    The AST.

    MemberTarget

    The AST.

    MetadataStatement

    The AST.

    Mixins

    The AST.

    NamespaceStatement

    The AST.

    Node

    The AST.

    NodeKvp

    The AST.

    NodePath

    The traversal: the node model, the cursor, the tree and the event types.

    NodeRef

    The traversal: the node model, the cursor, the tree and the event types.

    NodeValue

    The AST.

    NumberLiteral

    The AST.

    NumberValue

    The AST.

    Offset

    Spans (UTF-16 unit offsets), positions and source text.

    OperationProperty

    The AST.

    OperationTarget

    The AST.

    ParseResult

    The parser's result and declared version.

    PathStep

    The traversal: the node model, the cursor, the tree and the event types.

    Position

    Spans (UTF-16 unit offsets), positions and source text.

    Severity

    Diagnostics: severity, labels and the problem record.

    Shape

    The AST.

    ShapeId

    The AST.

    ShapeKind

    The AST.

    ShapeStatement

    The AST.

    SimpleType

    The AST.

    SourceText

    Spans (UTF-16 unit offsets), positions and source text.

    Span

    Spans (UTF-16 unit offsets), positions and source text.

    Statement

    The AST.

    StatementCst

    The CST: every token grouped per statement.

    StatementKind

    The CST: every token grouped per statement.

    Token

    The scanner: tokens, trivia and lexical errors.

    TokenKind

    The scanner: tokens, trivia and lexical errors.

    TokenStream

    The scanner: tokens, trivia and lexical errors.

    Trait

    The AST.

    TraitBody

    The AST.

    Tree

    The traversal: the node model, the cursor, the tree and the event types.

    TreeCursor

    The traversal: the node model, the cursor, the tree and the event types.

    Trivia

    The scanner: tokens, trivia and lexical errors.

    UseStatement

    The AST.

    Version

    The parser's result and declared version.

    Visitor

    The traversal: the node model, the cursor, the tree and the event types.

    accept

    Call one Visitor method per node; see @traversal.accept.

    codes

    fn codes() -> ArrayView[String]

    Every registered diagnostic code, in table order.

    decode_json_ast

    Decode a Smithy JSON AST value, with JSON Pointer paths on errors.

    diagnostic_description

    fn diagnostic_description(code : String) -> String?

    The one-line description of a diagnostic code, or None.

    encode_json_ast

    Encode a typed Smithy JSON AST document.

    fold

    Thread an accumulator through a walk; see @traversal.fold.

    kind_table

    fn kind_table() -> Array[(String, String, Array[String])]

    Every node type: (category, kind, fields); see @traversal.kind_table.

    parse

    Parse a source text with recovery per statement.

    parse_idl

    fn parse_idl(text : String, name? : String) ->
    ParseResult

    Parse Smithy IDL text. name is shown in rendered diagnostics.

    test {
    let r = @syntax.parse_idl(
    (
    #|$version: "2"
    #|namespace com.foo
    #|
    #|string Name
    #|
    ),
    name="a.smithy",
    )
    inspect(r.has_errors(), content="false")
    }

    parse_json_ast

    fn parse_json_ast(text : String) ->
    JsonAst
    raise

    Parse a Smithy JSON AST document from text.

    parse_json_value

    Parse an arbitrary JSON value while retaining every exact numeric token. Duplicate names keep the last value, as in the core JSON parser. Callers needing strict duplicate or input limits must enforce them before parsing.

    parse_tokens

    Parse an existing lossless token stream, with recovery per statement.

    push_events

    Drive an event source into a handler; see @traversal.push_events.

    reduce

    Reduce root bottom up; see @traversal.reduce.

    reduce_pruned

    Reduce bottom up, steered by enter; see @traversal.reduce_pruned.

    render_all

    Render every diagnostic in order, ending with an error/warning count.

    render_all_to

    Render every diagnostic and the summary into a caller's sink.

    render_plain

    Render one diagnostic without colour, as the Elm/Rust-style layout.

    render_short

    Render one diagnostic as a single line, for editors (path:line:col:severity[code]: message).

    render_terminal

    Render one diagnostic for a terminal, with ANSI colour by default.

    render_terminal_to

    Render into any core Logger, for callers with their own sink.

    tokenize

    Scan source into tokens with trivia. Lossless: to_source() is the text.

    version

    fn version() -> String

    The version of smithy-syntax, maintained from the root moon.mod by scripts/versions.mbtx set and checked by validate:versions.

    walk

    Visit root and every node below it in pre-order; see @traversal.walk.

    Source Files