Skip to main content

mccode-antlr

PyPI conda-forge License Python

ANTLR4-based compiler and Python API for the McStas and McXtrace Monte Carlo particle ray-tracing languages.

Documentation · PyPI · conda-forge · Issues

Quick start

Command line

pip install mccode_antlr

mcstas-antlr my_instrument.instr   # translate to C
mcrun-antlr  my_instrument.instr -n 1e6 E_i=5.0   # compile & run

Python API

from mccode_antlr import Flavor
from mccode_antlr.assembler import Assembler

a = Assembler("BrillouinSpec", flavor=Flavor.MCSTAS)
a.parameter("double E_i = 5.0")   # meV

src = a.component("Source", "Source_simple",
                  at=(0, 0, 0),
                  parameters={"E0": "E_i", "radius": 0.05})

instr = a.instrument()
instr.to_file("BrillouinSpec.instr")

# In Jupyter: just put `instr` on the last line of a cell for an interactive view

Installation

# pip
pip install mccode_antlr                 # latest release
pip install "mccode_antlr[hdf5]"         # with HDF5 output
pip install "mccode_antlr[mcpl]"         # with MCPL file support
pip install "mccode_antlr[ipython]"      # with IPython/Jupyter matcher support

# conda / mamba (conda-forge)
conda install conda-forge::mccode-antlr
mamba install -c conda-forge mccode-antlr

# development version
pip install git+https://github.com/mccode-dev/mccode-antlr.git

Documentation

Full documentation — including a getting-started guide, core concepts, how-to guides, and API reference — is at:

https://mccode-dev.github.io/mccode-antlr/

IPython / Jupyter completions

mccode_antlr can register an IPython matcher for Python authoring with Assembler and Simulation objects:

from mccode_antlr.integrations.ipython import register_ipython_matcher

register_ipython_matcher()

Or in IPython / Jupyter:

%load_ext mccode_antlr.integrations.ipython

This matcher is intended for Python-side completions such as component names, component parameter names, and simulation parameter names. It does not provide raw McCode DSL completion inside notebook cells. The %load_ext path is the recommended automatic registration mechanism; importing the module alone does not register the matcher as a side effect.

Why ANTLR4?

included in-rule code to implement some language features and called the code-generator to construct the intermediate instrument source file. The mixture of language parsing and multiple layers of generated functionality made understanding the program operation, and debugging introduced errors, difficult. Worst of all, there is no easy-to-use tooling available to help the programmer identify syntax errors on-the-fly.

This project reimplements the McCode languages using ANTLR4 which both tokenizes and parses the language into a recursive descent parse tree. ANTLR can include extra in-rule parsing code, but since it can produce output suited for multiple languages (and the extra code must be in the targeted language) this feature is not implemented in this project.

Other benefits of ANTLR4 include integration with Integrated Development Environments, including the freely available Community edition of PyCharm from JetBrains. IDE integration can identify syntax mistakes in the language grammar files, plus help to understand and debug language parsing.

McCode languages

Traditionally, McCode identifies as a single language able to read, parse, and construct programs to perform single particle statistical ray tracing. While McCode-3 uses a single language.l and language.y file pair for lexing and parsing, it actually implemented at least two related languages: one for component definitions in .comp files, one for instrument definitions in .instr files, plus arguably more for other specialised tasks. Notably the mcdisplay utilities of McCode make use of a special runtime output mode to identify the positions and shapes of components, and the paths of particles, which is then read by an independent ply parser to generated visualizations.

This project makes use of ANTLR's language dependency feature to separate the languages into McComp for components and McInstr for instruments, with common language features defined in a McCommon grammar.

Language translation

For use with the McCode runtimes (McStas and McXtrace), the input languages must be translated to C following the C99 standard. This translation was previously performed in C since the lex|flex, yacc|bison workflow produced programs written in C. The C programming language is a very good choice where execution speed is important, as in the McCode runtimes, but less so if speed is not the main goal and memory safety or cross-platform development is important. The McCode-3 translators do not always deallocate memory used in their runtime, and newly developed features are likely to introduce unallocated, out-of-bounds, or double-free memory errors which are then difficult to track down.

