dlang-community / Pegged

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

Unable to parse an Identifier starting with a keyword #245

Closed ghost closed 6 years ago

ghost commented 6 years ago

This is more a "help wanted" issue than a bug report. The problem is that when i exclude keywords from the identifiers an identifier cannot start with a keyword:

/+dub.sdl:
dependency "pegged" version="~>0.4.2"
+/
import pegged.grammar;
import std.stdio;

mixin(grammar(`
Test:
    Rule1 < (Identifier / Keyword)*
    Identifier  <~ !Keyword [a-zA-Z_] [a-zA-Z0-9_]*
    Keyword < "is" / "as"
`));

void main()
{
    enum parseTree1 = Test("is isChecked as");
    pragma(msg, parseTree1.matches);
    assert(parseTree1.matches == ["is", "isChecked", "as"]);
    writeln(parseTree1);
}

https://run.dlang.io/is/VGae1P

PhilippeSigaud commented 6 years ago

Try with this rules:

    Rule1 < (Keyword / Identifier)*
    Keyword <- ("is" / "as") !Identifier
    Identifier  <~ [a-zA-Z_] [a-zA-Z0-9_]*

Three changes :

https://run.dlang.io/is/f2A4Ws

(Man, this run.dlang.io site is quite cool!)

On Tue, May 8, 2018 at 4:05 PM, BBasile notifications@github.com wrote:

This is more a "help wanted" issue than a bug report. The problem is that when i exclude keywords from the identifiers an identifier cannot start with a keyword:

/+dub.sdl:dependency "pegged" version="~>0.4.2"+/import pegged.grammar;import std.stdio; mixin(grammar(Test: Rule1 < (Identifier / Keyword)* Identifier <~ !Keyword [a-zA-Z_] [a-zA-Z0-9_]* Keyword < "is" / "as")); void main() { enum parseTree1 = Test("is isChecked as"); pragma(msg, parseTree1.matches); assert(parseTree1.matches == ["is", "isChecked", "as"]); writeln(parseTree1); }

https://run.dlang.io/is/VGae1P

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/PhilippeSigaud/Pegged/issues/245, or mute the thread https://github.com/notifications/unsubscribe-auth/AAjYpx4T5lGq-mVu8ys7J23yuW-PVLhSks5twaYmgaJpZM4T2tS1 .

ghost commented 6 years ago

Thanks, adding !Identifier to the Keyword in the real stuff works.