PPRepeatingParser.st
changeset 0 739fe9b7253e
child 4 90de244a7fa2
equal deleted inserted replaced
-1:000000000000 0:739fe9b7253e
       
     1 "{ Package: 'squeak:petitparser' }"
       
     2 
       
     3 PPDelegateParser subclass:#PPRepeatingParser
       
     4 	instanceVariableNames:'min max'
       
     5 	classVariableNames:''
       
     6 	poolDictionaries:''
       
     7 	category:'PetitParser-Parsers'
       
     8 !
       
     9 
       
    10 PPRepeatingParser comment:'A parser that eagerly parses min to max instances of my delegate. The default instance parses eagerly an infinite number of elements, as min is set to 0 and max to infinity (SmallInteger maxVal).
       
    11 Instance Variables:
       
    12 	min	<Integer>	The minimum number of repetitions.
       
    13 	max	<Integer>	The maximum number of repetitions.'
       
    14 !
       
    15 
       
    16 
       
    17 !PPRepeatingParser class methodsFor:'instance creation'!
       
    18 
       
    19 on: aParser
       
    20 	^ (super on: aParser) setMin: 0 max: SmallInteger maxVal
       
    21 !
       
    22 
       
    23 on: aParser max: aMaxInteger
       
    24 	^ (self on: aParser) setMin: 0 max: aMaxInteger
       
    25 !
       
    26 
       
    27 on: aParser min: aMinInteger
       
    28 	^ (self on: aParser) setMin: aMinInteger max: SmallInteger maxVal 
       
    29 !
       
    30 
       
    31 on: aParser min: aMinInteger max: aMaxInteger
       
    32 	^ (self on: aParser) setMin: aMinInteger max: aMaxInteger
       
    33 ! !
       
    34 
       
    35 !PPRepeatingParser methodsFor:'accessing'!
       
    36 
       
    37 max
       
    38 	"Answer the maximum number of repetitions."
       
    39 
       
    40 	^ max
       
    41 !
       
    42 
       
    43 min
       
    44 	"Answer the minimum number of repetitions."
       
    45 	
       
    46 	^ min
       
    47 ! !
       
    48 
       
    49 !PPRepeatingParser methodsFor:'initialization'!
       
    50 
       
    51 setMin: aMinInteger max: aMaxInteger
       
    52 	min := aMinInteger.
       
    53 	max := aMaxInteger
       
    54 ! !
       
    55 
       
    56 !PPRepeatingParser methodsFor:'parsing'!
       
    57 
       
    58 parseOn: aStream
       
    59 	| start element elements |
       
    60 	start := aStream position.
       
    61 	elements := OrderedCollection new.
       
    62 	[ elements size < min ] whileTrue: [
       
    63 		(element := parser parseOn: aStream) isPetitFailure ifTrue: [
       
    64 			aStream position: start.
       
    65 			^ element ].
       
    66 		elements addLast: element ].
       
    67 	[ elements size < max ] whileTrue: [
       
    68 	 	(element := parser parseOn: aStream) isPetitFailure
       
    69 			ifTrue: [ ^ elements asArray ].
       
    70 		elements addLast: element ].
       
    71 	^ elements asArray
       
    72 ! !
       
    73 
       
    74 !PPRepeatingParser methodsFor:'printing'!
       
    75 
       
    76 printOn: aStream
       
    77 	super printOn: aStream.
       
    78 	aStream nextPutAll: ' ['; print: min; nextPutAll: ', '; nextPutAll: (max = SmallInteger maxVal
       
    79 		ifTrue: [ '*' ] ifFalse: [ max printString ]); nextPut: $]
       
    80 ! !
       
    81 
       
    82 !PPRepeatingParser class methodsFor:'documentation'!
       
    83 
       
    84 version_SVN
       
    85     ^ '$Id: PPRepeatingParser.st,v 1.1 2011-08-18 18:56:17 cg Exp $'
       
    86 ! !