author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Tue, 18 Sep 2012 13:00:02 +0000 | |
changeset 18 | fb5dc5aae98c |
parent 15 | 31a33727c629 |
child 20 | cdf3ee8ceeaa |
permissions | -rw-r--r-- |
1 | 1 |
"{ Package: 'stx:goodies/cypress' }" |
2 |
||
15
31a33727c629
- Getting rid of old Cypress implementation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
12
diff
changeset
|
3 |
Object subclass:#CypressJSONReader |
1 | 4 |
instanceVariableNames:'stream' |
5 |
classVariableNames:'' |
|
6 |
poolDictionaries:'' |
|
15
31a33727c629
- Getting rid of old Cypress implementation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
12
diff
changeset
|
7 |
category:'Cypress-New-Reader & Writer' |
1 | 8 |
! |
9 |
||
15
31a33727c629
- Getting rid of old Cypress implementation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
12
diff
changeset
|
10 |
CypressJSONReader comment:'Main comment stating the purpose of this class and relevant relationship to other classes. Possible useful expressions for doIt or printIt. Structure: instVar1 type -- comment about the purpose of instVar1 instVar2 type -- comment about the purpose of instVar2 Any further useful comments about the general approach of this implementation.' |
1 | 11 |
! |
12 |
||
13 |
||
15
31a33727c629
- Getting rid of old Cypress implementation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
12
diff
changeset
|
14 |
!CypressJSONReader class methodsFor:'instance creation'! |
1 | 15 |
|
16 |
new |
|
17 |
self error: 'Instantiate the parser with a stream.' |
|
18 |
! |
|
19 |
||
20 |
on: aStream |
|
21 |
^ self basicNew initializeOn: aStream |
|
22 |
! ! |
|
23 |
||
15
31a33727c629
- Getting rid of old Cypress implementation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
12
diff
changeset
|
24 |
!CypressJSONReader class methodsFor:'accessing'! |
1 | 25 |
|
12
ec118792047a
- Package writing rewritten to use new model. Writes package according to recent spec.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
1
diff
changeset
|
26 |
parse: aStringOrFilename |
ec118792047a
- Package writing rewritten to use new model. Writes package according to recent spec.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
1
diff
changeset
|
27 |
^ self parseStream: aStringOrFilename readStream |
ec118792047a
- Package writing rewritten to use new model. Writes package according to recent spec.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
1
diff
changeset
|
28 |
|
ec118792047a
- Package writing rewritten to use new model. Writes package according to recent spec.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
1
diff
changeset
|
29 |
"Modified (format): / 11-09-2012 / 11:38:41 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
1 | 30 |
! |
31 |
||
32 |
parseStream: aStream |
|
33 |
^ (self on: aStream) parse |
|
34 |
! ! |
|
35 |
||
15
31a33727c629
- Getting rid of old Cypress implementation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
12
diff
changeset
|
36 |
!CypressJSONReader methodsFor:'adding'! |
1 | 37 |
|
38 |
addProperty: anAssociation to: anObject |
|
39 |
"Add the property anAssociation described with key and value to anObject. Subclasses might want to refine this implementation." |
|
40 |
||
41 |
^ anObject |
|
42 |
add: anAssociation; |
|
43 |
yourself |
|
44 |
! |
|
45 |
||
46 |
addValue: anObject to: aCollection |
|
47 |
"Add anObject to aCollection. Subclasses might want to refine this implementation." |
|
48 |
||
49 |
^ aCollection copyWith: anObject |
|
50 |
! ! |
|
51 |
||
15
31a33727c629
- Getting rid of old Cypress implementation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
12
diff
changeset
|
52 |
!CypressJSONReader methodsFor:'creating'! |
1 | 53 |
|
54 |
createArray |
|
55 |
"Create an empty collection. Subclasses might want to refine this implementation." |
|
56 |
||
57 |
^ Array new |
|
58 |
! |
|
59 |
||
60 |
createFalse |
|
61 |
"Create the false literal. Subclasses might want to refine this implementation." |
|
62 |
||
63 |
^ false |
|
64 |
! |
|
65 |
||
66 |
createNull |
|
67 |
"Create the null literal. Subclasses might want to refine this implementation." |
|
68 |
||
69 |
^ nil |
|
70 |
! |
|
71 |
||
72 |
createNumber: aString |
|
73 |
"Create a number literal. Subclasses might want to refine this implementation." |
|
74 |
||
75 |
^ aString asNumber |
|
76 |
! |
|
77 |
||
78 |
createObject |
|
79 |
"Create an empty object. Subclasses might want to refine this implementation." |
|
80 |
||
81 |
^ Dictionary new |
|
82 |
! |
|
83 |
||
84 |
createProperty: aKey with: aValue |
|
85 |
"Create an empty attribute value pair. Subclasses might want to refine this implementation." |
|
86 |
||
87 |
^ aKey -> aValue |
|
88 |
! |
|
89 |
||
90 |
createString: aString |
|
91 |
"Create a string literal. Subclasses might want to refine this implementation." |
|
92 |
||
93 |
^ aString |
|
94 |
! |
|
95 |
||
96 |
createTrue |
|
97 |
"Create the true literal. Subclasses might want to refine this implementation." |
|
98 |
||
99 |
^ true |
|
100 |
! ! |
|
101 |
||
15
31a33727c629
- Getting rid of old Cypress implementation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
12
diff
changeset
|
102 |
!CypressJSONReader methodsFor:'initialization'! |
1 | 103 |
|
104 |
initializeOn: aStream |
|
105 |
self initialize. |
|
106 |
stream := aStream |
|
107 |
! ! |
|
108 |
||
15
31a33727c629
- Getting rid of old Cypress implementation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
12
diff
changeset
|
109 |
!CypressJSONReader methodsFor:'parsing'! |
1 | 110 |
|
111 |
parse |
|
112 |
| result | |
|
113 |
result := self whitespace; parseValue. |
|
114 |
stream atEnd |
|
115 |
ifFalse: [ self error: 'end of input expected' ]. |
|
116 |
^ result |
|
117 |
! |
|
118 |
||
119 |
parseArray |
|
120 |
| result | |
|
121 |
self expect: '['. |
|
122 |
result := self createArray. |
|
123 |
(self match: ']') |
|
124 |
ifTrue: [ ^ result ]. |
|
125 |
[ stream atEnd ] whileFalse: [ |
|
126 |
result := self |
|
127 |
addValue: self parseValue |
|
128 |
to: result. |
|
129 |
(self match: ']') |
|
130 |
ifTrue: [ ^ result ]. |
|
131 |
self expect: ',' ]. |
|
132 |
self error: 'end of array expected' |
|
133 |
! |
|
134 |
||
135 |
parseObject |
|
136 |
| result | |
|
137 |
self expect: '{'. |
|
138 |
result := self createObject. |
|
139 |
(self match: '}') |
|
140 |
ifTrue: [ ^ result ]. |
|
141 |
[ stream atEnd ] whileFalse: [ |
|
142 |
result := self |
|
143 |
addProperty: self parseProperty |
|
144 |
to: result. |
|
145 |
(self match: '}') |
|
146 |
ifTrue: [ ^ result ]. |
|
147 |
self expect: ',' ]. |
|
148 |
self error: 'end of object expected' |
|
149 |
! |
|
150 |
||
151 |
parseValue |
|
152 |
| char | |
|
153 |
stream atEnd ifFalse: [ |
|
154 |
char := stream peek. |
|
155 |
char = ${ |
|
156 |
ifTrue: [ ^ self parseObject ]. |
|
157 |
char = $[ |
|
158 |
ifTrue: [ ^ self parseArray ]. |
|
159 |
char = $" |
|
160 |
ifTrue: [ ^ self parseString ]. |
|
161 |
(char = $- or: [ char between: $0 and: $9 ]) |
|
162 |
ifTrue: [ ^ self parseNumber ]. |
|
163 |
(self match: 'true') |
|
164 |
ifTrue: [ ^ self createTrue ]. |
|
165 |
(self match: 'false') |
|
166 |
ifTrue: [ ^ self createFalse ]. |
|
167 |
(self match: 'null') |
|
168 |
ifTrue: [ ^ self createNull ] ]. |
|
169 |
self error: 'invalid input' |
|
170 |
! ! |
|
171 |
||
15
31a33727c629
- Getting rid of old Cypress implementation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
12
diff
changeset
|
172 |
!CypressJSONReader methodsFor:'parsing-internal'! |
1 | 173 |
|
174 |
parseCharacter |
|
175 |
| char | |
|
176 |
(char := stream next) = $\ |
|
177 |
ifFalse: [ ^ char ]. |
|
178 |
(char := stream next) = $" |
|
179 |
ifTrue: [ ^ char ]. |
|
180 |
char = $\ |
|
181 |
ifTrue: [ ^ char ]. |
|
182 |
char = $/ |
|
183 |
ifTrue: [ ^ char ]. |
|
184 |
char = $b |
|
185 |
ifTrue: [ ^ Character backspace ]. |
|
186 |
char = $f |
|
187 |
ifTrue: [ ^ Character newPage ]. |
|
188 |
char = $n |
|
189 |
ifTrue: [ ^ Character lfCharacter ]. |
|
190 |
char = $r |
|
191 |
ifTrue: [ ^ Character crCharacter ]. |
|
192 |
char = $t |
|
193 |
ifTrue: [ ^ Character tab ]. |
|
194 |
char = $u |
|
195 |
ifTrue: [ ^ self parseCharacterHex ]. |
|
196 |
self error: 'invalid escape character \' , (String with: char) |
|
197 |
! |
|
198 |
||
199 |
parseCharacterHex |
|
200 |
| value | |
|
201 |
value := self parseCharacterHexDigit. |
|
202 |
3 timesRepeat: [ value := (value << 4) + self parseCharacterHexDigit ]. |
|
203 |
^ Character unicodeCodePoint: value |
|
204 |
! |
|
205 |
||
206 |
parseCharacterHexDigit |
|
207 |
| digit | |
|
208 |
stream atEnd ifFalse: [ |
|
209 |
digit _ stream next asUppercase digitValue. |
|
210 |
"accept hex digits" |
|
211 |
(digit >= 0 and: [ digit < 16 ]) ifTrue: [ ^ digit ]]. |
|
212 |
self error: 'hex-digit expected'. |
|
213 |
! |
|
214 |
||
215 |
parseNumber |
|
216 |
| negated number | |
|
217 |
negated := stream peek = $-. |
|
218 |
negated ifTrue: [ stream next ]. |
|
219 |
number := self parseNumberInteger. |
|
220 |
(stream peek = $.) ifTrue: [ |
|
221 |
stream next. |
|
222 |
number := number + self parseNumberFraction ]. |
|
223 |
(stream peek = $e or: [ stream peek = $E ]) ifTrue: [ |
|
224 |
stream next. |
|
225 |
number := number * self parseNumberExponent ]. |
|
226 |
negated ifTrue: [ number := number negated ]. |
|
227 |
^ self whitespace; createNumber: number |
|
228 |
! |
|
229 |
||
230 |
parseNumberExponent |
|
231 |
| number negated | |
|
232 |
number := 0. |
|
233 |
negated := stream peek = $-. |
|
234 |
(negated or: [ stream peek = $+ ]) |
|
235 |
ifTrue: [ stream next ]. |
|
236 |
[ stream atEnd not and: [ stream peek isDigit ] ] whileTrue: [ number := 10 * number + (stream next digitValue) ]. |
|
237 |
negated |
|
238 |
ifTrue: [ number := number negated ]. |
|
239 |
^ 10 raisedTo: number |
|
240 |
! |
|
241 |
||
242 |
parseNumberFraction |
|
243 |
| number power | |
|
244 |
number := 0. |
|
245 |
power := 1.0. |
|
246 |
[ stream atEnd not and: [ stream peek isDigit ] ] |
|
247 |
whileTrue: [ |
|
248 |
number := 10 * number + (stream next digitValue). |
|
249 |
power := power * 10.0 ]. |
|
250 |
^ number / power |
|
251 |
! |
|
252 |
||
253 |
parseNumberInteger |
|
254 |
| number | |
|
255 |
number := 0. |
|
256 |
[ stream atEnd not and: [ stream peek isDigit ] ] whileTrue: [ number := 10 * number + (stream next asciiValue - 48) ]. |
|
257 |
^ number |
|
258 |
! |
|
259 |
||
260 |
parseProperty |
|
261 |
| name value | |
|
262 |
name := self parseString. |
|
263 |
self expect: ':'. |
|
264 |
value := self parseValue. |
|
265 |
^ self createProperty: name with: value. |
|
266 |
! |
|
267 |
||
268 |
parseString |
|
269 |
| result | |
|
270 |
self expect: '"'. |
|
271 |
result := WriteStream on: String new. |
|
272 |
[ stream atEnd or: [ stream peek = $" ] ] |
|
273 |
whileFalse: [ result nextPut: self parseCharacter ]. |
|
274 |
^ self expect: '"'; createString: result contents |
|
275 |
! ! |
|
276 |
||
15
31a33727c629
- Getting rid of old Cypress implementation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
12
diff
changeset
|
277 |
!CypressJSONReader methodsFor:'private'! |
1 | 278 |
|
279 |
expect: aString |
|
280 |
"Expects aString and consume input, throw an error otherwise." |
|
281 |
||
282 |
^ (self match: aString) ifFalse: [ self error: aString , ' expected' ] |
|
283 |
! |
|
284 |
||
285 |
match: aString |
|
286 |
"Tries to match aString, consume input and answer true if successful." |
|
287 |
||
288 |
| position | |
|
289 |
position := stream position. |
|
290 |
aString do: [ :each | |
|
291 |
(stream atEnd or: [ stream next ~= each ]) ifTrue: [ |
|
292 |
stream position: position. |
|
293 |
^ false ] ]. |
|
294 |
self whitespace. |
|
295 |
^ true |
|
296 |
! |
|
297 |
||
298 |
whitespace |
|
299 |
"Strip whitespaces from the input stream." |
|
300 |
||
301 |
[ stream atEnd not and: [ stream peek isSeparator ] ] |
|
302 |
whileTrue: [ stream next ] |
|
303 |
! ! |
|
304 |
||
15
31a33727c629
- Getting rid of old Cypress implementation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
12
diff
changeset
|
305 |
!CypressJSONReader class methodsFor:'documentation'! |
1 | 306 |
|
307 |
version_SVN |
|
308 |
^ '$Id:: $' |
|
309 |
! ! |