Module parser

Module parser 

Source
Expand description

§MON Parser

This module provides the Parser for the MON language. Its primary responsibility is to transform a linear sequence of tokens from the lexer into a hierarchical Abstract Syntax Tree (AST), as defined in the ast module.

§Architectural Overview

The Parser is a recursive descent parser. This parsing strategy uses a set of mutually recursive functions to process the token stream, with each function typically corresponding to a non-terminal symbol in the MON grammar. For example, parse_object(), parse_array(), and parse_member() each handle a specific part of the language syntax.

The parser’s entry point is Parser::parse_document, which orchestrates the parsing of the entire document, including any top-level import statements and the root object.

The parser does not perform semantic validation. It only checks for syntactic correctness. For example, it will successfully parse value: *non_existent_anchor, but the resolver will later flag an error because the anchor does not exist.

§Use Cases

Direct interaction with the parser is less common than using the top-level analyze function. However, it can be useful for:

  • Syntax Tree Inspection: Building tools that need to analyze the raw structure of a MON file without performing full semantic analysis.
  • Custom Analysis Pipelines: Creating a custom analysis process where the AST needs to be inspected or transformed before being passed to the resolver.

§Example: Direct Parser Usage

use mon_core::parser::Parser;
use mon_core::error::MonError;

let source = r#"
{
    // This is a syntactically correct MON file.
    key: "value",
    nested: { flag: on }
}
"#;

// 1. Create a new parser for the source code.
let mut parser = Parser::new_with_name(source, "my_file.mon".to_string())?;

// 2. Parse the source into a document (AST).
let document = parser.parse_document()?;

// The `document` can now be inspected.
assert!(document.imports.is_empty());
// Further processing would be needed to make sense of the values.

Structs§

Parser
A recursive descent parser for the MON language.