/Nompy

A Python parser combinator library similar to Nom for Rust

Primary LanguagePython

Nompy

A Python parser combinator library similar to the Nom library in Rust.

Examples

Parse Name

Parse a name and apply a simple transformation.

from nompy.combinators import succeeded, tag, take_rest, take_until, tuple_
from nompy.modifiers import apply

to_parse = "john doe"

parser = tuple_(
    apply(succeeded(take_until(" "), tag(" ")), str.capitalize),
    apply(take_rest(), str.capitalize),
)
result, remaining = parser(to_parse)
firstname, lastname = result
print(firstname, lastname)  # John Doe

Parse Phone Number

Parse an MSISDN with preceeding +

from nompy.combinators import preceeded, tag, take_while

to_parse = "+1234567890"

parser = preceeded(take_while(str.isnumeric), tag("+"))
result, remaining = parser(to_parse)
print(result)