This file is indexed.

/usr/share/gnu-smalltalk/examples/RegExp.st is in gnu-smalltalk-common 3.2.4-2.1.

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
"======================================================================
|
|   Regular expressions
|
|
 ======================================================================"


"======================================================================
|
| Copyright 1999 Free Software Foundation, Inc.
| Written by Paolo Bonzini.
|
| This file is part of GNU Smalltalk.
|
| GNU Smalltalk is free software; you can redistribute it and/or modify it
| under the terms of the GNU General Public License as published by the Free
| Software Foundation; either version 2, or (at your option) any later version.
| 
| GNU Smalltalk 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 General Public License for more
| details.
| 
| You should have received a copy of the GNU General Public License along with
| GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
| Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  
|
 ======================================================================"

Object subclass: #RegularExpression
    instanceVariableNames: 'selectors params noDollar string'
    classVariableNames: ''
    poolDictionaries: ''
    category: 'Examples-Useful'!

!RegularExpression class methodsFor: 'instance creation'!

new
    self shouldNotImplement
!

fromString: aString
    ^super new parseString: aString
! !

!RegularExpression class methodsFor: 'parsing'!

match: aString to: regExp
    ^(self fromString: regExp) match: aString
! !

!RegularExpression methodsFor: 'parsing'!

match: aString

    ^self matchStream: (ReadStream on: aString) from: 1
!

matchStream: aStream

    ^self matchStream: aStream from: 1
! !

!RegularExpression methodsFor: 'private'!

parseString: aString
    "Private - Convert a regular expression to its internal representation"

    | regexp endPos |

    params := OrderedCollection new.
    selectors := OrderedCollection new.

    "Zero-length aString is a special case"
    aString size = 0
	ifTrue: [
	    noDollar := true.
	    self addDotAsterisk.
	    ^self
	].

    regexp := ReadStream on: aString.
    noDollar := aString last ~= $$.

    (regexp peekFor: $^)
	ifFalse: [ self addDotAsterisk ]

    endPos := noDollar
	ifTrue: [ aString size ]
	ifFalse: [ aString size - 1 ].

    [ regexp position > endPos ] whileFalse: [ self parseAtom: regexp ]
!

addDotAsterisk

    "Add an implicit .* sequence"
    params addLast: nil.
    selectors addLast: #wild0any:index:
!

