moon_config

    MoonBit package configuration parser

    Download zip
    Version
    0.4.6
    License
    Apache-2.0
    Last updated
    11 hours ago
    Downloads
    145K

    #moonbitlang/moon_config

    moonbitlang/moon_config parses MoonBit project configuration files into a source-preserving syntax tree with locations, tokens and diagnostics.

    It provides parse_moon_mod, parse_moon_pkg, and parse_moon_work for moon.mod, moon.pkg, and moon.work content.

    The parse functions validate without rewriting syntax. Errors and warnings are separate; use validate=false for syntax only. Ast::to_json() preserves syntax except that repeated key(...) calls become key = [{...}, {...}].

    #Examples

    Parse a moon.mod file and convert the returned Ast to JSON:

    ///|
    test "parse moon.mod" {
    let source =
    #|name = "example/app"
    #|version = "0.1.0"
    #|import { "moonbitlang/x@0.4.6" }
    #|
    let result = @moon_config.parse_moon_mod(source)
    assert_eq(result.diagnostics.errors.length(), 0) // no error
    assert_true(result.ast.to_json() is Array([_, _, _]))
    }

    Parse a moon.pkg file:

    ///|
    test "parse moon.pkg" {
    let source =
    #|import {
    #| "moonbitlang/core/json" @json,
    #|}
    #|options(
    #| "is-main": true,
    #|)
    #|
    let result = @moon_config.parse_moon_pkg(source)
    assert_eq(result.diagnostics.errors.length(), 0) // no error
    assert_eq(result.ast.0.length(), 2)
    }

    Parse a moon.work file and check diagnostics:

    ///|
    test "parse moon.work" {
    let source =
    #|members = ["./app", "./shared"]
    #|preferred_target = "native"
    #|
    let result = @moon_config.parse_moon_work(source)
    assert_eq(result.diagnostics.errors.length(), 0) // no error
    assert_eq(result.ast.0.length(), 2)
    }

    #Conditional imports

    moon.pkg accepts repeated import blocks, optionally preceded by one or more #cfg(...) attributes. Each import retains its conditions, including the raw attribute, parsed Condition, and source location. Conditions support booleans, target = "wasm" | "wasm-gc" | "js" | "native" | "llvm", all(...), any(...), and single-argument not(...). No target selection is performed. moon.mod rejects conditional imports. experimental accepts boolean values only.

    ///|
    test "conditional imports" {
    let source = "#cfg(target = \"js\")\nimport { \"platform/js\" }\nexperimental = true"
    let result = @moon_config.parse_moon_pkg(source)
    assert_eq(result.diagnostics.errors.length(), 0)
    guard result.ast.0[0] is Import(conditions~, ..) else {
    fail("expected import")
    }
    assert_eq(conditions.length(), 1)
    assert_eq(conditions[0].parsed, Some(@moon_config.Condition::Target("js")))
    }

    Ast

    Configuration syntax is preserved for validation, formatting and editing.
    impl ToJson for Ast

    Ast::equal

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

    Ast::not_equal

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

    Ast::to_json

    fn Ast::to_json(self : Ast) ->
    Json

    Ast::to_repr

    Condition

    pub(all) enum Condition {
    Bool(Bool)
    Target(String)
    All(Array[Condition])
    Any(Array[Condition])
    Not(Condition)
    } derive(Eq,
    Debug
    ,
    ToJson
    )

    Conditions are preserved without selecting a compilation backend.

    Condition::equal

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

    Condition::not_equal

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

    Condition::to_json

    Expr

    Expr::equal

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

    Expr::not_equal

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

    Expr::to_repr

    Field

    pub(all) struct Field {
    key : String
    key_loc :
    Location

    value : Expr
    } derive(Eq,
    Debug
    )

    Field::equal

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

    Field::not_equal

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

    Field::to_repr

    ImportCondition

    pub(all) struct ImportCondition {
    raw : String
    parsed : Condition?
    loc :
    Location

    } derive(Eq,
    Debug
    )

    The original attribute is retained even if its payload is invalid.

    ImportCondition::equal

    ImportCondition::not_equal

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

    ImportItem

    ImportItem::equal

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

    ImportItem::not_equal

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

    ImportKind

    pub(all) enum ImportKind {
    Normal
    Test
    Wbtest
    } derive(Eq,
    Debug
    )

    ImportKind::equal

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

    ImportKind::not_equal

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

    Statement

    Statement::equal

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

    Statement::not_equal

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

    find_field

    fn find_field(fields : ArrayView[Field], key : String) -> Expr?

    find_field_in_options

    fn find_field_in_options(ast : Ast, key : String) -> Expr?

    find_statements

    fn find_statements(ast : Ast, key : String) -> Ast

    parse_moon_mod

    fn parse_moon_mod(name? : String, keep_comments? : Bool, validate? : Bool, source : String) -> ParseResult

    Parse configuration syntax and validate it without rewriting the tree.

    parse_moon_pkg

    fn parse_moon_pkg(name? : String, start_pos? :
    Position
    , keep_comments? : Bool, validate? : Bool, source : String) -> ParseResult

    Parse configuration syntax and validate it without rewriting the tree.

    parse_moon_work

    fn parse_moon_work(name? : String, keep_comments? : Bool, validate? : Bool, source : String) -> ParseResult

    validate_moon_mod

    fn validate_moon_mod(ast : Ast) -> Diagnostics

    validate_moon_pkg

    fn validate_moon_pkg(ast : Ast) -> Diagnostics

    validate_moon_work

    fn validate_moon_work(ast : Ast) -> Diagnostics