yaml

    YAML 1.2 parser in MoonBit

    yaml
    parser
    Download zip
    Author
    Version
    0.1.2
    License
    Apache-2.0
    Last updated
    2 days ago
    Downloads
    36

    No dependencies

    #bobzhang/yaml

    A YAML parser written in MoonBit, targeting YAML 1.2. It supports block and flow collections, quoted and block scalars, document streams, tags, anchors, aliases, merge keys, and binary values. It works on the native, JavaScript, Wasm, and Wasm GC backends.

    #Install

    moon add bobzhang/yaml

    Add the package to your moon.pkg:

    import {
    "bobzhang/yaml",
    }

    #Parse a document

    fn main {
    let document = try! @yaml.parse_yaml("name: MoonBit\nenabled: true\n")
    match document {
    Map(entries) => {
    for entry in entries {
    let (key, value) = entry
    println("\{key}: \{value}")
    }
    }
    _ => println("Expected a mapping")
    }
    }

    parse_yaml returns a Yaml value and raises ParseError for invalid input or multiple documents. Empty input returns Null. The example uses try! to keep it short; applications can handle errors with try ... catch or propagate them from a function declared with raise @yaml.ParseError.

    Yaml represents nulls, booleans, 32-bit integers, floating-point values, strings, bytes, sequences, mappings, and tagged values. Mappings are arrays of key/value pairs, allowing non-string keys and preserving entry order. This is a parser; it does not provide a general YAML serializer.

    #Streams and aliases

    • parse_yaml_stream(text) parses every document, replaces aliases with their referenced values, and applies merge keys. Anchor and tag wrappers remain in the returned tree.
    • parse_yaml_stream_raw(text) retains Anchor, Alias, and Tagged nodes.
    • resolve_aliases(value) resolves a raw tree and raises ParseError for unresolved anchors or invalid merges.
    • decode_base64(tag, text) and encode_base64(bytes) provide binary conversion.

    #Validation

    moon check --target all moon test --target all moon fmt --check

    CI also runs the YAML test suite at pinned commit 6ad3d2c62885d82fc349026c136ef560838fdf3d. To run the same corpus locally:

    git clone https://github.com/yaml/yaml-test-suite.git yaml-test-suite-data git -C yaml-test-suite-data checkout 6ad3d2c62885d82fc349026c136ef560838fdf3d moon build --target native python3 scripts/run_yaml_suite.py

    The first release passes 59 module tests on each backend and all 402 cases reported by that corpus runner. This describes the tested corpus, rather than a claim that every YAML feature or implementation edge case is covered.

    #License

    Apache License 2.0. See LICENSE.

    ParseError

    pub(all) suberror ParseError {
    UnexpectedChar(pos~ : Position, found~ : Char)
    UnexpectedEof(pos~ : Position)
    InvalidIndent(pos~ : Position)
    InvalidNumber(pos~ : Position, value~ : String)
    InvalidEscape(pos~ : Position, value~ : String)
    UnknownAnchor(name~ : String)
    InvalidMerge(detail~ : String)
    InvalidDirective(detail~ : String)
    InvalidTagValue(tag~ : String, value~ : String)
    MultipleDocuments(count~ : Int)
    NotImplemented(feature~ : String)
    } derive(Eq,
    Debug
    ,
    ToJson
    )

    impl Show for ParseError

    ParseError::equal

    fn ParseError::equal(ParseError, ParseError) -> Bool

    ParseError::not_equal

    fn ParseError::not_equal(x : ParseError, y : ParseError) -> Bool

    ParseError::output

    fn ParseError::output(self : ParseError, logger : &Logger) -> Unit

    ParseError::to_json

    ParseError::to_string

    fn ParseError::to_string(self : ParseError) -> String

    Position

    impl Show for Position

    Position::equal

    fn Position::equal(Position, Position) -> Bool

    Position::not_equal

    fn Position::not_equal(x : Position, y : Position) -> Bool

    Position::output

    fn Position::output(self : Position, logger : &Logger) -> Unit

    Position::to_json

    Position::to_repr

    Position::to_string

    fn Position::to_string(self : Position) -> String

    Yaml

    pub(all) enum Yaml {
    Null
    Bool(Bool)
    Int(Int)
    Float(Double)
    String(String)
    Binary(Bytes)
    Seq(Array[Yaml])
    Map(Array[(Yaml, Yaml)])
    Alias(String)
    Anchor(name~ : String, value~ : Yaml)
    Tagged(tag~ : String, value~ : Yaml)
    } derive(Eq,
    Debug
    ,
    ToJson
    )

    impl Show for Yaml

    Yaml::equal

    fn Yaml::equal(Yaml, Yaml) -> Bool

    Yaml::not_equal

    fn Yaml::not_equal(x : Yaml, y : Yaml) -> Bool

    Yaml::output

    fn Yaml::output(self : Yaml, logger : &Logger) -> Unit

    Yaml::to_json

    Yaml::to_repr

    Yaml::to_string

    fn Yaml::to_string(self : Yaml) -> String

    decode_base64

    fn decode_base64(tag : String, raw : String) -> Bytes raise ParseError

    encode_base64

    fn encode_base64(bytes : Bytes) -> String

    parse_yaml

    fn parse_yaml(input : String) -> Yaml raise ParseError

    parse_yaml_stream

    fn parse_yaml_stream(input : String) -> Array[Yaml] raise ParseError

    parse_yaml_stream_raw

    fn parse_yaml_stream_raw(input : String) -> Array[Yaml] raise ParseError

    resolve_aliases

    fn resolve_aliases(value : Yaml) -> Yaml raise ParseError