我想从另一个解析器组成一个解析器,以将消耗的输入作为 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>