This file is indexed.

/usr/share/gnu-smalltalk/scripts/Convert.st is in gnu-smalltalk-common 3.2.5-1build2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
"======================================================================
|
|   Smalltalk syntax conversion tool
|
|
 ======================================================================"


"======================================================================
|
| Copyright 2007, 2008, 2009 Free Software Foundation, Inc.
| Written by Daniele Sciascia.
|
| This file is part of the GNU Smalltalk class library.
|
| The GNU Smalltalk class library is free software; you can redistribute it
| and/or modify it under the terms of the GNU Lesser General Public License
| as published by the Free Software Foundation; either version 2.1, or (at
| your option) any later version.
| 
| The GNU Smalltalk class library is distributed in the hope that it will be
| useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
| General Public License for more details.
| 
| You should have received a copy of the GNU Lesser General Public License
| along with the GNU Smalltalk class library; see the file COPYING.LIB.
| If not, write to the Free Software Foundation, 59 Temple Place - Suite
| 330, Boston, MA 02110-1301, USA.  
|
 ======================================================================"

PackageLoader fileInPackage: #Parser.

STInST.OldSyntaxExporter class extend [
    emitEval: aBlock to: aStream for: namespace [
        namespace isNil
            ifFalse: [ aStream nextPutAll: 'Namespace current: ';
		       store: namespace; nextPut: $!; nl ].

	aBlock value.
        aStream nextPut: $!; nl; nl.
    ]
]

STInST.SqueakSyntaxExporter class extend [
    emitEval: aBlock to: aStream for: namespace [
	aBlock value.
	aStream nextPut: $!; nl; nl.
    ]
]

STInST.NewSyntaxExporter class extend [
    emitEval: aBlock to: aStream for: namespace [
        namespace isNil
            ifTrue: [ aStream nextPutAll: 'Eval' ]
            ifFalse: [ aStream nextPutAll: 'Namespace current: ';
		       store: namespace ].

	    aStream nextPutAll: ' ['.
	    aBlock value.
            aStream nl; nextPut: $]; nl; nl.
    ]
]


Object subclass: EmittedEntity [  
    emitTo: aStream filteredBy: aBlock [
        self subclassResponsibility
    ]
]

EmittedEntity subclass: EmittedComments [
    | comments |
    EmittedComments class >> comments: aCollection source: aString [
	^self new comments: (aCollection collect: [ :c |
	    aString copyFrom: c first to: c last ])
    ]

    emitTo: outStream filteredBy: aBlock [
	comments do: [ :c |
		STInST.FileOutExporter defaultExporter fileOutComment: c to: outStream.
		outStream nl; nl]
    ]

    comments: anArray [
	comments := anArray
   ]
]

EmittedEntity subclass: EmittedClass [
    | class methodsToEmit classMethodsToEmit isComplete |
    
    <comment: 'This class is responsible for emitting a class 
    by using a FormattingExporter.'>
    
    EmittedClass class >> forClass: aClass [        
	(aClass superclass notNil and: [
	    aClass superclass isDefined not ]) ifTrue: [
	        Warning signal:
		    ('superclass %1 is undefined' % {aClass superclass}) ].
        ^super new initializeWithClass: aClass complete: true
    ]
    
    EmittedClass class >> forExtension: aClass [
	aClass isDefined ifFalse: [
	    Warning signal:
		('extensions for undefined class %1' % {aClass}) ].
        ^super new initializeWithClass: aClass complete: false
    ]
    
    initializeWithClass: aClass complete: aBoolean [
        class := aClass.
        methodsToEmit := STInST.OrderedSet new.
	classMethodsToEmit := STInST.OrderedSet new.
	isComplete := aBoolean
    ]

    forClass [ 
        ^class
    ]
    
    addMethod: aMethod [
        methodsToEmit add: aMethod selector asSymbol.
    ]

    addClassMethod: aMethod [
	classMethodsToEmit add: aMethod selector asSymbol.
    ]
	
    emitTo: aStream filteredBy: aBlock [ 
	(aBlock value: class)
	    ifFalse: [
	        Notification signal: ('Skipping %1' % {class}).
		^self ].

        Notification signal: ('Converting %1...' % {class}).
        (STInST.FileOutExporter defaultExporter on: class to: aStream)
            completeFileOut: isComplete;
            fileOutSelectors: methodsToEmit classSelectors: classMethodsToEmit.
    ]
]

