Extract double-quoted string content using Parboiled

I am writing a parser, one of which must match and retrieve the string contents with double quotes. It gives only the quote, but not the whole string. For unaccounted for everything works well

Here is the relevant rule:

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

The problem is this:

  • input → "directive
  • expected output -> StringWrapper ("\" directive \ "")
  • real output -> StringWrapper ("\" ")
+4
source share
2 answers

I actually found a solution!

. , IDE ,

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

Rule0. 1 .

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

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

, normal* (special normal*)* . Java:

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

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

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

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

Source: https://habr.com/ru/post/1533781/


All Articles