59 self assert: parser parse: ' |
59 self assert: parser parse: ' |
60 ' to: Character cr. |
60 ' to: Character cr. |
61 ! |
61 ! |
62 |
62 |
63 testCompileAnyStar |
63 testCompileAnyStar |
64 parser := #any asParser star compileWithConfiguration: configuration. |
64 parser := #any asParser star compileUsingCompiler:compiler. |
65 |
65 |
66 |
66 |
67 self assert: parser parse: 'aaa' to: { $a. $a . $a }. |
67 self assert: parser parse: 'aaa' to: { $a. $a . $a }. |
68 self assert: context invocationCount = 1. |
68 self assert: context invocationCount = 1. |
69 self assert: parser parse: '' to: { }. |
69 self assert: parser parse: '' to: { }. |
70 ! |
70 ! |
71 |
71 |
72 testCompileBlock |
72 testCompileBlock |
73 parser := (#letter asParser) plus ==> [ :res | res collect: [:each | each asUppercase ]]. |
73 parser := (#letter asParser) plus ==> [ :res | res collect: [:each | each asUppercase ]]. |
74 parser := parser compileWithConfiguration: configuration. |
74 parser := parser compileUsingCompiler:compiler. |
75 |
75 |
76 self assert: parser parse: 'foo' to: { $F . $O . $O}. |
76 self assert: parser parse: 'foo' to: { $F . $O . $O}. |
77 self assert: parser parse: 'bar' to: { $B . $A . $R}. |
77 self assert: parser parse: 'bar' to: { $B . $A . $R}. |
78 self assert: parser fail: ''. |
78 self assert: parser fail: ''. |
79 ! |
79 ! |
80 |
80 |
81 testCompileCharacter |
81 testCompileCharacter |
82 parser := $a asParser compileWithConfiguration: configuration. |
82 parser := $a asParser compileUsingCompiler:compiler. |
83 |
83 |
84 self assert: parser parse: 'a' to: $a. |
84 self assert: parser parse: 'a' to: $a. |
85 self assert: parser fail: 'b'. |
85 self assert: parser fail: 'b'. |
86 |
86 |
87 parser := $# asParser compileWithConfiguration: configuration. |
87 parser := $# asParser compileUsingCompiler:compiler. |
88 self assert: parser parse: '#'. |
88 self assert: parser parse: '#'. |
89 ! |
89 ! |
90 |
90 |
91 testCompileChoice |
91 testCompileChoice |
92 parser := (#digit asParser / #letter asParser) compileWithConfiguration: configuration. |
92 parser := (#digit asParser / #letter asParser) compileUsingCompiler:compiler. |
93 |
93 |
94 self assert: parser parse: '1' to: $1. |
94 self assert: parser parse: '1' to: $1. |
95 self assert: parser parse: 'a' to: $a. |
95 self assert: parser parse: 'a' to: $a. |
96 self assert: parser fail: '_'. |
96 self assert: parser fail: '_'. |
97 |
97 |
98 ! |
98 ! |
99 |
99 |
100 testCompileChoice2 |
100 testCompileChoice2 |
101 parser := ('true' asParser / 'false' asParser) compileWithConfiguration: configuration. |
101 parser := ('true' asParser / 'false' asParser) compileUsingCompiler:compiler. |
102 |
102 |
103 self assert: parser parse: 'true' to: 'true'. |
103 self assert: parser parse: 'true' to: 'true'. |
104 self assert: parser parse: 'false' to: 'false'. |
104 self assert: parser parse: 'false' to: 'false'. |
105 self assert: parser fail: 'trulse'. |
105 self assert: parser fail: 'trulse'. |
106 |
106 |
107 ! |
107 ! |
108 |
108 |
109 testCompileLiteral |
109 testCompileLiteral |
110 parser := 'foo' asParser compileWithConfiguration: configuration. |
110 parser := 'foo' asParser compileUsingCompiler:compiler. |
111 |
111 |
112 self assert: parser parse: 'foo' to: 'foo'. |
112 self assert: parser parse: 'foo' to: 'foo'. |
113 self assert: parser parse: 'foobar' to: 'foo' end: 3. |
113 self assert: parser parse: 'foobar' to: 'foo' end: 3. |
114 self assert: parser fail: 'boo'. |
114 self assert: parser fail: 'boo'. |
115 |
115 |
116 parser := '#[' asParser compileWithConfiguration: configuration. |
116 parser := '#[' asParser compileUsingCompiler:compiler. |
117 self assert: parser parse: '#[1]' to: '#[' end: 2. |
117 self assert: parser parse: '#[1]' to: '#[' end: 2. |
118 ! |
118 ! |
119 |
119 |
120 testCompileLiteral2 |
120 testCompileLiteral2 |
121 | quote | |
121 | quote | |
122 quote := '''' asParser. |
122 quote := '''' asParser. |
123 parser := (quote, $a asParser ) compileWithConfiguration: configuration. |
123 parser := (quote , $a asParser) compileUsingCompiler:compiler. |
124 self assert: parser parse: '''a' to: {'''' . $a}. |
124 self assert: parser parse: '''a' to: {'''' . $a}. |
125 ! |
125 ! |
126 |
126 |
127 testCompileNegate |
127 testCompileNegate |
128 parser := #letter asParser negate star, #letter asParser. |
128 parser := #letter asParser negate star, #letter asParser. |
129 parser := parser compileWithConfiguration: configuration. |
129 parser := parser compileUsingCompiler:compiler. |
130 |
130 |
131 self assert: parser parse: '...a' to: { { $. . $. . $. } . $a }. |
131 self assert: parser parse: '...a' to: { { $. . $. . $. } . $a }. |
132 self assert: parser parse: 'aaa' to: { {} . $a } end: 1. |
132 self assert: parser parse: 'aaa' to: { {} . $a } end: 1. |
133 self assert: parser fail: '...'. |
133 self assert: parser fail: '...'. |
134 ! |
134 ! |
135 |
135 |
136 testCompileNil |
136 testCompileNil |
137 parser := nil asParser compileWithConfiguration: configuration. |
137 parser := nil asParser compileUsingCompiler:compiler. |
138 |
138 |
139 self assert: parser parse: 'a' to: nil end: 0. |
139 self assert: parser parse: 'a' to: nil end: 0. |
140 self assert: parser parse: '' to: nil end: 0. |
140 self assert: parser parse: '' to: nil end: 0. |
141 |
141 |
142 parser := nil asParser, 'foo' asParser. |
142 parser := nil asParser, 'foo' asParser. |
143 self assert: parser parse: 'foo' to: { nil . 'foo' } |
143 self assert: parser parse: 'foo' to: { nil . 'foo' } |
144 ! |
144 ! |
145 |
145 |
146 testCompileNot |
146 testCompileNot |
147 parser := #digit asParser not compileWithConfiguration: configuration. |
147 parser := #digit asParser not compileUsingCompiler:compiler. |
148 |
148 |
149 self assert: parser parse: 'a' to: nil end: 0. |
149 self assert: parser parse: 'a' to: nil end: 0. |
150 self assert: parser fail: '1'. |
150 self assert: parser fail: '1'. |
151 self assert: parser parse: '' to: nil end: 0. |
151 self assert: parser parse: '' to: nil end: 0. |
152 |
152 |
153 parser := 'foo' asParser, $: asParser not. |
153 parser := 'foo' asParser, $: asParser not. |
154 parser := parser compileWithConfiguration: configuration. |
154 parser := parser compileUsingCompiler:compiler. |
155 self assert: parser parse: 'foo' to: { 'foo'. nil } end: 3. |
155 self assert: parser parse: 'foo' to: { 'foo'. nil } end: 3. |
156 |
156 |
157 parser := 'foo' asParser, $: asParser not, 'bar' asParser. |
157 parser := 'foo' asParser, $: asParser not, 'bar' asParser. |
158 parser := parser compileWithConfiguration: configuration. |
158 parser := parser compileUsingCompiler:compiler. |
159 self assert: parser parse: 'foobar' to: { 'foo'. nil . 'bar' } end: 6. |
159 self assert: parser parse: 'foobar' to: { 'foo'. nil . 'bar' } end: 6. |
160 ! |
160 ! |
161 |
161 |
162 testCompileNot2 |
162 testCompileNot2 |
163 parser := ($a asParser, $b asParser) not compileWithConfiguration: configuration. |
163 parser := ($a asParser , $b asParser) not compileUsingCompiler:compiler. |
164 |
164 |
165 self assert: parser parse: '' to: nil end: 0. |
165 self assert: parser parse: '' to: nil end: 0. |
166 self assert: parser parse: 'a' to: nil end: 0. |
166 self assert: parser parse: 'a' to: nil end: 0. |
167 self assert: parser parse: 'aa' to: nil end: 0. |
167 self assert: parser parse: 'aa' to: nil end: 0. |
168 self assert: parser fail: 'ab'. |
168 self assert: parser fail: 'ab'. |
169 ! |
169 ! |
170 |
170 |
171 testCompileNot3 |
171 testCompileNot3 |
172 parser := ('foo' asParser not, 'fee' asParser) compileWithConfiguration: configuration. |
172 parser := ('foo' asParser not , 'fee' asParser) compileUsingCompiler:compiler. |
173 |
173 |
174 self assert: parser parse: 'fee' to: #(nil 'fee'). |
174 self assert: parser parse: 'fee' to: #(nil 'fee'). |
175 self assert: parser fail: 'foo'. |
175 self assert: parser fail: 'foo'. |
176 ! |
176 ! |
177 |
177 |
178 testCompileNotLiteral |
178 testCompileNotLiteral |
179 parser := 'foo' asParser not compileWithConfiguration: configuration. |
179 parser := 'foo' asParser not compileUsingCompiler:compiler. |
180 self assert: parser class methodDictionary size = 1. |
180 self assert: parser class methodDictionary size = 1. |
181 |
181 |
182 self assert: parser parse: 'bar' to: nil end: 0. |
182 self assert: parser parse: 'bar' to: nil end: 0. |
183 |
183 |
184 self assert: parser fail: 'foo'. |
184 self assert: parser fail: 'foo'. |
199 self assert: parser parse: 'foobar' to: { 'foo'. nil . #($b $a $r) } end: 6. |
199 self assert: parser parse: 'foobar' to: { 'foo'. nil . #($b $a $r) } end: 6. |
200 self assert: parser fail: 'foofoo'. |
200 self assert: parser fail: 'foofoo'. |
201 ! |
201 ! |
202 |
202 |
203 testCompileOptional |
203 testCompileOptional |
204 parser := #digit asParser optional compileWithConfiguration: configuration. |
204 parser := #digit asParser optional compileUsingCompiler:compiler. |
205 |
205 |
206 self assert: parser parse: '1' to: $1. |
206 self assert: parser parse: '1' to: $1. |
207 self assert: parser parse: 'a' to: nil end: 0. |
207 self assert: parser parse: 'a' to: nil end: 0. |
208 |
208 |
209 parser := (#digit asParser optional, #letter asParser) compile. |
209 parser := (#digit asParser optional, #letter asParser) compile. |
210 self assert: parser parse: '1a' to: { $1 . $a }. |
210 self assert: parser parse: '1a' to: { $1 . $a }. |
211 self assert: parser parse: 'a' to: { nil . $a }. |
211 self assert: parser parse: 'a' to: { nil . $a }. |
212 ! |
212 ! |
213 |
213 |
214 testCompilePlus |
214 testCompilePlus |
215 parser := #letter asParser plus compileWithConfiguration: configuration. |
215 parser := #letter asParser plus compileUsingCompiler:compiler. |
216 |
216 |
217 self assert: parser parse: 'lorem' to: {$l. $o. $r. $e. $m} . |
217 self assert: parser parse: 'lorem' to: {$l. $o. $r. $e. $m} . |
218 self assert: parser parse: 'a123' to: {$a} end: 1. |
218 self assert: parser parse: 'a123' to: {$a} end: 1. |
219 self assert: parser parse: 'ab123' to: {$a . $b} end: 2. |
219 self assert: parser parse: 'ab123' to: {$a . $b} end: 2. |
220 |
220 |
221 self assert: parser fail: ''. |
221 self assert: parser fail: ''. |
222 self assert: parser fail: '123'. |
222 self assert: parser fail: '123'. |
223 ! |
223 ! |
224 |
224 |
225 testCompilePredicate |
225 testCompilePredicate |
226 parser := #digit asParser compileWithConfiguration: configuration. |
226 parser := #digit asParser compileUsingCompiler:compiler. |
227 |
227 |
228 self assert: parser parse: '1' to: $1. |
228 self assert: parser parse: '1' to: $1. |
229 self assert: parser parse: '0' to: $0. |
229 self assert: parser parse: '0' to: $0. |
230 self assert: parser fail: 'a'. |
230 self assert: parser fail: 'a'. |
231 ! |
231 ! |
232 |
232 |
233 testCompilePredicate2 |
233 testCompilePredicate2 |
234 parser := #space asParser compileWithConfiguration: configuration. |
234 parser := #space asParser compileUsingCompiler:compiler. |
235 |
235 |
236 self assert: parser parse: ' ' to: Character space. |
236 self assert: parser parse: ' ' to: Character space. |
237 self assert: parser fail: 'a'. |
237 self assert: parser fail: 'a'. |
238 ! |
238 ! |
239 |
239 |
240 testCompileSequence |
240 testCompileSequence |
241 parser := (#digit asParser, #letter asParser) compileWithConfiguration: configuration. |
241 parser := (#digit asParser , #letter asParser) compileUsingCompiler:compiler. |
242 |
242 |
243 self assert: parser parse: '1a' to: {$1 .$a}. |
243 self assert: parser parse: '1a' to: {$1 .$a}. |
244 |
244 |
245 |
245 |
246 ! |
246 ! |
247 |
247 |
248 testCompileSequence2 |
248 testCompileSequence2 |
249 parser := (#digit asParser, #space asParser, #letter asParser) compileWithConfiguration: configuration. |
249 parser := (#digit asParser , #space asParser , #letter asParser) |
|
250 compileUsingCompiler:compiler. |
250 |
251 |
251 self assert: parser parse: '9 c' to: {$9 . Character space. $c }. |
252 self assert: parser parse: '9 c' to: {$9 . Character space. $c }. |
252 self assert: parser fail: '9c'. |
253 self assert: parser fail: '9c'. |
253 |
254 |
254 ! |
255 ! |
255 |
256 |
256 testCompileSequence3 |
257 testCompileSequence3 |
257 parser := (#any asParser, #any asParser, #any asParser) compileWithConfiguration: configuration. |
258 parser := (#any asParser , #any asParser , #any asParser) |
|
259 compileUsingCompiler:compiler. |
258 |
260 |
259 self assert: parser parse: 'foo' to: #($f $o $o). |
261 self assert: parser parse: 'foo' to: #($f $o $o). |
260 self assert: parser fail: 'fo'. |
262 self assert: parser fail: 'fo'. |
261 |
263 |
262 ! |
264 ! |
263 |
265 |
264 testCompileStar |
266 testCompileStar |
265 parser := #letter asParser star compileWithConfiguration: configuration. |
267 parser := #letter asParser star compileUsingCompiler:compiler. |
266 |
268 |
267 self assert: parser parse: 'lorem' to: {$l. $o. $r. $e. $m} . |
269 self assert: parser parse: 'lorem' to: {$l. $o. $r. $e. $m} . |
268 self assert: parser parse: '' to: {}. |
270 self assert: parser parse: '' to: {}. |
269 self assert: parser parse: '123' to: {} end: 0. |
271 self assert: parser parse: '123' to: {} end: 0. |
270 self assert: parser parse: 'ab123' to: {$a . $b} end: 2. |
272 self assert: parser parse: 'ab123' to: {$a . $b} end: 2. |
271 ! |
273 ! |
272 |
274 |
273 testCompileStarLiteral |
275 testCompileStarLiteral |
274 parser := 'foo' asParser star compileWithConfiguration: configuration. |
276 parser := 'foo' asParser star compileUsingCompiler:compiler. |
275 |
277 |
276 self assert: parser parse: 'foo' to: #('foo' ) . |
278 self assert: parser parse: 'foo' to: #('foo' ) . |
277 self assert: parser parse: 'foofoo' to: #('foo' 'foo') . |
279 self assert: parser parse: 'foofoo' to: #('foo' 'foo') . |
278 self assert: parser parse: 'foofoofoo' to: #('foo' 'foo' 'foo') . |
280 self assert: parser parse: 'foofoofoo' to: #('foo' 'foo' 'foo') . |
279 self assert: parser parse: '' to: #(). |
281 self assert: parser parse: '' to: #(). |
280 self assert: parser parse: 'bar' to: #() end: 0. |
282 self assert: parser parse: 'bar' to: #() end: 0. |
281 ! |
283 ! |
282 |
284 |
283 testCompileStarPredicate |
285 testCompileStarPredicate |
284 parser := #letter asParser star compileWithConfiguration: configuration. |
286 parser := #letter asParser star compileUsingCompiler:compiler. |
285 |
287 |
286 self assert: parser parse: 'foo' to: #($f $o $o ) . |
288 self assert: parser parse: 'foo' to: #($f $o $o ) . |
287 self assert: parser parse: '' to: #(). |
289 self assert: parser parse: '' to: #(). |
288 self assert: parser parse: '123' to: #() end: 0. |
290 self assert: parser parse: '123' to: #() end: 0. |
289 ! |
291 ! |
290 |
292 |
291 testCompileSymbolBlock |
293 testCompileSymbolBlock |
292 parser := (#letter asParser) plus ==> #second. |
294 parser := (#letter asParser) plus ==> #second. |
293 parser := parser compileWithConfiguration: configuration. |
295 parser := parser compileUsingCompiler:compiler. |
294 |
296 |
295 self assert: parser parse: 'foo' to: $o. |
297 self assert: parser parse: 'foo' to: $o. |
296 self assert: parser parse: 'bar' to: $a. |
298 self assert: parser parse: 'bar' to: $a. |
297 self assert: parser fail: ''. |
299 self assert: parser fail: ''. |
298 self should: [ parser parse: 'f' ] raise: Error. |
300 self should: [ parser parse: 'f' ] raise: Error. |
299 ! |
301 ! |
300 |
302 |
301 testCompileTrim |
303 testCompileTrim |
302 parser := $a asParser trim compileWithConfiguration: configuration. |
304 parser := $a asParser trim compileUsingCompiler:compiler. |
303 |
305 |
304 self assert: parser fail: ''. |
306 self assert: parser fail: ''. |
305 self assert: parser parse: 'a' to: $a. |
307 self assert: parser parse: 'a' to: $a. |
306 self assert: parser parse: ' a' to: $a. |
308 self assert: parser parse: ' a' to: $a. |
307 self assert: parser parse: 'a ' to: $a. |
309 self assert: parser parse: 'a ' to: $a. |
311 testCompileTrimmingToken |
313 testCompileTrimmingToken |
312 | token1 token2 | |
314 | token1 token2 | |
313 token1 := (#letter asParser) plus trimmingToken. |
315 token1 := (#letter asParser) plus trimmingToken. |
314 token2 := (#letter asParser) plus trimmingToken. |
316 token2 := (#letter asParser) plus trimmingToken. |
315 |
317 |
316 parser := (token1, token2) compileWithConfiguration: configuration. |
318 parser := (token1 , token2) compileUsingCompiler:compiler. |
317 |
319 |
318 self assert: parser parse: 'foo bar'. |
320 self assert: parser parse: 'foo bar'. |
319 self assert: parser parse: ' foo bar '. |
321 self assert: parser parse: ' foo bar '. |
320 ! |
322 ! |
321 |
323 |
322 testCompileTrimmingToken2 |
324 testCompileTrimmingToken2 |
323 | token1 token2 | |
325 | token1 token2 | |
324 token1 := (#letter asParser) plus trimmingToken. |
326 token1 := (#letter asParser) plus trimmingToken. |
325 token2 := (#letter asParser) plus trimmingToken / 'foo' asParser trimmingToken. |
327 token2 := (#letter asParser) plus trimmingToken / 'foo' asParser trimmingToken. |
326 |
328 |
327 parser := (token1, token2) compileWithConfiguration: configuration. |
329 parser := (token1 , token2) compileUsingCompiler:compiler. |
328 |
330 |
329 self assert: parser parse: 'foo bar'. |
331 self assert: parser parse: 'foo bar'. |
330 self assert: parser parse: ' foo bar '. |
332 self assert: parser parse: ' foo bar '. |
331 ! |
333 ! |
332 |
334 |
333 testCompileTrimmingToken3 |
335 testCompileTrimmingToken3 |
334 | token1 token2 | |
336 | token1 token2 | |
335 token1 := ($a asParser, $b asParser) trimmingToken name: 'token1'. |
337 token1 := ($a asParser, $b asParser) trimmingToken name: 'token1'. |
336 token2 := (token1 not, $c asParser) trimmingToken name: 'token2'. |
338 token2 := (token1 not, $c asParser) trimmingToken name: 'token2'. |
337 |
339 |
338 parser := (token1 / token2) compileWithConfiguration: configuration. |
340 parser := (token1 / token2) compileUsingCompiler:compiler. |
339 |
341 |
340 self assert: (parser class methodDictionary includesKey: #'token1'). |
342 self assert: (parser class methodDictionary includesKey: #'token1'). |
341 self assert: (parser class methodDictionary includesKey: #'token1_fast'). |
343 self assert: (parser class methodDictionary includesKey: #'token1_fast'). |
342 |
344 |
343 self assert: parser parse: 'ab'. |
345 self assert: parser parse: 'ab'. |
413 |
417 |
414 parser := (id wrapped, $: asParser) smalltalkToken |
418 parser := (id wrapped, $: asParser) smalltalkToken |
415 name: 'kw'; |
419 name: 'kw'; |
416 yourself. |
420 yourself. |
417 |
421 |
418 parser := parser compileWithConfiguration: configuration. |
422 parser := parser compileUsingCompiler:compiler. |
419 |
423 |
420 self assert: parser parse: 'foo:'. |
424 self assert: parser parse: 'foo:'. |
421 self assert: result inputValue = 'foo:'. |
425 self assert: result inputValue = 'foo:'. |
422 ! |
426 ! |
423 |
427 |
424 testToken |
428 testToken |
425 parser := (#letter asParser, (#digit asParser / #letter asParser) star) flatten compileWithConfiguration: configuration. |
429 parser := (#letter asParser , (#digit asParser / #letter asParser) star) flatten |
|
430 compileUsingCompiler:compiler. |
426 |
431 |
427 self assert: parser parse: 'foo' to: 'foo'. |
432 self assert: parser parse: 'foo' to: 'foo'. |
428 self assert: parser parse: 'a' to: 'a'. |
433 self assert: parser parse: 'a' to: 'a'. |
429 self assert: parser parse: 'f123a' to: 'f123a'. |
434 self assert: parser parse: 'f123a' to: 'f123a'. |
430 self assert: parser fail: ''. |
435 self assert: parser fail: ''. |
431 ! |
436 ! |
432 |
437 |
433 testToken2 |
438 testToken2 |
434 parser := (#letter asParser, (#digit asParser / #letter asParser) star) token compileWithConfiguration: configuration. |
439 parser := (#letter asParser , (#digit asParser / #letter asParser) star) token |
|
440 compileUsingCompiler:compiler. |
435 |
441 |
436 self assert: parser class methodDictionary size = 4. |
442 self assert: parser class methodDictionary size = 4. |
437 self assert: parser parse: 'foo'. |
443 self assert: parser parse: 'foo'. |
438 self assert: result inputValue = 'foo'. |
444 self assert: result inputValue = 'foo'. |
439 self assert: context invocationCount = 6. |
445 self assert: context invocationCount = 6. |