ご想像のとおり、アクションでPPFailureを返すようにすることができます。一般に、これは適切なスタイルではありませんが(構文分析と意味分析を組み合わせたもの)、役立つ場合があります。PetitParserのテストには、いくつかの例があります。PPXmlGrammar>>#element
とで見られる良い使い方PPSmalltalkGrammar>>#number
。
PPFailureの位置は、PetitParserがユーザー(ツール)に提供するものにすぎません。解析自体には使用されないため、怠惰な場合は0に設定できます。または、次の例を使用して、入力の現在の位置をフェッチすることもできます。
positionInInput
"A parser that does not consume anything, that always succeeds and that
returns the current position in the input."
^ [ :stream | stream position ] asParser
integerConstant
^ (self positionInInput , (#digit asParser min: 1 max: 5) flatten) map: [ :pos :string |
| value |
value := string asNumber.
(value between: 0 and: 32767)
ifTrue: [ value ]
ifFalse: [ PPFailure message: value , ' out of range' at: pos ] ]