3714
|
1 |
"
|
|
2 |
COPYRIGHT (c) 2015 by eXept Software AG
|
|
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:#VSEFileSourceWriter
|
3734
|
17 |
instanceVariableNames:'classNameTranslations'
|
3714
|
18 |
classVariableNames:''
|
|
19 |
poolDictionaries:''
|
|
20 |
category:'Kernel-Classes-Support'
|
|
21 |
!
|
|
22 |
|
|
23 |
Object subclass:#VSESourceRewriter
|
3734
|
24 |
instanceVariableNames:'source method methodClass classNamesToRewrite'
|
3714
|
25 |
classVariableNames:''
|
|
26 |
poolDictionaries:''
|
|
27 |
privateIn:VSEFileSourceWriter
|
|
28 |
!
|
|
29 |
|
|
30 |
!VSEFileSourceWriter class methodsFor:'documentation'!
|
|
31 |
|
|
32 |
copyright
|
|
33 |
"
|
|
34 |
COPYRIGHT (c) 2015 by eXept Software AG
|
|
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 |
[
|
|
60 |
VSEChunkFileSourceWriter new
|
|
61 |
fileOut:OrderedCollection on:s
|
|
62 |
] ensure:[
|
|
63 |
s close
|
|
64 |
]
|
|
65 |
[exEnd]
|
|
66 |
|
|
67 |
[exBegin]
|
|
68 |
|s|
|
|
69 |
|
|
70 |
s := '' writeStream.
|
|
71 |
[
|
|
72 |
VSEChunkFileSourceWriter new
|
|
73 |
fileOut:OrderedCollection on:s
|
|
74 |
] ensure:[
|
|
75 |
s close
|
|
76 |
].
|
|
77 |
s contents
|
|
78 |
[exEnd]
|
|
79 |
"
|
|
80 |
! !
|
|
81 |
|
3734
|
82 |
!VSEFileSourceWriter class methodsFor:'instance creation'!
|
|
83 |
|
|
84 |
new
|
|
85 |
"return an initialized instance"
|
|
86 |
|
|
87 |
^ self basicNew initialize.
|
|
88 |
! !
|
|
89 |
|
3727
|
90 |
!VSEFileSourceWriter class methodsFor:'api'!
|
|
91 |
|
|
92 |
fileOutPackage:currentProject on:aStream
|
|
93 |
self new fileOutPackage:currentProject on:aStream
|
|
94 |
! !
|
|
95 |
|
3714
|
96 |
!VSEFileSourceWriter class methodsFor:'class access'!
|
|
97 |
|
|
98 |
vseSourceRewriter
|
|
99 |
^ VSESourceRewriter
|
|
100 |
! !
|
|
101 |
|
3734
|
102 |
!VSEFileSourceWriter class methodsFor:'defaults'!
|
|
103 |
|
|
104 |
defaultClassNameTranslations
|
|
105 |
"for extensions"
|
|
106 |
|
|
107 |
^ Dictionary new
|
|
108 |
at:#SequenceableCollection put:#IndexedCollection;
|
|
109 |
at:#CharacterArray put:#String;
|
|
110 |
at:#Unicode16String put:#DoubleByteString;
|
3742
|
111 |
at:#UnicodeString put:#DoubleByteString;
|
3734
|
112 |
at:#Timestamp put:#TimeStamp;
|
|
113 |
yourself
|
|
114 |
|
|
115 |
"
|
|
116 |
self defaultClassNameTranslations
|
|
117 |
"
|
|
118 |
! !
|
|
119 |
|
|
120 |
!VSEFileSourceWriter methodsFor:'initialization'!
|
|
121 |
|
|
122 |
initialize
|
|
123 |
classNameTranslations := self class defaultClassNameTranslations
|
|
124 |
! !
|
|
125 |
|
3727
|
126 |
!VSEFileSourceWriter methodsFor:'utilities'!
|
3714
|
127 |
|
|
128 |
ensureNoUnicodeInClass:aClass
|
|
129 |
"/ check if we need UTF8 encoding
|
|
130 |
aClass withAllPrivateClasses do:[:cls |
|
|
131 |
cls instAndClassMethods contains:[:m |
|
|
132 |
self ensureNoUnicodeInMethod:m
|
|
133 |
]
|
|
134 |
].
|
|
135 |
!
|
|
136 |
|
|
137 |
ensureNoUnicodeInMethod:aMethod
|
|
138 |
|src|
|
|
139 |
|
|
140 |
src := aMethod source.
|
|
141 |
src isNil ifTrue:[
|
|
142 |
self error:'missing source in ',aMethod whoString
|
|
143 |
].
|
|
144 |
src isWideString ifTrue:[
|
|
145 |
self error:(aMethod whoString , ' contains unicode strings or character contants. Cannot be exported to VSE')
|
|
146 |
].
|
|
147 |
!
|
|
148 |
|
|
149 |
extensionMethodsFor:packageID
|
|
150 |
|methodsToFileOut|
|
|
151 |
|
|
152 |
methodsToFileOut := OrderedCollection new.
|
|
153 |
Smalltalk allClassesDo:[:eachClass |
|
|
154 |
eachClass instAndClassSelectorsAndMethodsDo:[:sel :mthd |
|
|
155 |
|mPckg|
|
|
156 |
|
|
157 |
mPckg := mthd package.
|
|
158 |
(mPckg = packageID and:[mPckg ~= eachClass package]) ifTrue:[
|
|
159 |
methodsToFileOut add:mthd.
|
|
160 |
]
|
|
161 |
]
|
|
162 |
].
|
|
163 |
^ methodsToFileOut
|
3730
|
164 |
!
|
|
165 |
|
|
166 |
rewrittenClassNameOf:aClass
|
3734
|
167 |
|stxName vseName idx|
|
3730
|
168 |
|
3734
|
169 |
stxName := aClass name.
|
|
170 |
vseName := classNameTranslations at:stxName ifAbsent:[stxName].
|
|
171 |
|
|
172 |
(vseName includesString:'::') ifTrue:[
|
|
173 |
idx := vseName indexOfSubCollection:'::'.
|
|
174 |
^ (vseName copyTo:idx-1) , VSESourceRewriter nameSpaceSeparatorReplacement , (vseName copyFrom:idx+2)
|
3730
|
175 |
].
|
3734
|
176 |
^ vseName
|
3714
|
177 |
! !
|
|
178 |
|
3727
|
179 |
!VSEFileSourceWriter::VSESourceRewriter class methodsFor:'defaults'!
|
|
180 |
|
|
181 |
nameSpaceSeparatorReplacement
|
|
182 |
^ '__'
|
|
183 |
! !
|
|
184 |
|
3714
|
185 |
!VSEFileSourceWriter::VSESourceRewriter class methodsFor:'translation'!
|
|
186 |
|
|
187 |
rewriteMethod:method
|
|
188 |
^ self new rewriteMethod:method
|
|
189 |
! !
|
|
190 |
|
|
191 |
!VSEFileSourceWriter::VSESourceRewriter methodsFor:'accessing'!
|
|
192 |
|
3734
|
193 |
classNamesToRewrite:something
|
|
194 |
classNamesToRewrite := something.
|
|
195 |
!
|
|
196 |
|
3714
|
197 |
method
|
|
198 |
^ method
|
|
199 |
!
|
|
200 |
|
|
201 |
method:something
|
|
202 |
method := something.
|
|
203 |
!
|
|
204 |
|
|
205 |
methodClass
|
|
206 |
^ methodClass
|
|
207 |
!
|
|
208 |
|
|
209 |
methodClass:something
|
|
210 |
methodClass := something.
|
|
211 |
!
|
|
212 |
|
|
213 |
source
|
|
214 |
^ source
|
|
215 |
!
|
|
216 |
|
|
217 |
source:something
|
|
218 |
source := something.
|
|
219 |
! !
|
|
220 |
|
|
221 |
!VSEFileSourceWriter::VSESourceRewriter methodsFor:'translation'!
|
|
222 |
|
3731
|
223 |
checkForUnimplementedMessages
|
|
224 |
"a naive search for messages which are known to be not implemented,
|
|
225 |
and which should be replaced by other messages"
|
|
226 |
|
|
227 |
|parser|
|
|
228 |
|
|
229 |
"/ all references to namespace variables
|
|
230 |
parser := Parser
|
|
231 |
parseMethod:source
|
|
232 |
in:methodClass
|
|
233 |
ignoreErrors:false
|
|
234 |
ignoreWarnings:false.
|
|
235 |
|
|
236 |
(parser messagesSent includesAny: #(
|
|
237 |
'answer:do:'
|
|
238 |
'ignoreIn:'
|
|
239 |
'codePoint'
|
|
240 |
'->'
|
|
241 |
)) ifTrue:[
|
|
242 |
self error:'please rewrite unimplemented messages in ',method whoString
|
|
243 |
].
|
|
244 |
!
|
|
245 |
|
3714
|
246 |
doRewrite
|
3731
|
247 |
self checkForUnimplementedMessages.
|
|
248 |
|
3714
|
249 |
self rewriteEOLComments.
|
3738
|
250 |
self rewriteAnnotationsToComments.
|
3727
|
251 |
"/ self rewriteGlobalsWithNamespace.
|
3724
|
252 |
self rewriteClassReferences.
|
3725
|
253 |
self rewriteExceptionHandlers.
|
3714
|
254 |
^ source.
|
|
255 |
!
|
|
256 |
|
3738
|
257 |
rewriteAnnotationsToComments
|
3740
|
258 |
|parser annots|
|
3738
|
259 |
|
|
260 |
parser := Parser new.
|
|
261 |
parser saveComments:true.
|
|
262 |
parser parseMethod:source in:methodClass ignoreErrors:true ignoreWarnings:true.
|
|
263 |
|
3739
|
264 |
(annots := parser annotationInfo) isEmptyOrNil ifTrue:[^ self].
|
|
265 |
annots := annots copy sort:[:a :b | a endPosition < b endPosition].
|
3740
|
266 |
"/ methodClass == SOAP::XeComponent class ifTrue:[self halt].
|
3739
|
267 |
"/ last one first, so we don't have to care for changed positions due to insertions
|
|
268 |
annots reverseDo:[:annot |
|
|
269 |
source := (source copyTo:annot startPosition-1)
|
|
270 |
, '"' , (source copyFrom:annot startPosition to:annot endPosition)
|
|
271 |
, '"' , (source copyFrom:annot endPosition+1).
|
|
272 |
].
|
3738
|
273 |
!
|
|
274 |
|
3724
|
275 |
rewriteClassReferences
|
3727
|
276 |
|parser globalsToRename symbolsToRename replacer newSource tree|
|
3724
|
277 |
|
|
278 |
"/ all references to namespace variables
|
|
279 |
parser := Parser
|
|
280 |
parseMethod:source
|
|
281 |
in:methodClass
|
|
282 |
ignoreErrors:false
|
|
283 |
ignoreWarnings:false.
|
|
284 |
|
|
285 |
globalsToRename := Dictionary new.
|
|
286 |
symbolsToRename := Dictionary new.
|
|
287 |
|
|
288 |
parser usedGlobals do:[:eachGlobal |
|
|
289 |
|idx nsName clsName|
|
|
290 |
|
|
291 |
(eachGlobal includes:$:) ifTrue:[
|
|
292 |
idx := eachGlobal indexOfSubCollection:'::'.
|
|
293 |
nsName := eachGlobal copyTo:idx-1.
|
|
294 |
clsName := eachGlobal copyFrom:idx+2.
|
3727
|
295 |
globalsToRename at:eachGlobal put:(nsName,(self class nameSpaceSeparatorReplacement),clsName).
|
3724
|
296 |
|
|
297 |
nsName = methodClass nameSpace name ifTrue:[
|
3727
|
298 |
globalsToRename at:clsName put:(nsName,(self class nameSpaceSeparatorReplacement),clsName).
|
3724
|
299 |
]
|
3734
|
300 |
] ifFalse:[
|
3751
|
301 |
(classNamesToRewrite includesKey:eachGlobal) ifTrue:[
|
|
302 |
globalsToRename at:eachGlobal put:(classNamesToRewrite at:eachGlobal).
|
|
303 |
].
|
3724
|
304 |
].
|
|
305 |
].
|
|
306 |
parser usedSymbols do:[:eachSymbol |
|
|
307 |
|idx nsName clsName|
|
|
308 |
|
|
309 |
(eachSymbol includesString:'::') ifTrue:[
|
|
310 |
idx := eachSymbol indexOfSubCollection:'::'.
|
|
311 |
nsName := eachSymbol copyTo:idx-1.
|
|
312 |
clsName := eachSymbol copyFrom:idx+2.
|
|
313 |
(nsName notEmpty
|
|
314 |
and:[ clsName notEmpty
|
|
315 |
and:[ nsName knownAsSymbol
|
|
316 |
and:[ (Smalltalk classNamed:nsName) isNameSpace ]]])
|
|
317 |
ifTrue:[
|
3727
|
318 |
symbolsToRename at:eachSymbol put:(nsName,(self class nameSpaceSeparatorReplacement),clsName) asSymbol.
|
3724
|
319 |
].
|
|
320 |
] ifFalse:[
|
|
321 |
(methodClass nameSpace includesKey:eachSymbol asSymbol) ifTrue:[
|
3727
|
322 |
symbolsToRename at:eachSymbol put:(methodClass nameSpace name,(self class nameSpaceSeparatorReplacement),eachSymbol) asSymbol.
|
3751
|
323 |
] ifFalse:[
|
|
324 |
(classNamesToRewrite includesKey:eachSymbol) ifTrue:[
|
|
325 |
self breakPoint:#cg.
|
|
326 |
globalsToRename at:eachSymbol put:(classNamesToRewrite at:eachSymbol).
|
|
327 |
].
|
|
328 |
].
|
3724
|
329 |
]
|
|
330 |
].
|
|
331 |
(globalsToRename isEmpty and:[symbolsToRename isEmpty]) ifTrue:[^ self ].
|
|
332 |
|
|
333 |
replacer := ParseTreeSourceRewriter new.
|
|
334 |
replacer alwaysPerformAllSearches:true.
|
|
335 |
|
|
336 |
globalsToRename keysAndValuesDo:[:oldName :newName |
|
|
337 |
replacer replaceVariable: oldName with: newName.
|
|
338 |
].
|
|
339 |
symbolsToRename keysAndValuesDo:[:oldName :newName |
|
|
340 |
replacer replaceLiteral: oldName with: newName.
|
|
341 |
].
|
3727
|
342 |
tree := RBParser
|
|
343 |
parseMethod:source
|
|
344 |
onError:[:aString :pos | self halt. ^self].
|
|
345 |
|
|
346 |
replacer executeTree: tree.
|
3724
|
347 |
newSource := replacer executeReplacementsInSource:source.
|
|
348 |
"/ self halt.
|
3727
|
349 |
|
|
350 |
"/ to verify, check parsability
|
|
351 |
RBParser
|
|
352 |
parseMethod:newSource
|
|
353 |
onError:[:aString :pos | self halt.].
|
|
354 |
|
3724
|
355 |
source := newSource.
|
|
356 |
!
|
|
357 |
|
3714
|
358 |
rewriteEOLComments
|
|
359 |
|tree parser eolComments|
|
|
360 |
|
|
361 |
parser := Parser new.
|
|
362 |
parser saveComments:true.
|
|
363 |
parser parseMethod:source in:methodClass ignoreErrors:true ignoreWarnings:true.
|
|
364 |
|
|
365 |
tree := parser tree.
|
|
366 |
eolComments := parser comments select:[:each | each isEndOfLineComment].
|
3740
|
367 |
|
3714
|
368 |
"/ start with the last (so I don't have to update the positions)
|
|
369 |
eolComments sort:[:a :b | a startPosition < b startPosition].
|
3740
|
370 |
|
3714
|
371 |
eolComments reverseDo:[:each |
|
3741
|
372 |
(source at:each endPosition) = $" ifTrue:[
|
|
373 |
"/ nothing to do...
|
|
374 |
] ifFalse:[
|
|
375 |
source := (source copyTo:(each endPosition)),'"',(source copyFrom:(each endPosition + 1))
|
|
376 |
].
|
3714
|
377 |
].
|
3727
|
378 |
|
|
379 |
"/ to verify, check parsability
|
|
380 |
RBParser
|
|
381 |
parseMethod:source
|
|
382 |
onError:[:aString :pos | self halt.].
|
3714
|
383 |
!
|
|
384 |
|
3725
|
385 |
rewriteExceptionHandlers
|
3727
|
386 |
|parser replacer newSource tree|
|
3725
|
387 |
|
|
388 |
"/ all references to namespace variables
|
|
389 |
parser := Parser
|
|
390 |
parseMethod:source
|
|
391 |
in:methodClass
|
|
392 |
ignoreErrors:false
|
|
393 |
ignoreWarnings:false.
|
|
394 |
|
|
395 |
(parser messagesSent includesAny: #( 'answer:do:' 'ignoreIn:' )) ifTrue:[
|
|
396 |
self halt
|
|
397 |
].
|
|
398 |
|
|
399 |
replacer := ParseTreeSourceRewriter new.
|
|
400 |
replacer alwaysPerformAllSearches:true.
|
|
401 |
|
|
402 |
replacer
|
|
403 |
replace: '[| `@temps | ``@.Statements. ] valueNowOrOnUnwindDo: ``@block'
|
|
404 |
"->"
|
|
405 |
with: '[| `@temps | ``@.Statements. ] ensure: ``@block';
|
|
406 |
|
|
407 |
replace: '[| `@temps | ``@.Statements. ] valueOnUnwindDo: ``@block'
|
|
408 |
"->"
|
|
409 |
with: '[| `@temps | ``@.Statements. ] ifCurtailed: ``@block';
|
|
410 |
|
|
411 |
replace:'``@err handle:``@handler do: ``@expr'
|
|
412 |
"->"
|
|
413 |
with: '``@expr on: ``@err do: ``@handler'.
|
|
414 |
|
3727
|
415 |
tree := RBParser
|
|
416 |
parseMethod:source
|
|
417 |
onError:[:aString :pos | self halt. ^ self].
|
|
418 |
|
|
419 |
(replacer executeTree: tree) ifTrue:[
|
3725
|
420 |
newSource := replacer executeReplacementsInSource:source.
|
|
421 |
"/ self halt.
|
|
422 |
source := newSource.
|
|
423 |
].
|
3727
|
424 |
|
|
425 |
"/ to verify, check parsability
|
|
426 |
RBParser
|
|
427 |
parseMethod:source
|
|
428 |
onError:[:aString :pos | self halt.].
|
3725
|
429 |
!
|
|
430 |
|
3714
|
431 |
rewriteGlobalsWithNamespace
|
3727
|
432 |
"obsolete old version - now done in rewriteClassReferences"
|
|
433 |
|
|
434 |
|parser namesToRewrite|
|
3714
|
435 |
|
|
436 |
namesToRewrite := OrderedCollection new.
|
|
437 |
|
|
438 |
parser := Parser new.
|
|
439 |
parser saveComments:true.
|
|
440 |
parser parseMethod:source in:methodClass ignoreErrors:true ignoreWarnings:true.
|
|
441 |
parser tree isNil ifTrue:[ ^ self ].
|
|
442 |
|
|
443 |
parser tree variableNodesDo:[:each |
|
|
444 |
|nameInSource|
|
|
445 |
|
|
446 |
each isGlobalVariable ifTrue:[
|
|
447 |
(each name includes:$:) ifTrue:[
|
|
448 |
nameInSource := source copyFrom:each startPosition to:each endPosition.
|
|
449 |
(nameInSource includes:$:) ifTrue:[
|
|
450 |
namesToRewrite add:each.
|
|
451 |
].
|
|
452 |
].
|
|
453 |
].
|
|
454 |
].
|
|
455 |
|
|
456 |
"/ start with the last (so I don't have to update the positions)
|
|
457 |
namesToRewrite sort:[:a :b | a position < b position].
|
|
458 |
namesToRewrite reverseDo:[:each |
|
|
459 |
|idx nameInSource ns nm|
|
|
460 |
|
|
461 |
nameInSource := source copyFrom:each startPosition to:each endPosition.
|
|
462 |
idx := nameInSource indexOf:$:.
|
|
463 |
ns := nameInSource copyTo:idx-1.
|
|
464 |
idx := nameInSource indexOf:$: startingAt:idx+1.
|
|
465 |
nm := nameInSource copyFrom:idx+1.
|
|
466 |
(ns = methodClass nameSpace name) ifFalse:[
|
|
467 |
Transcript show:'reference to other namespace'.
|
|
468 |
] ifTrue:[
|
|
469 |
source := (source copyTo:(each startPosition-1)),nm,(source copyFrom:(each endPosition + 1))
|
|
470 |
].
|
|
471 |
].
|
3727
|
472 |
|
|
473 |
"/ to verify, check parsability
|
|
474 |
RBParser
|
|
475 |
parseMethod:source
|
|
476 |
onError:[:aString :pos | self halt.].
|
3714
|
477 |
!
|
|
478 |
|
|
479 |
rewriteMethod:methodArg
|
|
480 |
method := methodArg.
|
|
481 |
source := method source.
|
|
482 |
methodClass := method mclass.
|
|
483 |
^ self doRewrite.
|
|
484 |
! !
|
|
485 |
|
|
486 |
!VSEFileSourceWriter class methodsFor:'documentation'!
|
|
487 |
|
|
488 |
version
|
3751
|
489 |
^ '$Header: /cvs/stx/stx/libbasic3/VSEFileSourceWriter.st,v 1.13 2015-02-03 22:40:43 cg Exp $'
|
3714
|
490 |
!
|
|
491 |
|
|
492 |
version_CVS
|
3751
|
493 |
^ '$Header: /cvs/stx/stx/libbasic3/VSEFileSourceWriter.st,v 1.13 2015-02-03 22:40:43 cg Exp $'
|
3714
|
494 |
! !
|
|
495 |
|