4

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("\"")
4

2 回答 2

2

请注意,您可以使用该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(), '"');
}
于 2014-04-21T08:13:53.870 回答
1

其实我找到了解决办法!

这段代码运行良好。实际上,我的 IDE 突出显示了我之前示例中的这部分代码

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

具有类型 Rule0。我强迫它使用 Rule1,现在它可以工作了。

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

def doubleQuoted: Rule1[StringWrapper] = rule { //same for singlequoted
  "\"" ~ doubleQouteBody ~ "\""
}
于 2014-03-28T12:59:52.380 回答