pyfcstm.dsl.parse
Grammar parsing utilities for the pyfcstm domain-specific language.
This module provides helper functions for parsing grammar-based input text using ANTLR4-generated lexer/parser classes and a parse-tree listener that converts ANTLR parse trees into internal node objects. It centralizes the parsing workflow for different grammar entry points and exposes convenience functions for parsing common DSL constructs such as conditions, preamble programs, and operation programs.
The module contains the following main components:
parse_with_grammar_entry()- Parse text using an arbitrary grammar entry rule.parse_condition()- Parse a condition expression.parse_state_machine_dsl()- Parse a complete state machine DSL document.parse_preamble()- Parse a preamble program.parse_operation()- Parse an operation program.
Note
All parsing relies on the ANTLR4-generated classes from
pyfcstm.dsl.grammar and may raise pyfcstm.dsl.error.GrammarParseError
when input does not conform to the grammar.
Example:
>>> from pyfcstm.dsl.parse import parse_condition, parse_preamble
>>> condition = parse_condition("x > 5 && y < 10")
>>> preamble = parse_preamble("x := 10;")
parse_with_grammar_entry
- pyfcstm.dsl.parse.parse_with_grammar_entry(input_text: str, entry_name: str, force_finished: bool = True) Any[source]
Parse input text using a specified grammar entry point.
This function looks up a parser method on
GrammarParserby name and parses the provided input text using that rule.- Parameters:
input_text (str) – The text to parse.
entry_name (str) – Name of the grammar rule to use as the entry point.
force_finished (bool, optional) – Whether to enforce that parsing consumed all input, defaults to
True.
- Returns:
Parsed node for the grammar entry.
- Return type:
Any
- Raises:
AttributeError – If
entry_namedoes not exist onGrammarParser.pyfcstm.dsl.error.GrammarParseError – If parsing fails or input is left unparsed when
force_finishedisTrue.
Example:
>>> result = parse_with_grammar_entry("x > 5", "condition")
parse_condition
- pyfcstm.dsl.parse.parse_condition(input_text: str) Any[source]
Parse input text as a condition expression.
This function uses the grammar’s
conditionrule as the entry point.- Parameters:
input_text (str) – The condition expression to parse.
- Returns:
Parsed condition node.
- Return type:
Any
- Raises:
pyfcstm.dsl.error.GrammarParseError – If parsing fails.
Example:
>>> condition_node = parse_condition("x > 5 && y < 10")
parse_state_machine_dsl
- pyfcstm.dsl.parse.parse_state_machine_dsl(input_text: str) StateMachineDSLProgram[source]
Parse input text as a complete state machine DSL document.
This helper uses the grammar’s
state_machine_dslrule and returns a pure DSL AST.- Parameters:
input_text (str) – The state machine DSL text to parse.
- Returns:
Parsed state machine DSL program node.
- Return type:
- Raises:
pyfcstm.dsl.error.GrammarParseError – If parsing fails.
Example:
>>> program = parse_state_machine_dsl("state Root;") >>> program.root_state.name 'Root'
parse_preamble
- pyfcstm.dsl.parse.parse_preamble(input_text: str) Any[source]
Parse input text as a preamble program.
This function uses the grammar’s
preamble_programrule as the entry point.- Parameters:
input_text (str) – The preamble program text to parse.
- Returns:
Parsed preamble program node.
- Return type:
Any
- Raises:
pyfcstm.dsl.error.GrammarParseError – If parsing fails.
Example:
>>> preamble_node = parse_preamble("x := 10;")
parse_operation
- pyfcstm.dsl.parse.parse_operation(input_text: str) Any[source]
Parse input text as an operation program.
This function uses the grammar’s
operation_programrule as the entry point.- Parameters:
input_text (str) – The operation program text to parse.
- Returns:
Parsed operation program node.
- Return type:
Any
- Raises:
pyfcstm.dsl.error.GrammarParseError – If parsing fails.
Example:
>>> operation_node = parse_operation("x := 10;")