PPLimitedChoiceParser.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Mon, 27 Jul 2015 16:28:48 +0100
changeset 506 e5d63143737f
parent 421 7e08b31e0dae
permissions -rw-r--r--
Added static analysis of blocks when inlining. Allow inlining only when block is functional ...i.e., does not access any shared state (in instance or class variables). If the block does a self-send, the sent method has to be (transitively) functional too. To allow for self-sends in action blocks, copy (transitively) self-sent methods to target parser. This is safe as these self-sent methods are guarnateed to be functional.

"{ 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
! !