API reference

OpenModelicaParser — Module
OpenModelicaParser

The Modelica parser of OM.jl, in two modules:

  • OpenModelicaParser.Absyn: the abstract syntax (omc's Absyn records);
  • OpenModelicaParser.Parser: a lexer (tokenize) and a recursive-descent parser with one function per grammar rule, written in Julia.

parseFile and parseString turn Modelica (or MetaModelica, ParModelica, Optimica, PDEModelica) source into an Absyn.Program, the same trees omc's parser builds. Errors are ParseErrors that say what was expected where. Modelica"..." (and @Modelica, which interpolates) does the same for Modelica source written in a Julia file.

source
OpenModelicaParser.Parser.parseFile — Function
parseFile(fileName::String, acceptedGram::Int64 = 1, languageStandard::Int64 = 9999)::Absyn.Program

Parse a Modelica file. Throws a ParseError (line 0 when the file cannot be read).

Language standards (languageStandard):

 ("1.x", 10), ("2.x", 20), ("3.0", 30), ("3.1", 31), ("3.2", 32), ("3.3", 33),
 ("3.4", 34), ("3.5", 35), ("latest", 1000), ("experimental", 9999)

Grammars (acceptedGram):

 1 = Modelica, 2 = MetaModelica, 3 = ParModelica, 4 = Optimica, 5 = PDEModelica
source
OpenModelicaParser.Parser.parseString — Function
parseString(contents::String, interactiveFileName::String = "<default>",
            acceptedGram::Int64 = 1, languageStandard::Int64 = 1000; firstLine::Int = 1)::Absyn.Program

Parse Modelica source text. interactiveFileName is the file name in the SourceInfos and in error messages, and the classes are marked read-only. The text ends at its first NUL character (as omc reads it). firstLine is the number of the text's first line: 1, or the line the text starts on in the file it is part of (Modelica"..." passes the line of the Julia file), so that the SourceInfos and the error messages give lines of that file. Throws a ParseError.

Language standards (languageStandard):

 ("1.x", 10), ("2.x", 20), ("3.0", 30), ("3.1", 31), ("3.2", 32), ("3.3", 33),
 ("3.4", 34), ("3.5", 35), ("latest", 1000), ("experimental", 9999)

Grammars (acceptedGram):

 1 = Modelica, 2 = MetaModelica, 3 = ParModelica, 4 = Optimica, 5 = PDEModelica
source
OpenModelicaParser.Parser.ParseError — Type
ParseError <: Exception

A lexical or syntax error: where it is (file, line, column of the first character, 1-based) and what is wrong. sourceLine is the text of the line, shown with a caret under the column. Line 0: the file could not be read.

source
OpenModelicaParser.@Modelica_str — Macro
Modelica"text"
Modelica"""text"""

Modelica source written in a Julia file, as its Absyn.Program.

program = Modelica"""
  model Decay
    Real x(start = 1.0);
  equation
    der(x) = -x;
  end Decay;
  """

The text is parsed when the macro is expanded: a syntax error is a ParseError when the file is loaded, and the program is a constant of the code (each evaluation gives the same object, nothing is parsed when the code runs). The SourceInfos and the error messages name the Julia file and its lines; columns count from the left margin of the text (Julia removes the indentation of a triple-quoted string).

The text is taken as written, as in a Modelica file: a dollar sign is a dollar sign and backslashes stay, so the escapes of a Modelica string ("one\ntwo") are written as there. The one exception is Julia's rule for string macros: a backslash before a double quote escapes the quote, so a quote escaped inside a Modelica string (backslash, quote) is written with two backslashes. Line ends are line feeds, also in a Julia file with CRLF line ends: Julia's parser reads every line end of a string literal as one.

A text built from values is written with @Modelica, which interpolates. A text held in a variable is parsed with parseString.

Julia gives a macro the line of its call, not the line of the text. A text of several lines is taken to start on the line after the call (a triple-quoted string opened on the line of the call), a text of one line on the line of the call; written otherwise, the lines are off by the difference.

The grammar is Modelica of the latest standard (the defaults of parseString).

source
OpenModelicaParser.@Modelica — Macro
@Modelica "text"

Modelica"..." with interpolation: a Modelica text built from values.

tank(name::String, n::Int) = @Modelica "model $name Real x[$n]; end $name;"

The text is put together and parsed each time the code runs (a syntax error is a ParseError then), with the positions of Modelica"...": the Julia file and its lines. The interpolated expressions are evaluated where the macro is called.

The text is a Julia string literal, so Julia's escapes apply before the parser sees it: a backslash of the Modelica text is written \\, a dollar sign \$. A text without interpolations is parsed when the macro is expanded, as with Modelica"...", which is the form to write such a text in.

Anything but a string literal is an ArgumentError when the macro is expanded: a text held in a variable is parsed with parseString.

source

Modules

OpenModelicaParser.Parser — Module
OpenModelicaParser.Parser

The lexer (tokenize, Lexer.jl, Tokens.jl) and the recursive-descent parser (Rules.jl, one function per rule of omc's grammar), with the entry points parseFile and parseString.

source

OpenModelicaParser.Absyn: see Absyn.