PPLimitedChoiceParser.st
changeset 421 7e08b31e0dae
equal deleted inserted replaced
420:b2f2f15cef26 421:7e08b31e0dae
       
     1 "{ Package: 'stx:goodies/petitparser' }"
       
     2 
       
     3 PPChoiceParser subclass:#PPLimitedChoiceParser
       
     4 	instanceVariableNames:'limit'
       
     5 	classVariableNames:''
       
     6 	poolDictionaries:''
       
     7 	category:'PetitParser-Parsers'
       
     8 !
       
     9 
       
    10 !PPLimitedChoiceParser methodsFor:'accessing'!
       
    11 
       
    12 limit
       
    13 	
       
    14 	^ limit
       
    15 !
       
    16 
       
    17 limit: anObject
       
    18 	
       
    19 	limit := anObject
       
    20 ! !
       
    21 
       
    22 !PPLimitedChoiceParser methodsFor:'as yet unclassified'!
       
    23 
       
    24 // aRule 
       
    25 	^ self copyWith: aRule
       
    26 !
       
    27 
       
    28 initialize
       
    29 	limit := nil asParser
       
    30 !
       
    31 
       
    32 parseOn: aPPContext
       
    33 	"This is optimized code that avoids unnecessary block activations, do not change. When all choices fail, the last failure is answered."
       
    34 
       
    35 	| element limitResult memento |
       
    36 	"self halt."
       
    37 	1 to: parsers size do: [ :index |
       
    38 		memento := aPPContext remember.
       
    39 		
       
    40 		element := (parsers at: index)
       
    41 			parseOn: aPPContext.
       
    42 		
       
    43 		(element isPetitFailure not) ifTrue: [ 
       
    44 			"check limit"
       
    45 			limitResult := limit parseOn: aPPContext.
       
    46 			limitResult isPetitFailure ifTrue: [ 
       
    47 				element := PPFailure message: 'limit failed' at: aPPContext position .
       
    48 				aPPContext restore: memento.
       
    49 			] ifFalse: [ ^ element ].
       
    50 		].
       
    51 	].	
       
    52 	^ element
       
    53 ! !
       
    54