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).
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
377
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 166
diff changeset
    13
parseOn: aPPContext
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 166
diff changeset
    14
	| memento element elements positions |
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 166
diff changeset
    15
	memento := aPPContext remember.
166
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: [ 
377
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 166
diff changeset
    18
		(element := parser parseOn: aPPContext) isPetitFailure ifTrue: [ 
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 166
diff changeset
    19
			aPPContext restore: memento.
166
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 ].
377
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 166
diff changeset
    22
	positions := OrderedCollection with: aPPContext remember.
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 166
diff changeset
    23
	[ elements size < max and: [ (element := parser parseOn: aPPContext) isPetitFailure not ] ] whileTrue: [
166
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
		elements addLast: element.
377
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 166
diff changeset
    25
		positions addLast: aPPContext remember ].
166
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
	[ positions isEmpty ] whileFalse: [
377
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 166
diff changeset
    27
		aPPContext restore: positions last.
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 166
diff changeset
    28
		element := limit parseOn: aPPContext.
166
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
		element isPetitFailure ifFalse: [
377
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 166
diff changeset
    30
			aPPContext restore: positions last.
166
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: [
377
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 166
diff changeset
    33
			aPPContext restore: memento.
166
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 ].
377
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 166
diff changeset
    37
	aPPContext restore: memento.
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 166
diff changeset
    38
	^ PPFailure message: 'overflow' context: aPPContext at: memento position
166
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 $'
380
8fe3cb4e607f Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    49
!
8fe3cb4e607f Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    50
8fe3cb4e607f Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    51
version_HG
8fe3cb4e607f Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    52
8fe3cb4e607f Remove Pharoisms to make code more portable and running on Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
    53
    ^ '$Changeset: <not expanded> $'
166
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
! !
749f1a5c6d3e initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55