Crate mon_core

Crate mon_core 

Source
Expand description

§MON-Core

CI Crates.io Documentation License: MIT Rust Version

mon-core is the reference Rust implementation of the MON (Mycel Object Notation) language — a human-focused configuration and data format designed to be readable, safe, and predictable.

This crate provides the parser, analyzer, validator, and core data model used by MON-based tooling, servers, CLIs, and compilers.

for more information please look at out website: www.mycel-lang.org


§Table of Contents


§Overview

MON aims to replace overly rigid formats (JSON) and overly permissive ones (YAML) with a syntax that stays readable without giving up safety or predictability.

mon-core implements:

  • A forgiving parser with clear, context-rich error messages
  • A semantic analyzer with anchor/alias resolution
  • Type checking via #struct, #enum, and validated bindings
  • An internal IR suitable for compilers and higher-level tooling

If you want to embed MON into your Rust application or build tooling around the language, this is the crate.

§For more information about mon docs are here :D

§Features

  • Clean syntax: unquoted keys, comments, trailing commas
  • Human-friendly booleans: on / off as well as true / false
  • Anchors & aliases: safe reuse with explicit copy semantics (&name, *name)
  • Deep merges: ...*anchor for structured overrides
  • Types built in: #struct, #enum, and :: for validation
  • Modular imports: import { A, B } from "./file.mon"
  • Detailed errors: location-aware, colorized, actionable

§Example

import { ServerSettings } from "./schemas.mon"

{
    &base: {
        host: "localhost",
        port: 8080,
    },

    User: #struct {
        id(Number),
        name(String),
        roles([String...]),
    },

    admin :: User = {
        id: 1,
        name: "Alice",
        roles: ["admin", "editor"],
    },

    dev: {
        ...*base,
        port: 9001,
        debug: on,
    },
}

§Rust Quick Start

Add to Cargo.toml:

[dependencies]
mon-core = "0.1"

Parse and analyze:

use mon_core::analyze;

fn main() {
    let text = r#"
        settings: {
            name: "Example",
            enabled: on,
        }
    "#;

    match analyze(text, "config.mon") {
        Ok(result) => {
            println!("JSON:\n{}", result.to_json().unwrap());
        }
        Err(err) => {
            eprintln!("MON error:\n{err}");
        }
    }
}

§Error Handling

MON is designed to fail loudly and helpfully.

Example error (format depends on your terminal capabilities):

error[E0012]: expected Number, got String
  --> config.mon:7:12
   |
 6 |   age: "twenty",
   |             ^^^ expected a Number here

Errors include:

  • the source span
  • the inferred and expected types
  • suggestions when applicable

§Development

Build:

cargo build

Test:

cargo test --all-features

Checks:

cargo check

The project follows standard Rust layout. Documentation lives in docs/. Any language or spec changes must be reflected there.


§Roadmap

  • Improved parser recovery modes
  • Type system stabilization
  • Performance pass on alias/anchor resolution
  • Better import graph validation
  • Tooling support (formatter, LSP)

§License

Licensed under the MIT license. See LICENSE for details.


for more information please look at out website: www.mycel-lang.org

Made with ❤️

§Crate Overview

mon-core is the foundational engine for the Mycel Object Notation (MON) language. It provides a complete pipeline for parsing, analyzing, and serializing MON data, designed to be fast, robust, and developer-friendly. This crate is the backbone of the Mycel ecosystem, powering tools like linters, language servers, and formatters.

§Architectural Overview

The library follows a standard compiler-front-end architecture, processing MON source code in several distinct stages:

  1. Lexical Analysis (lexer): The source text is first fed into the lexer, which scans the text and converts it into a stream of tokens (e.g., identifiers, keywords, symbols). This is the most basic level of tokenization.

  2. Parsing (parser): The stream of tokens is then passed to the parser, which enforces the MON language’s grammar rules. It constructs an Abstract Syntax Tree (AST), a hierarchical representation of the code’s structure, defined in the ast module.

  3. Semantic Analysis (resolver): The raw AST is processed by the resolver. This is a crucial step where the meaning of the code is analyzed. The resolver handles imports, resolves aliases and spreads, and validates data against type definitions (#struct and #enum).

  4. Public API (api): The api module provides the primary, high-level interface for the library. The analyze function wraps the entire Lexer -> Parser -> Resolver pipeline into a single, easy-to-use function, returning a fully resolved and validated result.

§Use Cases

  • Configuration Loading: Parse and validate .mon configuration files for an application.
  • Language Tooling: Build linters, formatters, or a Language Server Protocol (LSP) implementation for MON.
  • Data Serialization: Use the library to convert MON data into other formats like JSON or YAML.

§Example: A Complete Analysis

This example shows how to use the high-level analyze function to process a MON string from start to finish.

use mon_core::api::analyze;

let source = r#"
{
    Config: #struct { port(Number) },
    my_config :: Config = { port: 8080 }
}
"#;

// The `analyze` function handles lexing, parsing, and resolving.
let result = analyze(source, "config.mon")?;

// You can serialize the validated data to JSON.
let json_output = result.to_json().unwrap();
println!("{}", json_output);

// The output will be a well-formatted JSON string.
assert!(json_output.contains("\"port\": 8080.0"));

For more granular control over each stage of the process, you can use the components from the lexer, parser, and resolver modules directly.

Re-exports§

pub use api::analyze;
pub use api::AnalysisResult;

Modules§

api
Provides the public API for interacting with the MON core library, including parsing, analysis, and serialization functions.
ast
Abstract Syntax Tree (AST) for MON
error
Error Handling in mon-core
lexer
MON Lexer (Tokenizer)
parser
MON Parser
resolver
MON Semantic Analyzer (Resolver)
serialization
MON AST to Serializable Value Conversion