CodeGenerator.st
branchjv
changeset 15566 184cea584be5
parent 12431 9f0c59c742d5
parent 14717 7fd0faf76e90
equal deleted inserted replaced
13752:25c2a13f00c5 15566:184cea584be5
    24  OTHER DEALINGS IN THE SOFTWARE.
    24  OTHER DEALINGS IN THE SOFTWARE.
    25 "
    25 "
    26 "{ Package: 'stx:libtool' }"
    26 "{ Package: 'stx:libtool' }"
    27 
    27 
    28 RBProgramNodeVisitor subclass:#CodeGenerator
    28 RBProgramNodeVisitor subclass:#CodeGenerator
    29 	instanceVariableNames:'class protocol source change replacements'
    29 	instanceVariableNames:'class protocol source change replacements
       
    30 		recordedReplacementsInSource'
    30 	classVariableNames:''
    31 	classVariableNames:''
    31 	poolDictionaries:''
    32 	poolDictionaries:''
    32 	category:'Interface-Browsers'
    33 	category:'Interface-Browsers'
    33 !
    34 !
    34 
    35 
   147 
   148 
   148 !CodeGenerator methodsFor:'private'!
   149 !CodeGenerator methodsFor:'private'!
   149 
   150 
   150 createChange
   151 createChange
   151 
   152 
   152     | method |
   153     | parser method |
   153     method := RBParser parseRewriteMethod: source.
   154     parser := RBParser new.
       
   155     recordedReplacementsInSource := OrderedCollection new.
       
   156     parser errorBlock:[ :str :pos | self error: ('Error: %1: %2' bindWith: pos with: str). ^ self ].
       
   157     parser initializeParserWith: source type: #rewriteSavingCommentsOn:errorBlock:.
       
   158     method := parser parseMethod: source.    
       
   159 
   154     method source: nil.
   160     method source: nil.
   155     method acceptVisitor: self.
   161     method acceptVisitor: self.
       
   162     self replaceInSourceCode.
   156     (change := InteractiveAddMethodChange new)
   163     (change := InteractiveAddMethodChange new)
   157         class: class
   164         class: class
   158         protocol: protocol
   165         protocol: protocol
   159         source: method formattedCode.
   166         source: (source notNil ifTrue:[source] ifFalse:[method formattedCode]).
   160 
   167 
   161     "Created: / 07-07-2009 / 18:44:42 / Jan Vrany <vranyj1@fel.cvut.cz>"
   168     "Created: / 07-07-2009 / 18:44:42 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   169     "Modified: / 26-08-2014 / 23:51:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   170 !
       
   171 
       
   172 recordReplaceInSourceFrom:start to:stop by:code
       
   173     recordedReplacementsInSource add: { start. stop . code }.
       
   174 
       
   175     "Modified: / 26-08-2014 / 23:50:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   176 !
       
   177 
       
   178 replaceInSourceCode
       
   179     "Perform recorded replacements in source"
       
   180 
       
   181     recordedReplacementsInSource sort: [ :a :b | a second < b first ].
       
   182     recordedReplacementsInSource reverseDo:[ :replacement |
       
   183         source := 
       
   184             (source copyTo: replacement first - 1) , replacement third , (source copyFrom: replacement second + 1)  
       
   185     ].
       
   186 
       
   187     "Created: / 26-08-2014 / 23:51:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   162 !
   188 !
   163 
   189 
   164 replacePlaceholdersInSelectorPartsOf:aMessageNode 
   190 replacePlaceholdersInSelectorPartsOf:aMessageNode 
   165     aMessageNode selectorParts do:[:part | 
   191     aMessageNode selectorParts do:[:part | 
   166         part isPatternVariable ifTrue:[
   192         part isPatternVariable ifTrue:[
   168 
   194 
   169             replacement := self replacementFor:part value.
   195             replacement := self replacementFor:part value.
   170             (replacement isSymbol or:[ replacement isVariable ]) ifFalse:[
   196             (replacement isSymbol or:[ replacement isVariable ]) ifFalse:[
   171                 self error:'Replacement for selector parts must be a single selector'
   197                 self error:'Replacement for selector parts must be a single selector'
   172             ].
   198             ].
   173             part value:replacement formattedCode
   199             source notNil ifTrue:[
       
   200                 self 
       
   201                       recordReplaceInSourceFrom:part start
       
   202                       to:part stop
       
   203                       by:replacement formattedCode.
       
   204             ].
       
   205             part value:replacement formattedCode.                 
   174         ]
   206         ]
   175     ]
   207     ]
       
   208 
       
   209     "Modified: / 26-08-2014 / 23:37:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   176 ! !
   210 ! !
   177 
   211 
   178 !CodeGenerator methodsFor:'visitor-double dispatching'!
   212 !CodeGenerator methodsFor:'visitor-double dispatching'!
   179 
   213 
   180 acceptMessageNode:aMessageNode 
   214 acceptMessageNode:aMessageNode 
   192     "Created: / 07-07-2009 / 19:09:11 / Jan Vrany <vranyj1@fel.cvut.cz>"
   226     "Created: / 07-07-2009 / 19:09:11 / Jan Vrany <vranyj1@fel.cvut.cz>"
   193 !
   227 !
   194 
   228 
   195 acceptVariableNode: aVariableNode
   229 acceptVariableNode: aVariableNode
   196 
   230 
   197     aVariableNode isPatternNode ifTrue:
   231     aVariableNode isPatternNode ifTrue:[            
   198         [aVariableNode replaceWith:
   232         source notNil ifTrue:[ 
   199             (self replacementFor:aVariableNode name)]
   233             self 
       
   234                   recordReplaceInSourceFrom:aVariableNode start
       
   235                   to:aVariableNode stop
       
   236                   by:(self replacementFor:aVariableNode name) formattedCode
       
   237         ].
       
   238         aVariableNode replaceWith: (self replacementFor:aVariableNode name).
       
   239     ]
   200 
   240 
   201     "Created: / 30-12-2008 / 17:13:16 / Jan Vrany <vranyj1@fel.cvut.cz>"
   241     "Created: / 30-12-2008 / 17:13:16 / Jan Vrany <vranyj1@fel.cvut.cz>"
   202     "Modified: / 07-07-2009 / 19:13:55 / Jan Vrany <vranyj1@fel.cvut.cz>"
   242     "Modified: / 07-07-2009 / 19:13:55 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   243     "Modified: / 26-08-2014 / 23:37:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   203 ! !
   244 ! !
   204 
   245 
   205 !CodeGenerator class methodsFor:'documentation'!
   246 !CodeGenerator class methodsFor:'documentation'!
   206 
   247 
   207 version_CVS
   248 version_CVS
   208     ^ '§Header: /cvs/stx/stx/libtool/CodeGenerator.st,v 1.2 2011/07/03 13:41:45 cg Exp §'
   249     ^ '$Header: /cvs/stx/stx/libtool/CodeGenerator.st,v 1.4 2014-08-26 22:58:13 vrany Exp $'
   209 !
       
   210 
       
   211 version_HG
       
   212 
       
   213     ^ '$Changeset: <not expanded> $'
       
   214 !
   250 !
   215 
   251 
   216 version_SVN
   252 version_SVN
   217     ^ '$Id: CodeGenerator.st 7854 2012-01-30 17:49:41Z vranyj1 $'
   253     ^ '$Id: CodeGenerator.st,v 1.4 2014-08-26 22:58:13 vrany Exp $'
   218 ! !
   254 ! !
   219 
   255