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-levelimportstatements. -
MonValue: A wrapper around aMonValueKindthat 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 asObject,Array,String,Number,Alias, etc. -
Member: Represents the different kinds of entries within an object, including aPair(key-value), aSpread(...*my_anchor), or aTypeDefinition. -
TypeDefinition,StructDef,EnumDef: These structs represent the schema definition constructs (#structand#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
#enumdefinition. - Field
Def - Represents a single field within a
#structdefinition. - Import
Specifier - Represents a single item being imported by name.
- Import
Statement - Represents an
importstatement, 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, orarray… It also includes metadata like its source position and any associated anchor. - Pair
- Represents a key-value pair within a MON object.
- Struct
Def - Represents a
#structdefinition. - Symbol
Table - A table to store resolved symbols, such as type definitions, from a MON document and its imports.
- Type
Definition - Represents a type definition, either a
#structor an#enum.
Enums§
- Import
Spec - Defines what is being imported from a file.
- Member
- Represents a member of a MON object.
- MonValue
Kind - 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.
- Type
Spec - Represents a type specification used for validation, e.g.,
:: Stringor:: [Number, String].