Skip to main content

A parser/formatter library that's easy to use and that provides informative error messages.

Project description

Parcon is a parser combinator library. It can be used for parsing both normal text and binary data. It’s designed to be easy to use and to provide informative error messages.

Pargen, which is provided as a submodule of Parcon, is a formatter combinator library. It’s much the opposite of Parcon: while Parcon is used to parse text into various objects, Pargen is used to format objects into text. As an example, if you wanted to reimplement Python’s json module, you would use Parcon to implement json.loads and Pargen to implement json.dumps.

Static, which is also provided as a submodule of Parcon, is a static typing library and Python object pattern library. It lets you build patterns that match Python objects based on their type, their attributes, certain properties such as whether or not the object is a sequence, the types that make up the object’s values if the object is a sequence, and so on.

All of the classes, and most of the functions, in Parcon are comprehensively documented. The best place to look for help is in Parcon’s module documentation. Pargen’s classes and functions and Static’s classes and functions are also documented, though not quite as well as Parcon’s are at present.

Here’s an example of a simple expression evaluator written using Parcon:

from parcon import rational, Forward, InfixExpr
import operator
expr = Forward()
number = rational[float]
term = number | "(" + expr + ")"
term = InfixExpr(term, [("*", operator.mul), ("/", operator.truediv)])
term = InfixExpr(term, [("+", operator.add), ("-", operator.sub)])
expr << term

This expression evaluator can be used thus:

print expr.parse_string("1+2") # prints 3
print expr.parse_string("1+2+3") # prints 6
print expr.parse_string("1+2+3+4") # prints 10
print expr.parse_string("3*4") # prints 12
print expr.parse_string("5+3*4") # prints 17
print expr.parse_string("(5+3)*4") # prints 32
print expr.parse_string("10/4") # prints 2.5

More examples can be found on Parcon’s blog, and more information in general can be found at Parcon’s website.

Parcon is currently much more comprehensively documented than Pargen and Static are. Improved documentation for Pargen and Static will come soon.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

parcon-0.1.21.tar.gz (100.6 kB view details)

Uploaded Source

File details

Details for the file parcon-0.1.21.tar.gz.

File metadata

  • Download URL: parcon-0.1.21.tar.gz
  • Upload date:
  • Size: 100.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for parcon-0.1.21.tar.gz
Algorithm Hash digest
SHA256 e7de062ff351939a246c780ab807c7524a3f1027516f0a80b1faa57d79a51b09
MD5 600fa36f3d2cfc1863188359bfb5d9a7
BLAKE2b-256 4b20250c17decf3dea4f70d77638d7d5e9323429caf2839f84cc69b5e9ee67ec

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page