PPParser.st
changeset 645 2f598ee17a2d
parent 637 b19fe5d5f1dc
child 647 ee23ff60ceec
equal deleted inserted replaced
644:0bf7cd45f7eb 645:2f598ee17a2d
       
     1 "{ Encoding: utf8 }"
       
     2 
     1 "{ Package: 'stx:goodies/petitparser' }"
     3 "{ Package: 'stx:goodies/petitparser' }"
     2 
     4 
     3 "{ NameSpace: Smalltalk }"
     5 "{ NameSpace: Smalltalk }"
     4 
     6 
     5 Object subclass:#PPParser
     7 Object subclass:#PPParser
   294 				at: size put: (aBlock valueWithArguments: args) ].
   296 				at: size put: (aBlock valueWithArguments: args) ].
   295 		args at: size ]
   297 		args at: size ]
   296 !
   298 !
   297 
   299 
   298 map: aBlock
   300 map: aBlock
   299 	"Answer a new parser that works on the receiving sequence an passes in each element as a block argument."
   301         "Answer a new parser that works on the receiving sequence an passes in each element as a block argument."
   300 	
   302         
   301 	^ aBlock numArgs = 1
   303         ^ aBlock argumentCount == 1
   302 		ifTrue: [ self ==> aBlock ]
   304                 ifTrue: [ self ==> aBlock ]
   303 		ifFalse: [ self error: aBlock numArgs asString , ' arguments expected.' ]
   305                 ifFalse: [ self error: aBlock numArgs asString , ' arguments expected.' ]
   304 !
   306 !
   305 
   307 
   306 trim: aParser
   308 trim: aParser
   307 	"Answer a new parser that consumes and ignores aParser repeatedly before and after the receiving parser."
   309 	"Answer a new parser that consumes and ignores aParser repeatedly before and after the receiving parser."
   308 	
   310 	
   498 	
   500 	
   499 	^ self parseOn: anObject asPetitStream
   501 	^ self parseOn: anObject asPetitStream
   500 !
   502 !
   501 
   503 
   502 parse: anObject onError: aBlock
   504 parse: anObject onError: aBlock
   503 	"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."
   505         "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."
   504 	
   506         
   505 	| result |
   507         | result numArgs |
   506 	result := self parse: anObject.
   508 
   507 	result isPetitFailure
   509         result := self parse: anObject.
   508 		ifFalse: [ ^ result ].
   510         result isPetitFailure ifFalse: [ ^ result ].
   509 	aBlock numArgs = 0
   511         numArgs := aBlock argumentCount.
   510 		ifTrue: [ ^ aBlock value ].
   512         numArgs == 0 ifTrue: [ ^ aBlock value ].
   511 	aBlock numArgs = 1
   513         numArgs == 1 ifTrue: [ ^ aBlock value: result ].
   512 		ifTrue: [ ^ aBlock value: result ].
   514         ^ aBlock value:(result message) value:(result position)
   513 	^ aBlock value: result message value: result position
       
   514 !
   515 !
   515 
   516 
   516 parseOn: aStream
   517 parseOn: aStream
   517 	"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:."
   518 	"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:."
   518 	
   519