1
|
1 |
"{ Package: 'stx:goodies/smaCC' }"
|
|
2 |
|
|
3 |
"{ NameSpace: SmaCC }"
|
|
4 |
|
|
5 |
TestCase subclass:#ParserGeneratorTest
|
|
6 |
instanceVariableNames:'wrappers'
|
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
|
9 |
category:'SmaCC-Tests'
|
|
10 |
!
|
|
11 |
|
|
12 |
SmaCCParser subclass:#TestParser
|
|
13 |
instanceVariableNames:''
|
|
14 |
classVariableNames:''
|
|
15 |
poolDictionaries:''
|
|
16 |
privateIn:ParserGeneratorTest
|
|
17 |
!
|
|
18 |
|
|
19 |
SmaCCScanner subclass:#TestScanner
|
|
20 |
instanceVariableNames:''
|
|
21 |
classVariableNames:''
|
|
22 |
poolDictionaries:''
|
|
23 |
privateIn:ParserGeneratorTest
|
|
24 |
!
|
|
25 |
|
|
26 |
|
|
27 |
!ParserGeneratorTest methodsFor:'initialize-release'!
|
|
28 |
|
|
29 |
tearDown
|
|
30 |
Class withoutUpdatingChangesDo:[
|
|
31 |
SmaCC::TestParser notNil ifTrue:[
|
|
32 |
self
|
|
33 |
removeAllSelectorsFrom: SmaCC::TestParser;
|
|
34 |
removeAllSelectorsFrom: SmaCC::TestParser class.
|
|
35 |
SmaCC::TestParser removeFromSystem.
|
|
36 |
].
|
|
37 |
SmaCC::TestScanner notNil ifTrue:[
|
|
38 |
self
|
|
39 |
removeAllSelectorsFrom: SmaCC::TestScanner;
|
|
40 |
removeAllSelectorsFrom: SmaCC::TestScanner class.
|
|
41 |
SmaCC::TestScanner removeFromSystem.
|
|
42 |
].
|
|
43 |
]
|
|
44 |
! !
|
|
45 |
|
|
46 |
!ParserGeneratorTest methodsFor:'private'!
|
|
47 |
|
|
48 |
removeAllSelectorsFrom: class
|
|
49 |
Smalltalk isSmalltalkX ifTrue:[
|
|
50 |
Class withoutUpdatingChangesDo:[
|
|
51 |
class selectors do: [:each | class removeSelector: each]
|
|
52 |
]
|
|
53 |
].
|
|
54 |
class selectors do: [:each | class removeSelector: each]
|
|
55 |
!
|
|
56 |
|
|
57 |
supportsUnicodeStrings
|
|
58 |
^
|
|
59 |
[String with: (Character value: 16rFF00).
|
|
60 |
true] on: Error
|
|
61 |
do: [:ex | ex return: false]
|
|
62 |
! !
|
|
63 |
|
|
64 |
!ParserGeneratorTest methodsFor:'public'!
|
|
65 |
|
|
66 |
expressionParserString
|
|
67 |
^'
|
|
68 |
%left "+" "-" ;
|
|
69 |
%left "*" "/" ;
|
|
70 |
%right "^" ;
|
|
71 |
|
|
72 |
Expression: Expression ''first'' "+" Expression ''last'' {first + last}
|
|
73 |
| Expression "-" Expression {''1'' - ''3''}
|
|
74 |
| Expression "*" Expression {''1'' * ''3''}
|
|
75 |
| Expression "/" Expression {''1'' / ''3''}
|
|
76 |
| Expression "^" Expression {''1'' raisedTo: ''3''}
|
|
77 |
| "(" Expression ")" {''2''}
|
|
78 |
| <number> {''1'' value asNumber};'
|
|
79 |
!
|
|
80 |
|
|
81 |
expressionScannerString
|
|
82 |
^'
|
|
83 |
<number> : [0-9]+ (\. [0-9]+) ? ;
|
|
84 |
<whitespace> : \s+;'
|
|
85 |
!
|
|
86 |
|
|
87 |
testBadCharacterBlock
|
|
88 |
| parserCompiler |
|
|
89 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
90 |
self should:
|
|
91 |
[parserCompiler buildScanner: '<foo> : [a-] ;' andParser: 'Start : <foo>;'.
|
|
92 |
parserCompiler compileInto: TestScanner andParser: TestParser]
|
|
93 |
raise: SmaCCParserError
|
|
94 |
!
|
|
95 |
|
|
96 |
testBadSmalltalkBlock
|
|
97 |
| parserCompiler |
|
|
98 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
99 |
self should:
|
|
100 |
[parserCompiler buildScanner: '<foo> : foo ;' andParser: 'Start : <foo> {self printOn: };'.
|
|
101 |
parserCompiler compileInto: TestScanner andParser: TestParser]
|
|
102 |
raise: SmaCCParserError
|
|
103 |
!
|
|
104 |
|
|
105 |
testBraceRepeatToken
|
|
106 |
| parserCompiler |
|
|
107 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
108 |
parserCompiler
|
|
109 |
buildScanner: '<LETTER> : [a-zA-Z_] ;
|
|
110 |
<DIGIT> : [0-9] ;
|
|
111 |
<IDENTIFIER>: <LETTER> (<LETTER>|<DIGIT>)* ;
|
|
112 |
<whitespace>: \s+ ;'
|
|
113 |
andParser: 'begin : <%<IDENTIFIER>%>;'.
|
|
114 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
115 |
parserCompiler
|
|
116 |
compileItemSetsComment;
|
|
117 |
compileSymbolComment.
|
|
118 |
self
|
|
119 |
assert: ((TestParser parse: 'a b c') collect: [:each | each value]) asArray
|
|
120 |
= #('a' 'b' 'c').
|
|
121 |
self assert: (TestParser parse: '') size = 0
|
|
122 |
|
|
123 |
"Modified: / 04-04-2006 / 12:23:02 / janfrog"
|
|
124 |
!
|
|
125 |
|
|
126 |
testBracketOptionalToken
|
|
127 |
| parserCompiler |
|
|
128 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
129 |
parserCompiler
|
|
130 |
buildScanner: '<LETTER> : [a-zA-Z_] ;
|
|
131 |
<DIGIT> : [0-9] ;
|
|
132 |
<IDENTIFIER>: <LETTER> (<LETTER>|<DIGIT>)* ;
|
|
133 |
<whitespace>: \s+ ;'
|
|
134 |
andParser: 'begin : <IDENTIFIER> [":"] begin {''1'' value , ''3''} | <IDENTIFIER> {''1'' value};'.
|
|
135 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
136 |
parserCompiler
|
|
137 |
compileDefinitionComments;
|
|
138 |
compileItemSetsComment;
|
|
139 |
compileSymbolComment.
|
|
140 |
self assert: (TestParser parse: 'a b : c') = 'abc'
|
|
141 |
!
|
|
142 |
|
|
143 |
testErrorToken
|
|
144 |
| parserCompiler countingBlock |
|
|
145 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
146 |
Smalltalk isSmalltalkX ifTrue:[
|
|
147 |
parserCompiler buildScanner: '<name> : [a-zA-Z]+; <whitespace>: \s+;'
|
|
148 |
andParser: '%left "+"; Expression : <name> | Expression "+" Expression { Notification raiseSignal } | error "+" Expression ;'.
|
|
149 |
] ifFalse:[
|
|
150 |
parserCompiler buildScanner: '<name> : [a-zA-Z]+; <whitespace>: \s+;'
|
|
151 |
andParser: '%left "+"; Expression : <name> | Expression "+" Expression {[Notification signal] on: Error do: [:ex | ex return: (Notification raise)]} | error "+" Expression ;'.
|
|
152 |
].
|
|
153 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
154 |
countingBlock :=
|
|
155 |
[:each |
|
|
156 |
| count |
|
|
157 |
count := 0.
|
|
158 |
|
|
159 |
[[TestParser parse: each] on: Notification
|
|
160 |
do:
|
|
161 |
[:ex |
|
|
162 |
count := count + 1.
|
|
163 |
[ex resume] on: Error
|
|
164 |
do:
|
|
165 |
[:ex1 |
|
|
166 |
ex isResumable: true.
|
|
167 |
ex1 retry]]]
|
|
168 |
on: SmaCCParserError
|
|
169 |
do: [:ex | ex return].
|
|
170 |
count].
|
|
171 |
#(#('a + b + c' 2) #('+ a + b + c' 2) #('a + b +' 1) #('a + 1 / 2 + b + c + 3' 2) #('1 + 1 / 2 + b + c + 3' 1))
|
|
172 |
do: [:each | self assert: (countingBlock value: each first) = each last]
|
|
173 |
!
|
|
174 |
|
|
175 |
testErrors
|
|
176 |
| parserCompiler position |
|
|
177 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
178 |
parserCompiler buildScanner: self expressionScannerString
|
|
179 |
andParser: self expressionParserString.
|
|
180 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
181 |
TestParser parse: '+3*4' onError: [:str :pos | position := pos].
|
|
182 |
Smalltalk isSmalltalkX ifTrue:[
|
|
183 |
self assert: position = (1 + PositionableStream zeroPosition).
|
|
184 |
] ifFalse:[
|
|
185 |
self assert: position = 1.
|
|
186 |
].
|
|
187 |
TestParser parseStream: (ReadStream on: '1+a+4')
|
|
188 |
onError: [:str :pos | position := pos].
|
|
189 |
Smalltalk isSmalltalkX ifTrue:[
|
|
190 |
self assert: position = (3 + PositionableStream zeroPosition).
|
|
191 |
] ifFalse:[
|
|
192 |
self assert: position = 3
|
|
193 |
]
|
|
194 |
!
|
|
195 |
|
|
196 |
testExpressions
|
|
197 |
#(#LR1 #LALR1) do:
|
|
198 |
[:each |
|
|
199 |
| parserCompiler |
|
|
200 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
201 |
parserCompiler grammar type: each.
|
|
202 |
parserCompiler buildScanner: self expressionScannerString
|
|
203 |
andParser: self expressionParserString.
|
|
204 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
205 |
self assert: (TestParser parse: '2 + 3 * 4 / 5 ^ 3 ^ 2')
|
|
206 |
= (2 + (3 * 4 / (5 raisedTo: (3 raisedTo: 2)))).
|
|
207 |
self assert: (TestParser parse: '1.0*2/3+4.0^2^3+3')
|
|
208 |
= (1.0 * 2 / 3 + (4.0 raisedTo: (2 raisedTo: 3)) + 3).
|
|
209 |
self assert: (TestParser parse: '(((1 + 2) * 3) ^ 2) ^ 3')
|
|
210 |
= (((1 + 2) * 3 raisedTo: 2) raisedTo: 3)]
|
|
211 |
!
|
|
212 |
|
|
213 |
testIgnoreCaseKeywords
|
|
214 |
| parserCompiler case |
|
|
215 |
case := SmaCCGrammar ignoreCase.
|
|
216 |
SmaCCGrammar ignoreCase: true.
|
|
217 |
|
|
218 |
[parserCompiler := SmaCCGrammarCompiler new.
|
|
219 |
parserCompiler
|
|
220 |
buildScanner: '<identifier> : [a-z_]+ ; <whitespace> : \s+ ;'
|
|
221 |
andParser: 'Start ::= "Foo" {true} | <identifier> <identifier> {false};'.
|
|
222 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
223 |
self assert: (TestParser parse: 'FOo').
|
|
224 |
self deny: (TestParser parse: 'FoObAr ASDS')]
|
|
225 |
ensure: [SmaCCGrammar ignoreCase: case]
|
|
226 |
!
|
|
227 |
|
|
228 |
testInvertedCharactersBlock
|
|
229 |
| parserCompiler |
|
|
230 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
231 |
parserCompiler
|
|
232 |
buildScanner: '<A> : a{1,3} ;
|
|
233 |
<ID> : [^a]+ ;
|
|
234 |
<comment> : \s+ ;'
|
|
235 |
andParser: 'begin : {0} | begin <A> {''1'' + 1} | begin <ID> {''1'' - 1};'.
|
|
236 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
237 |
self assert: (TestParser parse: 'aa') = 1.
|
|
238 |
self assert: (TestParser parse: 'aaaa') = 2.
|
|
239 |
self assert: (TestParser parse: 'a bab') = 0
|
|
240 |
!
|
|
241 |
|
|
242 |
testKeywords
|
|
243 |
| parserCompiler size case |
|
|
244 |
size := SmaCCGrammar maximumCharacterValue.
|
|
245 |
case := SmaCCGrammar ignoreCase.
|
|
246 |
SmaCCGrammar ignoreCase: true.
|
|
247 |
self supportsUnicodeStrings
|
|
248 |
ifTrue: [SmaCCGrammar maximumCharacterValue: (2 raisedTo: 16) - 1].
|
|
249 |
|
|
250 |
[parserCompiler := SmaCCGrammarCompiler new.
|
|
251 |
parserCompiler buildScanner: '' andParser: 'Start ::= "Foo" "bar";'.
|
|
252 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
253 |
TestParser parse: 'FoObAr']
|
|
254 |
ensure:
|
|
255 |
[SmaCCGrammar
|
|
256 |
ignoreCase: case;
|
|
257 |
maximumCharacterValue: size]
|
|
258 |
!
|
|
259 |
|
|
260 |
testLALRErrorHandler
|
|
261 |
| parserCompiler |
|
|
262 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
263 |
parserCompiler buildScanner: '<whitespace> : \s+ ;'
|
|
264 |
andParser: 'Start : Foo "]" Foo ")";
|
|
265 |
Foo : "a" Bar ;
|
|
266 |
Bar : "b" | error ;'.
|
|
267 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
268 |
self should: [TestParser parse: 'a ] a b )'] raise: SmaCCParserError.
|
|
269 |
self should: [TestParser parse: 'a ) a b )'] raise: SmaCCParserError.
|
|
270 |
self should: [TestParser parse: 'a b ] a ]'] raise: SmaCCParserError
|
|
271 |
!
|
|
272 |
|
|
273 |
testMultipleStartingPositions
|
|
274 |
| parserCompiler position |
|
|
275 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
276 |
parserCompiler buildScanner: '<name> : \w+;'
|
|
277 |
andParser: '%start plus ;
|
|
278 |
|
|
279 |
multiply: plus "*" plus {true};
|
|
280 |
plus: <name> {false} | <name> "+" <name> {false};'.
|
|
281 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
282 |
self assert: (TestParser parse: 'a*a').
|
|
283 |
self assert: (TestParser parse: 'a+a*a').
|
|
284 |
self should: [TestParser parse: 'a+a'] raise: SmaCCParserError.
|
|
285 |
self
|
|
286 |
deny: (TestParser parse: 'a+a' startingAt: TestParser startingStateForplus).
|
|
287 |
TestParser
|
|
288 |
parse: 'a*a'
|
|
289 |
startingAt: TestParser startingStateForplus
|
|
290 |
onError: [:str :pos | position := pos].
|
|
291 |
Smalltalk isSmalltalkX ifTrue:[
|
|
292 |
self assert: position = (2 + PositionableStream zeroPosition)
|
|
293 |
] ifFalse:[
|
|
294 |
self assert: position = 2
|
|
295 |
].
|
|
296 |
|
|
297 |
!
|
|
298 |
|
|
299 |
testNonAssociativeTokens
|
|
300 |
| parserCompiler |
|
|
301 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
302 |
parserCompiler
|
|
303 |
buildScanner: '<IDENTIFIER> : \w+ ; <whitespace> : \s+ ;'
|
|
304 |
andParser: '%left "and" ;
|
|
305 |
%nonassoc "=" "<=";
|
|
306 |
%left "+" ;
|
|
307 |
|
|
308 |
expr : expr "=" expr {true} | expr "<=" expr {true} | expr "and" expr {true} | expr "+" expr {true} | <IDENTIFIER> {true};'.
|
|
309 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
310 |
self should: [TestParser parse: 'a=b=c'] raise: SmaCCParserError.
|
|
311 |
self should: [TestParser parse: 'a=b<=c'] raise: SmaCCParserError.
|
|
312 |
self should: [TestParser parse: 'a=b+c=d'] raise: SmaCCParserError.
|
|
313 |
"/self assert: (TestParser parse: 'a=b and c=d')
|
|
314 |
|
|
315 |
"Modified: / 02-10-2006 / 10:13:38 / janfrog"
|
|
316 |
!
|
|
317 |
|
|
318 |
testOptionalToken
|
|
319 |
| parserCompiler |
|
|
320 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
321 |
parserCompiler
|
|
322 |
buildScanner: '<LETTER> : [a-zA-Z_] ;
|
|
323 |
<DIGIT> : [0-9] ;
|
|
324 |
<IDENTIFIER>: <LETTER> (<LETTER>|<DIGIT>)* ;
|
|
325 |
<whitespace>: \s+ ;'
|
|
326 |
andParser: 'begin : <IDENTIFIER> ":" ? begin {''1'' value , ''3''} | <IDENTIFIER> {''1'' value};'.
|
|
327 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
328 |
parserCompiler
|
|
329 |
compileItemSetsComment;
|
|
330 |
compileSymbolComment.
|
|
331 |
self assert: (TestParser parse: 'a b : c') = 'abc'
|
|
332 |
!
|
|
333 |
|
|
334 |
testParenthesesOptionalToken
|
|
335 |
| parserCompiler |
|
|
336 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
337 |
parserCompiler
|
|
338 |
buildScanner: '<LETTER> : [a-zA-Z_] ;
|
|
339 |
<DIGIT> : [0-9] ;
|
|
340 |
<IDENTIFIER>: <LETTER> (<LETTER>|<DIGIT>)* ;
|
|
341 |
<whitespace>: \s+ ;'
|
|
342 |
andParser: 'begin : <IDENTIFIER> (":" | "|") begin {''1'' value , ''3''} | <IDENTIFIER> {''1'' value};'.
|
|
343 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
344 |
parserCompiler
|
|
345 |
compileItemSetsComment;
|
|
346 |
compileSymbolComment.
|
|
347 |
self assert: (TestParser parse: 'a | b : c') = 'abc'
|
|
348 |
!
|
|
349 |
|
|
350 |
testPriority
|
|
351 |
| parserCompiler |
|
|
352 |
TestScanner
|
|
353 |
compile: 'all self recordAndReportMatch: (Array with: self allId)'.
|
|
354 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
355 |
parserCompiler buildScanner: '<a> : \{ | \< \% ;
|
|
356 |
<all> : .;'
|
|
357 |
andParser: '%id <all> <a>; begin : A {true}
|
|
358 |
| All {false};
|
|
359 |
A : <a> ;
|
|
360 |
All: <all>;'.
|
|
361 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
362 |
self assert: (TestParser parse: '{').
|
|
363 |
self assert: (TestParser parse: '<%').
|
|
364 |
self deny: (TestParser parse: '}')
|
|
365 |
!
|
|
366 |
|
|
367 |
testRecursiveRule
|
|
368 |
#(#LR1 #LALR1) do:
|
|
369 |
[:each |
|
|
370 |
| parserCompiler |
|
|
371 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
372 |
parserCompiler grammar type: each.
|
|
373 |
parserCompiler buildScanner: self expressionScannerString
|
|
374 |
andParser: 'Test: Test1 Recursive;
|
|
375 |
Test1: <number> {''1'' value};
|
|
376 |
Recursive: | Recursive "|" <number> {''1'' add: ''3'' value; yourself};'.
|
|
377 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
378 |
self
|
|
379 |
assert: ((parserCompiler grammar nonTerminalSymbolNamed: 'Recursive')
|
|
380 |
firstTerminals
|
|
381 |
includes: (parserCompiler grammar keywordTerminalNamed: '"|"')).
|
|
382 |
self
|
|
383 |
assert: (TestParser parse: '2|3|4') = (OrderedCollection
|
|
384 |
with: '2'
|
|
385 |
with: '3'
|
|
386 |
with: '4')]
|
|
387 |
!
|
|
388 |
|
|
389 |
testReduceReduceConflict
|
|
390 |
| parserCompiler |
|
|
391 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
392 |
parserCompiler buildScanner: '<letter> : \w ;
|
|
393 |
<whitespace>: \s+ ;'
|
|
394 |
andParser: 'begin : Foo <letter> {true} | Bar <letter> {false};
|
|
395 |
Foo : ;
|
|
396 |
Bar : ;'.
|
|
397 |
self
|
|
398 |
assert: (
|
|
399 |
[parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
400 |
false]
|
|
401 |
on: SmaCCCompilationNotification
|
|
402 |
do:
|
|
403 |
[:ex |
|
|
404 |
('*Reduce/Reduce*' match: ex messageText)
|
|
405 |
ifTrue: [ex return: true]
|
|
406 |
ifFalse: [ex resume: nil]])
|
|
407 |
!
|
|
408 |
|
|
409 |
testRepeatMultipleToken
|
|
410 |
| parserCompiler |
|
|
411 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
412 |
parserCompiler
|
|
413 |
buildScanner: '<LETTER> : [a-zA-Z_] ;
|
|
414 |
<DIGIT> : [0-9] ;
|
|
415 |
<IDENTIFIER>: <LETTER> (<LETTER>|<DIGIT>){1,2} ;
|
|
416 |
<whitespace>: \s+ ;'
|
|
417 |
andParser: 'begin : <IDENTIFIER> + ;'.
|
|
418 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
419 |
parserCompiler
|
|
420 |
compileItemSetsComment;
|
|
421 |
compileSymbolComment.
|
|
422 |
self
|
|
423 |
assert: ((TestParser parse: 'a1 bb c44') collect: [:each | each value])
|
|
424 |
asArray = #('a1' 'bb' 'c44').
|
|
425 |
self
|
|
426 |
assert: ((TestParser parse: 'aaaaa') collect: [:each | each value]) asArray
|
|
427 |
= #('aaa' 'aa').
|
|
428 |
self should: [TestParser parse: ''] raise: SmaCCParserError
|
|
429 |
!
|
|
430 |
|
|
431 |
testRepeatToken
|
|
432 |
| parserCompiler |
|
|
433 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
434 |
parserCompiler
|
|
435 |
buildScanner: '<LETTER> : [a-zA-Z_] ;
|
|
436 |
<DIGIT> : [0-9] ;
|
|
437 |
<IDENTIFIER>: <LETTER> (<LETTER>|<DIGIT>)* ;
|
|
438 |
<whitespace>: \s+ ;'
|
|
439 |
andParser: 'begin : <IDENTIFIER> * ;'.
|
|
440 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
441 |
parserCompiler
|
|
442 |
compileItemSetsComment;
|
|
443 |
compileSymbolComment.
|
|
444 |
self
|
|
445 |
assert: ((TestParser parse: 'a b c') collect: [:each | each value]) asArray
|
|
446 |
= #('a' 'b' 'c').
|
|
447 |
self assert: (TestParser parse: '') size = 0
|
|
448 |
!
|
|
449 |
|
|
450 |
testRepeatingCharactersBlock
|
|
451 |
| parserCompiler |
|
|
452 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
453 |
parserCompiler
|
|
454 |
buildScanner: '<A> : a{1,3} ;
|
|
455 |
<ID> : \w+ ;
|
|
456 |
<whitespace> : \s+ ;'
|
|
457 |
andParser: 'begin : {true} | begin <A> {''1''} | begin <ID> {false};'.
|
|
458 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
459 |
self assert: (TestParser parse: 'aa').
|
|
460 |
self deny: (TestParser parse: 'aaaa')
|
|
461 |
!
|
|
462 |
|
|
463 |
testReuseCompositeToken
|
|
464 |
| parserCompiler |
|
|
465 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
466 |
parserCompiler
|
|
467 |
buildScanner: '<LETTERDIGIT> : [a-zA-Z_] \d ;
|
|
468 |
<AAorBB> : aa | bb;
|
|
469 |
<IDENTIFIER>: <LETTERDIGIT> | <AAorBB> ;
|
|
470 |
<whitespace>: \s+ ;'
|
|
471 |
andParser: 'begin : <IDENTIFIER> begin {false} | <IDENTIFIER> {true};'.
|
|
472 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
473 |
self should: [TestParser parse: '2'] raise: SmaCCParserError.
|
|
474 |
self assert: (TestParser parse: 'aa').
|
|
475 |
self assert: (TestParser parse: 'bb').
|
|
476 |
self assert: (TestParser parse: 'a4').
|
|
477 |
self should: [TestParser parse: 'ab'] raise: SmaCCParserError.
|
|
478 |
self should: [TestParser parse: '4b'] raise: SmaCCParserError
|
|
479 |
!
|
|
480 |
|
|
481 |
testReuseSimpleToken
|
|
482 |
| parserCompiler |
|
|
483 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
484 |
parserCompiler
|
|
485 |
buildScanner: '<LETTER> : [a-zA-Z_\\] ;
|
|
486 |
<DIGIT> : [0-9] ;
|
|
487 |
<IDENTIFIER>: <LETTER> (<LETTER>|<DIGIT>)* ;
|
|
488 |
<whitespace>: \s+ ;'
|
|
489 |
andParser: 'begin : <IDENTIFIER> begin {false} | <IDENTIFIER> {true};'.
|
|
490 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
491 |
self should: [TestParser parse: '2'] raise: SmaCCParserError.
|
|
492 |
self assert: (TestParser parse: '\')
|
|
493 |
!
|
|
494 |
|
|
495 |
testSimple
|
|
496 |
| parserCompiler |
|
|
497 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
498 |
parserCompiler buildScanner: '<a> : a ;' andParser: 'begin : <a> {true};'.
|
|
499 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
500 |
self should: [TestParser parse: '2'] raise: SmaCCParserError.
|
|
501 |
self assert: (TestParser parse: 'a')
|
|
502 |
!
|
|
503 |
|
|
504 |
testSpecialCharacters
|
|
505 |
| parserCompiler |
|
|
506 |
[String with: (Character value: 16rFF00)] on: Error do: [:ex | ^self]. "Unicode characters not supported"
|
|
507 |
parserCompiler := SmaCCGrammarCompiler new.
|
|
508 |
parserCompiler buildScanner: '<name> : \x3g \x20 \xFF \xFF1 \xFF00 \cC;'
|
|
509 |
andParser: 'begin ::= <name> {true};'.
|
|
510 |
parserCompiler compileInto: TestScanner andParser: TestParser.
|
|
511 |
self assert: (TestParser
|
|
512 |
parse: (String withAll: (#(3 103 16r20 16rFF 16rFF1 16rFF00 2)
|
|
513 |
collect: [:each | Character value: each])))
|
|
514 |
! !
|
|
515 |
|
2
|
516 |
!ParserGeneratorTest::TestParser class methodsFor:'documentation'!
|
1
|
517 |
|
2
|
518 |
version
|
|
519 |
^'$Id$'
|
1
|
520 |
! !
|
|
521 |
|
2
|
522 |
!ParserGeneratorTest::TestScanner class methodsFor:'documentation'!
|
1
|
523 |
|
2
|
524 |
version
|
|
525 |
^'$Id$'
|
1
|
526 |
! !
|
|
527 |
|
|
528 |
!ParserGeneratorTest::TestScanner methodsFor:'others'!
|
|
529 |
|
|
530 |
all self recordAndReportMatch: (Array with: self allId)
|
2
|
531 |
|
|
532 |
"Modified: / 08-06-2008 / 10:04:37 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
1
|
533 |
! !
|
|
534 |
|
|
535 |
!ParserGeneratorTest class methodsFor:'documentation'!
|
|
536 |
|
|
537 |
version
|
|
538 |
^ '$Header: /opt/data/cvs/stx/goodies/smaCC/SmaCC__ParserGeneratorTest.st,v 1.3 2006-10-02 08:14:37 vranyj1 Exp $'
|
15
|
539 |
!
|
|
540 |
|
|
541 |
version_SVN
|
|
542 |
^ '$Id$'
|
1
|
543 |
! !
|