dlang-community / Pegged

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

New line escape character handling #285

Closed Firstbober closed 4 years ago

Firstbober commented 4 years ago

Hello and thanks for this awesome library! I have problem with detecting new line characters, only thing i found in docs is about Spacing but this is not much help. Code:

Prm:
    Test < "test" identifier "\n\n"
    Spacing <- :(' ' / '\t' / '\r')*
    identifier <~ [a-zA-Z_] [a-zA-Z0-9_]*

Parsed file:

test abc
content...

Error:

Prm (failure)
 +-Prm.Test (failure)
    +-Prm.identifier [5, 9]["abc"]
    +-literal!("

") failure at line 1, col 0, after "test abc\n" expected "\"\n\n\"", but got "content..."

I edited Spacing to remove dropping of new line but it doesn't help

veelo commented 4 years ago

This works (can be tested in run.dlang.io):

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

enum PrmGram = `
Prm:
    Test <- "test" Spacing identifier "\n\n"
    Spacing <- :(' ' / '\t' / '\r')*
    identifier <~ [a-zA-Z_] [a-zA-Z0-9_]*`;

void main()
{
    mixin(grammar(PrmGram));
    writeln(Prm(`test abc

`));
}
Firstbober commented 4 years ago

Wow, now it works! Thanks.