# HG changeset patch # User Claus Gittinger # Date 1393947718 -3600 # Node ID 8cc5a5a8f416540950c66c072219f223e65d6e68 # Parent 29f7cfdac4927355efae28d7e778c92fbdec51b2 initial checkin diff -r 29f7cfdac492 -r 8cc5a5a8f416 analyzer/PPRewriter.st --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/analyzer/PPRewriter.st Tue Mar 04 16:41:58 2014 +0100 @@ -0,0 +1,72 @@ +"{ Package: 'stx:goodies/petitparser/analyzer' }" + +PPProcessor subclass:#PPRewriter + instanceVariableNames:'changed' + classVariableNames:'' + poolDictionaries:'' + category:'PetitAnalyzer-Core' +! + + +!PPRewriter methodsFor:'accessing'! + +replace: aSearchParser with: aReplaceParser + self replace: aSearchParser with: aReplaceParser when: [ :node | true ] +! + +replace: aSearchParser with: aReplaceParser when: aValidationBlock + self addRule: (PPParserReplaceRule searchFor: aSearchParser replaceWith: aReplaceParser when: aValidationBlock) +! + +replace: aSearchParser withValueFrom: aReplaceBlock + self replace: aSearchParser withValueFrom: aReplaceBlock when: [ :node | true ] +! + +replace: aSearchParser withValueFrom: aReplaceBlock when: aValidationBlock + self addRule: (PPBlockReplaceRule searchFor: aSearchParser replaceWith: aReplaceBlock when: aValidationBlock) +! ! + +!PPRewriter methodsFor:'initialization'! + +initialize + super initialize. + changed := false +! ! + +!PPRewriter methodsFor:'public'! + +execute: aParser + "Perform the replace rules of the receiver on aParser, answer the resulting parser." + + | previous result | + previous := context. + changed := false. + context := Dictionary new. + result := aParser transform: [ :each | + | transformed | + transformed := self performRulesOn: each. + transformed isNil + ifTrue: [ each ] + ifFalse: [ changed := true. transformed ] ]. + context := previous. + ^ result +! ! + +!PPRewriter methodsFor:'testing'! + +hasChanged + "Answer if the last operation has changed anything." + + ^ changed +! ! + +!PPRewriter class methodsFor:'documentation'! + +version + ^ '$Header: /cvs/stx/stx/goodies/petitparser/analyzer/PPRewriter.st,v 1.1 2014-03-04 15:41:58 cg Exp $' +! + +version_CVS + ^ '$Header: /cvs/stx/stx/goodies/petitparser/analyzer/PPRewriter.st,v 1.1 2014-03-04 15:41:58 cg Exp $' +! ! +