dlang-community / Pegged

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

How do you branch on complex names such as caseSensitiveLiteral!("let") (they get much more complicated) #329

Open enjoysmath opened 1 year ago

enjoysmath commented 1 year ago

So with the usual swith-case to handle the parse tree (do something with it), I'm having trouble seeing how to branch based on these long names. One solution I had was a pre-switch if-then statement that checked if "SomeText" is a substring of the name. I'm not liking this solution!

veelo commented 1 year ago

You define a rule in your grammar for the keywords:

Basic:
  LET <- "let"
switch (p.name)
{
    case "Basic:LET":
        // ...
        break;
}

If you don't use grammar composition, I suppose you can do

switch (p.name[6..$])
{
    case "LET":
        // ...
        break;
}