author | Claus Gittinger <cg@exept.de> |
Wed, 21 Jan 2015 10:48:42 +0100 | |
changeset 3706 | 25c5f07034a2 |
parent 3698 | 42c9858182a3 |
child 3713 | 714678c218e8 |
permissions | -rw-r--r-- |
3698 | 1 |
" |
3706
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
2 |
COPYRIGHT (c) 2015 by eXept Software AG |
3698 | 3 |
All Rights Reserved |
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
"{ Package: 'stx:libbasic3' }" |
|
13 |
||
14 |
"{ NameSpace: Smalltalk }" |
|
15 |
||
16 |
SmalltalkChunkFileSourceWriter subclass:#VSEChunkFileSourceWriter |
|
17 |
instanceVariableNames:'' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Kernel-Classes-Support' |
|
21 |
! |
|
22 |
||
23 |
Object subclass:#VSESourceRewriter |
|
24 |
instanceVariableNames:'source method methodClass' |
|
25 |
classVariableNames:'' |
|
26 |
poolDictionaries:'' |
|
27 |
privateIn:VSEChunkFileSourceWriter |
|
28 |
! |
|
29 |
||
30 |
!VSEChunkFileSourceWriter class methodsFor:'documentation'! |
|
31 |
||
32 |
copyright |
|
33 |
" |
|
3706
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
34 |
COPYRIGHT (c) 2015 by eXept Software AG |
3698 | 35 |
All Rights Reserved |
36 |
||
37 |
This software is furnished under a license and may be used |
|
38 |
only in accordance with the terms of that license and with the |
|
39 |
inclusion of the above copyright notice. This software may not |
|
40 |
be provided or otherwise made available to, or used by, any |
|
41 |
other person. No title to or ownership of the software is |
|
42 |
hereby transferred. |
|
43 |
" |
|
44 |
! |
|
45 |
||
46 |
documentation |
|
47 |
" |
|
48 |
fileout in a format which can be read by visualAge. |
|
49 |
For transporting software. |
|
50 |
" |
|
51 |
! |
|
52 |
||
53 |
examples |
|
54 |
" |
|
55 |
[exBegin] |
|
56 |
|s| |
|
57 |
||
58 |
s := 'test.st' asFilename writeStream. |
|
59 |
[ |
|
3706
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
60 |
VSEChunkFileSourceWriter new |
3698 | 61 |
fileOut:OrderedCollection on:s |
62 |
] ensure:[ |
|
63 |
s close |
|
64 |
] |
|
65 |
[exEnd] |
|
66 |
||
67 |
[exBegin] |
|
68 |
|s| |
|
69 |
||
70 |
s := '' writeStream. |
|
71 |
[ |
|
3706
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
72 |
VSEChunkFileSourceWriter new |
3698 | 73 |
fileOut:OrderedCollection on:s |
74 |
] ensure:[ |
|
75 |
s close |
|
76 |
]. |
|
77 |
s contents |
|
78 |
[exEnd] |
|
79 |
" |
|
80 |
! ! |
|
81 |
||
3706
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
82 |
!VSEChunkFileSourceWriter class methodsFor:'class access'! |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
83 |
|
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
84 |
vseSourceRewriter |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
85 |
^ VSESourceRewriter |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
86 |
! ! |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
87 |
|
3698 | 88 |
!VSEChunkFileSourceWriter methodsFor:'source writing'! |
89 |
||
90 |
fileOutCategory:aCategory of:aClass except:skippedMethods only:savedMethods methodFilter:methodFilter on:aStream |
|
91 |
"file out all methods belonging to aCategory, aString onto aStream. |
|
92 |
If skippedMethods is nonNil, those are not saved. |
|
93 |
If savedMethods is nonNil, only those are saved. |
|
94 |
If both are nil, all are saved. See version-method handling in |
|
95 |
fileOut for what this is needed." |
|
96 |
||
97 |
|sortedSelectors first prevPrivacy privacy interestingMethods| |
|
98 |
||
99 |
interestingMethods := OrderedCollection new. |
|
100 |
aClass methodsDo:[:aMethod | |
|
101 |
|wanted| |
|
102 |
||
103 |
(methodsAlreadySaved includes:aMethod) ifFalse:[ |
|
104 |
(aCategory = aMethod category) ifTrue:[ |
|
105 |
(methodFilter isNil or:[methodFilter value:aMethod]) ifTrue:[ |
|
106 |
skippedMethods notNil ifTrue:[ |
|
107 |
wanted := (skippedMethods includesIdentical:aMethod) not |
|
108 |
] ifFalse:[ |
|
109 |
wanted := savedMethods isNil or:[ savedMethods includesIdentical:aMethod ]. |
|
110 |
]. |
|
111 |
wanted ifTrue:[ |
|
112 |
aMethod selector isSymbol ifTrue:[ |
|
113 |
interestingMethods add:aMethod |
|
114 |
] ifFalse:[ |
|
115 |
Transcript showCR:'skipping non-symbol method ', aMethod selector printString. |
|
116 |
]. |
|
117 |
]. |
|
118 |
] |
|
119 |
] |
|
120 |
] |
|
121 |
]. |
|
122 |
interestingMethods notEmpty ifTrue:[ |
|
123 |
first := true. |
|
124 |
prevPrivacy := nil. |
|
125 |
||
126 |
"/ |
|
127 |
"/ sort by selector |
|
128 |
"/ |
|
129 |
sortedSelectors := interestingMethods collect:[:m | aClass selectorAtMethod:m]. |
|
130 |
sortedSelectors sortWith:interestingMethods. |
|
131 |
||
132 |
interestingMethods do:[:eachMethod | |
|
133 |
privacy := eachMethod privacy. |
|
134 |
||
135 |
first ifFalse:[ |
|
136 |
privacy ~~ prevPrivacy ifTrue:[ |
|
137 |
first := true. |
|
138 |
aStream space. |
|
139 |
aStream nextPutChunkSeparator. |
|
140 |
]. |
|
141 |
aStream cr; cr |
|
142 |
]. |
|
143 |
||
144 |
first ifTrue:[ |
|
145 |
aStream nextPutChunkSeparator. |
|
146 |
aClass printClassNameOn:aStream. |
|
147 |
privacy ~~ #public ifTrue:[ |
|
148 |
aStream nextPutAll:' privateMethods'. |
|
149 |
] ifFalse:[ |
|
150 |
aStream nextPutAll:' publicMethods'. |
|
151 |
]. |
|
152 |
aStream nextPutChunkSeparator; cr; cr. |
|
153 |
first := false. |
|
154 |
]. |
|
155 |
self fileOutMethod:eachMethod on:aStream. |
|
156 |
methodsAlreadySaved add:eachMethod. |
|
157 |
||
158 |
prevPrivacy := privacy. |
|
159 |
]. |
|
160 |
aStream space. |
|
161 |
aStream nextPutChunkSeparator. |
|
162 |
aStream cr |
|
163 |
]. |
|
164 |
aStream cr |
|
165 |
! |
|
166 |
||
167 |
fileOutDefinitionOf:aClass on:aStream |
|
168 |
"append an expression on aStream, which defines myself." |
|
169 |
||
170 |
|s owner ns superclass nm| |
|
171 |
||
172 |
owner := aClass owningClass. |
|
173 |
ns := aClass topNameSpace. |
|
174 |
||
175 |
"take care of nil-superclass" |
|
176 |
superclass := aClass superclass. |
|
177 |
superclass isNil ifTrue:[ |
|
178 |
s := 'nil' |
|
179 |
] ifFalse:[ |
|
180 |
s := superclass nameWithNameSpacePrefix. |
|
181 |
]. |
|
182 |
||
183 |
aStream nextPutAll:s. "/ superclass |
|
184 |
aStream space. |
|
185 |
aClass basicFileOutInstvarTypeKeywordOn:aStream. |
|
186 |
||
187 |
nm := aClass nameWithoutPrefix. |
|
188 |
aStream nextPut:$#. |
|
189 |
aStream nextPutAll:nm. |
|
190 |
||
191 |
aStream crtab. |
|
192 |
aStream nextPutAll:'instanceVariableNames:'''. |
|
193 |
aClass printInstVarNamesOn:aStream indent:16. |
|
194 |
aStream nextPutAll:''''. |
|
195 |
||
196 |
aStream crtab. |
|
197 |
aStream nextPutAll:'classVariableNames:'''. |
|
198 |
aClass printClassVarNamesOn:aStream indent:16. |
|
199 |
aStream nextPutAll:''''. |
|
200 |
||
201 |
aStream crtab. |
|
202 |
aStream nextPutAll:'poolDictionaries:'''. |
|
203 |
aClass printSharedPoolNamesOn:aStream indent:16. |
|
204 |
aStream nextPutAll:''''. |
|
205 |
||
206 |
aStream cr. |
|
207 |
! ! |
|
208 |
||
209 |
!VSEChunkFileSourceWriter::VSESourceRewriter class methodsFor:'translation'! |
|
210 |
||
211 |
rewriteMethod:method |
|
212 |
^ self new rewriteMethod:method |
|
213 |
! ! |
|
214 |
||
215 |
!VSEChunkFileSourceWriter::VSESourceRewriter methodsFor:'accessing'! |
|
216 |
||
217 |
method |
|
218 |
^ method |
|
219 |
! |
|
220 |
||
221 |
method:something |
|
222 |
method := something. |
|
223 |
! |
|
224 |
||
225 |
methodClass |
|
226 |
^ methodClass |
|
227 |
! |
|
228 |
||
229 |
methodClass:something |
|
230 |
methodClass := something. |
|
231 |
! |
|
232 |
||
233 |
source |
|
234 |
^ source |
|
235 |
! |
|
236 |
||
237 |
source:something |
|
238 |
source := something. |
|
239 |
! ! |
|
240 |
||
241 |
!VSEChunkFileSourceWriter::VSESourceRewriter methodsFor:'translation'! |
|
242 |
||
243 |
doRewrite |
|
3706
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
244 |
self rewriteEOLComments. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
245 |
self rewriteGlobalsWithNamespace. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
246 |
^ source. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
247 |
! |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
248 |
|
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
249 |
rewriteEOLComments |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
250 |
|tree parser eolComments| |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
251 |
|
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
252 |
parser := Parser new. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
253 |
parser saveComments:true. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
254 |
parser parseMethod:source in:methodClass ignoreErrors:true ignoreWarnings:true. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
255 |
|
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
256 |
tree := parser tree. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
257 |
eolComments := parser comments select:[:each | each isEndOfLineComment]. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
258 |
"/ start with the last (so I don't have to update the positions) |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
259 |
eolComments sort:[:a :b | a position < b position]. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
260 |
eolComments reverseDo:[:each | |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
261 |
source := (source copyTo:(each endPosition)),'"',(source copyFrom:(each endPosition + 1)) |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
262 |
]. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
263 |
! |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
264 |
|
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
265 |
rewriteGlobalsWithNamespace |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
266 |
|tree parser namesToRewrite| |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
267 |
|
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
268 |
namesToRewrite := OrderedCollection new. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
269 |
|
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
270 |
parser := Parser new. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
271 |
parser saveComments:true. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
272 |
parser parseMethod:source in:methodClass ignoreErrors:true ignoreWarnings:true. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
273 |
parser tree isNil ifTrue:[ ^ self ]. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
274 |
|
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
275 |
parser tree variableNodesDo:[:each | |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
276 |
|nameInSource| |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
277 |
|
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
278 |
each isGlobalVariable ifTrue:[ |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
279 |
(each name includes:$:) ifTrue:[ |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
280 |
nameInSource := source copyFrom:each startPosition to:each endPosition. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
281 |
(nameInSource includes:$:) ifTrue:[ |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
282 |
namesToRewrite add:each. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
283 |
]. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
284 |
]. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
285 |
]. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
286 |
]. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
287 |
|
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
288 |
"/ start with the last (so I don't have to update the positions) |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
289 |
namesToRewrite sort:[:a :b | a position < b position]. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
290 |
namesToRewrite reverseDo:[:each | |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
291 |
|idx nameInSource ns nm| |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
292 |
|
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
293 |
nameInSource := source copyFrom:each startPosition to:each endPosition. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
294 |
idx := nameInSource indexOf:$:. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
295 |
ns := nameInSource copyTo:idx-1. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
296 |
idx := nameInSource indexOf:$: startingAt:idx+1. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
297 |
nm := nameInSource copyFrom:idx+1. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
298 |
(ns = methodClass nameSpace name) ifFalse:[ |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
299 |
Transcript show:'reference to other namespace'. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
300 |
] ifTrue:[ |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
301 |
source := (source copyTo:(each startPosition-1)),nm,(source copyFrom:(each endPosition + 1)) |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
302 |
]. |
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
303 |
]. |
3698 | 304 |
! |
305 |
||
306 |
rewriteMethod:methodArg |
|
307 |
method := methodArg. |
|
308 |
source := method source. |
|
309 |
methodClass := method mclass. |
|
310 |
||
3706
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
311 |
^ self doRewrite. |
3698 | 312 |
! ! |
313 |
||
314 |
!VSEChunkFileSourceWriter class methodsFor:'documentation'! |
|
315 |
||
316 |
version |
|
3706
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
317 |
^ '$Header: /cvs/stx/stx/libbasic3/VSEChunkFileSourceWriter.st,v 1.2 2015-01-21 09:48:42 cg Exp $' |
3698 | 318 |
! |
319 |
||
320 |
version_CVS |
|
3706
25c5f07034a2
class: VSEChunkFileSourceWriter
Claus Gittinger <cg@exept.de>
parents:
3698
diff
changeset
|
321 |
^ '$Header: /cvs/stx/stx/libbasic3/VSEChunkFileSourceWriter.st,v 1.2 2015-01-21 09:48:42 cg Exp $' |
3698 | 322 |
! ! |
323 |