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