EmittedEntity subclass: EmittedEval [
    | statements comments namespace |
    
    <comment: 'This class is responsible for emitting a set of 
    statements that should be inside an Eval declaration.'>
    
    EmittedEval class >> new [
        ^super new initialize
    ]
    
    initialize [
        statements := OrderedCollection new
    ]
    
    namespace [ 
	^namespace
    ]

    namespace: aNamespace [ 
	namespace := aNamespace
    ]

    addStatement: aStatement [
        statements add: aStatement
    ] 
    
    emitTo: aStream filteredBy: aBlock [
	statements isEmpty ifTrue: [ ^self ].
	STInST.FileOutExporter defaultExporter
	    emitEval: [
		| formatter |
		formatter := STInST.RBFormatter new.
		formatter indent: 1 while: [
		    formatter indent.
	            aStream nextPutAll: (formatter formatAll: statements) ]]
	    to: aStream
	    for: namespace.
    ]
]



STInST.STClassLoader subclass: SyntaxConverter [
    | stuffToEmit classesToEmit createdNamespaces filter outStream rewriter |
    
    <comment: 'A class loader that creates a set of "EmittedEntity"
    based on the contents of the given file being loaded.
    When the contents of the file are loaded, the responsibilty of 
    emitting code using the new syntax belongs to those various 
    entities that have been constructed.'>
    
    
    SyntaxConverter class >> convertSqueakStream: in to: out [
        <category: 'instance creation'>
        ^self convertStream: in with: STInST.SqueakFileInParser to: out
    ]
    
    SyntaxConverter class >> convertSIFStream: in to: out [
        <category: 'instance creation'>
        ^self convertStream: in with: STInST.SIFFileInParser to: out
    ]
    
    SyntaxConverter class >> convertStream: in to: out [
        <category: 'instance creation'>
        ^self convertStream: in with: STInST.STFileInParser to: out
    ]
    
    SyntaxConverter class >> convertStream: in with: aParserClass to: out [
        <category: 'instance creation'>
        ^self new convertStream: in with: aParserClass to: out
    ]

    initialize [
        <category: 'initialization'>
	super initialize.
	filter := [ :class | [true] ].
        stuffToEmit := OrderedSet new.
        classesToEmit := Dictionary new.
        createdNamespaces := OrderedSet new.
    ]

    convertStream: in with: aParserClass to: out onError: aBlock [
        <category: 'operation'>
        self
	    outStream: out;
	    parseSmalltalkStream: in with: aParserClass onError: aBlock;
	    doEmitStuff.        
    ]

    convertStream: in with: aParserClass to: out [
        <category: 'operation'>
        self
	    outStream: out;
	    parseSmalltalkStream: in with: aParserClass;
	    doEmitStuff.        
    ]

    filter: aBlock [
        <category: 'accessing'>
        filter := aBlock.
    ]
    
    outStream: out [
        <category: 'accessing'>
        outStream := out.
    ]
    
    rewrite: node [
	^rewriter isNil
	    ifTrue: [ node ]
	    ifFalse: [ rewriter executeTree: node; tree ].
    ]

    evaluate: node [
        <category: 'overrides'>

	| rewritten |
	rewritten := self rewrite: node.
	node comments isEmpty ifFalse: [
	    stuffToEmit add: (EmittedComments comments: node comments source: node source) ].

        ^super evaluate: rewritten
    ]
    
    addRule: searchString parser: aParserClass [
	| tree rule |
	tree := aParserClass parseRewriteExpression: searchString.
	tree isMessage ifFalse: [ self error: 'expected ->' ].
	tree selector = #-> ifFalse: [ self error: 'expected ->' ].
	rule := RBStringReplaceRule
	    searchForTree: tree receiver
	    replaceWith: tree arguments first.

	rewriter isNil ifTrue: [ rewriter := ParseTreeRewriter new ].
	rewriter addRule: rule
    ]

    compile: node [
        <category: 'collecting entities'>
        
        | rewritten method |

	rewritten := self rewrite: node.
        method := self defineMethod: rewritten.                
        (classesToEmit includesKey: currentClass asClass)
            ifTrue: [ self addMethod: method toLoadedClass: currentClass ]
            ifFalse: [ self addMethod: method toExtensionClass: currentClass ].
	^method
    ]
    
    lastEval [
        <category: 'collecting entities'>

	| lastIsEval evalNamespace |

        evalNamespace := currentNamespace = self defaultNamespace
	    ifTrue: [ nil ]
	    ifFalse: [ currentNamespace ].

        lastIsEval := stuffToEmit notEmpty
	    and: [ (stuffToEmit last isKindOf: EmittedEval)
	    and: [ stuffToEmit last namespace = evalNamespace ]].

	^lastIsEval
	    ifTrue: [ stuffToEmit last ]
	    ifFalse: [ stuffToEmit add: (EmittedEval new namespace: evalNamespace) ]
    ]

    createNamespaces [
	createdNamespaces do: [ :each || stmt |
	    stmt := RBMessageNode
                receiver: (RBVariableNode named: (each superspace nameIn: self currentNamespace))
                selector: #addSubspace:
                arguments: { RBLiteralNode value: each name asSymbol }.
	    self lastEval addStatement: stmt
	].
	createdNamespaces := OrderedSet new
    ]

    unknown: node [
        <category: 'collecting entities'>
        
	self createNamespaces.
	self lastEval addStatement: node.
        ^false
    ]
    
    doSubclass: receiver selector: selector arguments: argumentNodes [
        <category: 'evaluating statements'>
        
        | class emittedClass |
        
	createdNamespaces remove: self currentNamespace ifAbsent: [ ].
	self createNamespaces.

        class := super defineSubclass: receiver 
                       selector: selector 
                       arguments: argumentNodes.        
                            
        Notification signal: ('Parsing %1' % {class}).
        emittedClass := EmittedClass forClass: class.
    
        classesToEmit at: class put: emittedClass.
        stuffToEmit add: emittedClass.
        
        ^false
    ]
    
    doAddNamespace: receiver selector: selector arguments: argumentNodes [
	| ns |
	super doAddNamespace: receiver selector: selector arguments: argumentNodes.

        ns := (self resolveNamespace: receiver) at: argumentNodes first value.
	createdNamespaces add: ns.
	^false
    ]

    doEmitStuff [
        <category: 'emitting'>

        stuffToEmit
	    do: [ :each | each emitTo: outStream filteredBy: filter ]
	    separatedBy: [ outStream nl; nextPut: 12 asCharacter; nl ].
    ]
    
    addMethod: aMethod toLoadedClass: aClass [
        <category: 'collecting entities'>

        (aClass isMetaclass)
            ifTrue: [ (classesToEmit at: currentClass asClass) addClassMethod: aMethod ]
            ifFalse: [ (classesToEmit at: currentClass) addMethod: aMethod ]
    ]
    
    addMethod: aMethod toExtensionClass: aClass [
        <category: 'collecting entities'>

        ((stuffToEmit size > 0)
            and: [ (stuffToEmit last isKindOf: EmittedClass) and: [ stuffToEmit last forClass = aClass ] ])                
                ifTrue: [ stuffToEmit last addMethod: aMethod ]
                ifFalse: [ stuffToEmit add: ((EmittedClass forExtension: currentClass) addMethod: aMethod) ]            
    ]
]


