私は、算術式のパーサを持っている:リユースパーサ
object FormulaParser extends JavaTokenParsers {
def apply(input: String) = parseAll(formula, input)
// ...
val formula: Parser[Formula] =
comparison | comparable | concatenable | term | factor
}
私は式を含めることができます別の言語を解析する必要があります。 X < formula
のようなものを解析する必要があるとしましょう。残念ながら、私は私の新しいパーサにFormulaParser.formula
を再利用することはできません。
object ConditionParser extends JavaTokenParsers {
def apply(input: String) = parseAll(condition, input)
// ...
val condition: Parser[Condition] =
"X" ~ ("<=" | "<") ~ FormulaParser.formula ^^ { ... } // doesn't work
}
その~
方法は、同じタイプで何かを期待するよう~
の左側にパーサは、タイプFormulaParser.Parser
の何かConditionParser.Parser
のインスタンスではありませんので、 。
パーサーコンビネータを使用することの全ポイントは、よくパーサーを組み合わせたものです!私の最初の試みはうまくいかなかったと私は馬鹿に思えますが、私はなぜそれが起こるのか理解しています(私たちは基本特性を拡張してベースパーザーを再利用しています)。
異なるタイプで定義されたパーサーを組み合わせる簡単な方法はありますか?
ありがとうございます!私は 'BaseParsers'特性を作成し、両方のパーサーにそれを継承させることになりました。 –