259
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
208
|
3 |
"{ Package: 'stx:goodies/petitparser/analyzer/tests' }"
|
|
4 |
|
|
5 |
PPAbstractParserTest subclass:#PPAnalyzerTest
|
|
6 |
instanceVariableNames:''
|
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
|
9 |
category:'PetitAnalyzer-Tests'
|
|
10 |
!
|
|
11 |
|
|
12 |
|
|
13 |
!PPAnalyzerTest class methodsFor:'accessing'!
|
|
14 |
|
|
15 |
packageNamesUnderTest
|
|
16 |
^ #('PetitAnalyzer')
|
|
17 |
! !
|
|
18 |
|
|
19 |
!PPAnalyzerTest methodsFor:'accessing'!
|
|
20 |
|
|
21 |
grammarA
|
259
|
22 |
"Güting, Erwig, Übersetzerbau, Springer (p.63)"
|
208
|
23 |
|
|
24 |
| grammar |
|
|
25 |
grammar := Dictionary new.
|
|
26 |
|
|
27 |
" terminals "
|
|
28 |
grammar at: #a put: $a asParser.
|
|
29 |
grammar at: #b put: $b asParser.
|
|
30 |
grammar at: #c put: $c asParser.
|
|
31 |
grammar at: #d put: $d asParser.
|
|
32 |
grammar at: #e put: nil asParser.
|
|
33 |
|
|
34 |
" non terminals "
|
|
35 |
grammar at: #B put: (grammar at: #b) / (grammar at: #e).
|
|
36 |
grammar at: #A put: (grammar at: #a) / (grammar at: #B).
|
|
37 |
grammar at: #S put: (grammar at: #A) , (grammar at: #B) , (grammar at: #c) , (grammar at: #d).
|
|
38 |
|
|
39 |
^ grammar
|
|
40 |
!
|
|
41 |
|
|
42 |
grammarB
|
|
43 |
"The canonical grammar to exercise first- and follow-set calculation, probably originally from the dragon-book."
|
|
44 |
|
|
45 |
| grammar |
|
|
46 |
grammar := Dictionary new.
|
|
47 |
#(E Ep T Tp F) do: [ :each | grammar at: each put: (PPUnresolvedParser named: each) ].
|
|
48 |
|
|
49 |
(grammar at: #E) def: (grammar at: #T) , (grammar at: #Ep).
|
|
50 |
(grammar at: #Ep) def: ($+ asParser , (grammar at: #T) , (grammar at: #Ep)) optional.
|
|
51 |
|
|
52 |
(grammar at: #T) def: (grammar at: #F) , (grammar at: #Tp).
|
|
53 |
(grammar at: #Tp) def: ($* asParser , (grammar at: #F) , (grammar at: #Tp)) optional.
|
|
54 |
|
|
55 |
(grammar at: #F) def: ($( asParser , (grammar at: #E) , $) asParser) / $i asParser.
|
|
56 |
|
|
57 |
#(E Ep T Tp F) do: [ :each | (grammar at: each) name: each ].
|
|
58 |
|
|
59 |
^ grammar
|
|
60 |
!
|
|
61 |
|
|
62 |
grammarC
|
|
63 |
"A highly recrusive grammar."
|
|
64 |
|
|
65 |
| grammar |
|
|
66 |
grammar := PPUnresolvedParser new.
|
|
67 |
grammar def: (grammar , $+ asParser , grammar) / $1 asParser.
|
|
68 |
^ grammar
|
|
69 |
!
|
|
70 |
|
|
71 |
grammarD
|
259
|
72 |
"A highly ambiguous grammar from: Saichaitanya Jampana. Exploring the problem of ambiguity in context-free grammars. Master’s thesis, Oklahoma State University, July 2005."
|
|
73 |
|
|
74 |
| grammar |
|
|
75 |
grammar := Dictionary new.
|
|
76 |
#(S A a B b) do: [ :each | grammar at: each put: (PPUnresolvedParser named: each) ].
|
|
77 |
|
|
78 |
(grammar at: #a) def: $a asParser.
|
|
79 |
(grammar at: #b) def: $b asParser.
|
|
80 |
|
|
81 |
(grammar at: #S) def: (grammar at: #A) , (grammar at: #B) / (grammar at: #a).
|
|
82 |
(grammar at: #A) def: (grammar at: #S) , (grammar at: #B) / (grammar at: #b).
|
|
83 |
(grammar at: #B) def: (grammar at: #B) , (grammar at: #A) / (grammar at: #a).
|
|
84 |
|
|
85 |
^ grammar
|
208
|
86 |
!
|
|
87 |
|
|
88 |
grammarE
|
|
89 |
"The most stupid parser, it just references itself and never consumes anything. All algorithms should survive such an attack."
|
|
90 |
|
|
91 |
| parser |
|
|
92 |
parser := PPDelegateParser new.
|
|
93 |
parser setParser: parser.
|
|
94 |
^ parser
|
|
95 |
! !
|
|
96 |
|
|
97 |
!PPAnalyzerTest methodsFor:'testing'!
|
|
98 |
|
|
99 |
testAllNamedParsers
|
|
100 |
| p1 p2 p3 |
|
|
101 |
p1 := (#digit asParser name: 'a').
|
|
102 |
p2 := (#digit asParser name: 'b') star.
|
|
103 |
p3 := (#digit asParser name: 'c') token end.
|
|
104 |
self assert: p1 allNamedParsers size = 1.
|
|
105 |
self assert: p1 allNamedParsers first name = 'a'.
|
|
106 |
self assert: p2 allNamedParsers size = 1.
|
|
107 |
self assert: p2 allNamedParsers first name = 'b'.
|
|
108 |
self assert: p3 allNamedParsers size = 1.
|
|
109 |
self assert: p3 allNamedParsers first name = 'c'
|
|
110 |
!
|
|
111 |
|
|
112 |
testAllParsers
|
|
113 |
| p1 p2 p3 |
|
|
114 |
p1 := #lowercase asParser.
|
|
115 |
p2 := p1 ==> #asUppercase.
|
|
116 |
p3 := PPUnresolvedParser new.
|
|
117 |
p3 def: p2 / p3.
|
|
118 |
self assert: p1 allParsers size = 1.
|
|
119 |
self assert: p2 allParsers size = 2.
|
|
120 |
self assert: p3 allParsers size = 3
|
|
121 |
!
|
|
122 |
|
|
123 |
testInnerChildren
|
|
124 |
| p1 p2 p3 |
|
|
125 |
p1 := (#digit asParser name: 'a').
|
|
126 |
p2 := (#digit asParser star name: 'b').
|
|
127 |
p3 := (#digit asParser name: 'c') token star end.
|
|
128 |
self assert: p1 innerChildren isEmpty.
|
|
129 |
self assert: p2 innerChildren size = 1.
|
|
130 |
self assert: (p2 innerChildren allSatisfy: [ :each | each name isNil ]).
|
|
131 |
self assert: p3 innerChildren size = 2.
|
|
132 |
self assert: (p3 innerChildren allSatisfy: [ :each | each name isNil ])
|
|
133 |
!
|
|
134 |
|
|
135 |
testIsNullable
|
|
136 |
self assert: $a asParser star isNullable.
|
|
137 |
self assert: nil asParser isNullable.
|
|
138 |
|
|
139 |
self deny: $a asParser plus isNullable.
|
|
140 |
self deny: PPLiteralSequenceParser new isNullable.
|
|
141 |
self deny: PPLiteralObjectParser new isNullable.
|
|
142 |
self deny: PPPredicateParser new isNullable.
|
|
143 |
self deny: PPChoiceParser new isNullable.
|
|
144 |
self deny: PPSequenceParser new isNullable.
|
|
145 |
self deny: PPAndParser new isNullable.
|
|
146 |
self deny: PPTokenParser new isNullable
|
|
147 |
!
|
|
148 |
|
|
149 |
testIsTerminal
|
|
150 |
self assert: PPEpsilonParser new isTerminal.
|
|
151 |
self assert: PPFailingParser new isTerminal.
|
|
152 |
self assert: PPPluggableParser new isTerminal.
|
|
153 |
self assert: PPLiteralObjectParser new isTerminal.
|
|
154 |
self assert: PPLiteralSequenceParser new isTerminal.
|
|
155 |
self assert: PPPredicateObjectParser new isTerminal.
|
|
156 |
self assert: PPPredicateSequenceParser new isTerminal.
|
|
157 |
|
|
158 |
self deny: ($a asParser / $b asParser) isTerminal.
|
|
159 |
self deny: ($a asParser , $b asParser) isTerminal.
|
|
160 |
self deny: ($a asParser and) isTerminal.
|
|
161 |
self deny: ($a asParser not) isTerminal
|
|
162 |
!
|
|
163 |
|
|
164 |
testNamedChildren
|
|
165 |
| p1 p2 p3 p4 |
|
|
166 |
p1 := (#digit asParser name: 'a').
|
|
167 |
p2 := (#digit asParser name: 'b') star.
|
|
168 |
p3 := (#digit asParser name: 'c') token end.
|
|
169 |
p4 := ((#digit asParser name: 'c') token name: 'd') end.
|
|
170 |
self assert: p1 namedChildren isEmpty.
|
|
171 |
self assert: p2 namedChildren size = 1.
|
|
172 |
self assert: p2 namedChildren first name = 'b'.
|
|
173 |
self assert: p3 namedChildren size = 1.
|
|
174 |
self assert: p3 namedChildren first name = 'c'.
|
|
175 |
self assert: p4 namedChildren size = 1.
|
|
176 |
self assert: p4 namedChildren first name = 'd'
|
|
177 |
! !
|
|
178 |
|
|
179 |
!PPAnalyzerTest methodsFor:'testing-cycleset'!
|
|
180 |
|
|
181 |
testCycleSetGrammarA
|
|
182 |
self grammarA do: [ :each | self assert: each cycleSet isEmpty ]
|
|
183 |
!
|
|
184 |
|
|
185 |
testCycleSetGrammarB
|
|
186 |
self grammarB do: [ :each | self assert: each cycleSet isEmpty ]
|
|
187 |
!
|
|
188 |
|
|
189 |
testCycleSetGrammarC
|
|
190 |
| grammar cycleSet |
|
|
191 |
grammar := self grammarC.
|
|
192 |
cycleSet := grammar cycleSet.
|
|
193 |
self assert: (cycleSet size = 2).
|
|
194 |
self assert: (cycleSet includes: grammar)
|
|
195 |
!
|
|
196 |
|
|
197 |
testCycleSetGrammarD
|
|
198 |
| grammar cycleSet |
|
|
199 |
grammar := self grammarD.
|
|
200 |
|
|
201 |
cycleSet := (grammar at: #S) cycleSet.
|
|
202 |
self assert: (cycleSet size = 4).
|
|
203 |
self assert: (cycleSet includes: (grammar at: #A)).
|
|
204 |
self assert: (cycleSet includes: (grammar at: #S)).
|
|
205 |
|
|
206 |
cycleSet := (grammar at: #A) cycleSet.
|
|
207 |
self assert: (cycleSet size = 4).
|
|
208 |
self assert: (cycleSet includes: (grammar at: #A)).
|
|
209 |
self assert: (cycleSet includes: (grammar at: #S)).
|
|
210 |
|
|
211 |
cycleSet := (grammar at: #B) cycleSet.
|
|
212 |
self assert: (cycleSet size = 2).
|
|
213 |
self assert: (cycleSet includes: (grammar at: #B))
|
|
214 |
!
|
|
215 |
|
|
216 |
testCycleSetGrammarE
|
|
217 |
| grammar cycleSet |
|
|
218 |
grammar := self grammarE.
|
|
219 |
cycleSet := grammar cycleSet.
|
|
220 |
self assert: (cycleSet size = 1).
|
|
221 |
self assert: (cycleSet includes: grammar)
|
|
222 |
!
|
|
223 |
|
|
224 |
testCycleSetInChoice
|
|
225 |
| parser cycleSet |
|
|
226 |
parser := PPUnresolvedParser new.
|
|
227 |
parser def: parser / $a asParser.
|
|
228 |
cycleSet := parser cycleSet.
|
|
229 |
self assert: (cycleSet size = 1).
|
|
230 |
self assert: (cycleSet includes: parser).
|
|
231 |
|
|
232 |
parser := PPUnresolvedParser new.
|
|
233 |
parser def: $a asParser / parser.
|
|
234 |
cycleSet := parser cycleSet.
|
|
235 |
self assert: (cycleSet size = 1).
|
|
236 |
self assert: (cycleSet includes: parser).
|
|
237 |
!
|
|
238 |
|
|
239 |
testCycleSetInSequence
|
|
240 |
| parser cycleSet |
|
|
241 |
parser := PPUnresolvedParser new.
|
|
242 |
parser def: parser , $a asParser.
|
|
243 |
cycleSet := parser cycleSet.
|
|
244 |
self assert: (cycleSet size = 1).
|
|
245 |
self assert: (cycleSet includes: parser).
|
|
246 |
|
|
247 |
parser := PPUnresolvedParser new.
|
|
248 |
parser def: nil asParser , parser.
|
|
249 |
cycleSet := parser cycleSet.
|
|
250 |
self assert: (cycleSet size = 1).
|
|
251 |
self assert: (cycleSet includes: parser).
|
|
252 |
|
|
253 |
parser := PPUnresolvedParser new.
|
|
254 |
parser def: $a asParser , parser.
|
|
255 |
cycleSet := parser cycleSet.
|
|
256 |
self assert: cycleSet isEmpty
|
|
257 |
! !
|
|
258 |
|
|
259 |
!PPAnalyzerTest methodsFor:'testing-firstset'!
|
|
260 |
|
|
261 |
testFirstSetExpression
|
|
262 |
| grammar |
|
|
263 |
grammar := PPArithmeticParser new.
|
|
264 |
self assert: grammar start firstSet includes: '(-0123456789' epsilon: false.
|
|
265 |
self assert: grammar addition firstSet includes: '(-0123456789' epsilon: false.
|
|
266 |
self assert: grammar factors firstSet includes: '(-0123456789' epsilon: false.
|
|
267 |
self assert: grammar multiplication firstSet includes: '(-0123456789' epsilon: false.
|
|
268 |
self assert: grammar number firstSet includes: '-0123456789' epsilon: false.
|
|
269 |
self assert: grammar parentheses firstSet includes: '(' epsilon: false.
|
|
270 |
self assert: grammar power firstSet includes: '(-0123456789' epsilon: false.
|
|
271 |
self assert: grammar primary firstSet includes: '(-0123456789' epsilon: false.
|
|
272 |
self assert: grammar terms firstSet includes: '(-0123456789' epsilon: false
|
|
273 |
!
|
|
274 |
|
|
275 |
testFirstSetGrammarA
|
|
276 |
| grammar |
|
|
277 |
grammar := self grammarA.
|
|
278 |
self assert: (grammar at: #a) firstSet includes: 'a' epsilon: false.
|
|
279 |
self assert: (grammar at: #b) firstSet includes: 'b' epsilon: false.
|
|
280 |
self assert: (grammar at: #c) firstSet includes: 'c' epsilon: false.
|
|
281 |
self assert: (grammar at: #d) firstSet includes: 'd' epsilon: false.
|
|
282 |
self assert: (grammar at: #e) firstSet includes: '' epsilon: true.
|
|
283 |
self assert: (grammar at: #S) firstSet includes: 'abc' epsilon: false.
|
|
284 |
self assert: (grammar at: #A) firstSet includes: 'ab' epsilon: true.
|
|
285 |
self assert: (grammar at: #B) firstSet includes: 'b' epsilon: true
|
|
286 |
!
|
|
287 |
|
|
288 |
testFirstSetGrammarB
|
|
289 |
| grammar |
|
|
290 |
grammar := self grammarB.
|
|
291 |
self assert: (grammar at: #E) firstSet includes: '(i' epsilon: false.
|
|
292 |
self assert: (grammar at: #Ep) firstSet includes: '+' epsilon: true.
|
|
293 |
self assert: (grammar at: #T) firstSet includes: '(i' epsilon: false.
|
|
294 |
self assert: (grammar at: #Tp) firstSet includes: '*' epsilon: true.
|
|
295 |
self assert: (grammar at: #F) firstSet includes: '(i' epsilon: false
|
|
296 |
!
|
|
297 |
|
|
298 |
testFirstSetGrammarC
|
|
299 |
| grammar |
|
|
300 |
grammar := self grammarC.
|
|
301 |
self assert: grammar firstSet includes: '1' epsilon: false
|
|
302 |
!
|
|
303 |
|
|
304 |
testFirstSetGrammarD
|
|
305 |
| grammar |
|
|
306 |
grammar := self grammarD.
|
|
307 |
self assert: (grammar at: #S) firstSet includes: 'ab' epsilon: false.
|
|
308 |
self assert: (grammar at: #A) firstSet includes: 'ab' epsilon: false.
|
|
309 |
self assert: (grammar at: #B) firstSet includes: 'a' epsilon: false.
|
|
310 |
self assert: (grammar at: #a) firstSet includes: 'a' epsilon: false.
|
|
311 |
self assert: (grammar at: #b) firstSet includes: 'b' epsilon: false
|
|
312 |
!
|
|
313 |
|
|
314 |
testFirstSetGrammarE
|
|
315 |
self assert: self grammarE firstSet includes: '' epsilon: false
|
|
316 |
!
|
|
317 |
|
|
318 |
testFirstSetLambda
|
|
319 |
| grammar |
|
|
320 |
grammar := PPLambdaParser new.
|
|
321 |
self assert: grammar start firstSet includes: '(ABCDEFGHIJKLMNOPQRSTUVWXYZ\abcdefghijklmnopqrstuvwxyz' epsilon: false.
|
|
322 |
self assert: grammar abstraction firstSet includes: '\' epsilon: false.
|
|
323 |
self assert: grammar application firstSet includes: '(' epsilon: false.
|
|
324 |
self assert: grammar expression firstSet includes: '(ABCDEFGHIJKLMNOPQRSTUVWXYZ\abcdefghijklmnopqrstuvwxyz' epsilon: false.
|
|
325 |
self assert: grammar variable firstSet includes: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' epsilon: false
|
|
326 |
! !
|
|
327 |
|
|
328 |
!PPAnalyzerTest methodsFor:'testing-followset'!
|
|
329 |
|
|
330 |
testFollowSetExampleA
|
|
331 |
| grammar followSets |
|
|
332 |
grammar := self grammarA.
|
|
333 |
followSets := (grammar at: #S) followSets.
|
|
334 |
self assert: (followSets at: (grammar at: #a)) includes: 'bc' epsilon: false.
|
|
335 |
self assert: (followSets at: (grammar at: #b)) includes: 'bc' epsilon: false.
|
|
336 |
self assert: (followSets at: (grammar at: #c)) includes: 'd' epsilon: false.
|
|
337 |
self assert: (followSets at: (grammar at: #d)) includes: '' epsilon: true.
|
|
338 |
self assert: (followSets at: (grammar at: #e)) includes: 'bc' epsilon: false.
|
|
339 |
self assert: (followSets at: (grammar at: #S)) includes: '' epsilon: true.
|
|
340 |
self assert: (followSets at: (grammar at: #A)) includes: 'bc' epsilon: false.
|
|
341 |
self assert: (followSets at: (grammar at: #B)) includes: 'bc' epsilon: false
|
|
342 |
!
|
|
343 |
|
|
344 |
testFollowSetExampleB
|
|
345 |
| grammar followSets |
|
|
346 |
grammar := self grammarB.
|
|
347 |
followSets := (grammar at: #E) followSets.
|
|
348 |
self assert: (followSets at: (grammar at: #E)) includes: ')' epsilon: true.
|
|
349 |
self assert: (followSets at: (grammar at: #Ep)) includes: ')' epsilon: true.
|
|
350 |
self assert: (followSets at: (grammar at: #T)) includes: ')+' epsilon: true.
|
|
351 |
self assert: (followSets at: (grammar at: #Tp)) includes: ')+' epsilon: true.
|
|
352 |
self assert: (followSets at: (grammar at: #F)) includes: ')*+' epsilon: true
|
|
353 |
!
|
|
354 |
|
|
355 |
testFollowSetExampleC
|
|
356 |
self assert: self grammarC followSet includes: '+' epsilon: true
|
|
357 |
!
|
|
358 |
|
|
359 |
testFollowSetExampleD
|
|
360 |
| grammar followSets |
|
|
361 |
grammar := self grammarD.
|
|
362 |
followSets := (grammar at: #S) followSets.
|
|
363 |
self assert: (followSets at: (grammar at: #S)) includes: 'a' epsilon: true.
|
|
364 |
self assert: (followSets at: (grammar at: #A)) includes: 'ab' epsilon: true.
|
|
365 |
self assert: (followSets at: (grammar at: #B)) includes: 'ab' epsilon: true.
|
|
366 |
self assert: (followSets at: (grammar at: #a)) includes: 'ab' epsilon: true.
|
|
367 |
self assert: (followSets at: (grammar at: #b)) includes: 'ab' epsilon: true
|
|
368 |
!
|
|
369 |
|
|
370 |
testFollowSetExampleE
|
|
371 |
self assert: self grammarE followSet includes: '' epsilon: true
|
|
372 |
! !
|
|
373 |
|
|
374 |
!PPAnalyzerTest methodsFor:'testing-transform'!
|
|
375 |
|
|
376 |
testDelegateReplace
|
|
377 |
| one other delegate |
|
|
378 |
one := $a asParser.
|
|
379 |
other := $b asParser.
|
|
380 |
delegate := one token.
|
|
381 |
self assert: delegate children first == one.
|
|
382 |
self deny: delegate children first == other.
|
|
383 |
|
|
384 |
delegate replace: other with: one.
|
|
385 |
self assert: delegate children first == one.
|
|
386 |
self deny: delegate children first == other.
|
|
387 |
|
|
388 |
delegate replace: one with: other.
|
|
389 |
self deny: delegate children first == one.
|
|
390 |
self assert: delegate children first == other
|
|
391 |
!
|
|
392 |
|
|
393 |
testListReplace
|
|
394 |
| one other another list |
|
|
395 |
one := $a asParser.
|
|
396 |
other := $b asParser.
|
|
397 |
another := $c asParser.
|
|
398 |
list := one , another , one.
|
|
399 |
self assert: list children first == one.
|
|
400 |
self assert: list children second == another.
|
|
401 |
self assert: list children last == one.
|
|
402 |
|
|
403 |
list replace: other with: one.
|
|
404 |
self assert: list children first == one.
|
|
405 |
self assert: list children second == another.
|
|
406 |
self assert: list children last == one.
|
|
407 |
|
|
408 |
list replace: one with: other.
|
|
409 |
self assert: list children first == other.
|
|
410 |
self assert: list children second == another.
|
|
411 |
self assert: list children last == other.
|
|
412 |
|
|
413 |
list replace: another with: one.
|
|
414 |
self assert: list children first == other.
|
|
415 |
self assert: list children second == one.
|
|
416 |
self assert: list children last == other
|
|
417 |
!
|
|
418 |
|
|
419 |
testRepetitionReplace
|
263
|
420 |
| one two otherone othertwo repetition |
|
|
421 |
one := $a asParser.
|
|
422 |
two := $b asParser.
|
|
423 |
otherone := $1 asParser.
|
|
424 |
othertwo := $2 asParser.
|
|
425 |
|
|
426 |
repetition := one starLazy: two.
|
|
427 |
self assert: repetition children first == one.
|
|
428 |
self assert: repetition children second == two.
|
|
429 |
|
|
430 |
repetition replace: one with: otherone.
|
|
431 |
self assert: repetition children first == otherone.
|
|
432 |
self assert: repetition children second == two.
|
|
433 |
|
|
434 |
repetition replace: two with: othertwo.
|
|
435 |
self assert: repetition children first == otherone.
|
|
436 |
self assert: repetition children second == othertwo
|
208
|
437 |
!
|
|
438 |
|
|
439 |
testTransformIdentityGrammarC
|
|
440 |
| orig tran |
|
|
441 |
orig := self grammarC.
|
|
442 |
tran := orig transform: [ :each | each ].
|
|
443 |
self deny: orig == tran.
|
|
444 |
self deny: orig children first == tran children first.
|
|
445 |
self deny: orig children first children first == tran children first children first.
|
|
446 |
self deny: orig children first children last == tran children first children last.
|
|
447 |
self deny: orig children last == tran children last.
|
|
448 |
|
|
449 |
self assert: orig class == PPChoiceParser.
|
|
450 |
self assert: orig children first class == PPSequenceParser.
|
|
451 |
self assert: orig children first children first == orig.
|
|
452 |
self assert: orig children first children last == orig.
|
|
453 |
self assert: orig children last class == PPLiteralObjectParser.
|
|
454 |
|
|
455 |
self assert: tran class == PPChoiceParser.
|
|
456 |
self assert: tran children first class == PPSequenceParser.
|
|
457 |
self assert: tran children first children first == tran.
|
|
458 |
self assert: tran children first children last == tran.
|
|
459 |
self assert: tran children last class == PPLiteralObjectParser
|
|
460 |
!
|
|
461 |
|
|
462 |
testTransformIdentityGrammarE
|
|
463 |
| orig tran |
|
|
464 |
orig := self grammarE.
|
|
465 |
tran := orig transform: [ :each | each ].
|
|
466 |
self deny: orig == tran.
|
|
467 |
self deny: orig children first = tran children first.
|
|
468 |
|
|
469 |
self assert: orig class == PPDelegateParser.
|
|
470 |
self assert: orig children first == orig.
|
|
471 |
|
|
472 |
self assert: tran class == PPDelegateParser.
|
|
473 |
self assert: tran children first == tran
|
|
474 |
!
|
|
475 |
|
|
476 |
testTransformWrapGrammarC
|
|
477 |
| orig tran |
|
|
478 |
orig := self grammarC.
|
|
479 |
tran := orig transform: [ :each | each memoized ].
|
|
480 |
|
|
481 |
self assert: orig class == PPChoiceParser.
|
|
482 |
self assert: orig children first class == PPSequenceParser.
|
|
483 |
self assert: orig children first children first == orig.
|
|
484 |
self assert: orig children first children last == orig.
|
|
485 |
self assert: orig children last class == PPLiteralObjectParser.
|
|
486 |
|
|
487 |
self assert: tran class == PPMemoizedParser.
|
|
488 |
self assert: tran children first class == PPChoiceParser.
|
|
489 |
self assert: tran children first children first class == PPMemoizedParser.
|
|
490 |
self assert: tran children first children first children first class == PPSequenceParser.
|
|
491 |
self assert: tran children first children first children first children first == tran.
|
|
492 |
self assert: tran children first children first children first children last == tran.
|
|
493 |
self assert: tran children first children last class == PPMemoizedParser.
|
|
494 |
self assert: tran children first children last children first class == PPLiteralObjectParser
|
|
495 |
!
|
|
496 |
|
|
497 |
testTransformWrapGrammarE
|
|
498 |
| orig tran |
|
|
499 |
orig := self grammarE.
|
|
500 |
tran := orig transform: [ :each | each memoized ].
|
|
501 |
|
|
502 |
self assert: orig class == PPDelegateParser.
|
|
503 |
self assert: orig children first == orig.
|
|
504 |
|
|
505 |
self assert: tran class == PPMemoizedParser.
|
|
506 |
self assert: tran children first class == PPDelegateParser.
|
|
507 |
self assert: tran children first children first == tran
|
|
508 |
! !
|
|
509 |
|
|
510 |
!PPAnalyzerTest methodsFor:'utilities'!
|
|
511 |
|
|
512 |
assert: aCollection includes: aString epsilon: aBoolean
|
|
513 |
| parsers checker stream |
|
|
514 |
parsers := aCollection
|
|
515 |
collect: [ :each | each end ].
|
|
516 |
checker := [ :string |
|
|
517 |
parsers anySatisfy: [ :parser |
|
|
518 |
(parser parse: string asPetitStream)
|
|
519 |
isPetitFailure not ] ].
|
|
520 |
stream := WriteStream on: String new.
|
|
521 |
32 to: 127 do: [ :index |
|
|
522 |
(checker value: (String with: (Character value: index)))
|
|
523 |
ifTrue: [ stream nextPut: (Character value: index) ] ].
|
|
524 |
self
|
|
525 |
assert: stream contents = aString
|
|
526 |
description: 'Expected ' , aString printString , ', but got ' , stream contents printString.
|
|
527 |
self
|
|
528 |
assert: (checker value: '') = aBoolean
|
|
529 |
description: 'Expected epsilon to ' , (aBoolean ifTrue: [ 'be' ] ifFalse: [ 'not be' ]) , ' included'
|
|
530 |
! !
|
|
531 |
|
|
532 |
!PPAnalyzerTest class methodsFor:'documentation'!
|
|
533 |
|
|
534 |
version
|
325
|
535 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/analyzer/tests/PPAnalyzerTest.st,v 1.4 2014-03-04 20:44:21 cg Exp $'
|
208
|
536 |
!
|
|
537 |
|
|
538 |
version_CVS
|
325
|
539 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/analyzer/tests/PPAnalyzerTest.st,v 1.4 2014-03-04 20:44:21 cg Exp $'
|
208
|
540 |
! !
|
|
541 |
|