Skip to main content

A Python parser for Context Mapper DSL (CML)

Project description

CML Parser

CI PyPI codecov Python License

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:

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

Contributing

Contributions are welcome! Please:

  1. Open an issue describing the change (grammar gaps, bugs, docs).
  2. Keep coverage: ensure pytest passes and new constructs are represented in examples/ or new fixtures.
  3. Submit a PR with a concise summary of the change.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cml_parser-0.2.1.tar.gz (70.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cml_parser-0.2.1-py3-none-any.whl (70.7 kB view details)

Uploaded Python 3

File details

Details for the file cml_parser-0.2.1.tar.gz.

File metadata

  • Download URL: cml_parser-0.2.1.tar.gz
  • Upload date:
  • Size: 70.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cml_parser-0.2.1.tar.gz
Algorithm Hash digest
SHA256 168de3aff64c859c7365014402a4826f9f80a83946e3fdb4f1ad6f9ff136e622
MD5 851e1305589c9bd46033968387c9b32e
BLAKE2b-256 3c857c7bea94a168d62ffc7e02116f87f247302c9320a97a6c5022fdeed3e435

See more details on using hashes here.

File details

Details for the file cml_parser-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: cml_parser-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 70.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cml_parser-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 84987113cde42e1e49c25f247d87967e7118f1f2dacfa416198a4b83e584e25a
MD5 7082a4f42e722256dd6b824cb416cf5d
BLAKE2b-256 6646821ca52f814b1b2040374192ec4b3be5bb6898f8b23a7f853fb84bb66a12

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page