Extract double quoted string content with Parboiled

718 Views Asked by At

I'm writing a parser, one of it's parts should match and retrieve double-quoted string content It yields only quotation mark, but not whole string. For unquoted ones everything works well

Here is the corresponding rule:

def doubleQuoted: Rule1[StringWrapper] = rule { //same for singlequoted
  "\"" ~ zeroOrMore( noneOf("\"\\") | ("\\" ~ "\"") ) ~ "\"" ~> StringWrapper
}

The problem is:

  • input -> "directive"
  • expected output -> StringWrapper("\"directive\"")
  • real output -> StringWrapper("\"")
2

There are 2 best solutions below

0
On BEST ANSWER

Actually I found out the solution!

This code works well. Actually, my IDE highlited me that this part of code from my previous example

zeroOrMore( noneOf("\"\\") | ("\\" ~ "\"") )

has type Rule0. I forced it to Rule1 And, now it works.

def doubleQouteBody: Rule1[StringWrapper] = rule {
  zeroOrMore( noneOf("\"\\") | ("\\" ~ "\"") ) ~> StringWrapper
}

def doubleQuoted: Rule1[StringWrapper] = rule { //same for singlequoted
  "\"" ~ doubleQouteBody ~ "\""
}
0
On

Note that you could use the normal* (special normal*)* pattern for faster parsing. In Java:

Rule Normal()
{
    return NoneOf("\\\"");
}

Rule Special()
{
    return String("\\\"");
}

Rule NSN()
{
    return Sequence(
        ZeroOrMore(Normal()),
        ZeroOrMore(Special(), ZeroOrMore(Normal()))
    );
}

Rule DoubleQuotedString()
{
    return Sequence('"', NSN(), '"');
}