compiler/tests/PPCNodeCompilingTest.st
changeset 415 f30eb7ea54cd
parent 414 0eaf09920532
child 416 b0fd54ee0412
equal deleted inserted replaced
414:0eaf09920532 415:f30eb7ea54cd
    90 	parser := self compileTree: (PPCCharacterNode new character: Character lf; yourself).
    90 	parser := self compileTree: (PPCCharacterNode new character: Character lf; yourself).
    91 	self assert: parser parse: String lf.
    91 	self assert: parser parse: String lf.
    92 !
    92 !
    93 
    93 
    94 testCompileChoice
    94 testCompileChoice
    95 	tree := PPCChoiceNode new
    95         tree := PPCChoiceNode new
    96 		children: { #digit asParser asCompilerNode. #letter asParser asCompilerNode };
    96                 children: { #digit asParser asCompilerNode. #letter asParser asCompilerNode };
    97 		yourself.
    97                 yourself.
    98 		
    98                 
    99 	parser := self compileTree: tree.
    99         parser := self compileTree: tree.
   100 	
   100         
   101 	self assert: parser class methods size = 4.
   101         self assert: parser class methodDictionary size = 4.
   102 	
   102         
   103 	self assert: parser parse: '1' to: $1.
   103         self assert: parser parse: '1' to: $1.
   104 	self assert: parser parse: 'a' to: $a.
   104         self assert: parser parse: 'a' to: $a.
   105 	self assert: parser fail: '_'.
   105         self assert: parser fail: '_'.
       
   106 
       
   107     "Modified: / 06-11-2014 / 00:48:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   106 !
   108 !
   107 
   109 
   108 testCompileLiteral
   110 testCompileLiteral
   109 	tree := PPCLiteralNode new
   111         tree := PPCLiteralNode new
   110 		literal: 'foo';
   112                 literal: 'foo';
   111 		yourself.
   113                 yourself.
   112 	parser := self compileTree: tree.
   114         parser := self compileTree: tree.
   113 	
   115         
   114 	self assert: parser class methods size = 2.
   116         self assert: parser class methodDictionary size = 2.
   115 	self assert: parser parse: 'foo'  to: 'foo'.
   117         self assert: parser parse: 'foo'  to: 'foo'.
   116 	self assert: parser parse: 'foobar'  to: 'foo' end: 3.
   118         self assert: parser parse: 'foobar'  to: 'foo' end: 3.
   117 	self assert: parser fail: 'boo'.
   119         self assert: parser fail: 'boo'.
       
   120 
       
   121     "Modified: / 06-11-2014 / 00:48:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   118 !
   122 !
   119 
   123 
   120 testCompileLiteral2
   124 testCompileLiteral2
   121 	|  |
   125 	|  |
   122 	
   126 	
   147 	self assert: parser fail: '1'.
   151 	self assert: parser fail: '1'.
   148 	self assert: parser parse: '' to: nil end: 0.
   152 	self assert: parser parse: '' to: nil end: 0.
   149 !
   153 !
   150 
   154 
   151 testCompileNotCharSetPredicate
   155 testCompileNotCharSetPredicate
   152 	tree := PPCNotCharSetPredicateNode new
   156         tree := PPCNotCharSetPredicateNode new
   153 		predicate: (PPCharSetPredicate on: [ :e | e = $a ]);
   157                 predicate: (PPCharSetPredicate on: [ :e | e = $a ]);
   154 		yourself.
   158                 yourself.
   155 	parser := self compileTree: tree.
   159         parser := self compileTree: tree.
   156 	
   160         
   157 	self assert: parser class methods size = 2.
   161         self assert: parser class methodDictionary size = 2.
   158 	self assert: parser parse: 'b' to: nil end: 0.
   162         self assert: parser parse: 'b' to: nil end: 0.
   159 	self assert: context invocationCount = 2.
   163         self assert: context invocationCount = 2.
   160 		
   164                 
   161 	self assert: parser fail: 'a'.
   165         self assert: parser fail: 'a'.
   162 	self assert: parser parse: '' to: nil end: 0.
   166         self assert: parser parse: '' to: nil end: 0.
       
   167 
       
   168     "Modified: / 06-11-2014 / 00:48:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   163 !
   169 !
   164 
   170 
   165 testCompileNotLiteral
   171 testCompileNotLiteral
   166 	tree := PPCNotLiteralNode new
   172         tree := PPCNotLiteralNode new
   167 		literal: 'foo';
   173                 literal: 'foo';
   168 		yourself.
   174                 yourself.
   169 	parser := self compileTree: tree.
   175         parser := self compileTree: tree.
   170 	
   176         
   171 	self assert: parser class methods size = 2.
   177         self assert: parser class methodDictionary size = 2.
   172 	self assert: parser parse: 'bar' to: nil end: 0.
   178         self assert: parser parse: 'bar' to: nil end: 0.
   173 	self assert: context invocationCount = 2.
   179         self assert: context invocationCount = 2.
   174 		
   180                 
   175 	self assert: parser fail: 'foo'.
   181         self assert: parser fail: 'foo'.
   176 	self assert: parser parse: '' to: nil end: 0.
   182         self assert: parser parse: '' to: nil end: 0.
       
   183 
       
   184     "Modified: / 06-11-2014 / 00:48:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   177 !
   185 !
   178 
   186 
   179 testCompileNotMessagePredicate
   187 testCompileNotMessagePredicate
   180 	tree := PPCNotMessagePredicateNode new
   188         tree := PPCNotMessagePredicateNode new
   181 		message: #isDigit;
   189                 message: #isDigit;
   182 		yourself.
   190                 yourself.
   183 	parser := self compileTree: tree.
   191         parser := self compileTree: tree.
   184 	
   192         
   185 	self assert: parser class methods size = 2.
   193         self assert: parser class methodDictionary size = 2.
   186 	self assert: parser parse: 'a' to: nil end: 0.
   194         self assert: parser parse: 'a' to: nil end: 0.
   187 	self assert: context invocationCount = 2.
   195         self assert: context invocationCount = 2.
   188 		
   196                 
   189 	self assert: parser fail: '1'.
   197         self assert: parser fail: '1'.
   190 	self assert: parser parse: '' to: nil end: 0.
   198         self assert: parser parse: '' to: nil end: 0.
       
   199 
       
   200     "Modified: / 06-11-2014 / 00:48:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   191 !
   201 !
   192 
   202 
   193 testCompileOptional
   203 testCompileOptional
   194 	tree := PPCOptionalNode new
   204 	tree := PPCOptionalNode new
   195 		child: ($a asParser asCompilerNode);
   205 		child: ($a asParser asCompilerNode);
   253 	self assert: parser parse: 'a' to: #($a).
   263 	self assert: parser parse: 'a' to: #($a).
   254 	self assert: parser parse: '' to: #().
   264 	self assert: parser parse: '' to: #().
   255 !
   265 !
   256 
   266 
   257 testCompileStarCharSetPredicate
   267 testCompileStarCharSetPredicate
   258 	tree := PPCStarCharSetPredicateNode new
   268         tree := PPCStarCharSetPredicateNode new
   259 		predicate: (PPCharSetPredicate on: [:e | e = $a ]);
   269                 predicate: (PPCharSetPredicate on: [:e | e = $a ]);
   260 		yourself.
   270                 yourself.
   261 	parser := self compileTree: tree.
   271         parser := self compileTree: tree.
   262 	
   272         
   263 	self assert: parser class methods size = 2.
   273         self assert: parser class methodDictionary size = 2.
   264 	self assert: parser parse: 'aaa1' to: #( $a $a $a) end: 3.
   274         self assert: parser parse: 'aaa1' to: #( $a $a $a) end: 3.
   265 	self assert: context invocationCount = 2.
   275         self assert: context invocationCount = 2.
   266 	self assert: parser parse: 'bba' to: #() end: 0.
   276         self assert: parser parse: 'bba' to: #() end: 0.
   267 	self assert: context invocationCount = 2.
   277         self assert: context invocationCount = 2.
   268 	
   278 
       
   279     "Modified: / 06-11-2014 / 00:48:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   269 !
   280 !
   270 
   281 
   271 testCompileStarMessagePredicate
   282 testCompileStarMessagePredicate
   272 	tree := PPCStarMessagePredicateNode new
   283         tree := PPCStarMessagePredicateNode new
   273 		message: #isLetter;
   284                 message: #isLetter;
   274 		yourself.
   285                 yourself.
   275 	parser := self compileTree: tree.
   286         parser := self compileTree: tree.
   276 	
   287         
   277 	self assert: parser class methods size = 2.
   288         self assert: parser class methodDictionary size = 2.
   278 	self assert: parser parse: 'abc1' to: #( $a $b $c) end: 3.
   289         self assert: parser parse: 'abc1' to: #( $a $b $c) end: 3.
   279 	self assert: context invocationCount = 2.
   290         self assert: context invocationCount = 2.
   280 	
   291         
   281 	self assert: parser parse: '123a' to: #() end: 0.
   292         self assert: parser parse: '123a' to: #() end: 0.
   282 	self assert: context invocationCount = 2.
   293         self assert: context invocationCount = 2.
   283 	
   294 
       
   295     "Modified: / 06-11-2014 / 00:48:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   284 !
   296 !
   285 
   297 
   286 testCompileSymbolAction
   298 testCompileSymbolAction
   287 	tree := PPCSymbolActionNode new
   299 	tree := PPCSymbolActionNode new
   288 		block: #second;
   300 		block: #second;
   339 	self assert: context lwRestoreCount = 1.
   351 	self assert: context lwRestoreCount = 1.
   340 	
   352 	
   341 !
   353 !
   342 
   354 
   343 testCompileTokenStarMessagePredicate
   355 testCompileTokenStarMessagePredicate
   344 	
   356         
   345 	tree := PPCTokenStarMessagePredicateNode new message: #isLetter.
   357         tree := PPCTokenStarMessagePredicateNode new message: #isLetter.
   346 	parser := self compileTree: tree params: {#guards -> false}.
   358         parser := self compileTree: tree params: {#guards -> false}.
   347 	
   359         
   348 	self assert: parser class methods size = 2.
   360         self assert: parser class methodDictionary size = 2.
   349 	
   361         
   350 	self assert: parser parse: 'foo' to: parser.
   362         self assert: parser parse: 'foo' to: parser.
   351 	self assert: context invocationCount = 2.
   363         self assert: context invocationCount = 2.
   352 	self assert: context lwRememberCount  = 0.
   364         self assert: context lwRememberCount  = 0.
   353 	self assert: context lwRestoreCount  = 0.
   365         self assert: context lwRestoreCount  = 0.
   354 	self assert: context rememberCount = 0.
   366         self assert: context rememberCount = 0.
   355 	
   367         
   356 	self assert: parser parse: 'foo123' to: parser end: 3.
   368         self assert: parser parse: 'foo123' to: parser end: 3.
       
   369 
       
   370     "Modified: / 06-11-2014 / 00:49:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   357 ! !
   371 ! !
   358 
   372 
   359 !PPCNodeCompilingTest methodsFor:'tests - guard'!
   373 !PPCNodeCompilingTest methodsFor:'tests - guard'!
   360 
   374 
   361 testSequenceTokenGuard
   375 testSequenceTokenGuard
   406 ! !
   420 ! !
   407 
   421 
   408 !PPCNodeCompilingTest methodsFor:'tests - inlining'!
   422 !PPCNodeCompilingTest methodsFor:'tests - inlining'!
   409 
   423 
   410 testInlineAny
   424 testInlineAny
   411 	tree := PPCSequenceNode new
   425         tree := PPCSequenceNode new
   412 		children: { PPCInlineAnyNode new. $a asParser asCompilerNode }.
   426                 children: { PPCInlineAnyNode new. $a asParser asCompilerNode }.
   413 	
   427         
   414 	parser := self compileTree: tree.
   428         parser := self compileTree: tree.
   415 	
   429         
   416 	self assert: parser class methods size = 3.
   430         self assert: parser class methodDictionary size = 3.
   417 	self assert: parser parse: '.a' to: #($. $a).
   431         self assert: parser parse: '.a' to: #($. $a).
       
   432 
       
   433     "Modified: / 06-11-2014 / 01:12:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   418 !
   434 !
   419 
   435 
   420 testInlineCharSetPredicate
   436 testInlineCharSetPredicate
   421 	tree := PPCPlusNode new
   437         tree := PPCPlusNode new
   422 		child: (PPCInlineCharSetPredicateNode new 
   438                 child: (PPCInlineCharSetPredicateNode new 
   423 			predicate: (PPCharSetPredicate on: [ :e | e = $a ]);
   439                         predicate: (PPCharSetPredicate on: [ :e | e = $a ]);
   424 			yourself);
   440                         yourself);
   425 		yourself.
   441                 yourself.
   426 	
   442         
   427 	parser := self compileTree: tree.
   443         parser := self compileTree: tree.
   428 
   444 
   429 	self assert: parser class methods size = 2.
   445         self assert: parser class methodDictionary size = 2.
   430 	self assert: parser parse: 'a'  to: #($a).
   446         self assert: parser parse: 'a'  to: #($a).
   431 	self assert: parser fail: 'b'.
   447         self assert: parser fail: 'b'.
       
   448 
       
   449     "Modified: / 06-11-2014 / 01:12:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   432 !
   450 !
   433 
   451 
   434 testInlineCharacter
   452 testInlineCharacter
   435 	tree := PPCSequenceNode new
   453         tree := PPCSequenceNode new
   436 		children: { PPCInlineCharacterNode new character: $b . $a asParser asCompilerNode }.
   454                 children: { PPCInlineCharacterNode new character: $b . $a asParser asCompilerNode }.
   437 	
   455         
   438 	parser := self compileTree: tree.
   456         parser := self compileTree: tree.
   439 	
   457         
   440 	self assert: parser class methods size = 3.
   458         self assert: parser class methodDictionary size = 3.
   441 	self assert: parser parse: 'ba' to: #($b $a).
   459         self assert: parser parse: 'ba' to: #($b $a).
       
   460 
       
   461     "Modified: / 06-11-2014 / 01:12:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   442 !
   462 !
   443 
   463 
   444 testInlineLiteral
   464 testInlineLiteral
   445 	tree := PPCSequenceNode new
   465         tree := PPCSequenceNode new
   446 		children: { PPCInlineLiteralNode new literal: 'foo'. $a asParser asCompilerNode }.
   466                 children: { PPCInlineLiteralNode new literal: 'foo'. $a asParser asCompilerNode }.
   447 	
   467         
   448 	parser := self compileTree: tree.
   468         parser := self compileTree: tree.
   449 	
   469         
   450 	self assert: parser class methods size = 3.
   470         self assert: parser class methodDictionary size = 3.
   451 	self assert: parser parse: 'fooa' to: #('foo' $a).
   471         self assert: parser parse: 'fooa' to: #('foo' $a).
       
   472 
       
   473     "Modified: / 06-11-2014 / 01:12:34 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   452 !
   474 !
   453 
   475 
   454 testInlineNil
   476 testInlineNil
   455 	tree := PPCSequenceNode new
   477         tree := PPCSequenceNode new
   456 		children: { PPCInlineNilNode new . $a asParser asCompilerNode }.
   478                 children: { PPCInlineNilNode new . $a asParser asCompilerNode }.
   457 	
   479         
   458 	parser := self compileTree: tree.
   480         parser := self compileTree: tree.
   459 	
   481         
   460 	self assert: parser class methods size = 3.
   482         self assert: parser class methodDictionary size = 3.
   461 	self assert: parser parse: 'a' to: #(nil $a).
   483         self assert: parser parse: 'a' to: #(nil $a).
       
   484 
       
   485     "Modified: / 06-11-2014 / 01:12:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   462 !
   486 !
   463 
   487 
   464 testInlineNotLiteral
   488 testInlineNotLiteral
   465 	tree := PPCSequenceNode new
   489         tree := PPCSequenceNode new
   466 		children: { PPCInlineNotLiteralNode new literal: 'foo'. $a asParser asCompilerNode }.
   490                 children: { PPCInlineNotLiteralNode new literal: 'foo'. $a asParser asCompilerNode }.
   467 	
   491         
   468 	parser := self compileTree: tree.
   492         parser := self compileTree: tree.
   469 	
   493         
   470 	self assert: parser class methods size = 3.
   494         self assert: parser class methodDictionary size = 3.
   471 	self assert: parser parse: 'a' to: #(nil $a).
   495         self assert: parser parse: 'a' to: #(nil $a).
       
   496 
       
   497     "Modified: / 06-11-2014 / 01:12:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   472 !
   498 !
   473 
   499 
   474 testInlinePluggable
   500 testInlinePluggable
   475 	tree := PPCSequenceNode new
   501         tree := PPCSequenceNode new
   476 		children: { PPCInlinePluggableNode new block: [ :ctx | ctx next ]. $a asParser asCompilerNode }.
   502                 children: { PPCInlinePluggableNode new block: [ :ctx | tree. ctx next ]. $a asParser asCompilerNode }.
   477 	
   503         
   478 	parser := self compileTree: tree.
   504         parser := self compileTree: tree.
   479 	
   505         
   480 	self assert: parser class methods size = 3.
   506         self assert: parser class methodDictionary size = 3.
   481 	self assert: parser parse: 'ba' to: #($b $a).
   507         self assert: parser parse: 'ba' to: #($b $a).
       
   508 
       
   509     "Modified: / 06-11-2014 / 01:20:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   482 ! !
   510 ! !
   483 
   511 
   484 !PPCNodeCompilingTest class methodsFor:'documentation'!
   512 !PPCNodeCompilingTest class methodsFor:'documentation'!
   485 
   513 
   486 version_HG
   514 version_HG