Skip to main content

A type-safe applicative parsing library

Project description

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().

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.

Some operator overloading weirdness

This library overloads the operators *, <, >, and so on to implement parser combinators. Usually this results in more readable parsers, but there are some quirks in order of evaluation whiich results in unexpected behavior. For example, let's take three parsers, a, b, and c, which parse the strings "a", "b", and "c", respectively. Then one would expect a > b < c to parse "abc" to the string "b". This is because > parses the left parser and discards the result, and proceeds to parse the right parser. Similarly for <. However, a > b < c produces a failure on "abc" with the error message String "abc" does not start with "b". The parser succeeds on "bca" with result "b" and remainder "a".

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

functional_parsing_library-0.0.14.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

functional_parsing_library-0.0.14-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

Details for the file functional_parsing_library-0.0.14.tar.gz.

File metadata

File hashes

Hashes for functional_parsing_library-0.0.14.tar.gz
Algorithm Hash digest
SHA256 29e16fbb9eff2216d9231eba6e5d0949efb5b506869212f79d92e3dec06093cd
MD5 5a4741210e6376d8b091762acfc0c408
BLAKE2b-256 7c5036863ea953654797e28797ff46b8e8277edabf5d3b702ad0496bb4c6bede

See more details on using hashes here.

File details

Details for the file functional_parsing_library-0.0.14-py3-none-any.whl.

File metadata

File hashes

Hashes for functional_parsing_library-0.0.14-py3-none-any.whl
Algorithm Hash digest
SHA256 dcff7e8b36270487cbd38044d8bc8fb30451c3e36a25e20ca672a75168336877
MD5 d8c3b7baa98e7fe9edd299306da214dc
BLAKE2b-256 15f581754834594f91318d98d3827e1c29b038b2745f98446b3eba3b70d59bec

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