1   modparc

https://img.shields.io/pypi/v/modparc.svg https://img.shields.io/travis/xie-dongping/modparc.svg Documentation Status Updates

modparc is a Modelica parser in Python based on parser combinator.

1.1   Quickstart

Install the package from PyPI:

$ pip install modparc

To parse a Modelica source file “your_modelica_file.mo”:

import modparc
with open("your_modelica_file.mo", 'r') as f:
    modelica_source_code = f.read()
    model_definition = modparc.parse(modelica_source_code)

To use the model_definition instance:

all_equations = model_definition.search('Equation')
for equation in all_equations:
    print(equation.code())  # The code of the equation as string

One could also parse a certain syntax element in Modelica:

import modparc
from modparc.syntax import tokenize
source_code = """
              if init==InitializationOptions.FixedPopulation then
                population = initial_population;
              elseif init==InitializationOptions.SteadyState then
                der(population) = 0;
              else
              end if
              """
tokens_list = tokenize(source_code)
if_equation_element = modparc.syntax.equations.if_equation(tokens_list)
sub_equations = if_equation_element.search('Equation')
for equation in sub_equations:
    print(equation.code())  # The code of the equation as string

1.2   Features

  • Experimentally parses Modelica Standard Library 3.2.1
  • Search element of a certain class

1.3   Known Issues

  • Handling tokenization of Q-IDENT and comments, which comes first?
  • Assertion syntax not defined in modelica specification
  • Recursion limit (since no tail recursion optimization)
  • Test case for long vector literals
  • Default recursion depth is not enough for long vector literals
  • Cyclic import is neccessary for the Modelica syntax definition

1.4   Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.