PPParser.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Mon, 03 Nov 2014 11:30:59 +0000
changeset 407 a0e6299c7337
parent 389 009c2e13973c
child 421 7e08b31e0dae
permissions -rw-r--r--
Removed unused / obsolete methods from PPToken * remove PPToken class>>on:start:stop * remove PPToken>>initializeOn:start:stop
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
90de244a7fa2 move to package
Claus Gittinger <cg@exept.de>
parents: 0
diff changeset
     1
"{ Package: 'stx:goodies/petitparser' }"
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
Object subclass:#PPParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
	instanceVariableNames:'properties'
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
	classVariableNames:''
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
	poolDictionaries:''
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
	category:'PetitParser-Parsers'
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
!PPParser class methodsFor:'instance creation'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
named: aString
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
	^ self new name: aString
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
new
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	^ self basicNew initialize
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
    21
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
    22
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
    23
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
    24
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
    25
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
    26
389
009c2e13973c Updated smalltalk parser to PetitSmalltalk-JanKurs.75
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 381
diff changeset
    27
009c2e13973c Updated smalltalk parser to PetitSmalltalk-JanKurs.75
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 381
diff changeset
    28
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
!PPParser methodsFor:'accessing'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
children
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
	"Answer a set of child parsers that could follow the receiver."
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
	^ #()
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
name
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
	"Answer the production name of the receiver."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    39
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
	^ self propertyAt: #name ifAbsent: [ nil ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
