PPPredicateSequenceParser.st
changeset 0 739fe9b7253e
child 4 90de244a7fa2
equal deleted inserted replaced
-1:000000000000 0:739fe9b7253e
       
     1 "{ Package: 'squeak:petitparser' }"
       
     2 
       
     3 PPPredicateParser subclass:#PPPredicateSequenceParser
       
     4 	instanceVariableNames:'size'
       
     5 	classVariableNames:''
       
     6 	poolDictionaries:''
       
     7 	category:'PetitParser-Parsers'
       
     8 !
       
     9 
       
    10 PPPredicateSequenceParser comment:'A parser that accepts if a given predicate on an arbitrary number of elements of the input sequence holds.
       
    11 Instance Variables:
       
    12 	size	<Integer>	The number of elements to consume.'
       
    13 !
       
    14 
       
    15 
       
    16 !PPPredicateSequenceParser class methodsFor:'instance creation'!
       
    17 
       
    18 on: aBlock message: aString negated: aNegatedBlock message: aNegatedString size: anInteger 
       
    19 	^ self new initializeOn: aBlock message: aString negated: aNegatedBlock message: aNegatedString size: anInteger
       
    20 !
       
    21 
       
    22 on: aBlock message: aString size: anInteger
       
    23 	^ self on: aBlock message: aString negated: [ :each | (aBlock value: each) not ] message: 'no ' , aString size: anInteger 
       
    24 ! !
       
    25 
       
    26 !PPPredicateSequenceParser methodsFor:'accessing'!
       
    27 
       
    28 size
       
    29 	"Answer the sequence size of the receiver."
       
    30 
       
    31 	^ size
       
    32 ! !
       
    33 
       
    34 !PPPredicateSequenceParser methodsFor:'initialization'!
       
    35 
       
    36 initializeOn: aBlock message: aString negated: aNegatedBlock message: aNegatedString size: anInteger
       
    37 	predicate := aBlock.
       
    38 	predicateMessage := aString.
       
    39 	negated := aNegatedBlock.
       
    40 	negatedMessage := aNegatedString.
       
    41 	size := anInteger 
       
    42 ! !
       
    43 
       
    44 !PPPredicateSequenceParser methodsFor:'operators'!
       
    45 
       
    46 negate
       
    47 	"Answer a parser that is the negation of the receiving predicate parser."
       
    48 	
       
    49 	^ self class 
       
    50 		on: negated message: negatedMessage
       
    51 		negated: predicate message: predicateMessage
       
    52 		size: size
       
    53 ! !
       
    54 
       
    55 !PPPredicateSequenceParser methodsFor:'parsing'!
       
    56 
       
    57 parseOn: aStream
       
    58 	| position result |
       
    59 	position := aStream position.
       
    60 	result := aStream next: size.
       
    61 	(result size = size and: [ predicate value: result ])
       
    62 		ifTrue: [ ^ result ].
       
    63 	aStream position: position.
       
    64 	^ PPFailure message: predicateMessage at: aStream position
       
    65 ! !
       
    66 
       
    67 !PPPredicateSequenceParser class methodsFor:'documentation'!
       
    68 
       
    69 version_SVN
       
    70     ^ '$Id: PPPredicateSequenceParser.st,v 1.1 2011-08-18 18:56:17 cg Exp $'
       
    71 ! !