ANTLR4 is a Java program, but produces parse-trees in multiple languages. This project uses the Python target so that language-translation can proceed in a language which is well suited to new-feature development, while removing memory handling issues and making cross-platform development significantly easier.

Download files

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

Source Distribution

mccode_antlr-0.25.0.tar.gz (1.8 MB view details)

Uploaded Source

Built Distribution

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

mccode_antlr-0.25.0-py3-none-any.whl (499.0 kB view details)

Uploaded Python 3

File details

Details for the file mccode_antlr-0.25.0.tar.gz.

File metadata

  • Download URL: mccode_antlr-0.25.0.tar.gz
  • Upload date:
  • Size: 1.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for mccode_antlr-0.25.0.tar.gz
Algorithm Hash digest
SHA256 ab795fc89078ad4d64838ab7b03ef94e4e0f2f31b4b377f886f5e93e7804455f
MD5 297bd517963d0fecc117d7378ea63209
BLAKE2b-256 90f88bd724616ca71b06bf4968f9c944eeaedcfc8519b207415028a4e938fd36

See more details on using hashes here.

Provenance

The following attestation bundles were made for mccode_antlr-0.25.0.tar.gz:

Publisher: wheels.yml on mccode-dev/mccode-antlr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mccode_antlr-0.25.0-py3-none-any.whl.

File metadata

  • Download URL: mccode_antlr-0.25.0-py3-none-any.whl
  • Upload date:
  • Size: 499.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for mccode_antlr-0.25.0-py3-none-any.whl
Algorithm Hash digest
SHA256 791757f94ddb25c514d95e32e21ac11b64a743ca5fbf01108ab19c6ff31778ab
MD5 fed17198b083a6e86cc4d94119661248
BLAKE2b-256 b44a15c3f4a10d868dfbada5f1c1ccbb7fc32de4e61561e58b68dfd7a7c78c05

See more details on using hashes here.

Provenance

The following attestation bundles were made for mccode_antlr-0.25.0-py3-none-any.whl:

Publisher: wheels.yml on mccode-dev/mccode-antlr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.28.0

2 files

0.27.0

2 files

0.26.0

2 files

This release

0.25.0 This release

2 files

0.24.0

2 files

0.23.0

2 files

0.22.0

2 files

0.21.0

2 files

0.20.3

2 files

0.20.2

2 files

0.20.1

2 files

0.20.0

2 files

0.19.0

2 files

0.18.9

2 files

0.18.8

2 files

0.18.7

2 files

0.18.6

2 files

0.18.5

2 files

0.18.4

2 files

0.18.3

2 files

0.18.2

2 files

0.18.1

2 files

0.18.0

2 files

0.17.2

2 files

0.17.1

2 files

0.17.0

2 files

0.16.2

2 files

0.16.1

2 files

0.16.0

2 files

0.15.2

2 files

0.15.1

2 files

0.15.0

2 files

0.14.0

2 files

0.13.0

2 files

0.12.0

2 files

0.11.0

2 files

0.10.3

2 files

0.10.2

2 files

0.10.1

2 files

0.10.0

2 files

0.9.3

2 files

0.9.2

2 files

0.9.1

2 files

0.9.0

2 files

0.8.4

2 files

0.8.3

2 files

0.8.2

2 files

0.8.1

2 files

0.8.0

2 files

0.7.4

2 files

0.7.3

2 files

0.7.2

2 files

0.7.1

2 files

0.7.0

2 files

0.6.1

2 files

0.6.0

2 files

0.5.3

2 files

0.5.2

2 files

0.5.1

2 files

0.5.0

2 files

0.4.1

2 files

0.4.0

2 files

0.3.3

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.11

2 files

0.2.10

2 files

0.2.9

2 files

0.2.8

2 files

0.2.7

2 files

0.2.6

2 files

0.2.5

2 files

0.2.4

2 files

0.2.3

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.2

2 files

0.1.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page