PPParser.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Mon, 24 Nov 2014 00:09:23 +0000
changeset 421 7e08b31e0dae
parent 389 009c2e13973c
child 427 a7f5e6de19d2
permissions -rw-r--r--
Merged JK's version from Monticello Name: PetitParser-JanKurs.260 Author: JanKurs Time: 17-11-2014, 12:09:05.490 PM UUID: 07411cef-ef69-40db-9d93-d4018a9b34ef Name: PetitTests-JanKurs.65 Author: JanKurs Time: 17-11-2014, 12:09:04.530 PM UUID: f98d613f-f4ce-4e0e-a7e9-310ee7c7e7a6 Name: PetitSmalltalk-JanKurs.78 Author: JanKurs Time: 14-11-2014, 05:05:07.765 PM UUID: 3d68330d-44d5-46c3-9705-97f627b3edbc Name: PetitCompiler-JanKurs.71 Author: JanKurs Time: 18-11-2014, 09:48:35.425 AM UUID: 06352c33-3c76-4382-8536-0cc48e225117 Name: PetitCompiler-Tests-JanKurs.21 Author: JanKurs Time: 17-11-2014, 05:51:53.134 PM UUID: 8d6c0799-14e7-4871-8d91-8b0f9886db83 Name: PetitCompiler-Benchmarks-JanKurs.2 Author: JanKurs Time: 17-11-2014, 05:51:07.887 PM UUID: d5e3a980-7871-487a-a232-e3ca93fc2483
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
421
7e08b31e0dae Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 389
diff changeset
    29
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
!PPParser methodsFor:'accessing'!
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
children
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
	"Answer a set of child parsers that could follow the receiver."
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
name
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
	"Answer the production name of the receiver."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    40
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
	^ self propertyAt: #name ifAbsent: [ nil ]
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
name: aString
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
	self propertyAt: #name put: aString
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
!PPParser methodsFor:'accessing-properties'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
hasProperty: aKey
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
	"Test if the property aKey is present."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    52
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
	^ properties notNil and: [ properties includesKey: aKey ]
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
propertyAt: aKey
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
	"Answer the property value associated with aKey."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
    58
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
	^ self propertyAt: aKey ifAbsent: [ self error: 'Property not found' ]
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
propertyAt: aKey ifAbsent: aBlock
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
	"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
    64
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
	^ properties isNil
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
		ifTrue: [ aBlock value ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
		ifFalse: [ properties at: aKey ifAbsent: aBlock ]
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
propertyAt: aKey ifAbsentPut: aBlock
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
	"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
    72
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
	^ self propertyAt: aKey ifAbsent: [ self propertyAt: aKey put: aBlock value ]
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
propertyAt: aKey put: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
	"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
    78
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
	^ (properties ifNil: [ properties := Dictionary new: 1 ])
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
		at: aKey put: anObject
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
removeProperty: aKey
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
	"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
    85
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
	^ self removeProperty: aKey ifAbsent: [ self error: 'Property not found' ]
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
removeProperty: aKey ifAbsent: aBlock
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
	"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
    91
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
	| answer |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
	properties isNil ifTrue: [ ^ aBlock value ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
	answer := properties removeKey: aKey ifAbsent: aBlock.
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
	properties isEmpty ifTrue: [ properties := nil ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
	^ answer
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
377
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
    99
!PPParser methodsFor:'context'!
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
parse: anObject withContext: aPPContext
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   102
	"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
   103
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   104
	aPPContext stream: anObject asPetitStream.
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   105
	^ self parseWithContext: aPPContext.
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
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   108
parseWithContext: context
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   109
	| result |
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   110
	context initializeFor: self.
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   111
	result := self parseOn: context.
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   112
	
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   113
	"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
   114
	result isPetitFailure ifTrue: [ ^ context furthestFailure ].
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   115
	^ result
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
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   119
updateContext: aPPContext
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   120
	"nothing to do"
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   121
! !
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   122
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
!PPParser methodsFor:'converting'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
asParser
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   126
	"Answer the receiving parser."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   127
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
	^ self
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
!PPParser methodsFor:'copying'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
postCopy
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
	super postCopy.
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
	properties := properties copy
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
381
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   138
!PPParser methodsFor:'enumerating'!
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
allParsers
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   141
	"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
   142
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
	result := OrderedCollection new.
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   145
	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
   146
	^ result
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
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   149
allParsersDo: aBlock
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   150
	"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
   151
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   152
	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
   153
!
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
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
   156
	"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
   157
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   158
	(aSet includes: self)
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   159
		ifTrue: [ ^ self ].
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   160
	aSet add: self.
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   161
	aBlock value: self.
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   162
	self children
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   163
		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
   164
! !
0bbbcf5da2d4 `PPParser>>allParsers`, `allParsersDo:(seen:)` moved to base PetitParser package.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 377
diff changeset
   165
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   166
!PPParser methodsFor:'initialization'!
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
initialize
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
!PPParser methodsFor:'operations'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   172
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   173
, aParser 
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   174
	"Answer a new parser that parses the receiver followed by aParser."
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   175
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   176
	^ PPSequenceParser with: self with: aParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   179
/ aParser 
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
	"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
   181
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
	^ PPChoiceParser with: self with: aParser
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
and
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
	"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
   187
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   188
	^ PPAndParser on: self
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   191
def: aParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   192
	"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
   193
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
	^ self becomeForward: (aParser name: self name)
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   197
end
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   198
	"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
   199
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   200
	^ PPEndOfInputParser on: self
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
memoized
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
	"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
   205
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   206
	^ PPMemoizedParser on: self
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   209
negate
92
a95137a3ab6a class: PPToken
Claus Gittinger <cg@exept.de>
parents: 22
diff changeset
   210
        "Answer a new parser consumes any input token but the receiver."
a95137a3ab6a class: PPToken
Claus Gittinger <cg@exept.de>
parents: 22
diff changeset
   211
        
a95137a3ab6a class: PPToken
Claus Gittinger <cg@exept.de>
parents: 22
diff changeset
   212
        ^ self not , #any asParser ==> #second
0
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   215
not
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   216
	"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
   217
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
	^ PPNotParser on: self
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   221
optional
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   222
	"Answer a new parser that parses the receiver, if possible."
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
	^ PPOptionalParser on: self
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   227
times: anInteger
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   228
	"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
   229
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   230
	^ self min: anInteger max: anInteger
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   233
wrapped
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   234
	"Answer a new parser that is simply wrapped."
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   235
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   236
	^ PPDelegateParser on: self
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   239
| aParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   240
	"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
   241
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   242
	^ (self not , aParser) / (aParser not , self) ==> #second
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   245
!PPParser methodsFor:'operations-convenience'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   246
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   247
delimitedBy: aParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   248
	"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
   249
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   250
	^ (self separatedBy: aParser) , (aParser optional) ==> [ :node |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   251
		node second isNil
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   252
			ifTrue: [ node first ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   253
			ifFalse: [ node first copyWith: node second ] ]
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   256
separatedBy: aParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   257
	"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
   258
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   259
	^ (PPSequenceParser with: self with: (PPSequenceParser with: aParser with: self) star) ==> [ :nodes |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   260
		| result |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   261
		result := Array new: 2 * nodes second size + 1.
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   262
		result at: 1 put: nodes first.
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   263
		nodes second 
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   264
			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
   265
		result ]
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   268
!PPParser methodsFor:'operations-mapping'!
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
==> aBlock
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   271
	"Answer a new parser that performs aBlock as action handler on success."
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
	^ PPActionParser on: self block: aBlock
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   276
answer: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   277
	"Answer a new parser that always returns anObject from a successful parse."
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
	^ self ==> [ :nodes | anObject ]
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   282
flatten
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   283
	"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
   284
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   285
	^ PPFlattenParser on: self
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   288
token
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   289
	"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
   290
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   291
	^ PPTokenParser on: self
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   294
token: aTokenClass
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   295
	"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
   296
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   297
	^ self token tokenClass: aTokenClass
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   300
trim
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   301
	"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
   302
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   303
	^ self trimSpaces
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   304
! !
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   305
421
7e08b31e0dae Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 389
diff changeset
   306
!PPParser methodsFor:'operators'!
7e08b31e0dae Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 389
diff changeset
   307
7e08b31e0dae Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 389
diff changeset
   308
// aParser 
7e08b31e0dae Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 389
diff changeset
   309
	"
7e08b31e0dae Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 389
diff changeset
   310
		Answer a new parser that parses the receiver, if the receiver fails try with aParser (ordered-choice).
7e08b31e0dae Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 389
diff changeset
   311
		If the receiver passes, limit must pass as well.
7e08b31e0dae Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 389
diff changeset
   312
	"
7e08b31e0dae Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 389
diff changeset
   313
	
7e08b31e0dae Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 389
diff changeset
   314
	^ PPLimitedChoiceParser with: self with: aParser
7e08b31e0dae Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 389
diff changeset
   315
! !
7e08b31e0dae Merged JK's version from Monticello
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 389
diff changeset
   316
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   317
!PPParser methodsFor:'operators-convenience'!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   318
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   319
withoutSeparators
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   320
	"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
   321
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   322
	^ self ==> [ :items |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   323
		| result |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   324
		result := Array new: items size + 1 // 2.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   325
		1 to: result size do: [ :index | result at: index put: (items at: 2 * index - 1) ].
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   326
		result ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   327
! !
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   328
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   329
!PPParser methodsFor:'operators-mapping'!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   330
377
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   331
>=> aBlock
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   332
	"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
   333
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   334
	^ PPWrappingParser on: self block: aBlock
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   335
!
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   336
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   337
foldLeft: aBlock
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   338
	"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
   339
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   340
	| size args |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   341
	size := aBlock numArgs.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   342
	args := Array new: size.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   343
	^ self ==> [ :nodes |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   344
		args at: 1 put: nodes first.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   345
		2 to: nodes size by: size - 1 do: [ :index |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   346
			args
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   347
				replaceFrom: 2 to: size with: nodes startingAt: index;
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   348
				at: 1 put: (aBlock valueWithArguments: args) ].
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   349
		args first ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   350
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   351
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   352
foldRight: aBlock
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   353
	"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
   354
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   355
	| size args |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   356
	size := aBlock numArgs.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   357
	args := Array new: size.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   358
	^ self ==> [ :nodes |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   359
		args at: size put: nodes last.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   360
		nodes size - size + 1 to: 1 by: 1 - size do: [ :index |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   361
			args
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   362
				replaceFrom: 1 to: size - 1 with: nodes startingAt: index;
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   363
				at: size put: (aBlock valueWithArguments: args) ].
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   364
		args at: size ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   365
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   366
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   367
map: aBlock
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   368
	"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
   369
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   370
	^ aBlock numArgs = 1
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   371
		ifTrue: [ self ==> aBlock ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   372
		ifFalse: [ self error: aBlock numArgs asString , ' arguments expected.' ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   373
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   374
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   375
trim: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   376
	"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
   377
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   378
	^ PPTrimmingParser on: self trimmer: aParser
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   379
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   380
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   381
trimBlanks
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   382
	"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
   383
	
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   384
	^ self trim: #blank asParser
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   385
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   386
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   387
trimSpaces
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   388
	"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
   389
	
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   390
	^ self trim: #space asParser
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
!PPParser methodsFor:'operators-repeating'!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   394
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   395
max: anInteger
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   396
	"Answer a new parser that parses the receiver at most anInteger times."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   397
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   398
	^ self star 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
max: anInteger greedy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   402
	"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
   403
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   404
	^ (self starGreedy: aParser) setMax: 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
max: anInteger lazy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   408
	"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
   409
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   410
	^ (self starLazy: aParser) setMax: 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
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   414
	"Answer a new parser that parses the receiver at least anInteger times."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   415
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   416
	^ self star 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: anInteger greedy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   420
	"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
   421
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   422
	^ (self starGreedy: aParser) setMin: anInteger
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: anInteger lazy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   426
	"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
   427
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   428
	^ (self starLazy: aParser) setMin: anInteger
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
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."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   433
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   434
	^ self star 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
min: aMinInteger max: aMaxInteger greedy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   438
	"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
   439
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   440
	^ (self starGreedy: aParser) setMin: aMinInteger; setMax: aMaxInteger
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
min: aMinInteger max: aMaxInteger lazy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   444
	"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
   445
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   446
	^ (self starLazy: aParser) setMin: aMinInteger; setMax: aMaxInteger
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
plus
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   450
	"Answer a new parser that parses the receiver one or more times."
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   451
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   452
	^ self star 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
plusGreedy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   456
	"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
   457
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   458
	^ (self starGreedy: aParser) setMin: 1
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
plusLazy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   462
	"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
   463
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   464
	^ (self starLazy: aParser) setMin: 1
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
star
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   468
	"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
   469
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   470
	^ PPPossessiveRepeatingParser on: self
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   471
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   472
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   473
starGreedy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   474
	"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
   475
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   476
	^ PPGreedyRepeatingParser on: self limit: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   477
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   478
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   479
starLazy: aParser
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   480
	"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
   481
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   482
	^ PPLazyRepeatingParser on: self limit: aParser
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   485
!PPParser methodsFor:'parsing'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   486
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   487
matches: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   488
	"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
   489
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   490
	^ (self parse: anObject) isPetitFailure not
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   493
matchesIn: anObject
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   494
	"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
   495
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   496
	| result |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   497
	result := OrderedCollection new.
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   498
	self 
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   499
		matchesIn: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   500
		do: [ :each | result addLast: each ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   501
	^ result
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   502
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   503
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   504
matchesIn: anObject do: aBlock
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   505
	"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
   506
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   507
	((self and ==> aBlock , #any asParser) / #any asParser) star parse: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   508
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   509
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   510
matchesSkipIn: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   511
	"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
   512
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   513
	| result |
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   514
	result := OrderedCollection new.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   515
	self 
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   516
		matchesSkipIn: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   517
		do: [ :each | result addLast: each ].
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   518
	^ result
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   519
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   520
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   521
matchesSkipIn: anObject do: aBlock
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   522
	"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
   523
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   524
	(self ==> aBlock / #any asParser) star parse: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   525
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   526
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   527
matchingRangesIn: anObject
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   528
	"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
   529
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   530
	| result |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   531
	result := OrderedCollection new.
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   532
	self
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   533
		matchingRangesIn: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   534
		do: [ :value | result addLast: value ].
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
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   537
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   538
matchingRangesIn: anObject do: aBlock
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   539
	"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
   540
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   541
	self token
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   542
		matchesIn: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   543
		do: [ :token | aBlock value: (token start to: token stop) ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   544
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   545
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   546
matchingSkipRangesIn: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   547
	"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
   548
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   549
	| result |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   550
	result := OrderedCollection new.
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   551
	self
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   552
		matchingSkipRangesIn: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   553
		do: [ :value | result addLast: value ].
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   554
	^ result
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
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   557
matchingSkipRangesIn: anObject do: aBlock
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   558
	"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
   559
	
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   560
	self token
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   561
		matchesSkipIn: anObject
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   562
		do: [ :token | aBlock value: (token start to: token stop) ]
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   563
!
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   564
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   565
parse: anObject
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   566
	"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
   567
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   568
	^ self parse: anObject withContext: PPContext new
0
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   571
parse: anObject onError: aBlock
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   572
	"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
   573
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   574
	| result |
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   575
	result := self parse: anObject.
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   576
	result isPetitFailure
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   577
		ifFalse: [ ^ result ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   578
	aBlock numArgs = 0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   579
		ifTrue: [ ^ aBlock value ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   580
	aBlock numArgs = 1
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   581
		ifTrue: [ ^ aBlock value: result ].
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   582
	^ aBlock value: result message value: result position
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
377
6112a403a52d Updated to latest version from Moose repository.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 366
diff changeset
   585
parseOn: aPPContext
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   586
	"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
   587
	
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   588
	self subclassResponsibility
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   589
! !
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
!PPParser methodsFor:'printing'!
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
printNameOn: aStream
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   594
	self name isNil
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   595
		ifTrue: [ aStream print: self hash ]
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   596
		ifFalse: [ aStream nextPutAll: self name ]
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
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   599
printOn: aStream
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   600
	super printOn: aStream.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   601
	aStream nextPut: $(.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   602
	self printNameOn: aStream.
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   603
	aStream nextPut: $)
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   604
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   605
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   606
!PPParser methodsFor:'testing'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   607
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   608
isPetitParser
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   609
	^ true
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   610
!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   611
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   612
isUnresolved
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   613
	^ false
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   614
! !
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   615
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   616
!PPParser class methodsFor:'documentation'!
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   617
22
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   618
version
366
225737f7f83f class: PPParser
Claus Gittinger <cg@exept.de>
parents: 173
diff changeset
   619
    ^ '$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
   620
!
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   621
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   622
version_CVS
366
225737f7f83f class: PPParser
Claus Gittinger <cg@exept.de>
parents: 173
diff changeset
   623
    ^ '$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
   624
!
c540c8649226 Checkin from browser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 4
diff changeset
   625
0
739fe9b7253e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   626
version_SVN
366
225737f7f83f class: PPParser
Claus Gittinger <cg@exept.de>
parents: 173
diff changeset
   627
    ^ '$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
   628
! !
173
Claus Gittinger <cg@exept.de>
parents: 126
diff changeset
   629