2017-01-25 12 views
0

パーサーコンビネータを使用して準構造のログファイルデータをJSONに変換しています。出力のデータ要素を元の入力ファイルに配置できるように、位置情報を使用して結果を豊かにしたいと思います。私はsource codepositioned方法の説明とAPI docsを見たとき、私は解決策は、右私の目の前にあったと思ったが、代わりに私はこのような例外に対して私の頭を叩いています:位置指定された演算子をRegexParserで使用できますか?

Error:(133, 24) inferred type arguments [String] 
do not conform to method positioned's type 
parameter bounds [T <: scala.util.parsing.input.Positional] 

リフティングtutorial blog post on parser combinatorsから私はフル-BLであるものの、この推測は、私はpositioned修飾子を使用している見つけることができる最初の完全な例だったthis blog postits accompanying source codeに触発された

object ReallySimpleParser2 extends RegexParsers { 
    def sentence = positioned { hello ~ world } 
    def hello = "hello" 
    def world = "world" 
} 

:ような何かを言うことができると期待独自のパーサー/レクサーの演習。 IDENTIFIERタイプを定義する際に関与魔法に

def identifier: Parser[IDENTIFIER] = positioned { 
    "[a-zA-Z_][a-zA-Z0-9_]*".r ^^ { str => IDENTIFIER(str) } 
} 

おそらく感謝:彼らはのようなものを言うことができました。

したがってでpositionedをそのまま使用することができますか?もしそうなら、positionedRegexParserを利用するために最低限必要な足場はどうなっていますか?

+0

私はまた、私にはあまりにも斜めすぎるが、http://stackoverflow.com/questions/20919068/using-positional-and-positioned-in-scala-parser-combinatorsが方法を指し示すかもしれないことを言及することを意味する。 –

答えて

0

positionedは、Positionalのサブクラスを生成するパーサのみをラップすることができます。次に例を示します。

import scala.util.parsing.combinator.RegexParsers 
import scala.util.parsing.input.Positional 

case class Content(value: String) extends Positional 

object ReallySimpleParser2 extends RegexParsers { 
    def sentence = positioned { hello ~ world ^^ { case a ~ b => Content(s"$a $b") } } 
    def hello = "hello" 
    def world = "world" 
} 

だから、第二の例ではIDENTIFIERPositionalを拡張する必要があります。

関連する問題