functional_parsing_library
A small production non-ready Python library implementing basic applicative parsers. Roughly speaking, these are functions
with signature str -> T | CouldNotParse transforming strings into structured data. For example, you might have a function
integer which will transform "1" and "-1919" to the integers 1 and -1919, and the string "boink" to CouldNotParse().
For an extensive example of how to use this library, see this blog post.
What makes these functions useful is that they can be combined with so-called parser combinators. This way, complicated parsers
can be gradually built up from smaller, simpler parsers.
For example, if we already have parsers nonnegative_integer and negative_integer, the integer parser from earlier
could be written as integer = nonnegative_integer | negative_integer, where | should be read as "or". This library
implements various such combinators, such as many, some, ignore_left, many_till, and so on.
Another piece of structure that makes these functions useful is that they're functorial: If I have a parser p of type
Parser[T] (that is, a function which parses strings to objects of type T), and a function f: T -> S, then f * p
will be a parser for objects of type S. For example, take len * many(word('borf')), and try to parse "borfborfborf".
Here word('borf') will parse "borf" to the string "borf" (and any other string to CouldNotParse), so the parser
many(word('borf') will try and match as many "borf"s as possible and parse our string to the list ['borf', 'borf', 'borf'].
The length of this list is 3, so len * many(word('borf')) parses our string to the integer 3.
This works with multi-argument functions as well. If f is a function of type [T, S] -> U, and we have parsers p
and q for objects of type T and S, then f * p & q will first try to match p, and if this succeeds it will try
and match q, and finally it will apply f.
Another feature of this library is its type safety. Running mypy on
from functional_parsing_library.strings import word
def add_strings(one: str, two: str, three: str) -> int:
return len(one + two + three)
reveal_type(add_strings * word('hi'))
reveal_type(add_strings * word('hi') & word('hi'))
reveal_type(add_strings * word('hi') & word('hi') & word('di'))
will show that the first parser has type MappedParser[int, str, str], the second MappedParser[int, str], and the
third Parser[int]. Expressions like
add_strings * word('hi') & word('hi') & integer
or
add_strings * word('hi') & word('hi') & word('hi') & word('hi')
will raise a TypeError, and mypy will catch this.
Documentation
To see some documentation, clone this repo, run
make serve-documentation
and in your browser you can peruse this library's docstrings at port 8000.
TODO list
- Backport to earlier Python versions, say 3.9 and up.
- Ambiguity in parsing, for example
char('a') | word('ab')should parse"ab"as both"a"with remainder"b"and as"ab"with no remainder. - Syntactic sugar for monadic structure of
Parser. Probably context managers can be used to craft some makeshift Haskell-likedonotation. - if
pis a parser for some unbound typevarU, andfa function fromUtoAny, mypy does not deal well with the expressionf * p.
Variable number of arguments
As it stands, mapping over parsers cannot be done with functions accepting a variable number of arguments. For example,
from functional_parsing_library.strings import any_char
def f(*x: str) -> str:
return ''
f * any_char & any_char
results in an internal mypy error for mypy==1.10.1 and below. From mypy==1.11.0, the internal error is gone, and the
snippet results in the following type error:
Unsupported operand types for & ("MappedParser[str, Never, *tuple[Never, ...]]" and "Parser[str]") [operator]
Metadata
Release files for functional-parsing-library 0.0.29
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| functional_parsing_library-0.0.29.tar.gz | 15.5 kB | With provenance | Details for functional_parsing_library-0.0.29.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| functional_parsing_library-0.0.29-py3-none-any.whl | Python 3 | none | any | Details for functional_parsing_library-0.0.29-py3-none-any.whl |
Total release size: 44.3 kB
Release files / functional_parsing_library-0.0.29.tar.gz
| Download URL | functional_parsing_library-0.0.29.tar.gz |
|---|---|
| Size | 15.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
134465a021706f2718562bc69b5b9efb752773681186cd7cfea1cdfb633c87ec
|
|
BLAKE2b-256 checksum How to use checksums |
e9446269052dc86ab60c35fc2f3d78716b92cfa507b7fa29f22c9affe854fab6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 12, 2025.
Transparency logRelease files / functional_parsing_library-0.0.29-py3-none-any.whl
| Download URL | functional_parsing_library-0.0.29-py3-none-any.whl |
|---|---|
| Size | 28.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b01ccfa12d832e20b3033e1acaec52ba354a45335695f98d7c71c7b86c04bb15
|
|
BLAKE2b-256 checksum How to use checksums |
f88cdc15a4d78ae254e3db970c4c4de589649c6051fdd0443136611a9d8db14a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 12, 2025.
Transparency logSecurity for functional-parsing-library 0.0.29
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for functional_parsing_library-0.0.29.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 12, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.