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