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