compiler/tests/PPCCompilerTest.st
changeset 438 20598d7ce9fa
parent 422 116d2b2af905
child 452 9f4558b3be66
equal deleted inserted replaced
437:54b3bc9e3987 438:20598d7ce9fa
     1 "{ Package: 'stx:goodies/petitparser/compiler/tests' }"
     1 "{ Package: 'stx:goodies/petitparser/compiler/tests' }"
     2 
     2 
     3 "{ NameSpace: Smalltalk }"
     3 "{ NameSpace: Smalltalk }"
     4 
     4 
     5 PPAbstractParserTest subclass:#PPCCompilerTest
     5 PPAbstractParserTest subclass:#PPCCompilerTest
     6 	instanceVariableNames:'parser result context'
     6 	instanceVariableNames:'parser result context node compiler id node2 id2 id1 node1 node3
       
     7 		arguments configuration'
     7 	classVariableNames:''
     8 	classVariableNames:''
     8 	poolDictionaries:''
     9 	poolDictionaries:''
     9 	category:'PetitCompiler-Tests-Core'
    10 	category:'PetitCompiler-Tests-Core'
    10 !
    11 !
    11 
    12 
    12 
    13 
    13 !PPCCompilerTest methodsFor:'context'!
    14 !PPCCompilerTest methodsFor:'as yet unclassified'!
       
    15 
       
    16 assert: p parse: whatever
       
    17 	^ result := super assert: p parse: whatever.
       
    18 !
    14 
    19 
    15 context	
    20 context	
    16 	^ context := PPCProfilingContext new
    21 	^ context := PPCProfilingContext new
    17 ! !
    22 !
    18 
    23 
    19 !PPCCompilerTest methodsFor:'test support'!
    24 setUp
    20 
    25 	arguments := PPCArguments default
    21 assert: p parse: whatever
    26 		profile: true;
    22 	^ result := super assert: p parse: whatever.
    27 		yourself.
    23 !
    28 		
    24 
    29 	configuration := PPCFirstPrototype new
    25 compile: aPPParser
    30 		arguments: arguments;
    26 	| compiler |
    31 		yourself.
    27 	compiler := PPCCompiler new.
       
    28 	compiler profile: true.
       
    29 	^ (compiler compile: aPPParser as: #PPGeneratedParser) new.
       
    30 !
       
    31 
       
    32 compile: aPPParser params: params
       
    33 	| compiler |
       
    34 	compiler := PPCCompiler new.
       
    35 	compiler profile: true.
       
    36 	^ (compiler compile: aPPParser as: #PPGeneratedParser params: params) new.
       
    37 !
       
    38 
       
    39 compileInlining: aPPParser
       
    40 	| compiler |
       
    41 	compiler := PPCCompiler new.
       
    42 	compiler inlining: true.
       
    43 	compiler profile: true.
       
    44 	^ (compiler compile: aPPParser as: #PPGeneratedParser) new.
       
    45 !
       
    46 
       
    47 compileTree: tree params: params
       
    48 	| compiler mock |
       
    49 	compiler := PPCCompiler new.
       
    50 	compiler profile: true.
       
    51 	mock := nil asParser.
       
    52 	^ (compiler compileTree: tree as: #PPGeneratedParser parser: mock params: params) new.
       
    53 !
       
    54 
       
    55 parse: whatever
       
    56 	^ result := super parse: whatever.
       
    57 !
    32 !
    58 
    33 
    59 tearDown
    34 tearDown
    60 	| parserClass |
    35 	| parserClass |
    61 
    36 
    62 	parserClass := (Smalltalk at: #PPGeneratedParser ifAbsent: [nil]).
    37 	parserClass := (Smalltalk at: #PPGeneratedParser ifAbsent: [nil]).
    63 	parserClass notNil ifTrue:[ 
    38 	parserClass notNil ifTrue:[ 
    64 		parserClass removeFromSystem
    39 		parserClass removeFromSystem
    65 	].
    40 	].
    66 ! !
       
    67 
       
    68 !PPCCompilerTest methodsFor:'tests - compiling'!
       
    69 
       
    70 testCompileAnd
       
    71 	parser := #digit asParser and compile.
       
    72 	
       
    73 	self assert: parser parse: '1' to: $1 end: 0.
       
    74 	self assert: parser fail: 'a'.
       
    75 	self assert: parser fail: ''.
       
    76 
       
    77 	parser := ('foo' asParser, ($: asParser and)) compile.
       
    78 	self assert: parser parse: 'foo:' to: { 'foo'. $: } end: 3.
       
    79 !
       
    80 
       
    81 testCompileAny
       
    82 	parser := #any asParser compile.
       
    83 	
       
    84 	self assert: parser parse: 'a' to: $a.
       
    85 	self assert: parser parse: '_' to: $_.
       
    86 	self assert: parser parse: '
       
    87 ' to: Character cr.
       
    88 !
       
    89 
       
    90 testCompileAnyStar
       
    91 	parser := #any asParser star compile.
       
    92 	
       
    93 	self assert: parser parse: 'aaa' to: { $a. $a . $a }.
       
    94 	self assert: parser parse: '' to: { }.
       
    95 	
       
    96 !
       
    97 
       
    98 testCompileBlock
       
    99 	parser := (#letter asParser) plus ==> [ :res | res collect: [:each | each asUppercase ]].
       
   100 	parser := parser compile.
       
   101 	
       
   102 	self assert: parser parse: 'foo' to: { $F . $O . $O}.
       
   103 	self assert: parser parse: 'bar' to: { $B . $A . $R}.
       
   104 	self assert: parser fail: ''.
       
   105 !
       
   106 
       
   107 testCompileCharacter
       
   108 	parser := $a asParser compile.
       
   109 	
       
   110 	self assert: parser parse: 'a'  to: $a.
       
   111 	self assert: parser fail: 'b'.
       
   112 
       
   113 	parser := $# asParser compile.
       
   114 	self assert: parser parse: '#'.
       
   115 !
       
   116 
       
   117 testCompileChoice
       
   118 	parser := (#digit asParser / #letter asParser) compile.
       
   119 	
       
   120 	self assert: parser parse: '1' to: $1.
       
   121 	self assert: parser parse: 'a' to: $a.
       
   122 	self assert: parser fail: '_'.
       
   123 	
       
   124 !
       
   125 
       
   126 testCompileLiteral
       
   127 	parser := 'foo' asParser compile.
       
   128 	
       
   129 	self assert: parser parse: 'foo'  to: 'foo'.
       
   130 	self assert: parser parse: 'foobar'  to: 'foo' end: 3.
       
   131 	self assert: parser fail: 'boo'.
       
   132 	
       
   133 	parser := '#[' asParser compile.
       
   134 	self assert: parser parse: '#[1]' to: '#[' end: 2.
       
   135 !
       
   136 
       
   137 testCompileLiteral2
       
   138 	| quote |
       
   139 	quote := '''' asParser.
       
   140 	parser := (quote, $a asParser )compile: #PPCompilerTest.	
       
   141 	self assert: parser parse: '''a'  to: {'''' . $a}.	
       
   142 !
       
   143 
       
   144 testCompileNegate
       
   145 	parser := #letter asParser negate star, #letter asParser.
       
   146 	parser := parser compile.
       
   147 	
       
   148 	self assert: parser parse: '...a' to: { { $. . $. . $. } . $a }.
       
   149 	self assert: parser parse: 'aaa' to: { {} . $a } end: 1.
       
   150 	self assert: parser fail: '...'.
       
   151 !
       
   152 
       
   153 testCompileNil
       
   154 	parser := nil asParser compile.
       
   155 	
       
   156 	self assert: parser parse: 'a' to: nil end: 0.
       
   157 	self assert: parser parse: '' to: nil end: 0.
       
   158 	
       
   159 	parser := nil asParser, 'foo' asParser.
       
   160 	self assert: parser parse: 'foo' to: { nil . 'foo' }
       
   161 !
       
   162 
       
   163 testCompileNot
       
   164 	parser := #digit asParser not compile.
       
   165 	
       
   166 	self assert: parser parse: 'a' to: nil end: 0.
       
   167 	self assert: parser fail: '1'.
       
   168 	self assert: parser parse: '' to: nil end: 0.
       
   169 
       
   170 	parser := 'foo' asParser, $: asParser not.
       
   171 	parser := parser compile: #PPCompilerTest.	
       
   172 	self assert: parser parse: 'foo' to: { 'foo'. nil } end: 3.
       
   173 	
       
   174 	parser := 'foo' asParser, $: asParser not, 'bar' asParser.
       
   175 	parser := parser compile: #PPCompilerTest.	
       
   176 	self assert: parser parse: 'foobar' to: { 'foo'. nil . 'bar' } end: 6.
       
   177 !
       
   178 
       
   179 testCompileNot2
       
   180 	parser := ($a asParser, $b asParser) not compile.
       
   181 		
       
   182 	self assert: parser parse: '' to: nil end: 0.
       
   183 	self assert: parser parse: 'a' to: nil end: 0.
       
   184 	self assert: parser parse: 'aa' to: nil end: 0.
       
   185 	self assert: parser fail: 'ab'.
       
   186 !
       
   187 
       
   188 testCompileNot3
       
   189 	parser := ('foo' asParser not, 'fee' asParser) compile.
       
   190 		
       
   191 	self assert: parser parse: 'fee' to: #(nil 'fee').
       
   192 	self assert: parser fail: 'foo'.
       
   193 !
       
   194 
       
   195 testCompileNotLiteral
       
   196 	parser := 'foo' asParser not compile.
       
   197 	self assert: parser class methodDictionary size = 2.
       
   198 
       
   199 	self assert: parser parse: 'bar' to: nil end: 0.
       
   200 		
       
   201 	self assert: parser fail: 'foo'.
       
   202 	self assert: parser parse: '' to: nil end: 0.
       
   203 
       
   204 	parser := '''' asParser not compile.
       
   205 	self assert: parser class methodDictionary size = 2.
       
   206 
       
   207 	self assert: parser parse: 'a' to: nil end: 0.
       
   208 	self assert: parser fail: ''''.
       
   209 	self assert: parser parse: '' to: nil end: 0.
       
   210 
       
   211 
       
   212 	parser := ('foo' asParser, 'bar' asParser not) compile.
       
   213 	self assert: parser parse: 'foofoo' to: { 'foo'. nil } end: 3.
       
   214 	
       
   215 	parser := ('foo' asParser, 'foo' asParser not, #any asParser star) compile.
       
   216 	self assert: parser parse: 'foobar' to: { 'foo'. nil . #($b $a $r) } end: 6.
       
   217 	self assert: parser fail: 'foofoo'.
       
   218 !
       
   219 
       
   220 testCompileOptional
       
   221 	parser := #digit asParser optional compile.
       
   222 	
       
   223 	self assert: parser parse: '1' to: $1.
       
   224 	self assert: parser parse: 'a' to: nil end: 0.
       
   225 	self assert: parser class parsers isEmpty.
       
   226 	
       
   227 	parser := (#digit asParser optional, #letter asParser) compile.
       
   228 	self assert: parser parse: '1a' to: { $1 . $a }.
       
   229 	self assert: parser parse: 'a' to: { nil . $a }.
       
   230 	self assert: parser class parsers isEmpty.
       
   231 !
       
   232 
       
   233 testCompilePlus
       
   234 	parser := #letter asParser plus compile: #PPCompilerTest.
       
   235 	
       
   236 	self assert: parser parse: 'lorem' to: {$l. $o. $r. $e. $m} .
       
   237 	self assert: parser parse: 'a123' to: {$a} end: 1.
       
   238 	self assert: parser parse: 'ab123' to: {$a . $b} end: 2.
       
   239 
       
   240 	self assert: parser fail: ''.
       
   241 	self assert: parser fail: '123'.
       
   242 !
       
   243 
       
   244 testCompilePredicate
       
   245 	parser := #digit asParser compile.
       
   246 	
       
   247 	self assert: parser parse: '1' to: $1.
       
   248 	self assert: parser parse: '0' to: $0.
       
   249 	self assert: parser fail: 'a'.
       
   250 !
       
   251 
       
   252 testCompilePredicate2
       
   253 	parser := #space asParser compile.
       
   254 	
       
   255 	self assert: parser parse: ' ' to: Character space.
       
   256 	self assert: parser fail: 'a'.
       
   257 !
       
   258 
       
   259 testCompileSequence
       
   260 	parser := (#digit asParser, #letter asParser) compile.
       
   261 	
       
   262 	self assert: parser parse: '1a' to: {$1 .$a}.
       
   263 	
       
   264 	
       
   265 !
       
   266 
       
   267 testCompileSequence2
       
   268 	parser := (#digit asParser, #space asParser, #letter asParser) compile: #PPCompilerTest.
       
   269 	
       
   270 	self assert: parser parse: '9 c' to: {$9 . Character space. $c }.	
       
   271 	self assert: parser fail: '9c'.
       
   272 	
       
   273 !
       
   274 
       
   275 testCompileSequence3
       
   276 	parser := (#any asParser, #any asParser, #any asParser) compile.
       
   277 	
       
   278 	self assert: parser parse: 'foo' to: #($f $o $o).	
       
   279 	self assert: parser fail: 'fo'.
       
   280 	
       
   281 !
       
   282 
       
   283 testCompileStar
       
   284 	parser := #letter asParser star compile.
       
   285 	
       
   286 	self assert: parser parse: 'lorem' to: {$l. $o. $r. $e. $m} .
       
   287 	self assert: parser parse: '' to: {}.
       
   288 	self assert: parser parse: '123' to: {} end: 0.
       
   289 	self assert: parser parse: 'ab123' to: {$a . $b} end: 2.
       
   290 !
       
   291 
       
   292 testCompileStarLiteral
       
   293 	parser := 'foo' asParser star compile.
       
   294 	
       
   295 	self assert: parser parse: 'foo' to: #('foo' ) .
       
   296 	self assert: parser parse: 'foofoo' to: #('foo' 'foo') .
       
   297 	self assert: parser parse: 'foofoofoo' to: #('foo' 'foo' 'foo') .
       
   298 	self assert: parser parse: '' to: #().
       
   299 	self assert: parser parse: 'bar' to: #() end: 0.
       
   300 !
       
   301 
       
   302 testCompileStarPredicate
       
   303 	parser := #letter asParser star compile.
       
   304 	
       
   305 	self assert: parser parse: 'foo' to: #($f $o $o ) .
       
   306 	self assert: parser parse: '' to: #().
       
   307 	self assert: parser parse: '123' to: #() end: 0.
       
   308 !
       
   309 
       
   310 testCompileSymbolBlock
       
   311 	parser := (#letter asParser) plus ==> #second.
       
   312 	parser := parser compile.
       
   313 	
       
   314 	self assert: parser parse: 'foo' to: $o.
       
   315 	self assert: parser parse: 'bar' to: $a.
       
   316 	self assert: parser fail: ''.
       
   317 	self should: [ parser parse: 'f' ] raise: Error.
       
   318 !
       
   319 
       
   320 testCompileTrimmingToken
       
   321 	| token1 token2 |
       
   322 	token1 := (#letter asParser) plus trimmingToken.
       
   323 	token2 := (#letter asParser) plus trimmingToken.
       
   324 	
       
   325 	parser := (token1, token2) compile.
       
   326 	
       
   327 	self assert: parser parse: 'foo bar'.
       
   328 	self assert: parser parse: ' foo bar '.
       
   329 !
       
   330 
       
   331 testCompileTrimmingToken2
       
   332 	| token1 token2 |
       
   333 	token1 := (#letter asParser) plus trimmingToken.
       
   334 	token2 := (#letter asParser) plus trimmingToken / 'foo' asParser trimmingToken.
       
   335 	
       
   336 	parser := (token1, token2) compile.
       
   337 	
       
   338 	self assert: parser parse: 'foo bar'.
       
   339 	self assert: parser parse: ' foo bar '.
       
   340 !
       
   341 
       
   342 testTrim
       
   343 	parser := self compile: $a asParser trim.
       
   344 	
       
   345 	self assert: parser fail: ''.
       
   346 	self assert: parser parse: 'a' to: $a.
       
   347 	self assert: parser parse: '   a' to: $a.
       
   348 	self assert: parser parse: 'a    ' to: $a.
       
   349 	self assert: parser parse: '  a    ' to: $a.
       
   350 ! !
       
   351 
       
   352 !PPCCompilerTest methodsFor:'tests - extra'!
       
   353 
       
   354 testCompileSmalltalkToken
       
   355 	parser := (#letter asParser, ((#letter asParser / #digit asParser) star)) smalltalkToken compile.
       
   356 	
       
   357 	self assert: parser parse: 'foo'.
       
   358 	self assert: result inputValue = 'foo'.
       
   359 	self assert: parser parse: 'a'.
       
   360 	self assert: result inputValue = 'a'.
       
   361 	self assert: parser parse: 'f123a'.
       
   362 	self assert: result inputValue = 'f123a'.
       
   363 	
       
   364 	self assert: parser fail: ''.
       
   365 	self assert: parser fail: '12'.
       
   366 
       
   367 	self assert: parser parse: ' "comment" foo'.
       
   368 	self assert: result inputValue = 'foo'.
       
   369 	
       
   370 	self assert: parser parse: ' "comment" bar "another comment" '.
       
   371 	self assert: result inputValue = 'bar'.
       
   372 	self assert: parser parse: '
       
   373 		"b"
       
   374 		"b"
       
   375 		foo
       
   376 		"and yet, another comment"
       
   377 
       
   378 		"one more to make sure :)"
       
   379 	'.
       
   380 	self assert: result inputValue = 'foo'.
       
   381 !
       
   382 
       
   383 testCycle
       
   384 	| p1 block |
       
   385 	
       
   386 	p1 := PPDelegateParser new.
       
   387 	block := ${ asParser, p1, $} asParser / nil asParser.
       
   388 	p1 setParser: block.
       
   389 	
       
   390 	parser := block compile.
       
   391 	self assert: parser parse: '{}' to: { ${. nil . $} }.
       
   392 	self assert: parser parse: '{{}}' to: { ${. { ${ . nil . $} } . $} }.
       
   393 	
       
   394 !
       
   395 
       
   396 testSmalltalkToken
       
   397 	parser := (#letter asParser, (#digit asParser / #letter asParser) star) smalltalkToken compileWithParameters: {#profile -> true}.
       
   398 	
       
   399 	self assert: parser class methodDictionary size = 6.
       
   400 	self assert: parser parse: 'foo'.
       
   401 	self assert: result inputValue = 'foo'.
       
   402 	self assert: context invocationCount = 9.
       
   403 	self assert: context rememberCount = 0.
       
   404 	self assert: context lwRememberCount = 1.
       
   405 	self assert: context lwRestoreCount = 0.	
       
   406 !
       
   407 
       
   408 testSmalltalkToken2
       
   409 	|id|
       
   410 	id := (#letter asParser, (#digit asParser / #letter asParser) star)
       
   411 		name: 'identifier';
       
   412 		yourself.
       
   413 		
       
   414 	parser := (id wrapped, $: asParser) smalltalkToken 
       
   415 		name: 'kw';
       
   416 		yourself.
       
   417 	
       
   418 	parser := parser compileWithParameters: {#profile -> true}.
       
   419 	
       
   420 	self assert: parser parse: 'foo:'.
       
   421 	self assert: result inputValue = 'foo:'.
       
   422 !
       
   423 
       
   424 testToken
       
   425 	parser := (#letter asParser, (#digit asParser / #letter asParser) star) flatten compile.
       
   426 	
       
   427 	self assert: parser parse: 'foo' to: 'foo'.
       
   428 	self assert: parser parse: 'a' to: 'a'.
       
   429 	self assert: parser parse: 'f123a' to: 'f123a'.
       
   430 	self assert: parser fail: ''.
       
   431 !
       
   432 
       
   433 testToken2
       
   434 	parser := (#letter asParser, (#digit asParser / #letter asParser) star) token compileWithParameters: {#profile -> true}.
       
   435 	
       
   436 	self assert: parser class methodDictionary size = 5.
       
   437 	self assert: parser parse: 'foo'.
       
   438 	self assert: result inputValue = 'foo'.
       
   439 	self assert: context invocationCount = 7.
       
   440 	self assert: context rememberCount = 0.
       
   441 	self assert: context lwRememberCount = 1.
       
   442 	self assert: context lwRestoreCount = 0.	
       
   443 !
       
   444 
       
   445 testTrimmingToken
       
   446 	parser := (#letter asParser, (#digit asParser / #letter asParser) star) trimmingToken compileWithParameters: { #profile -> true }.
       
   447 
       
   448 	self assert: parser class methodDictionary size = 5.
       
   449 
       
   450 	self assert: parser parse: 'foo'.
       
   451 	self assert: result inputValue = 'foo'.
       
   452 
       
   453 	self assert: context invocationCount = 7.
       
   454 	self assert: context rememberCount = 0.
       
   455 	self assert: context lwRememberCount = 1.
       
   456 	self assert: context lwRestoreCount = 0.	
       
   457 
       
   458 	self assert: parser parse: ' foo '.
       
   459 	self assert: result inputValue = 'foo'.
       
   460 
       
   461 
       
   462 
       
   463 	self assert: parser fail: '123'.
       
   464 
       
   465 	self assert: context invocationCount = 2.
       
   466 	self assert: context rememberCount = 0.
       
   467 	self assert: context lwRememberCount = 0.
       
   468 	self assert: context lwRestoreCount = 0.	
       
   469 
       
   470 
       
   471 	self assert: parser fail: ''.
       
   472 !
       
   473 
       
   474 testTrimmingTokenNested
       
   475 	| identifier kw |
       
   476 	kw := 'false' asParser trimmingToken name: #kw.
       
   477 	identifier := (kw not, (#letter asParser, #word asParser star)) trimmingToken name: #identifier.
       
   478 	
       
   479 	parser := identifier / kw.
       
   480 	parser := parser compileWithParameters: { #profile -> true }.
       
   481 	self assert: parser class methodDictionary size = 6.
       
   482 
       
   483 	self assert: parser parse: 'foo'.
       
   484 	self assert: result inputValue = 'foo'.
       
   485 
       
   486 	self assert: parser parse: 'false'.
       
   487 	self assert: result inputValue = 'false'.
       
   488 !
       
   489 
       
   490 testTrimmingTokenNested2
       
   491 	| identifier kw |
       
   492 	kw := 'false' asParser trimmingToken name: #kw.
       
   493 	identifier := (kw not, (#letter asParser, #word asParser star)) trimmingToken name: #identifier.
       
   494 	
       
   495 	parser := identifier / kw.
       
   496 	parser := parser compileWithParameters: { #profile -> true }.
       
   497 	self assert: parser class methodDictionary size = 6.
       
   498 
       
   499 	self assert: parser parse: 'foo'.
       
   500 	self assert: result inputValue = 'foo'.
       
   501 
       
   502 	self assert: parser parse: 'false'.
       
   503 	self assert: result inputValue = 'false'.
       
   504 !
       
   505 
       
   506 testTrimmingTokenNested3
       
   507 	| identifier kw |
       
   508 	kw := ('false' asParser, #word asParser not) trimmingToken name: #kw.
       
   509 	identifier := (kw not, (#letter asParser, #word asParser star)) trimmingToken name: #identifier.
       
   510 	
       
   511 	parser := identifier / kw.
       
   512 	parser := parser compileWithParameters: { #profile -> true }.
       
   513 	self assert: parser class methodDictionary size = 9.
       
   514 	self assert: (parser class methods anySatisfy: [ :m | m selector = #kw ]).
       
   515 	self assert: (parser class methods anySatisfy: [ :m | m selector = #kw_fast ]).
       
   516 
       
   517 	self assert: parser parse: 'foo'.
       
   518 	self assert: result inputValue = 'foo'.
       
   519 
       
   520 	self assert: parser parse: 'false'.
       
   521 	self assert: result inputValue = 'false'.
       
   522 ! !
    41 ! !
   523 
    42 
   524 !PPCCompilerTest methodsFor:'tests - first set'!
    43 !PPCCompilerTest methodsFor:'tests - first set'!
   525 
    44 
   526 testFirstSetSuchThat
    45 testFirstSetSuchThat
   567 
    86 
   568 !PPCCompilerTest methodsFor:'tests - guard'!
    87 !PPCCompilerTest methodsFor:'tests - guard'!
   569 
    88 
   570 testChoiceGuard
    89 testChoiceGuard
   571 	parser := ('foo' asParser trimmingToken / 'bar' asParser trimmingToken / $d asParser trimmingToken plus) 
    90 	parser := ('foo' asParser trimmingToken / 'bar' asParser trimmingToken / $d asParser trimmingToken plus) 
   572 		compileWithParameters: {#profile -> true}.
    91 		compileWithConfiguration: configuration.
   573 	
    92 	
   574 	self assert: parser parse: 'foo'.
    93 	self assert: parser parse: 'foo'.
   575 	self assert: result inputValue = 'foo'.	
    94 	self assert: result inputValue = 'foo'.	
   576 	self assert: (context invocations anySatisfy: [ :e | e beginsWith: 'token' ]).
    95 	self assert: (context invocations anySatisfy: [ :e | e beginsWith: 'token' ]).
   577 
    96 
   592 	self assert: (context invocations noneSatisfy: [ :e | e beginsWith: 'token' ]).
   111 	self assert: (context invocations noneSatisfy: [ :e | e beginsWith: 'token' ]).
   593 !
   112 !
   594 
   113 
   595 testEmptyChoiceGuard
   114 testEmptyChoiceGuard
   596 	parser := ('foo' asParser trimmingToken / 'bar' asParser trimmingToken / $d asParser trimmingToken star) 
   115 	parser := ('foo' asParser trimmingToken / 'bar' asParser trimmingToken / $d asParser trimmingToken star) 
   597 		compileWithParameters: {#profile -> true}.
   116 		compileWithConfiguration: configuration.
   598 	
   117 	
   599 	self assert: parser parse: 'foo'.
   118 	self assert: parser parse: 'foo'.
   600 	self assert: result inputValue = 'foo'.	
   119 	self assert: result inputValue = 'foo'.	
   601 
   120 
   602 	self assert: parser parse: 'bar'.
   121 	self assert: parser parse: 'bar'.
   612 
   131 
   613 	self assert: parser parse: 'zorg' end: 0.	
   132 	self assert: parser parse: 'zorg' end: 0.	
   614 !
   133 !
   615 
   134 
   616 testGuardSmalltlakToken
   135 testGuardSmalltlakToken
   617 	parser := (#letter asParser, #word asParser star) smalltalkToken compileWithParameters: { #profile -> true }.
   136 	parser := (#letter asParser, #word asParser star) smalltalkToken compileWithConfiguration: configuration.
       
   137 	
   618 	self assert: parser parse: 'bar'.
   138 	self assert: parser parse: 'bar'.
   619 	self assert: (context invocations anySatisfy: [ :e | e beginsWith: 'seq' ]).
   139 	self assert: (context invocations anySatisfy: [ :e | e beginsWith: 'seq' ]).
   620 	
   140 	
   621 	self assert: parser fail: '123'.
   141 	self assert: parser fail: '123'.
   622 	self assert: (context invocations noneSatisfy: [ :e | e beginsWith: 'seq' ]).
   142 	self assert: (context invocations noneSatisfy: [ :e | e beginsWith: 'seq' ]).
   623 !
   143 !
   624 
   144 
   625 testSequenceGuard
   145 testSequenceGuard
   626 	parser := ((#any asParser, #any asParser) wrapped, (#any asParser, #any asParser)) compile.
   146 	parser := ((#any asParser, #any asParser) wrapped, (#any asParser, #any asParser)) compileWithConfiguration: configuration.
   627 	
   147 	
   628 	self assert: parser parse: 'fooo' to: #(#($f $o) #($o $o)).	
   148 	self assert: parser parse: 'fooo' to: #(#($f $o) #($o $o)).	
   629 	self assert: parser parse: 'fo oo' to: #(#($f $o) #($  $o)) end: 4.	
   149 	self assert: parser parse: 'fo oo' to: #(#($f $o) #($  $o)) end: 4.	
   630 	self assert: parser fail: 'fo'.
   150 	self assert: parser fail: 'fo'.
   631 	
   151 	
   632 !
   152 !
   633 
   153 
   634 testTrimmerGuard
   154 testTrimmerGuard
   635 	parser := $a asParser trim, $b asParser compile: #PPGeneratedParser parameters: { #profile -> true }.
   155 	parser := $a asParser trim, $b asParser compileWithConfiguration: configuration.
   636 	
   156 	
   637 	self assert: parser parse: 'ab'.
   157 	self assert: parser parse: 'ab'.
   638 	self assert: parser parse: ' ab'.
   158 	self assert: parser parse: ' ab'.
   639 ! !
   159 ! !
   640 
   160 
       
   161 !PPCCompilerTest methodsFor:'tests - ids'!
       
   162 
       
   163 testId1
       
   164 	node := PPCNode new
       
   165 		name: 'foo'.
       
   166 	compiler := PPCCompiler new.
       
   167 	
       
   168 	id := compiler idFor: node.
       
   169 	
       
   170 	self assert: compiler ids size = 1.
       
   171 	self assert: id = 'foo'.
       
   172 !
       
   173 
       
   174 testId2
       
   175 	node1 := PPCNode new
       
   176 		name: 'foo'.
       
   177 	
       
   178 	node2 := PPCNode new
       
   179 		name: 'foo'.
       
   180 	compiler := PPCCompiler new.
       
   181 
       
   182 	id1 := compiler idFor: node1.
       
   183 	self assert: compiler ids size = 1.
       
   184 	self assert: id1 = 'foo'.
       
   185 	
       
   186 	id2 := compiler idFor: node2.
       
   187 	self assert: compiler ids size = 2.
       
   188 	self assert: id2 = 'foo_1'.	
       
   189 			
       
   190 	self assert: (id1 = id2) not.
       
   191 !
       
   192 
       
   193 testId3
       
   194 	node1 := PPCNode new
       
   195 		name: 'foo'.
       
   196 	
       
   197 	node2 := node1.
       
   198 	compiler := PPCCompiler new.
       
   199 		
       
   200 	id1 := compiler idFor: node1.
       
   201 	self assert: compiler ids size = 1.
       
   202 	self assert: id1 = 'foo'.
       
   203 	
       
   204 	id2 := compiler idFor: node2.
       
   205 	self assert: compiler ids size = 1.
       
   206 	self assert: id2 = 'foo'.	
       
   207 			
       
   208 	self assert: (id1 == id2).
       
   209 !
       
   210 
       
   211 testId4
       
   212 	node1 := PPCNode new
       
   213 		name: 'foo+='.
       
   214 	
       
   215 	node2 := PPCNode new
       
   216 		name: 'foo+='.
       
   217 	compiler := PPCCompiler new.
       
   218 		
       
   219 	id1 := compiler idFor: node1.
       
   220 	self assert: compiler ids size = 1.
       
   221 	self assert: id1 = 'foo'.
       
   222 	
       
   223 	id2 := compiler idFor: node2.
       
   224 	self assert: compiler ids size = 2.
       
   225 	self assert: id2 = 'foo_1'.	
       
   226 			
       
   227 	self assert: (id1 = id2) not.
       
   228 ! !
       
   229 
   641 !PPCCompilerTest class methodsFor:'documentation'!
   230 !PPCCompilerTest class methodsFor:'documentation'!
   642 
   231 
   643 version_HG
   232 version_HG
   644 
   233 
   645     ^ '$Changeset: <not expanded> $'
   234     ^ '$Changeset: <not expanded> $'