Another PEG Parsing Tool
Project description
More Parsing!
An experimental fork of pyparsing
| Branch | Status |
|---|---|
| master | |
| dev |
Summary of Differences
This has been forked to experiment with faster parsing in the moz-sql-parser.
More features
- Added
Engine, which controls parsing context and whitespace (a basic lexxer) - faster infix operator parsing (main reason for this fork)
- ParseResults point to ParserElement for reduced size
- packrat parser is always on
- less stack used
- the wildcard ("
*") could be used to indicate multi-values are expected; this is not allowed: all values are multi-values - all actions are in
f(token, index, string)form, which is opposite of pyparsing'sf(string, index token)form
More focused
- removed all backward-compatibility settings
- no support for binary serialization (no pickle)
More functional
- ParseResults are static, can not be changed, parsing functions must emit new objects
- ParserElements are static: Many are generated during language definition
Details
The Engine
The mo_parsing.engine.CURRENT is used during parser creation: It is effectively the lexxer with additional features to simplify the language definition. You declare a standard Engine like so:
with Engine() as engine:
# PUT YOUR LANGUAGE DEFINITION HERE
If you are declaring a large language, and you want to minimize indentation, and you are careful, you may also use this pattern:
engine = Engine().use()
# PUT YOUR LANGUAGE DEFINITION HERE
engine.release()
The engine can be used to set global parsing parameters, like
set_whitespace()- set the ignored characters (like whitespace)add_ignore()- include whole patterns that are ignored (like comments)set_debug_actions()- insert functions to run for detailed debuggingset_literal()- Set the definition for whatLiteral()meansset_keyword_chars()- For defaultKeyword()
The engine.CURRENT is added to every parse element created, and it is used during parsing to packrat the current parsed string.
Navigating ParseResults
ParseResults are in the form of an n-ary tree; with the children found in ParseResults.tokens. Each ParseResult.type points to the ParserElement that made it. In general, if you want to get fancy with post processing (or in a parseAction), you will be required to navigate the raw tokens to generate a final result
There are some convenience methods;
__iter__()- allows you to iterate through parse results in depth first search. Empty results are skipped, andGrouped results are treated as atoms (which can be further iterated if required)nameis a convenient property forParseResults.type.token_name__getitem__()- allows you to jump into the parse tree to the givenname. This is blocked by any names found insideGrouped results (because groups are considered atoms).
addParseAction
Parse actions are methods that are run after a ParserElement found a match.
- Parameters must be accepted in
(tokens, index, string)order (the opposite of pyparsing) - Parse actions are wrapped to ensure the output is a legitimate ParseResult
- If your parse action returns
Nonethen the result is the originaltokens - If your parse action returns an object, or list, or tuple, then it will be packaged in a
ParseResultwith same type astokens. - If your parse action returns a
ParseResultthen it is accepted even if is belongs to some other pattern
- If your parse action returns
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file mo-parsing-4.12.21016.tar.gz.
File metadata
- Download URL: mo-parsing-4.12.21016.tar.gz
- Upload date:
- Size: 57.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ecd5297e6c8be3850fae4496985bf553a42742d361ec5c349643a1a8989c6cb
|
|
| MD5 |
c3c39e7e695045291c9e92df4cc1fc59
|
|
| BLAKE2b-256 |
9a66d0c6ba7081a1c3ea2991a6b6161f27855b248f41a2d40f80618edcef8e74
|