|
1 "{ Package: 'stx:goodies/petitparser/compiler/tests' }" |
|
2 |
|
3 "{ NameSpace: Smalltalk }" |
|
4 |
|
5 TestCase subclass:#PEGFsaChoiceDeterminizationTest |
|
6 instanceVariableNames:'fsa a b c result d interpreter e t1 t2 state anotherState parser |
|
7 generator' |
|
8 classVariableNames:'' |
|
9 poolDictionaries:'' |
|
10 category:'PetitCompiler-Tests-FSA' |
|
11 ! |
|
12 |
|
13 !PEGFsaChoiceDeterminizationTest methodsFor:'as yet unclassified'! |
|
14 |
|
15 assert: anFsa fail: input |
|
16 | stream | |
|
17 stream := input asPetitStream. |
|
18 |
|
19 result := interpreter interpret: anFsa on: stream. |
|
20 |
|
21 self assert: result isEmpty. |
|
22 ^ result |
|
23 ! |
|
24 |
|
25 assert: anFsa parse: input |
|
26 ^ self assert: anFsa parse: input end: input size |
|
27 ! |
|
28 |
|
29 assert: anFsa parse: input end: end |
|
30 | stream | |
|
31 stream := input asPetitStream. |
|
32 |
|
33 result := interpreter interpret: anFsa on: stream. |
|
34 |
|
35 self assert: result size = 1. |
|
36 self assert: ((result anyOne) = end) description: 'wrong position'. |
|
37 |
|
38 ^ result anyOne |
|
39 ! |
|
40 |
|
41 determinizator |
|
42 ^ PEGFsaChoiceDeterminizator new |
|
43 ! |
|
44 |
|
45 determinize: anFsa |
|
46 ^ self determinizator determinize: anFsa |
|
47 ! |
|
48 |
|
49 fsaFrom: aNode |
|
50 ^ (aNode accept: generator) |
|
51 yourself |
|
52 ! |
|
53 |
|
54 joinState: s1 with: s2 |
|
55 ^ self determinizator joinState: s1 with: s2 |
|
56 ! |
|
57 |
|
58 setUp |
|
59 a := PEGFsaState new name: #a; retval: #token; yourself. |
|
60 b := PEGFsaState new name: #b; retval: #token; yourself. |
|
61 c := PEGFsaState new name: #c; retval: #token; yourself. |
|
62 d := PEGFsaState new name: #d; retval: #token; yourself. |
|
63 e := PEGFsaState new name: #e; retval: #token; yourself. |
|
64 |
|
65 state := PEGFsaState new name: #state; retval: #token; yourself. |
|
66 anotherState := PEGFsaState new name: #anotherState; retval: #token; yourself. |
|
67 |
|
68 t1 := PEGFsaCharacterTransition new. |
|
69 t2 := PEGFsaCharacterTransition new. |
|
70 |
|
71 fsa := PEGFsa new. |
|
72 generator := PEGFsaGenerator new. |
|
73 |
|
74 interpreter := PEGFsaInterpret new |
|
75 yourself. |
|
76 ! |
|
77 |
|
78 testAAorA |
|
79 parser := 'aa' asParser / 'a' asParser. |
|
80 fsa := self fsaFrom: parser asCompilerTree. |
|
81 |
|
82 " self assert: fsa states size = 2." |
|
83 self assert: fsa isDeterministic. |
|
84 self assert: fsa isWithoutEpsilons. |
|
85 |
|
86 self assert: fsa parse: 'a'. |
|
87 self assert: fsa parse: 'aa'. |
|
88 self assert: fsa fail: 'b'. |
|
89 ! |
|
90 |
|
91 testAorAA |
|
92 parser := 'a' asParser / 'aa' asParser. |
|
93 fsa := self fsaFrom: parser asCompilerTree. |
|
94 |
|
95 " self assert: fsa states size = 2." |
|
96 self assert: fsa isDeterministic. |
|
97 self assert: fsa isWithoutEpsilons. |
|
98 |
|
99 self assert: fsa parse: 'a'. |
|
100 self assert: fsa parse: 'aa' end: 1. |
|
101 self assert: fsa fail: 'b'. |
|
102 ! |
|
103 |
|
104 testDeterminizeFsa |
|
105 fsa addState: a. |
|
106 fsa addState: b. |
|
107 fsa addState: c. |
|
108 fsa addState: d. |
|
109 fsa addState: e. |
|
110 fsa startState: a. |
|
111 fsa finalState: c. |
|
112 fsa finalState: e. |
|
113 |
|
114 fsa addTransitionFrom: a to: b on: $a. |
|
115 fsa addTransitionFrom: b to: c on: $b. |
|
116 |
|
117 b final: true. |
|
118 b priority: 0. |
|
119 c final: true. |
|
120 c priority: 0. |
|
121 c failure: true. |
|
122 |
|
123 fsa addTransitionFrom: a to: d on: $a priority: -1. |
|
124 fsa addTransitionFrom: d to: e on: $a priority: -1. |
|
125 |
|
126 d priority: -1. |
|
127 e final: true. |
|
128 e priority: -1. |
|
129 e failure: true. |
|
130 |
|
131 |
|
132 self determinize: fsa. |
|
133 |
|
134 self assert: fsa states size = 3. |
|
135 self assert: a transitions size = 1. |
|
136 self assert: a destination isFinal. |
|
137 self assert: a destination destination isFinal. |
|
138 ! |
|
139 |
|
140 testDeterminizeFsa2 |
|
141 fsa addState: a. |
|
142 fsa addState: b. |
|
143 fsa addState: c. |
|
144 fsa addState: d. |
|
145 fsa addState: e. |
|
146 fsa startState: a. |
|
147 fsa finalState: c. |
|
148 fsa finalState: e. |
|
149 |
|
150 fsa addTransitionFrom: a to: b on: $a. |
|
151 fsa addTransitionFrom: b to: c on: $b. |
|
152 |
|
153 b final: true. |
|
154 b priority: 0. |
|
155 c final: true. |
|
156 c priority: 0. |
|
157 c failure: true. |
|
158 |
|
159 fsa addTransitionFrom: a to: d on: $a priority: -1. |
|
160 fsa addTransitionFrom: d to: e on: $b priority: -1. |
|
161 |
|
162 d priority: -1. |
|
163 e final: true. |
|
164 e priority: -1. |
|
165 e failure: true. |
|
166 |
|
167 self determinize: fsa. |
|
168 |
|
169 self assert: fsa states size = 3. |
|
170 self assert: a transitions size = 1. |
|
171 self assert: a destination isFinal. |
|
172 self assert: a destination transitions size = 1. |
|
173 self assert: a destination destination isFsaFailure. |
|
174 ! |
|
175 |
|
176 testNot |
|
177 parser := ('aa' asParser, 'aa' asParser not) / ('aa' asParser, 'aa' asParser). |
|
178 fsa := self fsaFrom: parser asCompilerTree. |
|
179 |
|
180 |
|
181 " self assert: fsa states size = 2." |
|
182 self assert: fsa isDeterministic. |
|
183 self assert: fsa isWithoutEpsilons. |
|
184 |
|
185 self assert: fsa parse: 'aabc' end: 2. |
|
186 self assert: fsa parse: 'aaa' end: 2. |
|
187 self assert: fsa parse: 'aa'. |
|
188 |
|
189 self assert: fsa parse: 'aaaa'. |
|
190 self assert: fsa parse: 'aaaaa' end: 4. |
|
191 |
|
192 self assert: fsa fail: 'ab'. |
|
193 ! ! |
|
194 |