CodeGenerator.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 27 Feb 2013 12:34:59 +0000
branchjv
changeset 12431 9f0c59c742d5
parent 12128 a7ff7d66ee85
child 15566 184cea584be5
permissions -rw-r--r--
Added LintRuleSettingsApplication and LintRuleEditDialog to define user-defined rule sets.

"
 Copyright (c) 2007-2010 Jan Vrany, SWING Research Group, Czech Technical University in Prague
 Copyright (c) 2009-2010 eXept Software AG

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the 'Software'), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
"
"{ Package: 'stx:libtool' }"

RBProgramNodeVisitor subclass:#CodeGenerator
	instanceVariableNames:'class protocol source change replacements'
	classVariableNames:''
	poolDictionaries:''
	category:'Interface-Browsers'
!

!CodeGenerator class methodsFor:'documentation'!

copyright
"
 Copyright (c) 2007-2010 Jan Vrany, SWING Research Group, Czech Technical University in Prague
 Copyright (c) 2009-2010 eXept Software AG

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the 'Software'), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
"
! !

!CodeGenerator methodsFor:'accessing'!

category:aString

    self protocol: aString

    "Created: / 30-12-2008 / 17:41:44 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 07-07-2009 / 18:46:20 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

change

   change ifNil:[self createChange].
   ^change

    "Created: / 30-12-2008 / 17:14:14 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 07-07-2009 / 18:45:07 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

class:aClass

    class := aClass

    "Created: / 30-12-2008 / 15:38:00 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 07-07-2009 / 18:46:34 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

protocol
    ^ protocol
!

protocol:aString
    protocol := aString.
!

replace: placeholder with: code

    replacements 
        at: placeholder
        put: (code isSymbol 
                ifTrue:[code]
                ifFalse:[RBParser parseRewriteExpression: code])

    "Created: / 07-07-2009 / 18:48:21 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 07-07-2009 / 19:58:51 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

replacementFor: placeholder

    ^replacements 
        at: placeholder
        ifAbsent:[self error:'No replacement for ', placeholder]

    "Created: / 07-07-2009 / 19:13:18 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

source:aString
    source := aString.

    "Created: / 30-12-2008 / 17:04:14 / Jan Vrany <vranyj1@fel.cvut.cz>"
! !

!CodeGenerator methodsFor:'compiling'!

compile

    ^self change apply

    "Created: / 07-07-2009 / 18:47:52 / Jan Vrany <vranyj1@fel.cvut.cz>"
! !

!CodeGenerator methodsFor:'initialization'!

initialize
    "Invoked when a new instance is created."

    super initialize.
    replacements := Dictionary new.

    "Created: / 30-12-2008 / 15:29:17 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 07-07-2009 / 18:45:17 / Jan Vrany <vranyj1@fel.cvut.cz>"
! !

!CodeGenerator methodsFor:'private'!

createChange

    | method |
    method := RBParser parseRewriteMethod: source.
    method source: nil.
    method acceptVisitor: self.
    (change := InteractiveAddMethodChange new)
        class: class
        protocol: protocol
        source: method formattedCode.

    "Created: / 07-07-2009 / 18:44:42 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

replacePlaceholdersInSelectorPartsOf:aMessageNode 
    aMessageNode selectorParts do:[:part | 
        part isPatternVariable ifTrue:[
            |replacement|

            replacement := self replacementFor:part value.
            (replacement isSymbol or:[ replacement isVariable ]) ifFalse:[
                self error:'Replacement for selector parts must be a single selector'
            ].
            part value:replacement formattedCode
        ]
    ]
! !

!CodeGenerator methodsFor:'visitor-double dispatching'!

acceptMessageNode:aMessageNode 
    self replacePlaceholdersInSelectorPartsOf:aMessageNode.
    super acceptMessageNode:aMessageNode.

    "Created: / 07-07-2009 / 19:23:08 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

acceptMethodNode: aMethodNode

    self replacePlaceholdersInSelectorPartsOf: aMethodNode.
    super acceptMethodNode: aMethodNode.

    "Created: / 07-07-2009 / 19:09:11 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

acceptVariableNode: aVariableNode

    aVariableNode isPatternNode ifTrue:
        [aVariableNode replaceWith:
            (self replacementFor:aVariableNode name)]

    "Created: / 30-12-2008 / 17:13:16 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 07-07-2009 / 19:13:55 / Jan Vrany <vranyj1@fel.cvut.cz>"
! !

!CodeGenerator class methodsFor:'documentation'!

version_CVS
    ^ '§Header: /cvs/stx/stx/libtool/CodeGenerator.st,v 1.2 2011/07/03 13:41:45 cg Exp §'
!

version_HG

    ^ '$Changeset: <not expanded> $'
!

version_SVN
    ^ '$Id: CodeGenerator.st 7854 2012-01-30 17:49:41Z vranyj1 $'
! !