Parsing heredoc style constructs with parboiled

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Parsing heredoc style constructs with parboiled

bshishani
Greetings,

I'm testing Parboiled with a grammar that contains heredoc constructs (terminator label
matching starter label).

I experimented with other PEG tools (notably Grako in Python), and I see they offer a special regex production that invokes the regex engine of the host language. So heredoc parsing can easily be performed using regex back referencing.

I don't see a regex production offered in Parboiled (AFAIK) and I'm wondering how heredoc parsing should be performed. I can see that action variables or the value stack can be used to capture the starter label and somehow dynamically adjust the rest of the rule, but I'm not sure about the details of this or if it's the best solution.

Would you care to illuminate me on this.


Regards
Basel Shishani
fge
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Parsing heredoc style constructs with parboiled

fge
On Sun, Jun 15, 2014 at 7:27 AM, bshishani [via parboiled users]
<[hidden email]> wrote:

> Greetings,
>
> I'm testing Parboiled with a grammar that contains heredoc constructs
> (terminator label
> matching starter label).
>
> I experimented with other PEG tools (notably Grako in Python), and I see
> they offer a special regex production that invokes the regex engine of the
> host language. So heredoc parsing can easily be performed using regex back
> referencing.
>
> I don't see a regex production offered in Parboiled (AFAIK) and I'm
> wondering how heredoc parsing should be performed. I can see that action
> variables or the value stack can be used to capture the starter label and
> somehow dynamically adjust the rest of the rule, but I'm not sure about the
> details of this or if it's the best solution.
>
> Would you care to illuminate me on this.
>
>

Hello,

Something like this with grappa (see signature):

Rule heredocContent()
{
    Var<String> marker = new Var<String>();

    return sequence(
        oneOrMore(noneOf("\r\n")), marker.set(match()), // heredoc marker
        join(sequence(testNot(marker.get()),
noneOf("\r\n"))).using(crlf()).min(1)),
        crlf(), marker.get()
    );
}

--
Francis Galiegue, [hidden email], https://github.com/fge
JSON Schema in Java: http://json-schema-validator.herokuapp.com
Parsers in pure Java: https://github.com/parboiled1/grappa (redde
Caesaris: https://github.com/sirthias)
Loading...