PPLimitedChoiceParser.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Tue, 18 Aug 2015 22:46:10 +0100
changeset 523 09afcf28ed60
parent 421 7e08b31e0dae
permissions -rw-r--r--
Fixed PEGFsaTransition>>disjunction: - xor: does not take blocks as xor is not subject to lazy evaluation. While in Pharo it worked, it does not work well under Smalltalk/X which does not send value to the passed argument. (partially because xor: is inlined by the stc/JIT compiler)

"{ Package: 'stx:goodies/petitparser' }"

PPChoiceParser subclass:#PPLimitedChoiceParser
	instanceVariableNames:'limit'
	classVariableNames:''
	poolDictionaries:''
	category:'PetitParser-Parsers'
!

!PPLimitedChoiceParser methodsFor:'accessing'!

limit
	
	^ limit
!

limit: anObject
	
	limit := anObject
! !

!PPLimitedChoiceParser methodsFor:'as yet unclassified'!

// aRule 
	^ self copyWith: aRule
!

initialize
	limit := nil asParser
!

parseOn: aPPContext
	"This is optimized code that avoids unnecessary block activations, do not change. When all choices fail, the last failure is answered."

	| element limitResult memento |
	"self halt."
	1 to: parsers size do: [ :index |
		memento := aPPContext remember.
		
		element := (parsers at: index)
			parseOn: aPPContext.
		
		(element isPetitFailure not) ifTrue: [ 
			"check limit"
			limitResult := limit parseOn: aPPContext.
			limitResult isPetitFailure ifTrue: [ 
				element := PPFailure message: 'limit failed' at: aPPContext position .
				aPPContext restore: memento.
			] ifFalse: [ ^ element ].
		].
	].	
	^ element
! !