0

我正在阅读 fastparse 的教程/解释,并收到错误消息

error: No implicit view available from fastparse.P[Any] => fastparse.P[Unit]

对于序列示例。

我正在使用 sbt 1.3.8 和 scala 2.13.1。fastparse 的定义版本是 2.2.2。

scala> import fastparse._
import fastparse._

scala> def ab[_:P] = P("a" ~ "b")
                           ^
       error: No implicit view available from fastparse.P[Any] => fastparse.P[Unit].

scala> def a[_:P] = P("a")
a: [_](implicit evidence$1: fastparse.P[Any])fastparse.package.P[Unit]

scala> def b[_:P] = P("b")
b: [_](implicit evidence$1: fastparse.P[Any])fastparse.package.P[Unit]

scala> def ab[_:P] = P(a ~ b)
                         ^
       error: No implicit view available from fastparse.package.P[Any] => fastparse.package.P[Unit].

scala> def ab[_:P] = P("a" | "b")
ab: [_](implicit evidence$1: fastparse.P[Any])fastparse.package.P[Unit]

scala> fastparse.parse("a", ab(_))
res2: fastparse.Parsed[Unit] = Parsed.Success((), 1)

这个错误是什么意思,我做错了什么/我怎样才能在没有错误的情况下结束这个教程步骤?

4

2 回答 2

4

您需要指定如何处理空格,例如以下失败

import fastparse._
import fastparse.NoWhitespace._

def ab[_:P] = P("a" ~ "b")
assert(parse("a      b", ab(_)).isSuccess)

因为"a b"包含空格,而以下通过

import fastparse._
import fastparse.SingleLineWhitespace._

def ab[_:P] = P("a" ~ "b")
assert(parse("a      b", ab(_)).isSuccess)

因为import fastparse.SingleLineWhitespace._提供空白消费者。如果我们看一下定义~

def ~[V, R](other:  P[V])
           (implicit s: Implicits.Sequencer[T, V, R],
            whitespace: P[Any] => P[Unit],
            ctx: P[_]): P[R] = macro MacroImpls.parsedSequence[T, V, R]

我们看到implicit whitespace: P[Any] => P[Unit]了解释No implicit view available错误的要求。空白导入提供了这种能力,例如

object NoWhitespace {
  implicit object noWhitespaceImplicit extends (ParsingRun[_] => ParsingRun[Unit]){
    def apply(ctx: ParsingRun[_]) = ctx.freshSuccessUnit()
  }
}

我们看到ParsingRun[_] => ParsingRun[Unit]隐式函数的地方。

于 2020-08-05T17:55:31.003 回答
2

您又错过了一次导入

import NoWhitespace._

https://scastie.scala-lang.org/qCKZEnRHS7eOhjpVjtWNIQ

于 2020-08-05T17:33:36.823 回答