J-F-Liu / pom

PEG parser combinators using operator overloading without macros.
MIT License
496 stars 30 forks source link

add whitespace-aware example parser using >> #30

Closed blakehawkins closed 5 years ago

blakehawkins commented 5 years ago

Greetings

The point of this example is to demonstrate how to make whitespace-aware parsers in pom.

I created this as a proof of concept when I wasn't sure if such a job was possible with pom's API.

It works by using >> and take(0) to "tap" the parser and inject the output of an inner parser.

In the process, I had some struggles with lifetimes, and nearly changed parser.rs to use FnMut, but eventually saw how it's possible without any changes needed.

High-level description is something like this:

Parse a white-space aware grammar by recursively parsing blocks of lines with the same indentation level

J-F-Liu commented 5 years ago

This code seems still not optimal.

blakehawkins commented 5 years ago

Yes, but I don't have any great ideas to improve it. Let me know if you think of something.

I am happy that it's possible without changes to pom, at least.