PPParser.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sat, 04 Oct 2014 21:26:15 +0100
changeset 381 0bbbcf5da2d4
parent 377 6112a403a52d
child 389 009c2e13973c
permissions -rw-r--r--
`PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package. The method #allParsersDo: is actually used used by PPContext>>initializeFor: therefore it has to be in the package stx:goodies/petitparser (MC package PetitParser) and _NOT_ in stx:goodies/petitparser/analyzer (MC package PetitAnalyzer)
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
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
!PPParser methodsFor:'accessing'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
children
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
	"Answer a set of child parsers that could follow the receiver."
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
	^ #()
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
name
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
	"Answer the production name of the receiver."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    37
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
	^ self propertyAt: #name ifAbsent: [ nil ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
name: aString
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
	self propertyAt: #name put: aString
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
!PPParser methodsFor:'accessing-properties'!
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
hasProperty: aKey
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
	"Test if the property aKey is present."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    49
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
	^ properties notNil and: [ properties includesKey: aKey ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
propertyAt: aKey
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
	"Answer the property value associated with aKey."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    55
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
	^ self propertyAt: aKey ifAbsent: [ self error: 'Property not found' ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
propertyAt: aKey ifAbsent: aBlock
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
	"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
    61
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
	^ properties isNil
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
		ifTrue: [ aBlock value ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
		ifFalse: [ properties at: aKey ifAbsent: aBlock ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
propertyAt: aKey ifAbsentPut: aBlock
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
	"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
    69
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
	^ self propertyAt: aKey ifAbsent: [ self propertyAt: aKey put: aBlock value ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
propertyAt: aKey put: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
	"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
    75
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
	^ (properties ifNil: [ properties := Dictionary new: 1 ])
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
		at: aKey put: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
removeProperty: aKey
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
	"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
    82
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
	^ self removeProperty: aKey ifAbsent: [ self error: 'Property not found' ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
removeProperty: aKey ifAbsent: aBlock
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
	"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
    88
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
	| answer |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
	properties isNil ifTrue: [ ^ aBlock value ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
	answer := properties removeKey: aKey ifAbsent: aBlock.
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
	properties isEmpty ifTrue: [ properties := nil ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
	^ answer
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
377
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
    96
!PPParser methodsFor:'context'!
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
    97
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
    98
parse: anObject withContext: aPPContext
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
    99
	"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
   100
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   101
	aPPContext stream: anObject asPetitStream.
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   102
	^ self parseWithContext: aPPContext.
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   103
!
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   104
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   105
parseWithContext: context
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   106
	| result |
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   107
	context initializeFor: self.
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   108
	result := self parseOn: context.
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   109
	
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   110
	"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
   111
	result isPetitFailure ifTrue: [ ^ context furthestFailure ].
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   112
	^ result
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   113
	
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   114
!
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
updateContext: aPPContext
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   117
	"nothing to do"
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   118
! !
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   119
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
!PPParser methodsFor:'converting'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
asParser
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   123
	"Answer the receiving parser."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   124
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
	^ self
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
!PPParser methodsFor:'copying'!
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
postCopy
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
	super postCopy.
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
	properties := properties copy
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
381
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   135
!PPParser methodsFor:'enumerating'!
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   136
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   137
allParsers
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   138
	"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
   139
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   140
	| result |
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   141
	result := OrderedCollection new.
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   142
	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
   143
	^ result
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   144
!
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   145
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   146
allParsersDo: aBlock
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   147
	"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
   148
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   149
	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
   150
!
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   151
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   152
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
   153
	"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
   154
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   155
	(aSet includes: self)
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   156
		ifTrue: [ ^ self ].
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   157
	aSet add: self.
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   158
	aBlock value: self.
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   159
	self children
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   160
		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
   161
! !
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   162
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
!PPParser methodsFor:'initialization'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
initialize
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
!PPParser methodsFor:'operations'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   169
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   170
, aParser 
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
	"Answer a new parser that parses the receiver followed by aParser."
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   172
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
	^ PPSequenceParser with: self with: 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
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   176
/ aParser 
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
	"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
   178
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
	^ PPChoiceParser with: self with: aParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
and
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   183
	"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
   184
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
	^ PPAndParser on: self
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   188
def: aParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   189
	"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
   190
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   191
	^ self becomeForward: (aParser name: self name)
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
end
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
	"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
   196
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   197
	^ PPEndOfInputParser on: self
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   200
memoized
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   201
	"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
   202
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
	^ PPMemoizedParser on: self
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   206
negate
92
a95137a3ab6a class: PPToken
Claus Gittinger <cg@exept.de>
parents: 22
diff changeset
   207
        "Answer a new parser consumes any input token but the receiver."
a95137a3ab6a class: PPToken
Claus Gittinger <cg@exept.de>
parents: 22
diff changeset
   208
        
a95137a3ab6a class: PPToken
Claus Gittinger <cg@exept.de>
parents: 22
diff changeset
   209
        ^ self not , #any asParser ==> #second
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   210
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   211
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   212
not
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   213
	"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
   214
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   215
	^ PPNotParser on: self
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
optional
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
	"Answer a new parser that parses the receiver, if possible."
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   221
	^ PPOptionalParser on: self
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   224
times: anInteger
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   225
	"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
   226
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   227
	^ self min: anInteger max: anInteger
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   228
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   229
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   230
wrapped
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   231
	"Answer a new parser that is simply wrapped."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   232
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   233
	^ PPDelegateParser on: self
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   234
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   235
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   236
| aParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   237
	"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
   238
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   239
	^ (self not , aParser) / (aParser not , self) ==> #second
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   242
!PPParser methodsFor:'operations-convenience'!
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
delimitedBy: aParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   245
	"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
   246
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   247
	^ (self separatedBy: aParser) , (aParser optional) ==> [ :node |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   248
		node second isNil
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   249
			ifTrue: [ node first ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   250
			ifFalse: [ node first copyWith: node second ] ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   251
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   252
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   253
separatedBy: aParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   254
	"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
   255
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   256
	^ (PPSequenceParser with: self with: (PPSequenceParser with: aParser with: self) star) ==> [ :nodes |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   257
		| result |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   258
		result := Array new: 2 * nodes second size + 1.
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   259
		result at: 1 put: nodes first.
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   260
		nodes second 
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   261
			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
   262
		result ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   263
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   264
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   265
!PPParser methodsFor:'operations-mapping'!
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
==> aBlock
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   268
	"Answer a new parser that performs aBlock as action handler on success."
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   269
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   270
	^ PPActionParser on: self block: aBlock
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   273
answer: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   274
	"Answer a new parser that always returns anObject from a successful parse."
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   275
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   276
	^ self ==> [ :nodes | anObject ]
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   279
flatten
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   280
	"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
   281
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   282
	^ PPFlattenParser on: self
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   283
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   284
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   285
token
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   286
	"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
   287
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   288
	^ PPTokenParser on: self
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   289
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   290
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   291
token: aTokenClass
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   292
	"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
   293
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   294
	^ self token tokenClass: aTokenClass
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   295
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   296
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   297
trim
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   298
	"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
   299
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   300
	^ self trimSpaces
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   301
! !
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   302
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   303
!PPParser methodsFor:'operators-convenience'!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   304
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   305
withoutSeparators
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   306
	"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
   307
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   308
	^ self ==> [ :items |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   309
		| result |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   310
		result := Array new: items size + 1 // 2.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   311
		1 to: result size do: [ :index | result at: index put: (items at: 2 * index - 1) ].
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   312
		result ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   313
! !
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   314
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   315
!PPParser methodsFor:'operators-mapping'!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   316
377
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   317
>=> aBlock
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   318
	"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
   319
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   320
	^ PPWrappingParser on: self block: aBlock
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
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   323
foldLeft: aBlock
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   324
	"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
   325
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   326
	| size args |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   327
	size := aBlock numArgs.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   328
	args := Array new: size.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   329
	^ self ==> [ :nodes |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   330
		args at: 1 put: nodes first.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   331
		2 to: nodes size by: size - 1 do: [ :index |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   332
			args
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   333
				replaceFrom: 2 to: size with: nodes startingAt: index;
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   334
				at: 1 put: (aBlock valueWithArguments: args) ].
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   335
		args first ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   336
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   337
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   338
foldRight: aBlock
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   339
	"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
   340
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   341
	| size args |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   342
	size := aBlock numArgs.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   343
	args := Array new: size.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   344
	^ self ==> [ :nodes |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   345
		args at: size put: nodes last.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   346
		nodes size - size + 1 to: 1 by: 1 - size do: [ :index |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   347
			args
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   348
				replaceFrom: 1 to: size - 1 with: nodes startingAt: index;
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   349
				at: size put: (aBlock valueWithArguments: args) ].
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   350
		args at: size ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   351
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   352
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   353
map: aBlock
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   354
	"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
   355
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   356
	^ aBlock numArgs = 1
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   357
		ifTrue: [ self ==> aBlock ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   358
		ifFalse: [ self error: aBlock numArgs asString , ' arguments expected.' ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   359
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   360
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   361
trim: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   362
	"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
   363
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   364
	^ PPTrimmingParser on: self trimmer: aParser
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   365
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   366
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   367
trimBlanks
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   368
	"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
   369
	
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   370
	^ self trim: #blank asParser
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   371
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   372
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   373
trimSpaces
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   374
	"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
   375
	
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   376
	^ self trim: #space asParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   377
! !
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   378
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   379
!PPParser methodsFor:'operators-repeating'!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   380
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   381
max: anInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   382
	"Answer a new parser that parses the receiver at most anInteger times."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   383
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   384
	^ self star setMax: anInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   385
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   386
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   387
max: anInteger greedy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   388
	"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
   389
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   390
	^ (self starGreedy: aParser) setMax: anInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   391
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   392
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   393
max: anInteger lazy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   394
	"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
   395
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   396
	^ (self starLazy: aParser) setMax: anInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   397
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   398
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   399
min: anInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   400
	"Answer a new parser that parses the receiver at least anInteger times."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   401
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   402
	^ self star setMin: anInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   403
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   404
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   405
min: anInteger greedy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   406
	"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
   407
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   408
	^ (self starGreedy: aParser) setMin: anInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   409
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   410
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   411
min: anInteger lazy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   412
	"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
   413
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   414
	^ (self starLazy: aParser) setMin: anInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   415
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   416
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   417
min: aMinInteger max: aMaxInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   418
	"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
   419
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   420
	^ self star setMin: aMinInteger; setMax: aMaxInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   421
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   422
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   423
min: aMinInteger max: aMaxInteger greedy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   424
	"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
   425
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   426
	^ (self starGreedy: aParser) setMin: aMinInteger; setMax: aMaxInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   427
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   428
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   429
min: aMinInteger max: aMaxInteger lazy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   430
	"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
   431
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   432
	^ (self starLazy: aParser) setMin: aMinInteger; setMax: aMaxInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   433
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   434
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   435
plus
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   436
	"Answer a new parser that parses the receiver one or more times."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   437
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   438
	^ self star setMin: 1
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   439
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   440
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   441
plusGreedy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   442
	"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
   443
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   444
	^ (self starGreedy: aParser) setMin: 1
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   445
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   446
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   447
plusLazy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   448
	"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
   449
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   450
	^ (self starLazy: aParser) setMin: 1
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   451
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   452
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   453
star
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   454
	"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
   455
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   456
	^ PPPossessiveRepeatingParser on: self
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   457
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   458
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   459
starGreedy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   460
	"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
   461
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   462
	^ PPGreedyRepeatingParser on: self limit: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   463
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   464
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   465
starLazy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   466
	"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
   467
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   468
	^ PPLazyRepeatingParser on: self limit: aParser
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   469
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   470
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   471
!PPParser methodsFor:'parsing'!
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
matches: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   474
	"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
   475
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   476
	^ (self parse: anObject) isPetitFailure not
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   477
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   478
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   479
matchesIn: anObject
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   480
	"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
   481
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   482
	| result |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   483
	result := OrderedCollection new.
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   484
	self 
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   485
		matchesIn: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   486
		do: [ :each | result addLast: each ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   487
	^ result
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   488
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   489
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   490
matchesIn: anObject do: aBlock
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   491
	"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
   492
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   493
	((self and ==> aBlock , #any asParser) / #any asParser) star parse: anObject
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
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   496
matchesSkipIn: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   497
	"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
   498
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   499
	| result |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   500
	result := OrderedCollection new.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   501
	self 
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   502
		matchesSkipIn: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   503
		do: [ :each | result addLast: each ].
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   504
	^ result
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   505
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   506
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   507
matchesSkipIn: anObject do: aBlock
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   508
	"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
   509
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   510
	(self ==> aBlock / #any asParser) star parse: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   511
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   512
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   513
matchingRangesIn: anObject
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   514
	"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
   515
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   516
	| result |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   517
	result := OrderedCollection new.
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   518
	self
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   519
		matchingRangesIn: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   520
		do: [ :value | result addLast: value ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   521
	^ result
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   522
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   523
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   524
matchingRangesIn: anObject do: aBlock
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   525
	"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
   526
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   527
	self token
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   528
		matchesIn: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   529
		do: [ :token | aBlock value: (token start to: token stop) ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   530
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   531
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   532
matchingSkipRangesIn: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   533
	"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
   534
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   535
	| result |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   536
	result := OrderedCollection new.
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   537
	self
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   538
		matchingSkipRangesIn: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   539
		do: [ :value | result addLast: value ].
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   540
	^ result
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   541
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   542
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   543
matchingSkipRangesIn: anObject do: aBlock
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   544
	"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
   545
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   546
	self token
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   547
		matchesSkipIn: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   548
		do: [ :token | aBlock value: (token start to: token stop) ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   549
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   550
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   551
parse: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   552
	"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
   553
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   554
	^ self parse: anObject withContext: PPContext new
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   555
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   556
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   557
parse: anObject onError: aBlock
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   558
	"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
   559
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   560
	| result |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   561
	result := self parse: anObject.
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   562
	result isPetitFailure
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   563
		ifFalse: [ ^ result ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   564
	aBlock numArgs = 0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   565
		ifTrue: [ ^ aBlock value ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   566
	aBlock numArgs = 1
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   567
		ifTrue: [ ^ aBlock value: result ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   568
	^ aBlock value: result message value: result position
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   569
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   570
377
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   571
parseOn: aPPContext
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   572
	"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
   573
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   574
	self subclassResponsibility
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   575
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   576
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   577
!PPParser methodsFor:'printing'!
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
printNameOn: aStream
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   580
	self name isNil
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   581
		ifTrue: [ aStream print: self hash ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   582
		ifFalse: [ aStream nextPutAll: self name ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   583
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   584
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   585
printOn: aStream
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   586
	super printOn: aStream.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   587
	aStream nextPut: $(.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   588
	self printNameOn: aStream.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   589
	aStream nextPut: $)
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   590
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   591
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   592
!PPParser methodsFor:'testing'!
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
isPetitParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   595
	^ true
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   596
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   597
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   598
isUnresolved
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   599
	^ false
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   600
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   601
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   602
!PPParser class methodsFor:'documentation'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   603
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   604
version
366
225737f7f83f class: PPParser
Claus Gittinger <cg@exept.de>
parents: 173
diff changeset
   605
    ^ '$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
   606
!
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   607
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   608
version_CVS
366
225737f7f83f class: PPParser
Claus Gittinger <cg@exept.de>
parents: 173
diff changeset
   609
    ^ '$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
   610
!
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   611
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   612
version_SVN
366
225737f7f83f class: PPParser
Claus Gittinger <cg@exept.de>
parents: 173
diff changeset
   613
    ^ '$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
   614
! !
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   615