Ha, I wonder what that makes the rest of us.
It seems even the creator of Python had the same commentary. PEP 617 – New PEG parser for CPython | peps.python.org .
Looks like python is changing their internal parser to PEG. From some cursory research, https://janet-lang.org/ has PEG by default instead of PCRE/Regex in the standard library.
After all this exploration, I’m surprised Pegasus isn’t more popular. I’m guessing it might be because people don’t quite understand how to use it despite its awesomeness (or they don’t need to write grammars).Or it might be because it’s not so easy to do a mental-model replacement of Regex. A ton of people use nimble_parsec though it looks like, so it’s probably just a ergonomics thing. Here are some of my first impressions here while I was trying to get it working.
From the docs:
- parser options [:collect, :token, :tag, :post_traverse, :ignore] work on elements of a PEG, but I didn’t figure that out until I read the code from the other repos.
- parser options [:start_position, :export, :parser, :alias] seem to operate on the data and I’m not sure what they are for.
I was just trying to figure out how to turn the functions in Pegasus into something I understood from other languages aka:
- Regex.capture
- Regex.match
So I had to go thru your other packages to understand that I actually wanted defparsec because I first tried using [parser: true, export: true].
Regarding captures, I think the issues I had are just documentation related. I ended up going thru your other codebases to figure out that I needed to use [:tag, :collect, :post_traverse]. There’s a little more boilerplate than regex, but I don’t think that can be gotten rid of. But perhaps the default should be [collect: true]? It was very confusing to see a series of characters.
Regarding match, I added a bit of extra boilerplate to get the equivalent functionality of Regex.match.
Definitely, would be nice to have both a match and capture setup that worked cleanly after putting a grammar string in.
@email_options [
Name: [tag: :name],
Domain: [tag: :domain],
At: [ignore: true],
TLD: [tag: :tld],
Dot: [ignore: true]
]
Pegasus.parser_from_string(
"""
Email <- Name At (Domain)+ TLD
Name <- ([a-zA-Z0-9_\.\\-]+)
Domain <- ([A-Za-z0-9\\-]+ Dot)+
TLD <- ([A-Za-z\.]) ([A-Za-z\.])+
Dot <- '.'
At <- '@'
""",
@email_options
)
defparsec :parse_email, parsec(:Email)
def peg_match_email(email) do
case parse_email(email) do
{:ok, result, "", _, _, _} -> :ok
_ -> :error
end
end
Anyway, thank you and great work. I’m halfway done writing a parser for semi-structured text, if it’s useful, I could take some notes on what was tough to figure out.






















