別のパーサーからパーサーを作成して、消費された入力をast構文の引数として使用したいと思います。
私が持っていると言う
def ingredient = amount ~ nameOfIngredient ^^ {
case amount ~ name => Ingredient(name, amount)
}
私が探しているのは、次の要素を構築するための別のパーサーを持つ方法です。
case class RecipeRow(orginalText: String, ingredient: Ingredient)
そのため、コンポジション内のパーサーへの元の消費された入力を取得する方法を探しています。多分次のようなものです:
def recipeRow = ingredient withConsumedInput ^^ {
case (amount ~ name, consumed) => RecipeRow(consumed, Ingredient(name, amount))
}
この場合の署名は次のようになると思います。
def withConsumedInput [U](p: => Parser[U]): Parser[(U, String)]
私が欲しいものを手に入れる別の簡単な方法はありますか、それとも私はそのことを書く必要がありますか?おそらくもっと良い方法だと思います…</p>