author | Claus Gittinger <cg@exept.de> |
Fri, 24 Feb 2017 16:54:59 +0100 | |
changeset 17444 | 4506360ae3c8 |
parent 14717 | 7fd0faf76e90 |
child 15566 | 184cea584be5 |
permissions | -rw-r--r-- |
10020 | 1 |
" |
10047 | 2 |
Copyright (c) 2007-2010 Jan Vrany, SWING Research Group, Czech Technical University in Prague |
3 |
Copyright (c) 2009-2010 eXept Software AG |
|
10020 | 4 |
|
10047 | 5 |
Permission is hereby granted, free of charge, to any person |
6 |
obtaining a copy of this software and associated documentation |
|
7 |
files (the 'Software'), to deal in the Software without |
|
8 |
restriction, including without limitation the rights to use, |
|
9 |
copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10 |
copies of the Software, and to permit persons to whom the |
|
11 |
Software is furnished to do so, subject to the following |
|
12 |
conditions: |
|
13 |
||
14 |
The above copyright notice and this permission notice shall be |
|
15 |
included in all copies or substantial portions of the Software. |
|
16 |
||
17 |
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, |
|
18 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
19 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
20 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
21 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
22 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
23 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
24 |
OTHER DEALINGS IN THE SOFTWARE. |
|
10020 | 25 |
" |
26 |
"{ Package: 'stx:libtool' }" |
|
27 |
||
28 |
RBProgramNodeVisitor subclass:#CodeGenerator |
|
14717
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
29 |
instanceVariableNames:'class protocol source change replacements |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
30 |
recordedReplacementsInSource' |
10020 | 31 |
classVariableNames:'' |
32 |
poolDictionaries:'' |
|
33 |
category:'Interface-Browsers' |
|
34 |
! |
|
35 |
||
36 |
!CodeGenerator class methodsFor:'documentation'! |
|
37 |
||
38 |
copyright |
|
39 |
" |
|
10047 | 40 |
Copyright (c) 2007-2010 Jan Vrany, SWING Research Group, Czech Technical University in Prague |
41 |
Copyright (c) 2009-2010 eXept Software AG |
|
42 |
||
43 |
Permission is hereby granted, free of charge, to any person |
|
44 |
obtaining a copy of this software and associated documentation |
|
45 |
files (the 'Software'), to deal in the Software without |
|
46 |
restriction, including without limitation the rights to use, |
|
47 |
copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
48 |
copies of the Software, and to permit persons to whom the |
|
49 |
Software is furnished to do so, subject to the following |
|
50 |
conditions: |
|
10020 | 51 |
|
10047 | 52 |
The above copyright notice and this permission notice shall be |
53 |
included in all copies or substantial portions of the Software. |
|
54 |
||
55 |
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, |
|
56 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
57 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
58 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
59 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
60 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
61 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
62 |
OTHER DEALINGS IN THE SOFTWARE. |
|
10020 | 63 |
" |
64 |
! ! |
|
65 |
||
66 |
!CodeGenerator methodsFor:'accessing'! |
|
67 |
||
68 |
category:aString |
|
69 |
||
70 |
self protocol: aString |
|
71 |
||
72 |
"Created: / 30-12-2008 / 17:41:44 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
73 |
"Modified: / 07-07-2009 / 18:46:20 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
74 |
! |
|
75 |
||
76 |
change |
|
77 |
||
78 |
change ifNil:[self createChange]. |
|
79 |
^change |
|
80 |
||
81 |
"Created: / 30-12-2008 / 17:14:14 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
82 |
"Modified: / 07-07-2009 / 18:45:07 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
83 |
! |
|
84 |
||
85 |
class:aClass |
|
86 |
||
87 |
class := aClass |
|
88 |
||
89 |
"Created: / 30-12-2008 / 15:38:00 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
90 |
"Modified: / 07-07-2009 / 18:46:34 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
91 |
! |
|
92 |
||
93 |
protocol |
|
94 |
^ protocol |
|
95 |
! |
|
96 |
||
97 |
protocol:aString |
|
98 |
protocol := aString. |
|
99 |
! |
|
100 |
||
101 |
replace: placeholder with: code |
|
102 |
||
103 |
replacements |
|
104 |
at: placeholder |
|
105 |
put: (code isSymbol |
|
106 |
ifTrue:[code] |
|
107 |
ifFalse:[RBParser parseRewriteExpression: code]) |
|
108 |
||
109 |
"Created: / 07-07-2009 / 18:48:21 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
110 |
"Modified: / 07-07-2009 / 19:58:51 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
111 |
! |
|
112 |
||
113 |
replacementFor: placeholder |
|
114 |
||
115 |
^replacements |
|
116 |
at: placeholder |
|
117 |
ifAbsent:[self error:'No replacement for ', placeholder] |
|
118 |
||
119 |
"Created: / 07-07-2009 / 19:13:18 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
120 |
! |
|
121 |
||
122 |
source:aString |
|
123 |
source := aString. |
|
124 |
||
125 |
"Created: / 30-12-2008 / 17:04:14 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
126 |
! ! |
|
127 |
||
128 |
!CodeGenerator methodsFor:'compiling'! |
|
129 |
||
130 |
compile |
|
131 |
||
132 |
^self change apply |
|
133 |
||
134 |
"Created: / 07-07-2009 / 18:47:52 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
135 |
! ! |
|
136 |
||
137 |
!CodeGenerator methodsFor:'initialization'! |
|
138 |
||
139 |
initialize |
|
140 |
"Invoked when a new instance is created." |
|
141 |
||
142 |
super initialize. |
|
143 |
replacements := Dictionary new. |
|
144 |
||
145 |
"Created: / 30-12-2008 / 15:29:17 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
146 |
"Modified: / 07-07-2009 / 18:45:17 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
147 |
! ! |
|
148 |
||
149 |
!CodeGenerator methodsFor:'private'! |
|
150 |
||
151 |
createChange |
|
152 |
||
14423
ce279b3422b2
Fix to preserve comment.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
10047
diff
changeset
|
153 |
| parser method | |
ce279b3422b2
Fix to preserve comment.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
10047
diff
changeset
|
154 |
parser := RBParser new. |
14717
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
155 |
recordedReplacementsInSource := OrderedCollection new. |
14423
ce279b3422b2
Fix to preserve comment.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
10047
diff
changeset
|
156 |
parser errorBlock:[ :str :pos | self error: ('Error: %1: %2' bindWith: pos with: str). ^ self ]. |
ce279b3422b2
Fix to preserve comment.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
10047
diff
changeset
|
157 |
parser initializeParserWith: source type: #rewriteSavingCommentsOn:errorBlock:. |
ce279b3422b2
Fix to preserve comment.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
10047
diff
changeset
|
158 |
method := parser parseMethod: source. |
ce279b3422b2
Fix to preserve comment.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
10047
diff
changeset
|
159 |
|
10020 | 160 |
method source: nil. |
161 |
method acceptVisitor: self. |
|
14717
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
162 |
self replaceInSourceCode. |
10020 | 163 |
(change := InteractiveAddMethodChange new) |
164 |
class: class |
|
165 |
protocol: protocol |
|
14717
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
166 |
source: (source notNil ifTrue:[source] ifFalse:[method formattedCode]). |
10020 | 167 |
|
168 |
"Created: / 07-07-2009 / 18:44:42 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
14717
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
169 |
"Modified: / 26-08-2014 / 23:51:54 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
170 |
! |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
171 |
|
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
172 |
recordReplaceInSourceFrom:start to:stop by:code |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
173 |
recordedReplacementsInSource add: { start. stop . code }. |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
174 |
|
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
175 |
"Modified: / 26-08-2014 / 23:50:10 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
176 |
! |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
177 |
|
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
178 |
replaceInSourceCode |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
179 |
"Perform recorded replacements in source" |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
180 |
|
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
181 |
recordedReplacementsInSource sort: [ :a :b | a second < b first ]. |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
182 |
recordedReplacementsInSource reverseDo:[ :replacement | |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
183 |
source := |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
184 |
(source copyTo: replacement first - 1) , replacement third , (source copyFrom: replacement second + 1) |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
185 |
]. |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
186 |
|
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
187 |
"Created: / 26-08-2014 / 23:51:54 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
10020 | 188 |
! |
189 |
||
190 |
replacePlaceholdersInSelectorPartsOf:aMessageNode |
|
191 |
aMessageNode selectorParts do:[:part | |
|
192 |
part isPatternVariable ifTrue:[ |
|
193 |
|replacement| |
|
194 |
||
195 |
replacement := self replacementFor:part value. |
|
196 |
(replacement isSymbol or:[ replacement isVariable ]) ifFalse:[ |
|
197 |
self error:'Replacement for selector parts must be a single selector' |
|
198 |
]. |
|
14717
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
199 |
source notNil ifTrue:[ |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
200 |
self |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
201 |
recordReplaceInSourceFrom:part start |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
202 |
to:part stop |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
203 |
by:replacement formattedCode. |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
204 |
]. |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
205 |
part value:replacement formattedCode. |
10020 | 206 |
] |
207 |
] |
|
14717
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
208 |
|
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
209 |
"Modified: / 26-08-2014 / 23:37:31 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
10020 | 210 |
! ! |
211 |
||
212 |
!CodeGenerator methodsFor:'visitor-double dispatching'! |
|
213 |
||
214 |
acceptMessageNode:aMessageNode |
|
215 |
self replacePlaceholdersInSelectorPartsOf:aMessageNode. |
|
216 |
super acceptMessageNode:aMessageNode. |
|
217 |
||
218 |
"Created: / 07-07-2009 / 19:23:08 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
219 |
! |
|
220 |
||
221 |
acceptMethodNode: aMethodNode |
|
222 |
||
223 |
self replacePlaceholdersInSelectorPartsOf: aMethodNode. |
|
224 |
super acceptMethodNode: aMethodNode. |
|
225 |
||
226 |
"Created: / 07-07-2009 / 19:09:11 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
227 |
! |
|
228 |
||
229 |
acceptVariableNode: aVariableNode |
|
230 |
||
14717
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
231 |
aVariableNode isPatternNode ifTrue:[ |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
232 |
source notNil ifTrue:[ |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
233 |
self |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
234 |
recordReplaceInSourceFrom:aVariableNode start |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
235 |
to:aVariableNode stop |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
236 |
by:(self replacementFor:aVariableNode name) formattedCode |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
237 |
]. |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
238 |
aVariableNode replaceWith: (self replacementFor:aVariableNode name). |
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
239 |
] |
10020 | 240 |
|
241 |
"Created: / 30-12-2008 / 17:13:16 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
242 |
"Modified: / 07-07-2009 / 19:13:55 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
14717
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
243 |
"Modified: / 26-08-2014 / 23:37:48 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
10020 | 244 |
! ! |
245 |
||
246 |
!CodeGenerator class methodsFor:'documentation'! |
|
247 |
||
248 |
version_CVS |
|
14717
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
249 |
^ '$Header: /cvs/stx/stx/libtool/CodeGenerator.st,v 1.4 2014-08-26 22:58:13 vrany Exp $' |
10020 | 250 |
! |
251 |
||
252 |
version_SVN |
|
14717
7fd0faf76e90
Fixed CodeGenerator to preserve original source code formatting.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14423
diff
changeset
|
253 |
^ '$Id: CodeGenerator.st,v 1.4 2014-08-26 22:58:13 vrany Exp $' |
10020 | 254 |
! ! |
14423
ce279b3422b2
Fix to preserve comment.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
10047
diff
changeset
|
255 |