YARP - Yet Another Recursive Parser.
Project description
YARP
YARP - Yet Another Recursive Parser
YARP provides a framework for easily creating recursive parsers in Python. Hand-built parsers are fast, flexible, and allow easy error detection and reporting when compared to parser generators that use EBNF.
Two examples of parsers created with YARP are provided in the examples directory.
JSON parsing example
Getting started
To create a parser, start by extending the Parser class. Use the @lexemes annotation to set the expected lexemes for the rule (and all rules until the lexemes are redefined).
json_lexemes = {'{', '}', ',', '[', ']', ':', 'true', 'false', 'null', '"', '\\'}
class JSONParser(Parser):
@lexemes(json_lexemes)
def parse(self):
super().parse()
self.value() #parse a json value
AST production rules
Rules that add nodes to the abstract syntax tree can be created using the @ast annotation. The rule below will parse the lexeme 'true' and add a node to the AST. If this rule is encountered and a token mismatch occurs, a syntax error will be printed along with information about where in the token stream it occurred. Note that the @ast annotation also allows for optional rules with the 'optional' parameter.
@ast("true", require=['true'], description="boolean")
def true_val(self, tokens):
pass
Pattern matching
You can match patterns forward into the token stream without consuming tokens. This is essentially the lookahead functionality from other parser generators.
@ast("array", require=['['], description="array")
def json_array(self, tokens):
self.consume_whitespace()
while (not self.match_pattern(']')):
self.value()
self.accept(',')
self.accept(']')
Rules with alternatives
Rules with alternatives can be added using the parse_alternatives function. This uses pattern matching (of one or more tokens) to determine which production rule to use. If no alternative is valid, then a syntax error will occur with the provided description of what was expected.
@ast("value")
def value(self):
self.consume_whitespace()
self.parse_alternatives([(['{'], self.json_object),
(['['], self.json_array),
(['"'], self.string),
(['true'], self.true_val),
(['false'], self.false_val),
(['null'], self.null_val),
([RegularExpression(NUMBER_REGEX)], self.number)],
"value")
self.consume_whitespace()
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file yarp_parser-0.2.tar.gz.
File metadata
- Download URL: yarp_parser-0.2.tar.gz
- Upload date:
- Size: 12.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f6b2582128275c31ad22b2c51fa25ec1b24d23adc72ef9395feaa81f9f24a6d
|
|
| MD5 |
b12f72d26bb79fb8fe69a493c17cd051
|
|
| BLAKE2b-256 |
c984a17c8a1f33179b7d77b71c396ee11646e5f8a04dbcaeb9c2ada2a8741bf3
|
File details
Details for the file yarp_parser-0.2-py3-none-any.whl.
File metadata
- Download URL: yarp_parser-0.2-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4882cdda52f5a1877f4af3c69bc91f8b259ac4ce2aeb6027d3e080b1f44e823d
|
|
| MD5 |
8deb98ab845995fd9b557b90bea4a3df
|
|
| BLAKE2b-256 |
c3e765fc8a8dd01b386ab66e5f113bb4961b37eadb65e6525c3fbb424dda6dd2
|