dlang-community / Pegged

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

rules & enums & final switch usage #156

Open Robert-M-Muench opened 9 years ago

Robert-M-Muench commented 9 years ago

Hi, I have written a quite complex set of templates, mixins, and conversions to derive an enum from the rules, that can be used in a final switch statement so that the compiler will catch cases that are not handled.

Like this:

// generate ENUM with all our rules for final switch
mixin(generateEnum!MyGrammer("rules"));

// reduce string rule name to enum member, special handling of base rule
auto rule_name = p.name.replaceFirst("MyGrammer.","");
if(rule_name == "MyGrammer") {rule_name = "empty";}

// convert string to enum type
final switch (rule_name.to!rules) {
    case rules.empty: ... 
    case rules.pBlock: ...
    case rules.pValues: ...
    ...

I think being able to use final switch with a PEG to handle cases would be a very useful generic pattern.

  1. Is there a better way to get the same effect with what's already available in PEG?
  2. How about adding something like this to PEG so that one can reference an enum directly?
PhilippeSigaud commented 9 years ago

Hi Robert,

I understand your idea and find it quite interesting (having the compiler check things with a final switch is a very good idea) But I'm halas totally not able to answer your questions. I'm afraid I haven't used D for quite a long time now (I don't even have a D compiler on my current machine). I would have to take quite a long time to find again how the Pegged code is structured and see how to integrate your code. I cannot help right now, sorry.

Of course, if you find the time to integrate your idea into Pegged, I'd be delighted to push it into the github repo and update the version for Dub to pick it.

I'm sorry for not being able to contribute to your idea. It seems my coding years are passed :-(

Robert-M-Muench commented 9 years ago

Hi Philippe, you created such a nice tool with PEG and don't use D anymore? Hmm... what happend? Your coding years shouldn't have passed... we need everyone that creates cool D stuff.

I haven't looked into the PEG code (yet) but would expect that it's not very easy. Do you have any notes, design information etc. that could help to get into it?

PhilippeSigaud commented 9 years ago

Hi Philippe, you created such a nice tool with PEG and don't use D anymore? Hmm... what happend? Your coding years shouldn't have passed...

Oh, just Real Life (tm). Job changes, interesting work opportunities, more responsibility, more children, etc. Coding is (was) a hobby for me, not my day job.

I haven't looked into the PEG code (yet) but would expect that it's not very easy. Do you have any notes, design information etc. that could help to get into it?

Halas no. Just many wiki pages on the github site and the git history, but the latter is not very informative.