Module ast

Module ast 

Source
Expand description

§Abstract Syntax Tree (AST) for MON

This module defines the data structures that represent the Abstract Syntax Tree (AST) of a parsed MON document. The AST is a tree-like representation of the source code’s structure, and it is the central data structure used throughout the compilation pipeline.

§Architectural Overview

The AST is generated by the Parser from a stream of tokens. The initial AST is a “raw” representation that has not been semantically analyzed. This raw tree is then passed to the Resolver, which processes it by resolving imports, expanding aliases and spreads, and performing type validation. The final, resolved AST is the main output of the mon-core library.

The nodes of the tree are designed to be easily traversable and to store all necessary information from the source, including positional data (spans) for accurate error reporting.

§Key Data Structures

  • MonDocument: The root of the AST, containing the root value (typically an object) and a list of all top-level import statements.

  • MonValue: A wrapper around a MonValueKind that also includes metadata like a potential anchor name and its source code position. This is the primary representation of any value in MON.

  • MonValueKind: An enum that defines the actual type of a value, such as Object, Array, String, Number, Alias, etc.

  • Member: Represents the different kinds of entries within an object, including a Pair (key-value), a Spread (...*my_anchor), or a TypeDefinition.

  • TypeDefinition, StructDef, EnumDef: These structs represent the schema definition constructs (#struct and #enum) in MON.

  • SymbolTable: A container generated by the resolver to store all resolved type definitions for easy lookup during validation.

Developers typically do not need to construct these AST nodes manually. Instead, they are generated by the Parser and consumed by other parts of the library or by language tools.

Structs§

EnumDef
Represents an #enum definition.
FieldDef
Represents a single field within a #struct definition.
ImportSpecifier
Represents a single item being imported by name.
ImportStatement
Represents an import statement, e.g., import "path/to/file.mon" as my_namespace;
MonDocument
Represents a fully parsed MON document, including the root value and any import statements.
MonValue
Represents a value in a MON document, such as a string, number, object, or array… It also includes metadata like its source position and any associated anchor.
Pair
Represents a key-value pair within a MON object.
StructDef
Represents a #struct definition.
SymbolTable
A table to store resolved symbols, such as type definitions, from a MON document and its imports.
TypeDefinition
Represents a type definition, either a #struct or an #enum.

Enums§

ImportSpec
Defines what is being imported from a file.
Member
Represents a member of a MON object.
MonValueKind
An enum representing the different kinds of values that can exist in a MON document.
TypeDef
An enum that holds either a struct or an enum definition.
TypeSpec
Represents a type specification used for validation, e.g., :: String or :: [Number, String].