简体   繁体   English

使用Parboiled提取双引号字符串内容

[英]Extract double quoted string content with Parboiled

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\\"") 预期输出 - > StringWrapper(“\\”指令\\“”)
  • real output -> StringWrapper("\\"") 实际输出 - > StringWrapper(“\\”“)

Note that you could use the normal* (special normal*)* pattern for faster parsing. 请注意,您可以使用normal* (special normal*)*模式来加快解析速度。 In Java: 在Java中:

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

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

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

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

Actually I found out the solution! 其实我发现了解决方案!

This code works well. 这段代码效果很好。 Actually, my IDE highlited me that this part of code from my previous example 实际上,我的IDE高举了我前面例子中的这部分代码

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

has type Rule0. 类型为Rule0。 I forced it to Rule1 And, now it works. 我强迫它去Rule1,现在它有效。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM