0
|
1 |
"{ Package: 'stx:goodies/ston' }"
|
|
2 |
|
|
3 |
"{ NameSpace: Smalltalk }"
|
|
4 |
|
|
5 |
Object subclass:#STONCStyleCommentsSkipStream
|
|
6 |
instanceVariableNames:'stream peekedCharacter delimiter escape'
|
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
|
9 |
category:'STON-Core-Reader'
|
|
10 |
!
|
|
11 |
|
|
12 |
!STONCStyleCommentsSkipStream class methodsFor:'instance creation'!
|
|
13 |
|
|
14 |
on: readStream
|
|
15 |
^ self new
|
|
16 |
on: readStream;
|
|
17 |
yourself
|
|
18 |
! !
|
|
19 |
|
|
20 |
!STONCStyleCommentsSkipStream methodsFor:'accessing'!
|
|
21 |
|
|
22 |
collectionSpecies
|
|
23 |
^ String
|
|
24 |
!
|
|
25 |
|
|
26 |
next
|
|
27 |
^ peekedCharacter
|
|
28 |
ifNil: [
|
|
29 |
stream atEnd
|
|
30 |
ifFalse: [ self nextNonCommentChar ] ]
|
|
31 |
ifNotNil: [ | character |
|
|
32 |
character := peekedCharacter.
|
|
33 |
peekedCharacter := nil.
|
|
34 |
character ]
|
|
35 |
!
|
|
36 |
|
|
37 |
next: requestedCount
|
|
38 |
"Read requestedCount elements into new collection and return it,
|
|
39 |
it could be that less elements were available"
|
|
40 |
|
|
41 |
^ self
|
|
42 |
next: requestedCount
|
|
43 |
into: (self collectionSpecies new: requestedCount)
|
|
44 |
!
|
|
45 |
|
|
46 |
next: requestedCount into: collection
|
|
47 |
"Read requestedCount elements into collection,
|
|
48 |
returning a copy if less elements are available"
|
|
49 |
|
|
50 |
^ self
|
|
51 |
next: requestedCount
|
|
52 |
into: collection
|
|
53 |
startingAt: 1
|
|
54 |
!
|
|
55 |
|
|
56 |
next: requestedCount into: collection startingAt: offset
|
|
57 |
"Read requestedCount elements into collection starting at offset,
|
|
58 |
returning a copy if less elements are available"
|
|
59 |
|
|
60 |
| readCount |
|
|
61 |
readCount := self
|
|
62 |
readInto: collection
|
|
63 |
startingAt: offset
|
|
64 |
count: requestedCount.
|
|
65 |
^ requestedCount = readCount
|
|
66 |
ifTrue: [ collection ]
|
|
67 |
ifFalse: [ collection copyFrom: 1 to: offset + readCount - 1 ]
|
|
68 |
!
|
|
69 |
|
|
70 |
nextLine
|
|
71 |
"Read a CR, LF or CRLF terminated line, returning the contents of the line without the EOL. Return nil when the receiver is #atEnd."
|
|
72 |
|
|
73 |
self atEnd ifTrue: [ ^ nil ].
|
|
74 |
^ self collectionSpecies streamContents: [ :out | | eol char |
|
|
75 |
eol := false.
|
|
76 |
[ eol ] whileFalse: [
|
|
77 |
char := self next.
|
|
78 |
(char isNil or: [ char = Character lf ])
|
|
79 |
ifTrue: [ eol := true ]
|
|
80 |
ifFalse: [
|
|
81 |
char = Character cr
|
|
82 |
ifTrue: [ eol := true. self peekFor: Character lf ]
|
|
83 |
ifFalse: [ out nextPut: char ] ] ] ]
|
|
84 |
!
|
|
85 |
|
|
86 |
peek
|
|
87 |
^ peekedCharacter
|
|
88 |
ifNil: [
|
|
89 |
stream atEnd
|
|
90 |
ifFalse: [
|
|
91 |
peekedCharacter := self nextNonCommentChar ] ]
|
|
92 |
!
|
|
93 |
|
|
94 |
peekFor: object
|
|
95 |
^ self peek = object
|
|
96 |
ifTrue: [
|
|
97 |
self next.
|
|
98 |
true ]
|
|
99 |
ifFalse: [ false ]
|
|
100 |
!
|
|
101 |
|
|
102 |
position
|
|
103 |
^ stream position
|
|
104 |
!
|
|
105 |
|
|
106 |
readInto: collection startingAt: offset count: requestedCount
|
|
107 |
"Read count elements and place them in collection starting at offset.
|
|
108 |
Return the number of elements actually read."
|
|
109 |
|
|
110 |
^ peekedCharacter
|
|
111 |
ifNil: [
|
|
112 |
0 to: requestedCount - 1 do: [ :count | | object |
|
|
113 |
(object := self nextNonCommentChar) ifNil: [ ^ count ].
|
|
114 |
collection at: offset + count put: object ].
|
|
115 |
^ requestedCount ]
|
|
116 |
ifNotNil: [
|
|
117 |
collection at: offset put: peekedCharacter.
|
|
118 |
peekedCharacter := nil.
|
|
119 |
(self
|
|
120 |
readInto: collection
|
|
121 |
startingAt: offset + 1
|
|
122 |
count: requestedCount - 1) + 1 ]
|
|
123 |
!
|
|
124 |
|
|
125 |
skip: count
|
|
126 |
count timesRepeat: [ self next ]
|
|
127 |
!
|
|
128 |
|
|
129 |
upTo: anObject
|
|
130 |
^ self collectionSpecies
|
|
131 |
streamContents: [ :out | | element |
|
|
132 |
[ self atEnd or: [ (element := self next) = anObject ] ] whileFalse: [
|
|
133 |
out nextPut: element ] ]
|
|
134 |
!
|
|
135 |
|
|
136 |
upToEnd
|
|
137 |
^ self collectionSpecies
|
|
138 |
streamContents: [ :collectionStream |
|
|
139 |
[ self atEnd ] whileFalse: [ collectionStream nextPut: self next ] ]
|
|
140 |
!
|
|
141 |
|
|
142 |
wrappedStream
|
|
143 |
^ stream
|
|
144 |
! !
|
|
145 |
|
|
146 |
!STONCStyleCommentsSkipStream methodsFor:'initialize-release'!
|
|
147 |
|
|
148 |
close
|
|
149 |
stream close
|
|
150 |
!
|
|
151 |
|
|
152 |
on: readStream
|
|
153 |
stream := readStream
|
|
154 |
! !
|
|
155 |
|
|
156 |
!STONCStyleCommentsSkipStream methodsFor:'private'!
|
|
157 |
|
|
158 |
consumeComment
|
|
159 |
stream peek = $/ ifTrue: [ self consumeToEOL ].
|
|
160 |
stream peek = $* ifTrue: [ self consumeToCommentEnd ]
|
|
161 |
!
|
|
162 |
|
|
163 |
consumeToCommentEnd
|
|
164 |
[ stream atEnd or: [ stream next = $* and: [ stream peekFor: $/ ] ] ] whileFalse
|
|
165 |
!
|
|
166 |
|
|
167 |
consumeToEOL
|
|
168 |
| eol char |
|
|
169 |
eol := false.
|
|
170 |
[ eol ] whileFalse: [
|
|
171 |
char := stream next.
|
|
172 |
(char isNil or: [ char = Character lf ])
|
|
173 |
ifTrue: [ eol := true ]
|
|
174 |
ifFalse: [
|
|
175 |
char = Character cr
|
|
176 |
ifTrue: [
|
|
177 |
eol := true.
|
|
178 |
stream peekFor: Character lf ] ] ]
|
|
179 |
!
|
|
180 |
|
|
181 |
escape
|
|
182 |
"Return true when we previously read a backslash escape inside a string,
|
|
183 |
so that the next string delimiter should be returned as is"
|
|
184 |
|
|
185 |
^ escape = true
|
|
186 |
!
|
|
187 |
|
|
188 |
handleStringDelimiter: char
|
|
189 |
self escape
|
|
190 |
ifTrue: [ escape := false ]
|
|
191 |
ifFalse: [
|
|
192 |
self insideString
|
|
193 |
ifTrue: [
|
|
194 |
char = delimiter
|
|
195 |
ifTrue: [ delimiter := nil ] ]
|
|
196 |
ifFalse: [ delimiter := char ] ].
|
|
197 |
^ char
|
|
198 |
!
|
|
199 |
|
|
200 |
insideString
|
|
201 |
"Return true when we are currently inside a string where comments should be ignored."
|
|
202 |
|
|
203 |
^ (delimiter = $') | (delimiter = $")
|
|
204 |
!
|
|
205 |
|
|
206 |
nextNonCommentChar
|
|
207 |
| char |
|
|
208 |
char := stream next.
|
|
209 |
(self insideString and: [ char = $\ ])
|
|
210 |
ifTrue: [
|
|
211 |
escape := true.
|
|
212 |
^ char ].
|
|
213 |
(char = $') | (char = $")
|
|
214 |
ifTrue: [
|
|
215 |
^ self handleStringDelimiter: char ].
|
|
216 |
escape := false.
|
|
217 |
^ (char = $/ and: [ self insideString not and: [ (stream peek = $/) | (stream peek = $*) ] ])
|
|
218 |
ifTrue: [
|
|
219 |
self consumeComment.
|
|
220 |
stream next ]
|
|
221 |
ifFalse: [ char ]
|
|
222 |
! !
|
|
223 |
|
|
224 |
!STONCStyleCommentsSkipStream methodsFor:'testing'!
|
|
225 |
|
|
226 |
atEnd
|
|
227 |
^ self peek isNil
|
|
228 |
!
|
|
229 |
|
|
230 |
isBinary
|
|
231 |
^ false
|
|
232 |
! !
|
|
233 |
|