PPParser.st
changeset 377 6112a403a52d
parent 366 225737f7f83f
child 381 0bbbcf5da2d4
equal deleted inserted replaced
376:a2656b27cace 377:6112a403a52d
    23 
    23 
    24 
    24 
    25 
    25 
    26 
    26 
    27 
    27 
    28 
       
    29 
       
    30 
       
    31 
       
    32 
       
    33 !PPParser methodsFor:'accessing'!
    28 !PPParser methodsFor:'accessing'!
    34 
    29 
    35 children
    30 children
    36 	"Answer a set of child parsers that could follow the receiver."
    31 	"Answer a set of child parsers that could follow the receiver."
    37 
    32 
    95 	| answer |
    90 	| answer |
    96 	properties isNil ifTrue: [ ^ aBlock value ].
    91 	properties isNil ifTrue: [ ^ aBlock value ].
    97 	answer := properties removeKey: aKey ifAbsent: aBlock.
    92 	answer := properties removeKey: aKey ifAbsent: aBlock.
    98 	properties isEmpty ifTrue: [ properties := nil ].
    93 	properties isEmpty ifTrue: [ properties := nil ].
    99 	^ answer
    94 	^ answer
       
    95 ! !
       
    96 
       
    97 !PPParser methodsFor:'context'!
       
    98 
       
    99 parse: anObject withContext: aPPContext
       
   100 	"Parse anObject with the receiving parser and answer the parse-result or an instance of PPFailure."
       
   101 
       
   102 	aPPContext stream: anObject asPetitStream.
       
   103 	^ self parseWithContext: aPPContext.
       
   104 !
       
   105 
       
   106 parseWithContext: context
       
   107 	| result |
       
   108 	context initializeFor: self.
       
   109 	result := self parseOn: context.
       
   110 	
       
   111 	"Return the furthest failure, it gives better results than the last failure"
       
   112 	result isPetitFailure ifTrue: [ ^ context furthestFailure ].
       
   113 	^ result
       
   114 	
       
   115 !
       
   116 
       
   117 updateContext: aPPContext
       
   118 	"nothing to do"
   100 ! !
   119 ! !
   101 
   120 
   102 !PPParser methodsFor:'converting'!
   121 !PPParser methodsFor:'converting'!
   103 
   122 
   104 asParser
   123 asParser
   222 	"Answer a new parser that performs aBlock as action handler on success."
   241 	"Answer a new parser that performs aBlock as action handler on success."
   223 
   242 
   224 	^ PPActionParser on: self block: aBlock
   243 	^ PPActionParser on: self block: aBlock
   225 !
   244 !
   226 
   245 
   227 >=> aBlock
       
   228         "Answer a new parser that wraps the receiving parser with a two argument block. 
       
   229          The first argument is the parsed stream, the second argument a continuation block on the delegate parser."
       
   230 
       
   231         ^ PPWrappingParser on: self block: aBlock
       
   232 !
       
   233 
       
   234 answer: anObject
   246 answer: anObject
   235 	"Answer a new parser that always returns anObject from a successful parse."
   247 	"Answer a new parser that always returns anObject from a successful parse."
   236 
   248 
   237 	^ self ==> [ :nodes | anObject ]
   249 	^ self ==> [ :nodes | anObject ]
   238 !
   250 !
   272 		1 to: result size do: [ :index | result at: index put: (items at: 2 * index - 1) ].
   284 		1 to: result size do: [ :index | result at: index put: (items at: 2 * index - 1) ].
   273 		result ]
   285 		result ]
   274 ! !
   286 ! !
   275 
   287 
   276 !PPParser methodsFor:'operators-mapping'!
   288 !PPParser methodsFor:'operators-mapping'!
       
   289 
       
   290 >=> aBlock
       
   291 	"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."
       
   292 
       
   293 	^ PPWrappingParser on: self block: aBlock
       
   294 !
   277 
   295 
   278 foldLeft: aBlock
   296 foldLeft: aBlock
   279 	"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."
   297 	"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."
   280 	
   298 	
   281 	| size args |
   299 	| size args |
   503 		do: [ :token | aBlock value: (token start to: token stop) ]
   521 		do: [ :token | aBlock value: (token start to: token stop) ]
   504 !
   522 !
   505 
   523 
   506 parse: anObject
   524 parse: anObject
   507 	"Parse anObject with the receiving parser and answer the parse-result or an instance of PPFailure."
   525 	"Parse anObject with the receiving parser and answer the parse-result or an instance of PPFailure."
   508 	
   526 
   509 	^ self parseOn: anObject asPetitStream
   527 	^ self parse: anObject withContext: PPContext new
   510 !
   528 !
   511 
   529 
   512 parse: anObject onError: aBlock
   530 parse: anObject onError: aBlock
   513 	"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."
   531 	"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."
   514 	
   532 	
   521 	aBlock numArgs = 1
   539 	aBlock numArgs = 1
   522 		ifTrue: [ ^ aBlock value: result ].
   540 		ifTrue: [ ^ aBlock value: result ].
   523 	^ aBlock value: result message value: result position
   541 	^ aBlock value: result message value: result position
   524 !
   542 !
   525 
   543 
   526 parseOn: aStream
   544 parseOn: aPPContext
   527 	"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:."
   545 	"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:."
   528 	
   546 	
   529 	self subclassResponsibility
   547 	self subclassResponsibility
   530 ! !
   548 ! !
   531 
   549 
   552 
   570 
   553 isUnresolved
   571 isUnresolved
   554 	^ false
   572 	^ false
   555 ! !
   573 ! !
   556 
   574 
   557 
       
   558 !PPParser class methodsFor:'documentation'!
   575 !PPParser class methodsFor:'documentation'!
   559 
   576 
   560 version
   577 version
   561     ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPParser.st,v 1.7 2014-03-04 23:58:41 cg Exp $'
   578     ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPParser.st,v 1.7 2014-03-04 23:58:41 cg Exp $'
   562 !
   579 !