kswoll / npeg

This parser is an implementation of a Packrat Parser with support for left-recursion. The algorithm for left recursion is a modified version of Packrat parsers can support left recursion.
MIT License
16 stars 5 forks source link

"Peek" operator? #2

Open zatherz opened 7 years ago

zatherz commented 7 years ago

Is there any way to parse only when a character is found, but don't return it? For example, with the quoted string example, I'd like to return the actual inner string. "abc" would return the string abc, "a\"bc" would return the string a"bc, etc.

kswoll commented 7 years ago

Is this what you're looking for?

var body = (-(!('"'._() | @"\") + Peg.Any | @"\\" | @"\""")).Capture();
var pattern = '"' + body + '"';

Console.WriteLine(pattern.Parse("\"abc\"")[body]);
Console.WriteLine(pattern.Parse("\"a\\\"bc\"")[body]);

Prints out:

abc a\"bc

zatherz commented 7 years ago

Hey, how do I make that work with this? It still prints the "s around. Also, how can I make inserting a C# string "\"abc\\\"\"" result in abc" instead of abc\"?

public virtual Expression Keyword() {
    var alnum = +('A'.To('Z') | 'a'.To('z') | '0'.To('9'));
    // https://github.com/kswoll/npeg/issues/2
    var quotedbase = (-(!('"'._() | @"\") + Peg.Any | @"\\" | @"\""")).Capture();
    return alnum | ('"' + quotedbase + '"');
}

Sorry for all the questions but I'm only somewhat used to lpeg in lua, npeg works a bit differently.

kswoll commented 7 years ago

Can you show me the code where you're printing out the result (that still shows the "s)?

zatherz commented 7 years ago

Yes, it's here: https://gitlab.com/Zatherz/eggshell. Relevant file: https://gitlab.com/Zatherz/eggshell/blob/master/EGGShell/EGGGrammar.cs.