diff -r a2656b27cace -r 6112a403a52d PPParser.st --- a/PPParser.st Fri Oct 03 01:59:10 2014 +0100 +++ b/PPParser.st Fri Oct 03 02:33:08 2014 +0100 @@ -25,11 +25,6 @@ - - - - - !PPParser methodsFor:'accessing'! children @@ -99,6 +94,30 @@ ^ answer ! ! +!PPParser methodsFor:'context'! + +parse: anObject withContext: aPPContext + "Parse anObject with the receiving parser and answer the parse-result or an instance of PPFailure." + + aPPContext stream: anObject asPetitStream. + ^ self parseWithContext: aPPContext. +! + +parseWithContext: context + | result | + context initializeFor: self. + result := self parseOn: context. + + "Return the furthest failure, it gives better results than the last failure" + result isPetitFailure ifTrue: [ ^ context furthestFailure ]. + ^ result + +! + +updateContext: aPPContext + "nothing to do" +! ! + !PPParser methodsFor:'converting'! asParser @@ -224,13 +243,6 @@ ^ PPActionParser on: self block: aBlock ! ->=> aBlock - "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." - - ^ PPWrappingParser on: self block: aBlock -! - answer: anObject "Answer a new parser that always returns anObject from a successful parse." @@ -275,6 +287,12 @@ !PPParser methodsFor:'operators-mapping'! +>=> aBlock + "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." + + ^ PPWrappingParser on: self block: aBlock +! + foldLeft: aBlock "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." @@ -505,8 +523,8 @@ parse: anObject "Parse anObject with the receiving parser and answer the parse-result or an instance of PPFailure." - - ^ self parseOn: anObject asPetitStream + + ^ self parse: anObject withContext: PPContext new ! parse: anObject onError: aBlock @@ -523,7 +541,7 @@ ^ aBlock value: result message value: result position ! -parseOn: aStream +parseOn: aPPContext "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:." self subclassResponsibility @@ -554,7 +572,6 @@ ^ false ! ! - !PPParser class methodsFor:'documentation'! version