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