mo-parsing
mo-parsing is a Python PEG (Parsing Expression Grammar) parsing tool. It is a fork of `pyparsing`, designed for faster parsing, and allows users to define parsers using predefined patterns and Python operators. The library is currently at version 8.694.25301, with its latest release uploaded on October 27, 2025 (likely 2024, indicating active development with frequent updates).
Common errors
-
TypeError: 'NoneType' object is not callable (when a parse action doesn't seem to run)
cause You likely applied a parse action to a ParserElement without reassigning the result, e.g., `some_parser.add_parse_action(my_func)` instead of `some_parser = some_parser.add_parse_action(my_func)`.fixRemember that `ParserElement` methods like `add_parse_action` return a *new* ParserElement object. Always assign the result back to the variable: `my_parser = my_parser.add_parse_action(my_func)`. -
mo_parsing.exceptions.ParseException: Expected 'something_else' (when parser consumes too much or wrong input)
cause A common cause is misusing `Word(chars)` where `Literal(exact_string)` or `Keyword(exact_word)` was intended. `Word('abc')` will match 'apple', 'banana', 'cat' because 'a', 'b', 'c' are in its character set, not just 'abc'.fixIf you need to match a specific string exactly, use `Literal('your_string')`. If it must be a whole word, use `Keyword('your_word')`. -
mo_parsing.exceptions.ParseException: Expected ... (when trying to match a literal phrase with spaces)
cause Using `Literal('multiple words')` will attempt to match the entire string literally, including spaces, without allowing for intervening whitespace skipping behavior.fixFor phrases with spaces that should allow for arbitrary whitespace (and comments if configured), split them into separate `Literal` or `Keyword` elements: `Literal('first') + Literal('second')`.
Warnings
- breaking ParserElements are static. Methods like `.add_parse_action(action)` create and return a *new* ParserElement. If the result is not assigned back to a variable, the parse action (or any other modification) will be lost.
- breaking Whitespace handling has been refactored. `mo-parsing` uses the `mo_parsing.whitespaces.Whitespace` context manager to define ignored characters, replacing `pyparsing`'s global `whitespace_chars` or `parser.set_whitespace_chars()` methods, which are removed.
- gotcha The parameter order for parse actions is `(tokens, index, string)`, which is the opposite of `pyparsing`'s `(string, location, tokens)`.
- gotcha Do not confuse `Word()`, `Literal()`, and `Keyword()` for exact string matching. `Word(string_of_characters)` matches any contiguous sequence of *characters found within* `string_of_characters`, not the exact `string_of_characters` itself.
Install
-
pip install mo-parsing
Imports
- Word
from mo_parsing import Word
- Literal
from mo_parsing import Literal
- Keyword
from mo_parsing import Keyword
- Group
from mo_parsing import Group
- Regex
from mo_parsing import Regex
- alphas
from mo_parsing.utils import alphas
- Whitespace
from mo_parsing import Whitespace
from mo_parsing.whitespaces import Whitespace
Quickstart
from mo_parsing import Word, Literal
from mo_parsing.utils import alphas
from mo_parsing.whitespaces import Whitespace
# Define a simple grammar for 'Hello, World!' with a custom whitespace rule
with Whitespace(' \t') as whitespace_context:
greet = Word(alphas)('greeting') + Literal(',') + Word(alphas)('person') + Literal('!')
# Parse a string
result = greet.parse_string('Hello, World!')
# Access results as a list
print(f"List view: {list(result)}")
# Access results as a dictionary (for named tokens)
print(f"Dict view: {dict(result)}")
# Example with different input
result_named = greet.parse_string('Hi, There!')
print(f"Named tokens: {result_named.greeting}, {result_named.person}")