Another PEG Parsing Tool
Maintained. Last release 3 months ago, commits in 1 of the last 26 weeks. Weaker on commit activity and contributor spread.
pip install mo-parsingMore Parsing!
A fork of pyparsing for faster parsing
Installation
This is a pypi package
pip install mo-parsing
Usage
This module allows you to define a PEG parser using predefined patterns and Python operators. Here is an example
>>> from mo_parsing import Word
>>> from mo_parsing.utils import alphas
>>>
>>> greet = Word(alphas)("greeting") + "," + Word(alphas)("person") + "!"
>>> result = greet.parse_string("Hello, World!")
The result can be accessed as a nested list
>>> list(result)
['Hello', ',', 'World', '!']
The result can also be accessed as a dictionary
>>> dict(result)
{'greeting': 'Hello', 'person': 'World'}
Read the pyparsing documentation for more
The Whitespace Context
The mo_parsing.whitespaces.CURRENT is used during parser creation: It is effectively defines what "whitespace" to skip during parsing, with additional features to simplify the language definition. You declare "standard" Whitespace like so:
with Whitespace() as whitespace:
# PUT YOUR LANGUAGE DEFINITION HERE (space, tab and CR are "whitespace")
If you are declaring a large language, and you want to minimize indentation, and you are careful, you may also use this pattern:
whitespace = Whitespace().use()
# PUT YOUR LANGUAGE DEFINITION HERE
whitespace.release()
The whitespace can be used to set global parsing parameters, like
set_whitespace()- set the ignored characters (default:"\t\n ")