parseAtom: regexp
    "Private - Parse an 'atom' of the regular expression. Add to selectors the
     selector to be called to match it, and add to params the first parameter that
     will be passed to this selector"

    | next |
    (next := regexp next) = $\
	ifTrue: [
	    params addLast: regexp next.
	    ^selectors addLast: #char:index:
	].

    (next = $+) & selectors notEmpty
	ifTrue: [
	    ^selectors
		at: selectors size
		put: ('wild1', selectors last) asSymbol
	].

    (next = $*) & selectors notEmpty
	ifTrue: [
	    ^selectors
		at: selectors size
		put: ('wild0', selectors last) asSymbol
	].

    next = $.
	ifTrue: [
	    params addLast: nil.
	    ^selectors addLast: #any:index:
	].

    next = $[
	ifTrue: [
	    (regexp peekFor: $^)
		ifTrue: [ selectors addLast: #notRange:index: ]
		ifFalse: [ selectors addLast: #range:index: ].

	    params addLast: (self parseRange: regexp).
	    ^selectors last
	].

    next = ${
	ifTrue: [
	    params addLast: (self parseOptional: regexp).
	    ^selectors addLast: #optional:index:
	].

    params addLast: next.
    ^selectors addLast: #char:index:
!

parseRange: regexp

    "Private - Parse a 'range atom', that is an atom that can match to seve-
     ral characters."

    | next answerStream |
    answerStream := WriteStream on: (String new: 8).  "Number out of a hat"

    [   (next := regexp next) = $] ] whileFalse: [
	answerStream nextPut: next.
	regexp atEnd ifTrue: [ self errorBadRegexp ].

	(regexp peekFor: $-) ifTrue: [
	    regexp atEnd ifTrue: [ self errorBadRegexp ].

	    next asciiValue + 1 to: regexp next asciiValue do: [:i |
		answerStream nextPut: i asCharacter
	    ]
	]
    ].
    ^answerStream contents!

parseOptional: regexp

    "Private - Parse an 'optional atom', that is an atom that can match to
     several regular expressions."

    | pos next result |
    pos := regexp position.
    result := OrderedCollection new.
    [
	(next := regexp next) = $\
	    ifTrue: [regexp next]
	    ifFalse: [
		next = $| ifTrue: [
		    result addLast: (self class fromString:
			'^', (regexp copyFrom: pos to: regexp position - 2)).
		    pos := regexp position
		].
		next = $} ifTrue: [
		    result addLast: (self class fromString:
			'^', (regexp copyFrom: pos to: regexp position - 2)).
		    ^result
		]
	    ].
	regexp atEnd
    ]   whileFalse: [ ].

    "If we come here, we have found no } : bad regular expression"
    self errorBadRegexp
!

errorBadRegexp

    "Seems like we had some problems parsing the regular expression"
    self error: 'Bad regular expression'
!

char: aCharacter index: dummy

    "Private - Check if the next character matchs to aCharacter"

    ^string atEnd
	ifTrue: [ false ]
	ifFalse: [ aCharacter = string next ]
!

any: dummy index: dummy2

    "Private - If we aren't at the end of the stream, skip a character, else
     answer false"

    ^string atEnd
	ifTrue: [ false ]
	ifFalse: [ string next. true ]
!

range: aString index: dummy

    "Private - Check if the next character is included in aString"

    ^string atEnd
	ifTrue: [ false ]
	ifFalse: [ aString includes: string next ]
!

notRange: aString index: dummy

    "Private - Check if the next character is not included in aString"

    ^string atEnd
	ifTrue: [ false]
	ifFalse: [ (aString includes: string next) not ]
!

optional: listOfRegexp index: dummy

    "Private - Check if the next characters match to any of the RegularExpression
     objects in listOfRegexp"

    | pos |

    string atEnd ifTrue: [^false].

    pos := string position.
    listOfRegexp do: [ :re |
	(re matchStream: string from: 1) ifTrue: [^true].
	string position: pos.
    ].
    ^false
!

wild0any: atLeast1 index: index

    "Private - Match a .* sequence"

    ^self matchWild: #any:index: with: nil following:
	[ self matchStream: string from: index + 1 ]
!

wild1any: atLeast1 index: index

    "Private - Match a .+ sequence"

    (self any: nil index: index) ifFalse: [^false].

    ^self matchWild: #any:index: with: nil following:
	[ self matchStream: string from: index + 1 ]
!

wild0range: aString index: index

    "Private - Match a [...]* sequence"

    ^self matchWild: #range:index: with: aString following:
	[ self matchStream: string from: index + 1 ]
!

wild1range: aString index: index

    "Private - Match a [...]+ sequence"

    (self range: aString index: index) ifFalse: [^false].
    ^self matchWild: #range:index: with: aString following:
	[ self matchStream: string from: index + 1 ]
!

wild0notRange: aString index: index

    "Private - Match a [...]* sequence"

    ^self matchWild: #notRange:index: with: aString following:
	[ self matchStream: string from: index + 1 ]
!

wild1notRange: aString index: index

    "Private - Match a [...]+ sequence"

    (self notRange: aString index: index) ifFalse: [^false].
    ^self matchWild: #notRange:index: with: aString following:
	[ self matchStream: string from: index + 1 ]
!

wild0char: aCharacter index: index

    "Private - Match a x* sequence"

    ^self matchWild: #char:index: with: aCharacter following:
	[ self matchStream: string from: index + 1 ]
!

wild1char: aCharacter index: index

    "Private - Match a x+ sequence"

    (self char: aCharacter index: index) ifFalse: [^false].
    ^self matchWild: #char:index: with: aCharacter following:
	[ self matchStream: string from: index + 1 ]
!

matchWild: aSymbol with: arg following: aBlock

    "Private - Helper function for * sequences (+ sequences are parsed by
     checking for a match and then treating them as * sequences: for example,
     x+ becomes xx*). Try to match as many characters as possible and then
     look if the remaining part of the string matches the rest of the regular
     expression (to do so, aBlock is evaluated): if yes, answer nil; if no,
     try again with one character less. For example, matching [ABC]*AC to the
     string BAC works in this way:
	  - try with the longest run of As, Bs or Cs (BAC). The rest of the
	    string (that is, nothing) doesn't match the regular expression AC, so...
	  - ...try with BA. The rest of the string (that is, C) doesn't match
	    the regular expression AC, so...
	  - ...try with B. The rest of the string (that is, AC) does match the
	    regular expression AC, so we answer nil"

    | first last |
    first := string position.
    last := self findLastWild: aSymbol with: arg.

    last to: first by: -1 do: [ :i |
	(aBlock value == false)
	    ifFalse: [ ^nil ].

	i > 1 ifTrue: [self position: i - 1].
    ].
    ^false
!

findLastWild: aSymbol with: arg
    "Send aSymbol with arg and nil as its parameter until it answers false and
     answer the position of the last character for which aSymbol answered true"

    [   string atEnd ifTrue: [ ^string position ].
	self perform: aSymbol with: arg with: nil ] whileTrue: [ ].

    string skip: -1.
    ^string position
!

matchStream: aStream from: firstIndex

    "Private - Match all the atoms from the firstIndex-th to the string on
     which aStream is streaming. Answer true or false"

    | result oldString |

    oldString := string.
    self string: aStream.

    firstIndex to: self numberOfAtoms do: [ :i |
	" result = true ---> go on; result = false ---> answer false;
	  result = nil ---> answer true"
	result := self matchAtom: i.
	result == true ifFalse: [ string := oldString. ^result isNil ].
    ].
    result := self checkIfAtEnd.
    string := oldString.
    ^result
!

string: aStream

    "Private - Tell the other methods which string is being parsed"

    string := aStream
!

checkIfAtEnd

    "Private - Answer true if there is no $ or if we're at the end of the
     parsed string"

    ^noDollar or: [string atEnd]
!

numberOfAtoms

    "Private - Answer the number of atoms in the receiver"

    ^selectors size
!

matchAtom: index

    "Private - Try to match an atom to string"

    | result |

    "index print. $  print.
     (selectors at: index) print. $  print.
     (params at: index) print. $  print.
     string peek print. $  printNl."

    ^self perform: (selectors at: index) with: (params at: index) with: index
! !