PPGreedyRepeatingParser.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sun, 05 Oct 2014 00:05:20 +0100
changeset 380 8fe3cb4e607f
parent 377 6112a403a52d
permissions -rw-r--r--
Remove Pharoisms to make code more portable and running on Smalltalk/X * Use ANSI `(Character codePoint: 13)` (`10`) instead of `Character cr` (`lf`), This is more portable and does not depend on dialects interpretation of `#cr` - Smalltalk/X convert it according to platform line end convention (UNIX/Windows/Mac) * Do not assume exact value of a printstring in tests, i.e., instead of `msg includesSubstring: '$a' code `msg includesSubstring: $a printString. This way, the test is independent on the printString value, which may differ among dialects. Q: Is printString value of String and/or Character defined in ANSI? * In assestions, instead of `#equals:` use plain old `#=`, which is more portable. * Removed Character>>- used to create range parser. Use portable `(Interval from: $a to: $z) asParser` instead of just `$a - $z`. Do not use ($a to: $z) asParser as in Pharo, Character>>to: does not create an Interval but an Array (sigh).

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

PPLimitedRepeatingParser subclass:#PPGreedyRepeatingParser
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'PetitParser-Parsers'
!


!PPGreedyRepeatingParser methodsFor:'parsing'!

parseOn: aPPContext
	| memento element elements positions |
	memento := aPPContext remember.
	elements := OrderedCollection new.
	[ elements size < min ] whileTrue: [ 
		(element := parser parseOn: aPPContext) isPetitFailure ifTrue: [ 
			aPPContext restore: memento.
			^ element ].
		elements addLast: element ].
	positions := OrderedCollection with: aPPContext remember.
	[ elements size < max and: [ (element := parser parseOn: aPPContext) isPetitFailure not ] ] whileTrue: [
		elements addLast: element.
		positions addLast: aPPContext remember ].
	[ positions isEmpty ] whileFalse: [
		aPPContext restore: positions last.
		element := limit parseOn: aPPContext.
		element isPetitFailure ifFalse: [
			aPPContext restore: positions last.
			^ elements asArray ].
		elements isEmpty ifTrue: [
			aPPContext restore: memento.
			^ element ].
		elements removeLast.
		positions removeLast ].
	aPPContext restore: memento.
	^ PPFailure message: 'overflow' context: aPPContext at: memento position
! !

!PPGreedyRepeatingParser class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPGreedyRepeatingParser.st,v 1.1 2014-03-04 14:32:42 cg Exp $'
!

version_CVS
    ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPGreedyRepeatingParser.st,v 1.1 2014-03-04 14:32:42 cg Exp $'
!

version_HG

    ^ '$Changeset: <not expanded> $'
! !