name: aString
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
	self propertyAt: #name put: aString
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
!PPParser methodsFor:'accessing-properties'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
hasProperty: aKey
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
	"Test if the property aKey is present."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    51
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
	^ properties notNil and: [ properties includesKey: aKey ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
propertyAt: aKey
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
	"Answer the property value associated with aKey."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    57
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
	^ self propertyAt: aKey ifAbsent: [ self error: 'Property not found' ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
propertyAt: aKey ifAbsent: aBlock
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
	"Answer the property value associated with aKey or, if aKey isn't found, answer the result of evaluating aBlock."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    63
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
	^ properties isNil
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
		ifTrue: [ aBlock value ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
		ifFalse: [ properties at: aKey ifAbsent: aBlock ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
propertyAt: aKey ifAbsentPut: aBlock
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
	"Answer the property associated with aKey or, if aKey isn't found store the result of evaluating aBlock as new value."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    71
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
	^ self propertyAt: aKey ifAbsent: [ self propertyAt: aKey put: aBlock value ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
propertyAt: aKey put: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
	"Set the property at aKey to be anObject. If aKey is not found, create a new entry for aKey and set is value to anObject. Answer anObject."
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
	^ (properties ifNil: [ properties := Dictionary new: 1 ])
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
		at: aKey put: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
removeProperty: aKey
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
	"Remove the property with aKey. Answer the property or raise an error if aKey isn't found."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    84
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
	^ self removeProperty: aKey ifAbsent: [ self error: 'Property not found' ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
removeProperty: aKey ifAbsent: aBlock
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
	"Remove the property with aKey. Answer the value or, if aKey isn't found, answer the result of evaluating aBlock."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    90
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
	| answer |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
	properties isNil ifTrue: [ ^ aBlock value ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
	answer := properties removeKey: aKey ifAbsent: aBlock.
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
	properties isEmpty ifTrue: [ properties := nil ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
	^ answer
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
377
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
    98
!PPParser methodsFor:'context'!
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
    99
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   100
parse: anObject withContext: aPPContext
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   101
	"Parse anObject with the receiving parser and answer the parse-result or an instance of PPFailure."
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   102
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   103
	aPPContext stream: anObject asPetitStream.
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   104
	^ self parseWithContext: aPPContext.
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   105
!
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   106
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   107
parseWithContext: context
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   108
	| result |
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   109
	context initializeFor: self.
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   110
	result := self parseOn: context.
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   111
	
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   112
	"Return the furthest failure, it gives better results than the last failure"
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   113
	result isPetitFailure ifTrue: [ ^ context furthestFailure ].
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   114
	^ result
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   115
	
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   116
!
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   117
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   118
updateContext: aPPContext
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   119
	"nothing to do"
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   120
! !
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   121
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
!PPParser methodsFor:'converting'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
asParser
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   125
	"Answer the receiving parser."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   126
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
	^ self
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   130
!PPParser methodsFor:'copying'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
postCopy
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
	super postCopy.
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
	properties := properties copy
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
381
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   137
!PPParser methodsFor:'enumerating'!
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   138
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   139
allParsers
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   140
	"Answer all the parse nodes of the receiver."
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   141
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   142
	| result |
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   143
	result := OrderedCollection new.
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   144
	self allParsersDo: [ :parser | result addLast: parser ].
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   145
	^ result
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   146
!
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   147
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   148
allParsersDo: aBlock
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   149
	"Iterate over all the parse nodes of the receiver."
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   150
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   151
	self allParsersDo: aBlock seen: IdentitySet new
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   152
!
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   153
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   154
allParsersDo: aBlock seen: aSet
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   155
	"Iterate over all the parse nodes of the receiver, do not visit and follow the ones contained in aSet."
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   156
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   157
	(aSet includes: self)
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   158
		ifTrue: [ ^ self ].
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   159
	aSet add: self.
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   160
	aBlock value: self.
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   161
	self children
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   162
		do: [ :each | each allParsersDo: aBlock seen: aSet ]
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   163
! !
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   164
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
!PPParser methodsFor:'initialization'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   166
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
initialize
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   169
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   170
!PPParser methodsFor:'operations'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   172
, aParser 
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
	"Answer a new parser that parses the receiver followed by aParser."
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   174
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   175
	^ PPSequenceParser with: self with: aParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   176
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   178
/ aParser 
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
	"Answer a new parser that parses the receiver, if the receiver fails try with aParser (ordered-choice)."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   180
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
	^ PPChoiceParser with: self with: aParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   183
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   184
and
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
	"Answer a new parser (logical and-predicate) that succeeds whenever the receiver does, but never consumes input."
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   187
	^ PPAndParser on: self
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   188
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   189
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   190
def: aParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   191
	"Redefine the receiver as the argument aParser. This method is useful when defining recursive parsers: instantiate a PPUnresolvedParser and later redefine it with another one."
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   192
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   193
	^ self becomeForward: (aParser name: self name)
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   196
end
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   197
	"Answer a new parser that succeeds at the end of the input and return the result of the receiver."
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   198
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   199
	^ PPEndOfInputParser on: self
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   200
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   201
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
memoized
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
	"Answer a new memoized parser, for refraining redundant computations. This ensures polynomial time O(n^4) for left-recursive grammars and O(n^3) for non left-recursive grammars in the worst case. Not necessary for most grammars that are carefully written and in O(n) anyway."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   204
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
	^ PPMemoizedParser on: self
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   206
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   207
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   208
negate
92
a95137a3ab6a class: PPToken
Claus Gittinger <cg@exept.de>
parents: 22
diff changeset
   209
        "Answer a new parser consumes any input token but the receiver."
a95137a3ab6a class: PPToken
Claus Gittinger <cg@exept.de>
parents: 22
diff changeset
   210
        
a95137a3ab6a class: PPToken
Claus Gittinger <cg@exept.de>
parents: 22
diff changeset
   211
        ^ self not , #any asParser ==> #second
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   212
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   213
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   214
not
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   215
	"Answer a new parser (logical not-predicate) that succeeds whenever the receiver fails, but never consumes input."
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   216
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   217
	^ PPNotParser on: self
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
optional
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   221
	"Answer a new parser that parses the receiver, if possible."
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   222
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   223
	^ PPOptionalParser on: self
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   224
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   225
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   226
times: anInteger
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   227
	"Answer a new parser that parses the receiver exactly anInteger times."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   228
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   229
	^ self min: anInteger max: anInteger
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   230
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   231
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   232
wrapped
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   233
	"Answer a new parser that is simply wrapped."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   234
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   235
	^ PPDelegateParser on: self
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   236
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   237
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   238
| aParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   239
	"Answer a new parser that either parses the receiver or aParser. Fail if both pass or fail (exclusive choice, unordered choice)."
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   240
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   241
	^ (self not , aParser) / (aParser not , self) ==> #second
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   242
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   243
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   244
!PPParser methodsFor:'operations-convenience'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   245
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   246
delimitedBy: aParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   247
	"Answer a new parser that parses the receiver one or more times, separated and possibly ended by aParser."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   248
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   249
	^ (self separatedBy: aParser) , (aParser optional) ==> [ :node |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   250
		node second isNil
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   251
			ifTrue: [ node first ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   252
			ifFalse: [ node first copyWith: node second ] ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   253
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   254
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   255
separatedBy: aParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   256
	"Answer a new parser that parses the receiver one or more times, separated by aParser."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   257
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   258
	^ (PPSequenceParser with: self with: (PPSequenceParser with: aParser with: self) star) ==> [ :nodes |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   259
		| result |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   260
		result := Array new: 2 * nodes second size + 1.
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   261
		result at: 1 put: nodes first.
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   262
		nodes second 
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   263
			keysAndValuesDo: [ :index :pair | result replaceFrom: 2 * index to: 2 * index + 1 with: pair startingAt: 1 ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   264
		result ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   265
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   266
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   267
!PPParser methodsFor:'operations-mapping'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   268
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   269
==> aBlock
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   270
	"Answer a new parser that performs aBlock as action handler on success."
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   271
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   272
	^ PPActionParser on: self block: aBlock
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   273
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   274
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   275
answer: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   276
	"Answer a new parser that always returns anObject from a successful parse."
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   277
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   278
	^ self ==> [ :nodes | anObject ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   279
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   280
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   281
flatten
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   282
	"Answer a new parser that flattens the underlying collection."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   283
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   284
	^ PPFlattenParser on: self
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   285
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   286
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   287
token
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   288
	"Answer a new parser that transforms the input to a token."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   289
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   290
	^ PPTokenParser on: self
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   291
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   292
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   293
token: aTokenClass
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   294
	"Answer a new parser that transforms the input to a token of class aTokenClass."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   295
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   296
	^ self token tokenClass: aTokenClass
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   297
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   298
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   299
trim
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   300
	"Answer a new parser that consumes spaces before and after the receiving parser."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   301
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   302
	^ self trimSpaces
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   303
! !
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   304
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   305
!PPParser methodsFor:'operators-convenience'!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   306
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   307
withoutSeparators
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   308
	"Filters out the separators from a parse result produced by one of the productions #delimitedBy: or #separatedBy:."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   309
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   310
	^ self ==> [ :items |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   311
		| result |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   312
		result := Array new: items size + 1 // 2.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   313
		1 to: result size do: [ :index | result at: index put: (items at: 2 * index - 1) ].
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   314
		result ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   315
! !
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   316
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   317
!PPParser methodsFor:'operators-mapping'!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   318
377
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   319
>=> aBlock
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   320
	"Answer a new parser that wraps the receiving parser with a two argument block. The first argument is the parsed stream, the second argument a continuation block on the delegate parser."
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   321
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   322
	^ PPWrappingParser on: self block: aBlock
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   323
!
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   324
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   325
foldLeft: aBlock
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   326
	"Answer a new parser that that folds the result of the receiver from left-to-right into aBlock. The argument aBlock must take two or more arguments."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   327
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   328
	| size args |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   329
	size := aBlock numArgs.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   330
	args := Array new: size.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   331
	^ self ==> [ :nodes |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   332
		args at: 1 put: nodes first.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   333
		2 to: nodes size by: size - 1 do: [ :index |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   334
			args
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   335
				replaceFrom: 2 to: size with: nodes startingAt: index;
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   336
				at: 1 put: (aBlock valueWithArguments: args) ].
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   337
		args first ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   338
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   339
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   340
foldRight: aBlock
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   341
	"Answer a new parser that that folds the result of the receiver from right-to-left into aBlock. The argument aBlock must take two or more arguments."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   342
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   343
	| size args |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   344
	size := aBlock numArgs.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   345
	args := Array new: size.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   346
	^ self ==> [ :nodes |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   347
		args at: size put: nodes last.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   348
		nodes size - size + 1 to: 1 by: 1 - size do: [ :index |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   349
			args
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   350
				replaceFrom: 1 to: size - 1 with: nodes startingAt: index;
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   351
				at: size put: (aBlock valueWithArguments: args) ].
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   352
		args at: size ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   353
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   354
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   355
map: aBlock
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   356
	"Answer a new parser that works on the receiving sequence an passes in each element as a block argument."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   357
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   358
	^ aBlock numArgs = 1
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   359
		ifTrue: [ self ==> aBlock ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   360
		ifFalse: [ self error: aBlock numArgs asString , ' arguments expected.' ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   361
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   362
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   363
trim: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   364
	"Answer a new parser that consumes and ignores aParser repeatedly before and after the receiving parser."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   365
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   366
	^ PPTrimmingParser on: self trimmer: aParser
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   367
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   368
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   369
trimBlanks
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   370
	"Answer a new parser that consumes blanks before and after the receiving parser."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   371
	
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   372
	^ self trim: #blank asParser
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   373
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   374
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   375
trimSpaces
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   376
	"Answer a new parser that consumes spaces before and after the receiving parser."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   377
	
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   378
	^ self trim: #space asParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   379
! !
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   380
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   381
!PPParser methodsFor:'operators-repeating'!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   382
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   383
max: anInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   384
	"Answer a new parser that parses the receiver at most anInteger times."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   385
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   386
	^ self star setMax: anInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   387
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   388
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   389
max: anInteger greedy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   390
	"Answer a new parser that parses the receiver at most anInteger times until it reaches aParser. This is a greedy non-blind implementation. aParser is not consumed."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   391
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   392
	^ (self starGreedy: aParser) setMax: anInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   393
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   394
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   395
max: anInteger lazy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   396
	"Answer a new parser that parses the receiver at most anInteger times until it reaches aParser. This is a lazy non-blind implementation. aParser is not consumed."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   397
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   398
	^ (self starLazy: aParser) setMax: anInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   399
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   400
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   401
min: anInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   402
	"Answer a new parser that parses the receiver at least anInteger times."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   403
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   404
	^ self star setMin: anInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   405
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   406
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   407
min: anInteger greedy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   408
	"Answer a new parser that parses the receiver at least anInteger times until it reaches aParser. This is a greedy non-blind implementation. aParser is not consumed."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   409
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   410
	^ (self starGreedy: aParser) setMin: anInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   411
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   412
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   413
min: anInteger lazy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   414
	"Answer a new parser that parses the receiver at least anInteger times until it reaches aParser. This is a lazy non-blind implementation. aParser is not consumed."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   415
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   416
	^ (self starLazy: aParser) setMin: anInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   417
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   418
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   419
min: aMinInteger max: aMaxInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   420
	"Answer a new parser that parses the receiver at least aMinInteger and at most aMaxInteger times."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   421
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   422
	^ self star setMin: aMinInteger; setMax: aMaxInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   423
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   424
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   425
min: aMinInteger max: aMaxInteger greedy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   426
	"Answer a new parser that parses the receiver at least aMinInteger and at most aMaxInteger times until it reaches aParser. This is a greedy non-blind implementation. aParser is not consumed."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   427
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   428
	^ (self starGreedy: aParser) setMin: aMinInteger; setMax: aMaxInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   429
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   430
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   431
min: aMinInteger max: aMaxInteger lazy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   432
	"Answer a new parser that parses the receiver at least aMinInteger and at most aMaxInteger times until it reaches aParser. This is a greedy non-blind implementation. aParser is not consumed."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   433
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   434
	^ (self starLazy: aParser) setMin: aMinInteger; setMax: aMaxInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   435
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   436
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   437
plus
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   438
	"Answer a new parser that parses the receiver one or more times."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   439
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   440
	^ self star setMin: 1
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   441
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   442
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   443
plusGreedy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   444
	"Answer a new parser that parses the receiver one or more times until it reaches aParser. This is a greedy non-blind implementation of the star operator. aParser is not consumed."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   445
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   446
	^ (self starGreedy: aParser) setMin: 1
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   447
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   448
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   449
plusLazy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   450
	"Answer a new parser that parses the receiver one or more times until it reaches aParser. This is a lazy non-blind implementation of the star operator. aParser is not consumed."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   451
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   452
	^ (self starLazy: aParser) setMin: 1
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   453
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   454
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   455
star
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   456
	"Answer a new parser that parses the receiver zero or more times. This is a greedy and blind implementation that tries to consume as much input as possible and it does not consider what comes afterwards."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   457
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   458
	^ PPPossessiveRepeatingParser on: self
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   459
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   460
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   461
starGreedy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   462
	"Answer a new parser that parses the receiver zero or more times until it reaches aParser. This is a greedy non-blind implementation of the star operator. aParser is not consumed."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   463
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   464
	^ PPGreedyRepeatingParser on: self limit: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   465
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   466
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   467
starLazy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   468
	"Answer a new parser that parses the receiver zero or more times until it reaches aParser. This is a lazy non-blind implementation of the star operator. aParser is not consumed."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   469
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   470
	^ PPLazyRepeatingParser on: self limit: aParser
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   471
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   472
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   473
!PPParser methodsFor:'parsing'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   474
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   475
matches: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   476
	"Answer if anObject can be parsed by the receiver."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   477
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   478
	^ (self parse: anObject) isPetitFailure not
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   479
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   480
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   481
matchesIn: anObject
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   482
	"Search anObject repeatedly for the matches of the receiver. Answered an OrderedCollection of the matched parse-trees."
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   483
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   484
	| result |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   485
	result := OrderedCollection new.
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   486
	self 
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   487
		matchesIn: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   488
		do: [ :each | result addLast: each ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   489
	^ result
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   490
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   491
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   492
matchesIn: anObject do: aBlock
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   493
	"Search anObject repeatedly for the matches of the receiver. Evaluate aBlock for each match with the matched parse-tree as the argument. Make sure to always consume exactly one character with each step, to not miss any match."
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   494
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   495
	((self and ==> aBlock , #any asParser) / #any asParser) star parse: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   496
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   497
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   498
matchesSkipIn: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   499
	"Search anObject repeatedly for the matches of the receiver. Answer an OrderedCollection of the matched parse-trees. Skip over matches."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   500
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   501
	| result |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   502
	result := OrderedCollection new.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   503
	self 
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   504
		matchesSkipIn: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   505
		do: [ :each | result addLast: each ].
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   506
	^ result
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   507
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   508
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   509
matchesSkipIn: anObject do: aBlock
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   510
	"Search anObject repeatedly for the matches of the receiver. Evaluate aBlock for each match with the matched parse-tree as the argument. Skip over matches."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   511
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   512
	(self ==> aBlock / #any asParser) star parse: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   513
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   514
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   515
matchingRangesIn: anObject
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   516
	"Search anObject repeatedly for the matches of the receiver. Answer an OrderedCollection of ranges of each match (index of first character to: index of last character)."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   517
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   518
	| result |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   519
	result := OrderedCollection new.
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   520
	self
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   521
		matchingRangesIn: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   522
		do: [ :value | result addLast: value ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   523
	^ result
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   524
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   525
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   526
matchingRangesIn: anObject do: aBlock
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   527
	"Search anObject repeatedly for the matches of the receiver. Evaluate aBlock with the range of each match (index of first character to: index of last character)."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   528
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   529
	self token
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   530
		matchesIn: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   531
		do: [ :token | aBlock value: (token start to: token stop) ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   532
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   533
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   534
matchingSkipRangesIn: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   535
	"Search anObject repeatedly for the matches of the receiver. Skip over matches. Answer an OrderedCollection of ranges of each match (index of first character to: index of last character)."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   536
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   537
	| result |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   538
	result := OrderedCollection new.
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   539
	self
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   540
		matchingSkipRangesIn: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   541
		do: [ :value | result addLast: value ].
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   542
	^ result
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   543
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   544
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   545
matchingSkipRangesIn: anObject do: aBlock
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   546
	"Search anObject repeatedly for the matches of the receiver. Skip over matches. Evaluate aBlock with the range of each match (index of first character to: index of last character)."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   547
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   548
	self token
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   549
		matchesSkipIn: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   550
		do: [ :token | aBlock value: (token start to: token stop) ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   551
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   552
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   553
parse: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   554
	"Parse anObject with the receiving parser and answer the parse-result or an instance of PPFailure."
377
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   555
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   556
	^ self parse: anObject withContext: PPContext new
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   557
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   558
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   559
parse: anObject onError: aBlock
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   560
	"Parse anObject with the receiving parser and answer the parse-result or answer the result of evaluating aBlock. Depending on the number of arguments of the block it is simply evaluated, evaluated with the failure object, or evaluated with the error message and position."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   561
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   562
	| result |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   563
	result := self parse: anObject.
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   564
	result isPetitFailure
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   565
		ifFalse: [ ^ result ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   566
	aBlock numArgs = 0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   567
		ifTrue: [ ^ aBlock value ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   568
	aBlock numArgs = 1
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   569
		ifTrue: [ ^ aBlock value: result ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   570
	^ aBlock value: result message value: result position
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   571
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   572
377
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   573
parseOn: aPPContext
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   574
	"Parse aStream with the receiving parser and answer the parse-result or an instance of PPFailure. Override this method in subclasses to specify custom parse behavior. Do not call this method from outside, instead use #parse:."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   575
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   576
	self subclassResponsibility
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   577
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   578
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   579
!PPParser methodsFor:'printing'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   580
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   581
printNameOn: aStream
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   582
	self name isNil
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   583
		ifTrue: [ aStream print: self hash ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   584
		ifFalse: [ aStream nextPutAll: self name ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   585
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   586
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   587
printOn: aStream
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   588
	super printOn: aStream.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   589
	aStream nextPut: $(.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   590
	self printNameOn: aStream.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   591
	aStream nextPut: $)
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   592
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   593
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   594
!PPParser methodsFor:'testing'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   595
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   596
isPetitParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   597
	^ true
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   598
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   599
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   600
isUnresolved
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   601
	^ false
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   602
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   603
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   604
!PPParser class methodsFor:'documentation'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   605
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   606
version
366
225737f7f83f class: PPParser
Claus Gittinger <cg@exept.de>
parents: 173
diff changeset
   607
    ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPParser.st,v 1.7 2014-03-04 23:58:41 cg Exp $'
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   608
!
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   609
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   610
version_CVS
366
225737f7f83f class: PPParser
Claus Gittinger <cg@exept.de>
parents: 173
diff changeset
   611
    ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPParser.st,v 1.7 2014-03-04 23:58:41 cg Exp $'
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   612
!
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   613
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   614
version_SVN
366
225737f7f83f class: PPParser
Claus Gittinger <cg@exept.de>
parents: 173
diff changeset
   615
    ^ '$Id: PPParser.st,v 1.7 2014-03-04 23:58:41 cg Exp $'
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   616
! !
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   617