|
1 "{ Package: 'stx:goodies/petitparser/compiler/tests' }" |
|
2 |
|
3 "{ NameSpace: Smalltalk }" |
|
4 |
|
5 TestCase subclass:#PPCScannerCodeGeneratorTest |
|
6 instanceVariableNames:'fsa a b c d e codeGenerator scanner result' |
|
7 classVariableNames:'' |
|
8 poolDictionaries:'' |
|
9 category:'PetitCompiler-Tests-Scanner' |
|
10 ! |
|
11 |
|
12 !PPCScannerCodeGeneratorTest methodsFor:'as yet unclassified'! |
|
13 |
|
14 fail: stream rule: rule |
|
15 scanner initialize. |
|
16 scanner stream: stream asPetitStream. |
|
17 result := scanner perform: rule. |
|
18 |
|
19 self assert: result isEmpty |
|
20 ! |
|
21 |
|
22 parse: stream token: token rule: rule |
|
23 self parse: stream token: token rule: rule position: stream size. |
|
24 ! |
|
25 |
|
26 parse: stream token: token rule: rule position: position |
|
27 scanner initialize. |
|
28 scanner stream: stream asPetitStream. |
|
29 result := scanner perform: rule. |
|
30 |
|
31 self assert: (result at: token) = position. |
|
32 ! |
|
33 |
|
34 setUp |
|
35 a := PEGFsaState new name: #a; retval: #a; yourself. |
|
36 b := PEGFsaState new name: #b; retval: #b; yourself. |
|
37 c := PEGFsaState new name: #c; retval: #c; yourself. |
|
38 d := PEGFsaState new name: #d; retval: #d; yourself. |
|
39 e := PEGFsaState new name: #e; retval: #e; yourself. |
|
40 |
|
41 fsa := PEGFsa new. |
|
42 |
|
43 codeGenerator := PPCScannerCodeGenerator new. |
|
44 ! |
|
45 |
|
46 testA |
|
47 fsa addState: a. |
|
48 fsa addState: b. |
|
49 |
|
50 fsa startState: a. |
|
51 fsa finalState: b. |
|
52 |
|
53 fsa addTransitionFrom: a to: b on: $a. |
|
54 fsa name: #nextTokenA. |
|
55 b retval: #a. |
|
56 |
|
57 scanner := (codeGenerator generate: fsa). |
|
58 |
|
59 self parse: 'aaa' token: #a rule: #nextTokenA position: 1. |
|
60 self fail: 'b' rule: #nextTokenA. |
|
61 ! |
|
62 |
|
63 testAAstarA |
|
64 fsa addState: a. |
|
65 fsa addState: b. |
|
66 fsa addState: c. |
|
67 fsa startState: a. |
|
68 fsa finalState: b. |
|
69 |
|
70 fsa addTransitionFrom: a to: b on: $a. |
|
71 fsa addTransitionFrom: b to: c on: $a. |
|
72 fsa addTransitionFrom: c to: b on: $a. |
|
73 |
|
74 fsa name: #nextTokenAAstarA. |
|
75 b priority: -1. |
|
76 c priority: 0. |
|
77 b retval: #AAstarA. |
|
78 |
|
79 scanner := (codeGenerator generate: fsa). |
|
80 |
|
81 self parse: 'a' token: #AAstarA rule: #nextTokenAAstarA. |
|
82 self parse: 'aaa' token: #AAstarA rule: #nextTokenAAstarA. |
|
83 self parse: 'aaaaa' token: #AAstarA rule: #nextTokenAAstarA. |
|
84 |
|
85 self fail: '' rule: #nextTokenAAstarA. |
|
86 self fail: 'aa' rule: #nextTokenAAstarA. |
|
87 self fail: 'aaaa' rule: #nextTokenAAstarA. |
|
88 ! |
|
89 |
|
90 testAB |
|
91 fsa addState: a. |
|
92 fsa addState: b. |
|
93 fsa addState: c. |
|
94 fsa startState: a. |
|
95 fsa finalState: c. |
|
96 |
|
97 fsa addTransitionFrom: a to: b on: $a. |
|
98 fsa addTransitionFrom: b to: c on: $b. |
|
99 |
|
100 fsa name: #nextTokenAB. |
|
101 c retval: #ab. |
|
102 |
|
103 scanner := (codeGenerator generate: fsa). |
|
104 |
|
105 self parse: 'ab' token: #ab rule: #nextTokenAB position: 2. |
|
106 ! |
|
107 |
|
108 testABorBC |
|
109 fsa addState: a. |
|
110 fsa addState: b. |
|
111 fsa addState: c. |
|
112 fsa addState: d. |
|
113 fsa addState: e. |
|
114 fsa startState: a. |
|
115 fsa finalState: c. |
|
116 fsa finalState: e. |
|
117 |
|
118 |
|
119 fsa addTransitionFrom: a to: b on: $a. |
|
120 fsa addTransitionFrom: b to: c on: $b. |
|
121 |
|
122 fsa addTransitionFrom: a to: d on: $b. |
|
123 fsa addTransitionFrom: d to: e on: $c. |
|
124 |
|
125 fsa name: #nextTokenABorBC. |
|
126 c retval: #ab. |
|
127 e retval: #bc. |
|
128 |
|
129 scanner := (codeGenerator generate: fsa). |
|
130 |
|
131 self parse: 'ab' token: #ab rule: #nextTokenABorBC position: 2. |
|
132 self parse: 'abbc' token: #ab rule: #nextTokenABorBC position: 2. |
|
133 self parse: 'bc' token: #bc rule: #nextTokenABorBC position: 2. |
|
134 |
|
135 self fail: 'ac' rule: #nextTokenABorBC. |
|
136 ! |
|
137 |
|
138 testABstarA |
|
139 fsa addState: a. |
|
140 fsa addState: b. |
|
141 fsa addState: c. |
|
142 fsa startState: a. |
|
143 fsa finalState: b. |
|
144 |
|
145 fsa addTransitionFrom: a to: b on: $a. |
|
146 fsa addTransitionFrom: b to: c on: $b. |
|
147 fsa addTransitionFrom: c to: b on: $a. |
|
148 |
|
149 fsa name: #nextTokenABstarA. |
|
150 b retval: #ABstarA. |
|
151 |
|
152 scanner := (codeGenerator generate: fsa). |
|
153 |
|
154 self parse: 'a' token: #ABstarA rule: #nextTokenABstarA position: 1. |
|
155 self parse: 'aa' token: #ABstarA rule: #nextTokenABstarA position: 1. |
|
156 self parse: 'aba' token: #ABstarA rule: #nextTokenABstarA position: 3. |
|
157 self parse: 'abaa' token: #ABstarA rule: #nextTokenABstarA position: 3. |
|
158 self parse: 'ababa' token: #ABstarA rule: #nextTokenABstarA position: 5. |
|
159 |
|
160 |
|
161 |
|
162 self fail: '' rule: #nextTokenABstarA. |
|
163 ! |
|
164 |
|
165 testA_Bstar_A |
|
166 fsa addState: a. |
|
167 fsa addState: b. |
|
168 fsa addState: c. |
|
169 fsa startState: a. |
|
170 fsa finalState: c. |
|
171 |
|
172 fsa addTransitionFrom: a to: b on: $a. |
|
173 fsa addTransitionFrom: b to: b on: $b. |
|
174 fsa addTransitionFrom: b to: c on: $a. |
|
175 |
|
176 fsa name: #nextTokenA_Bstar_A. |
|
177 c retval: #A_Bstar_A. |
|
178 |
|
179 scanner := (codeGenerator generate: fsa). |
|
180 |
|
181 self parse: 'aa' token: #A_Bstar_A rule: #nextTokenA_Bstar_A. |
|
182 self parse: 'aba' token: #A_Bstar_A rule: #nextTokenA_Bstar_A. |
|
183 |
|
184 self fail: '' rule: #nextTokenA_Bstar_A. |
|
185 ! |
|
186 |
|
187 testAorB |
|
188 fsa addState: a. |
|
189 fsa addState: b. |
|
190 fsa addState: c. |
|
191 fsa startState: a. |
|
192 fsa finalState: b. |
|
193 fsa finalState: c. |
|
194 |
|
195 |
|
196 fsa addTransitionFrom: a to: b on: $a. |
|
197 fsa addTransitionFrom: a to: c on: $b. |
|
198 |
|
199 fsa name: #nextTokenAorB. |
|
200 b retval: #a. |
|
201 c retval: #b. |
|
202 |
|
203 scanner := (codeGenerator generate: fsa). |
|
204 |
|
205 self parse: 'a' token: #a rule: #nextTokenAorB. |
|
206 self parse: 'b' token: #b rule: #nextTokenAorB. |
|
207 |
|
208 self fail: 'c' rule: #nextTokenAorB. |
|
209 self fail: 'c' rule: #nextTokenAorB. |
|
210 ! |
|
211 |
|
212 testAstarA |
|
213 fsa addState: a. |
|
214 fsa addState: b. |
|
215 fsa startState: a. |
|
216 fsa finalState: b. |
|
217 |
|
218 fsa addTransitionFrom: a to: a on: $a. |
|
219 fsa addTransitionFrom: a to: b on: $a. |
|
220 |
|
221 fsa name: #nextTokenAstarA. |
|
222 b retval: #AstarA. |
|
223 |
|
224 self should: [codeGenerator generate: fsa ] raise: Exception. |
|
225 ! |
|
226 |
|
227 testAstarB |
|
228 fsa addState: a. |
|
229 fsa addState: b. |
|
230 fsa startState: a. |
|
231 fsa finalState: b. |
|
232 |
|
233 fsa addTransitionFrom: a to: a on: $a. |
|
234 fsa addTransitionFrom: a to: b on: $b. |
|
235 |
|
236 fsa name: #nextTokenAstarB. |
|
237 b retval: #AstarB. |
|
238 |
|
239 scanner := (codeGenerator generate: fsa). |
|
240 |
|
241 self parse: 'ab' token: #AstarB rule: #nextTokenAstarB. |
|
242 self parse: 'b' token: #AstarB rule: #nextTokenAstarB. |
|
243 self parse: 'aaab' token: #AstarB rule: #nextTokenAstarB. |
|
244 |
|
245 self fail: 'c' rule: #nextTokenAstarB. |
|
246 ! ! |
|
247 |