A Python parser for Context Mapper DSL (CML)
Project description
CML Parser
A Python library to parse the Context Mapper Language (CML) using ANTLR4. It aims to cover the language defined by the Context Mapper project and is validated against the official sample models.
Docs: https://martin882003.github.io/cml-parser
Context
CML is the DSL for Context Mapper, a toolkit for strategic/tactical Domain-Driven Design modeling. Reference material and canonical examples are maintained by the Context Mapper team:
- Language docs: https://contextmapper.org/
- Official examples repository: https://github.com/ContextMapper/context-mapper-examples
Installation
Using uv (Recommended) ⚡
uv is a fast Python package manager that's 10-100x faster than pip:
# Install from PyPI
uv pip install cml-parser
# Or clone and install from source
git clone https://github.com/martin882003/cml-parser.git
cd cml-parser
uv sync
Using pip
pip install cml-parser
From Source (pip)
git clone https://github.com/martin882003/cml-parser.git
cd cml-parser
pip install -e .
Usage
from cml_parser import parse_file
# Strict mode: raises CmlSyntaxError on parse errors
model = parse_file("path/to/model.cml")
# Safe mode: returns CML object with parse_results
from cml_parser import parse_file_safe
cml = parse_file_safe("path/to/model.cml")
if cml.parse_results.ok:
print(f"✓ Parsed successfully")
print(f" Domains: {len(cml.domains)}")
print(f" Context Maps: {len(cml.context_maps)}")
# Access tactical DDD elements
for cm in cml.context_maps:
for ctx in cm.contexts:
for agg in ctx.aggregates:
print(f" Aggregate: {agg.name}")
for entity in agg.entities:
print(f" Entity: {entity.name}")
for attr in entity.attributes:
print(f" - {attr.name}: {attr.type}")
else:
print("Parse errors:")
for err in cml.parse_results.errors:
print(f" {err.pretty()}")
Architecture
This parser is built with ANTLR4, providing:
- ✅ Robust parsing of CML syntax
- ✅ Full support for tactical DDD elements (Entities, ValueObjects, Aggregates, etc.)
- ✅ Strategic DDD (BoundedContexts, ContextMaps, Relationships)
- ✅ Deferred reference linking for forward declarations
- ✅ Rich Python object model with accessor methods
Development
Setup with uv
git clone https://github.com/martin882003/cml-parser.git
cd cml-parser
uv sync
uv run pytest
Setup with pip
git clone https://github.com/martin882003/cml-parser.git
cd cml-parser
python -m venv venv
source venv/bin/activate
pip install -e .
pytest
Regenerating the Parser
If you modify the ANTLR4 grammar (src/cml_parser/CML.g4):
# Install development dependencies
uv add --dev antlr4-tools
# Regenerate parser
uv run antlr4 -Dlanguage=Python3 -visitor -o src/cml_parser/antlr src/cml_parser/CML.g4
License
MIT License — see LICENSE.
Contact
- Maintainer: Martin Herran — martin882003@gmail.com
Contributing
Contributions are welcome! Please:
- Open an issue describing the change (grammar gaps, bugs, docs).
- Keep coverage: ensure
pytestpasses and new constructs are represented inexamples/or new fixtures. - Submit a PR with a concise summary of the change.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cml_parser-0.5.0.tar.gz.
File metadata
- Download URL: cml_parser-0.5.0.tar.gz
- Upload date:
- Size: 192.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d5cde6a17576e593de79e33386668f9c937b230fd8ffd4fcf8b0fb7e673245e4
|
|
| MD5 |
f581f71ff11f8efc96f250bbfea0602b
|
|
| BLAKE2b-256 |
6a5a07a40ac13390c9ef5328f41361849f0b2f09ef22db7743d2d048441ba6e2
|
File details
Details for the file cml_parser-0.5.0-py3-none-any.whl.
File metadata
- Download URL: cml_parser-0.5.0-py3-none-any.whl
- Upload date:
- Size: 161.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
09c0b7e4e7624a0df3b0dd6108a5b822cf461115a4f154d01b40b14590d48cd3
|
|
| MD5 |
83a365142b2b3460a83fb5b7259101dd
|
|
| BLAKE2b-256 |
4f440ef6007f8a6dd1f3368c8791caf493cab321e852539451de6700bff810a3
|