PyMeta3

0.5.1 · maintenance · verified Mon Apr 13

PyMeta3 is a Python 3 compatible fork of PyMeta, an implementation of OMeta, an object-oriented pattern-matching language. It provides a compact syntax based on Parsing Expression Grammars (PEGs) for common lexing, parsing, and tree-transforming activities in a way that's easy to reason about for Python programmers. The current version is 0.5.1, with the last update released in February 2015, suggesting a low release cadence.

Warnings

Install

Imports

Quickstart

This example demonstrates how to define a simple grammar using PyMeta3, create a grammar object, and apply a rule to an input string. It defines rules 'ones' and 'twos' to match sequences of '1's and '2's respectively, and a 'stuff' rule that matches one or more occurrences of 'ones' or 'twos'. The `apply` method attempts to parse the input according to the specified rule and returns the result and any error encountered.

from pymeta.grammar import OMeta

exampleGrammar = """
ones ::= '1' '1' => 1
twos ::= '2' '2' => 2
stuff ::= (<ones> | <twos>)+
"""

Example = OMeta.makeGrammar(exampleGrammar, {})
g = Example("11221111")
result, error = g.apply("stuff")

print(f"Result: {result}")
print(f"Error: {error}")

view raw JSON →