理解`not`在解析器组合

问题描述:

我写了与fail意图-ing上的空白以下解析器:理解`not`在解析器组合

import scala.util.parsing.combinator._ 

object Foo extends JavaTokenParsers { 
    val wsTest = not(whiteSpace) // uses whitespace inherited from `RegexParsers` 
} 

为什么解析一堆空白的全成?

scala> Foo.parseAll(Foo.wsTest, "   ") 
res5: Foo.ParseResult[Unit] = [1.11] parsed:() 

scala> res5.successful 
res6: Boolean = true 

project望着Parsers#not,我会一直期待我的上述测试Failure

/** Wrap a parser so that its failures and errors become success and 
    * vice versa -- it never consumes any input. 
    */ 
    def not[T](p: => Parser[T]): Parser[Unit] = Parser { in => 
    p(in) match { 
     case Success(_, _) => Failure("Expected failure", in) 
     case _    => Success((), in) 
    } 
    } 
+0

'不'正常工作。我的猜测是解析器默认跳过空格,你必须禁用它。也许这有助于:http://stackoverflow.com/questions/3564094/parsing-a-blank-whitespace-with-regexparsers – Kigyo 2014-09-01 16:27:46

+0

'我的猜测是,解析器跳过默认空白' - 我已经观察到这种行为与类扩展'JavaTokenParsers'。但是,我不希望'Foo.parseAll(Foo.wsTest,“”)'成功。 – 2014-09-01 17:32:46

JavaTokenParsers延伸RegexParsers,RegexParsers有:

protected val whiteSpace = """\s+""".r 

def skipWhitespace = whiteSpace.toString.length > 0 

implicit def regex(r: Regex): Parser[String] = new Parser[String] { 
    ... 
    val start = handleWhiteSpace(source, offset) 
    ... 
} 

protected def handleWhiteSpace(source: java.lang.CharSequence, offset: Int): Int = 
    if (skipWhitespace) 
    (whiteSpace findPrefixMatchOf (source.subSequence(offset, source.length))) match { 
     case Some(matched) => offset + matched.end 
     case None => offset 
    } 
    else 
    offset 

所以跳过空白

所以解析器" "等于 “”

(您可以通过高清覆盖skipWhitespace = FALSE覆盖此)

空格尝试匹配“”但它失败(“”“\ s +”“”至少需要一个空格)和不是转换成功

+0

谢谢,@Siphor。实际上,我问这个问题是为了实现这个[答案](http://stackoverflow.com/a/25294257/409976)的后续工作。为了验证我正在解析的“输入”,即令牌,我添加了val nonWhitespaceRegex:Regex =“\\ S +”。r'和'guard(nonWhitespaceRegex)〜> ...'空白。否则,检查一堆空格是EOF将返回false。 – 2014-09-02 00:42:56