PPGreedyRepeatingParser.st
author sr
Wed, 04 Jul 2018 15:30:19 +0200
changeset 611 38338f2de417
parent 166 749f1a5c6d3e
child 377 6112a403a52d
permissions -rw-r--r--
build order was wrong
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
166
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"{ Package: 'stx:goodies/petitparser' }"
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
PPLimitedRepeatingParser subclass:#PPGreedyRepeatingParser
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
	instanceVariableNames:''
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
	classVariableNames:''
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
	poolDictionaries:''
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
	category:'PetitParser-Parsers'
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
!
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
!PPGreedyRepeatingParser methodsFor:'parsing'!
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
parseOn: aStream
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
	| start element elements positions |
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
	start := aStream position.
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
	elements := OrderedCollection new.
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	[ elements size < min ] whileTrue: [ 
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
		(element := parser parseOn: aStream) isPetitFailure ifTrue: [ 
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
			aStream position: start.
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
			^ element ].
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
		elements addLast: element ].
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
	positions := OrderedCollection with: aStream position.
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
	[ elements size < max and: [ (element := parser parseOn: aStream) isPetitFailure not ] ] whileTrue: [
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
		elements addLast: element.
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
		positions addLast: aStream position ].
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
	[ positions isEmpty ] whileFalse: [
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
		aStream position: positions last.
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
		element := limit parseOn: aStream.
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
		element isPetitFailure ifFalse: [
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
			aStream position: positions last.
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
			^ elements asArray ].
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
		elements isEmpty ifTrue: [
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
			aStream position: start.
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
			^ element ].
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
		elements removeLast.
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
		positions removeLast ].
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
	aStream position: start.
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
	^ PPFailure message: 'overflow' at: start
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
! !
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
!PPGreedyRepeatingParser class methodsFor:'documentation'!
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
version
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
    ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPGreedyRepeatingParser.st,v 1.1 2014-03-04 14:32:42 cg Exp $'
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
!
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
version_CVS
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
    ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPGreedyRepeatingParser.st,v 1.1 2014-03-04 14:32:42 cg Exp $'
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
! !
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50