1
|
1 |
"{ Package: 'stx:goodies/smaCC' }"
|
|
2 |
|
|
3 |
"{ NameSpace: SmaCC }"
|
|
4 |
|
|
5 |
PeekableStream subclass:#SmaCCLineNumberStream
|
|
6 |
instanceVariableNames:'sourceStream previousWasCR eolPositions lastPosition'
|
|
7 |
classVariableNames:'CR LF'
|
|
8 |
poolDictionaries:''
|
|
9 |
category:'SmaCC-Runtime'
|
|
10 |
!
|
|
11 |
|
|
12 |
|
|
13 |
!SmaCCLineNumberStream class methodsFor:'initialization'!
|
|
14 |
|
|
15 |
initialize
|
|
16 |
CR := Character cr.
|
|
17 |
Smalltalk isSmalltalkX ifTrue:[
|
|
18 |
CR := Character return.
|
|
19 |
].
|
|
20 |
LF := Character nl.
|
|
21 |
|
|
22 |
"
|
|
23 |
self initialize
|
|
24 |
"
|
|
25 |
! !
|
|
26 |
|
|
27 |
!SmaCCLineNumberStream class methodsFor:'instance creation'!
|
|
28 |
|
|
29 |
on: aReadStream
|
|
30 |
^(self basicNew)
|
|
31 |
on: aReadStream;
|
|
32 |
yourself
|
|
33 |
! !
|
|
34 |
|
|
35 |
!SmaCCLineNumberStream class methodsFor:'accessing'!
|
|
36 |
|
|
37 |
zeroPosition
|
|
38 |
|
|
39 |
^0
|
|
40 |
|
|
41 |
"Created: / 05-04-2006 / 21:27:49 / janfrog"
|
|
42 |
! !
|
|
43 |
|
|
44 |
!SmaCCLineNumberStream methodsFor:'accessing'!
|
|
45 |
|
|
46 |
contents
|
|
47 |
^sourceStream contents
|
|
48 |
!
|
|
49 |
|
|
50 |
flush
|
|
51 |
^sourceStream flush
|
|
52 |
!
|
|
53 |
|
|
54 |
lineNumber
|
|
55 |
| index start stop pos |
|
|
56 |
pos := sourceStream position.
|
|
57 |
pos >= eolPositions last ifTrue: [^eolPositions size].
|
|
58 |
start := 1.
|
|
59 |
stop := eolPositions size.
|
|
60 |
[start + 1 < stop] whileTrue:
|
|
61 |
[index := (start + stop) // 2.
|
|
62 |
(eolPositions at: index) <= pos
|
|
63 |
ifTrue: [start := index]
|
|
64 |
ifFalse: [stop := index]].
|
|
65 |
^start
|
|
66 |
!
|
|
67 |
|
|
68 |
next
|
|
69 |
| character |
|
|
70 |
character := sourceStream next.
|
|
71 |
sourceStream position - 1 == lastPosition
|
|
72 |
ifTrue:
|
|
73 |
[lastPosition := lastPosition + 1.
|
|
74 |
character == CR
|
|
75 |
ifTrue:
|
|
76 |
[eolPositions add: sourceStream position.
|
|
77 |
previousWasCR := true]
|
|
78 |
ifFalse:
|
|
79 |
[(previousWasCR not and: [character == LF])
|
|
80 |
ifTrue: [eolPositions add: sourceStream position].
|
|
81 |
previousWasCR := false]].
|
|
82 |
^character
|
|
83 |
!
|
|
84 |
|
|
85 |
nextPut: anObject
|
|
86 |
^sourceStream nextPut: anObject
|
|
87 |
!
|
|
88 |
|
|
89 |
position
|
|
90 |
^sourceStream position
|
|
91 |
!
|
|
92 |
|
|
93 |
position: anInteger
|
|
94 |
anInteger > lastPosition
|
|
95 |
ifTrue:
|
|
96 |
[sourceStream position: lastPosition.
|
|
97 |
[sourceStream position < anInteger and: [sourceStream atEnd not]]
|
|
98 |
whileTrue: [self next]]
|
|
99 |
ifFalse: [sourceStream position: anInteger]
|
|
100 |
!
|
|
101 |
|
|
102 |
skip: anInteger
|
|
103 |
^self position: self position + anInteger
|
|
104 |
! !
|
|
105 |
|
|
106 |
!SmaCCLineNumberStream methodsFor:'error handling'!
|
|
107 |
|
|
108 |
doesNotUnderstand: aMessage
|
|
109 |
^sourceStream perform: aMessage selector withArguments: aMessage arguments
|
|
110 |
! !
|
|
111 |
|
|
112 |
!SmaCCLineNumberStream methodsFor:'initialize-release'!
|
|
113 |
|
|
114 |
on: aReadStream
|
|
115 |
sourceStream := aReadStream.
|
|
116 |
eolPositions := OrderedCollection with: aReadStream position.
|
|
117 |
lastPosition := aReadStream position.
|
|
118 |
previousWasCR := false
|
|
119 |
! !
|
|
120 |
|
|
121 |
!SmaCCLineNumberStream methodsFor:'testing'!
|
|
122 |
|
|
123 |
atEnd
|
|
124 |
^sourceStream atEnd
|
|
125 |
!
|
|
126 |
|
|
127 |
isReadable
|
|
128 |
^sourceStream isReadable
|
|
129 |
!
|
|
130 |
|
|
131 |
isWritable
|
|
132 |
^sourceStream isWritable
|
|
133 |
! !
|
|
134 |
|
|
135 |
!SmaCCLineNumberStream class methodsFor:'documentation'!
|
|
136 |
|
|
137 |
version
|
|
138 |
^ '$Header: /opt/data/cvs/stx/goodies/smaCC/SmaCC__SmaCCLineNumberStream.st,v 1.2 2006-04-05 20:07:51 vranyj1 Exp $'
|
15
|
139 |
!
|
|
140 |
|
|
141 |
version_SVN
|
|
142 |
^ '$Id$'
|
1
|
143 |
! !
|
|
144 |
|
|
145 |
SmaCCLineNumberStream initialize!
|