String extend [
   asFilterOn: aBlock through: valueBlock [
	| regex |
	self first = $+ ifTrue: [
	    regex := self allButFirst asRegex.
	    ^[ :obj | (aBlock value: obj)
			 or: [ (valueBlock value: obj) ~ regex ] ] ].

	self first = $- ifTrue: [
	    regex := self allButFirst asRegex.
	    ^[ :obj | (aBlock value: obj)
			 and: [ ((valueBlock value: obj) ~ regex) not ] ] ].

	regex := self asRegex.
	^[ :obj | (aBlock value: obj) and: [ (valueBlock value: obj) ~ regex ] ]
    ]
]


Eval [ 
    | helpString inFile outFile quiet verbose converter filter parser
	args inFormats outFormats rules |
    args := OrderedCollection new.
    parser := STInST.STFileInParser.
    quiet := false.
    verbose := false.
    outFile := nil.
    filter := [ :class | true ].
    converter := SyntaxConverter new.
    STInST.FileOutExporter defaultExporter: STInST.FormattingExporter.
    outFormats := Dictionary from: {
	'gst2' -> STInST.OldSyntaxExporter.
	'gst' -> STInST.FormattingExporter.
	'squeak' -> STInST.SqueakSyntaxExporter.
    }.
    inFormats := Dictionary from: {
	'gst2' -> STInST.STFileInParser.
	'gst' -> STInST.GSTFileInParser.
	'squeak' -> STInST.SqueakFileInParser.
	'sif' -> STInST.SIFFileInParser
    }.
    rules := OrderedCollection new.
    
    helpString :=
'Usage:
    gst-convert [OPTION]... [INFILE [OUTFILE]]
    gst-convert [OPTION]... -o|--output OUTFILE INFILES

Options:
    -q, --quiet               don''t show any message
    -v, --verbose             print extra information while processing
    -f, --format=FORMAT       convert from given input format (supported
                              formats are %1)
    -F, --output-format=FORMAT convert to given output format (supported
                              formats are %2)
    -C, --class=REGEX         convert only classes matching REGEX
    -C, --class=+REGEX        in addition, convert classes matching REGEX
    -C, --class=-REGEX        do not convert classes matching REGEX
    -c, --category=REGEX      convert only classes whose category matches REGEX
    -c, --category=+REGEX     in addition, convert those whose category
                              matches REGEX
    -c, --category=-REGEX     do not convert classes whose category
                              matches REGEX
    -r, --rule=''CODE->REPL''   look for CODE and replace it with REPL

    -o, --output OUTFILE      concatenate multiple input files into a single
                              converted output file
        --help                display this message and exit
        --version             print version information and exit

' % {inFormats keys asSortedCollection fold: [ :a :b | a, ', ', b ].
     outFormats keys asSortedCollection fold: [ :a :b | a, ', ', b ]}.

    Smalltalk
        arguments: '-h|--help --version -q|--quiet -v|-V|--verbose -r|--rule:
		    -C|--class: -c|--category: -f|--format: -o|--output:
		    -F|--output-format: -I|--image-file: --kernel-directory:'
        do: [ :opt :arg |
	    opt = 'help' ifTrue: [
	        helpString displayOn: stdout.
	        ObjectMemory quit: 0 ].

	    opt = 'version' ifTrue: [
	        ('gst-convert - %1' % {Smalltalk version}) displayNl.
	        ObjectMemory quit: 0 ].

	    opt = 'quiet' ifTrue: [
		quiet := true.
		verbose := false ].

	    opt = 'verbose' ifTrue: [
		quiet := false.
		verbose := true ].

	    opt = 'output' ifTrue: [
		outFile isNil ifFalse: [
		    helpString displayOn: stderr.
		    ObjectMemory quit: 1 ].
		outFile := arg ]. 

	    opt = 'rule' ifTrue: [
		rules add: arg].

	    opt = 'class' ifTrue: [
		[ 'a' ~ arg ] on: Error do: [ :ex |
		    helpString displayOn: stderr.
		    ObjectMemory quit: 1 ].

		filter := arg
		    asFilterOn: filter
		    through: [ :class | class asClass nameIn: Smalltalk ] ].

	    opt = 'category' ifTrue: [
		[ 'a' ~ arg ] on: Error do: [ :ex |
		    helpString displayOn: stderr.
		    ObjectMemory quit: 1 ].

		filter := arg
		    asFilterOn: filter
		    through: [ :class | class category ifNil: [ '' ] ] ].

	    opt = 'output-format' ifTrue: [
		STInST.FileOutExporter defaultExporter:
		    (outFormats at: arg ifAbsent: [
		        helpString displayOn: stderr.
		        ObjectMemory quit: 1 ]) ].

	    opt = 'format' ifTrue: [
		parser := inFormats at: arg ifAbsent: [
		    helpString displayOn: stderr.
		    ObjectMemory quit: 1 ] ].

	    opt isNil ifTrue: [
		args addLast: arg ].
        ]

        ifError: [
            helpString displayOn: stderr.
            ObjectMemory quit: 1 ].

    "Post process the rules now we know the target."
    rules do: [:rule |
	converter addRule: rule parser: parser].

    [
        outFile isNil
	    ifTrue: [
	        args size > 2 ifTrue: [
	            helpString displayOn: stderr.
	            ObjectMemory quit: 1 ].

	        inFile := (args size = 0 or: [ args first = '-' ])
		    ifTrue: [ stdin ]
		    ifFalse: [ FileStream open: args first mode: FileStream read ].
	        outFile := (args size <= 1 or: [ args last = '-' ])
		    ifTrue: [ stdout ]
		    ifFalse: [ FileStream open: args last mode: FileStream write ] ]
	    ifFalse: [
		args := args collect: [ :f |
		    f = '-'
			ifTrue: [ stdin ]
			ifFalse: [ FileStream open: f mode: FileStream read ] ].
		inFile := args fold: [ :a :b | a, b ].

	        outFile := outFile = '-'
		    ifTrue: [ stdout ]
		    ifFalse: [ FileStream open: outFile mode: FileStream write ] ].

	converter filter: filter.
	converter
	    convertStream: inFile
	    with: parser
	    to: outFile.

	inFile close.
	outFile close
    ]
	on: Notification do: [ :ex |
	    verbose ifTrue: [ stderr nextPutAll: 'gst-convert: ', ex messageText; nl; flush ].
	    ex resume ]
	on: Warning do: [ :ex |
	    quiet ifFalse: [ stderr nextPutAll: 'gst-convert: warning: ', ex messageText; nl; flush ].
	    ex resume ]
	on: Error do: [ :ex |
	    stderr nextPutAll: 'gst-convert: error: ', ex messageText; nl; flush.
	    outFile = stdout ifFalse: [
	        outFile close.

		"TODO: don't do this on non-regular files.  It will make
		 /dev/null disappear if you run gst-convert as root (which
		 you shouldn't)."
		[ (File name: outFile name) remove ] on: Error do: [ :ex | ] ].
	    "ex pass." ObjectMemory quit: 1 ].
]