robrix / Madness

Recursive Descent Into Madness
MIT License
291 stars 17 forks source link

`not` combinator #111

Closed bencochran closed 8 years ago

bencochran commented 8 years ago

Implements a negative lookahead combinator as described in #51.

I started with it called notFollowedBy, but most of my uses look something like many(not(something) *> any) and having notFollowedBy(something) *> any felt confusing.

This could also yield a subtraction combinator (#59) via not(right) *> left if we want one.

robrix commented 8 years ago

This is great, thank you!

robrix commented 8 years ago

I wonder if we should document that you can run into trouble with e.g.:

many(not(something))

Since not doesn’t advance the cursor, it can infinite loop. Alternatively, maybe many should not repeat at the same index.

Any thoughts?

bencochran commented 8 years ago

That’s a good catch.

Poked around at making many (and therefore also some) fail if they don’t consume any input. I’m not convinced it’s the cleanest approach, so I may come take a second look at it, but wanted to get it up here for others’ eyes in the meantime.

bencochran commented 8 years ago

Ok, coming back for a second ponder: the current approach is sitting alright with me. The only hesitation I have is wondering if an error is the correct choice. That is, I could see an argument that some(not(x)) should match once then bail (that is, it would effectively produce [()]). But that feels like it would be uglier to implement, and I’m not convinced there’s a real reason for that behavior above this one.

robrix commented 8 years ago

:+1: Thanks @bencochran. This is looking really good.

bencochran commented 8 years ago

Awesome, thank you!