2017-06-07 5 views
0

私はscala fastparse 0.4.3を使用して次のコードがtypecheckingに失敗する理由に戸惑います。scala fastparse typechecking

val White = WhitespaceApi.Wrapper{ 
    import fastparse.all._ 
    NoTrace(CharIn(" \t\n").rep) 
} 
import fastparse.noApi._ 
import White._ 

case class Term(tokens: Seq[String]) 
case class Terms(terms: Seq[Term]) 

val token = P[String] (CharIn('a' to 'z', 'A' to 'Z', '0' to '9').rep(min=1).!) 
val term: P[Term] = P("[" ~ token.!.rep(sep=" ", min=1) ~ "]").map(x => Term(x)) 
val terms = P("(" ~ term.!.rep(sep=" ", min=1) ~ ")").map{x => Terms(x)} 
val parse = terms.parse("([ab bd ef] [xy wa dd] [jk mn op])") 

エラーメッセージ:

[error] .../MyParser.scala: type mismatch; 
[error] found : Seq[String] 
[error] required: Seq[Term] 
[error]  val terms = P("(" ~ term.!.rep(sep=" ", min=1) ~")").map{x => Terms(x)} 
[error]                  ^

私はterm以来のタイプTermであり、termsパターンがterm.!.rep(...を使用するので、それはSeq[Term]を取得する必要があることを想像するだろう。

答えて

0

私はそれを理解しました。私の間違いはtermsに(!で)重複してキャプチャしていた。その行が代わりに記述する必要があります:term.!.rep(term.rep(に書き換えられていることを

val terms = P("(" ~ term.rep(sep=" ", min=1) ~ ")").map{x => Terms(x)} 

お知らせ。 明らかにどんなルールでもキャプチャすると、キャプチャされたサブルールが一致するテキストが返され、サブルーレが実際に返すものがオーバーライドされます。私はこれが正しく使用されたときの機能だと思います。 :)