dlang-community / Pegged

A Parsing Expression Grammar (PEG) module, using the D programming language.
533 stars 66 forks source link

Add equivalent of “important” blocks? #298

Open LunaTheFoxgirl opened 3 years ago

LunaTheFoxgirl commented 3 years ago

I have some grammar that needs to be able to repeat, it would be nice to have a tag you could wrap some grammar in to to force an error to be output instead of discarded when * or + is used. Eg.

MyGrammar:
Body < Item+
Item <- :'#' {:'(' identifier :')'}

Would not throw an error if # was not encountered, but would if # was encountered followed by a malformed grammar inside the {}

#(MyItem)
^ Would not error

#MyItem2)
^ Expected '(' got 'M'

(MyItem2)
^ Body would be considered terminated as no # was found

That would make error handling a lot more useful for grammars that contain statements in a non recursive fashion.

veelo commented 3 years ago

I don't quite understand what you propose. Maybe you can start with a currently working example, and describe what you don't like about its behaviour. Note that {...} are reserved for semantic actions. Poor error messages are a known limitation of Pegged, error reporting is probably the hardest part of a parser generator.

LunaTheFoxgirl commented 3 years ago

I am unable to provide a "working example" at current time, but essentially what I want is a way to force pegged to stop parsing and output an error if a rule fails. And yeah, I later realised that { and } are already taken.

But yeah, what I need is a way to tell pegged a way to halt parsing and display an error if a rule fails, without using semantic actions + throwing exceptions from that.

veelo commented 3 years ago

I think there is a way to get what you want using parser composition and semantic actions. So why wouldn't you want to use semantic actions?