compiler/tests/PPCNodeCompilingTest.st
changeset 414 0eaf09920532
parent 401 538267cab6ec
child 415 f30eb7ea54cd
equal deleted inserted replaced
413:5389e6fbb3bc 414:0eaf09920532
    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 methodDictionary size = 4.
   101 	self assert: parser class methods 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: / 30-10-2014 / 23:48:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   108 !
   106 !
   109 
   107 
   110 testCompileLiteral
   108 testCompileLiteral
   111         tree := PPCLiteralNode new
   109 	tree := PPCLiteralNode new
   112                 literal: 'foo';
   110 		literal: 'foo';
   113                 yourself.
   111 		yourself.
   114         parser := self compileTree: tree.
   112 	parser := self compileTree: tree.
   115         
   113 	
   116         self assert: parser class methodDictionary size = 2.
   114 	self assert: parser class methods size = 2.
   117         self assert: parser parse: 'foo'  to: 'foo'.
   115 	self assert: parser parse: 'foo'  to: 'foo'.
   118         self assert: parser parse: 'foobar'  to: 'foo' end: 3.
   116 	self assert: parser parse: 'foobar'  to: 'foo' end: 3.
   119         self assert: parser fail: 'boo'.
   117 	self assert: parser fail: 'boo'.
   120 
       
   121     "Modified: / 30-10-2014 / 23:48:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   122 !
   118 !
   123 
   119 
   124 testCompileLiteral2
   120 testCompileLiteral2
   125 	|  |
   121 	|  |
   126 	
   122 	
   151 	self assert: parser fail: '1'.
   147 	self assert: parser fail: '1'.
   152 	self assert: parser parse: '' to: nil end: 0.
   148 	self assert: parser parse: '' to: nil end: 0.
   153 !
   149 !
   154 
   150 
   155 testCompileNotCharSetPredicate
   151 testCompileNotCharSetPredicate
   156         tree := PPCNotCharSetPredicateNode new
   152 	tree := PPCNotCharSetPredicateNode new
   157                 predicate: (PPCharSetPredicate on: [ :e | e = $a ]);
   153 		predicate: (PPCharSetPredicate on: [ :e | e = $a ]);
   158                 yourself.
   154 		yourself.
   159         parser := self compileTree: tree.
   155 	parser := self compileTree: tree.
   160         
   156 	
   161         self assert: parser class methodDictionary size = 2.
   157 	self assert: parser class methods size = 2.
   162         self assert: parser parse: 'b' to: nil end: 0.
   158 	self assert: parser parse: 'b' to: nil end: 0.
   163         self assert: context invocationCount = 2.
   159 	self assert: context invocationCount = 2.
   164                 
   160 		
   165         self assert: parser fail: 'a'.
   161 	self assert: parser fail: 'a'.
   166         self assert: parser parse: '' to: nil end: 0.
   162 	self assert: parser parse: '' to: nil end: 0.
   167 
       
   168     "Modified: / 30-10-2014 / 23:49:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   169 !
   163 !
   170 
   164 
   171 testCompileNotLiteral
   165 testCompileNotLiteral
   172         tree := PPCNotLiteralNode new
   166 	tree := PPCNotLiteralNode new
   173                 literal: 'foo';
   167 		literal: 'foo';
   174                 yourself.
   168 		yourself.
   175         parser := self compileTree: tree.
   169 	parser := self compileTree: tree.
   176         
   170 	
   177         self assert: parser class methodDictionary size = 2.
   171 	self assert: parser class methods size = 2.
   178         self assert: parser parse: 'bar' to: nil end: 0.
   172 	self assert: parser parse: 'bar' to: nil end: 0.
   179         self assert: context invocationCount = 2.
   173 	self assert: context invocationCount = 2.
   180                 
   174 		
   181         self assert: parser fail: 'foo'.
   175 	self assert: parser fail: 'foo'.
   182         self assert: parser parse: '' to: nil end: 0.
   176 	self assert: parser parse: '' to: nil end: 0.
   183 
       
   184     "Modified: / 30-10-2014 / 23:50:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   185 !
   177 !
   186 
   178 
   187 testCompileNotMessagePredicate
   179 testCompileNotMessagePredicate
   188         tree := PPCNotMessagePredicateNode new
   180 	tree := PPCNotMessagePredicateNode new
   189                 message: #isDigit;
   181 		message: #isDigit;
   190                 yourself.
   182 		yourself.
   191         parser := self compileTree: tree.
   183 	parser := self compileTree: tree.
   192         
   184 	
   193         self assert: parser class methodDictionary size = 2.
   185 	self assert: parser class methods size = 2.
   194         self assert: parser parse: 'a' to: nil end: 0.
   186 	self assert: parser parse: 'a' to: nil end: 0.
   195         self assert: context invocationCount = 2.
   187 	self assert: context invocationCount = 2.
   196                 
   188 		
   197         self assert: parser fail: '1'.
   189 	self assert: parser fail: '1'.
   198         self assert: parser parse: '' to: nil end: 0.
   190 	self assert: parser parse: '' to: nil end: 0.
   199 
       
   200     "Modified: / 30-10-2014 / 23:50:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   201 !
   191 !
   202 
   192 
   203 testCompileOptional
   193 testCompileOptional
   204 	tree := PPCOptionalNode new
   194 	tree := PPCOptionalNode new
   205 		child: ($a asParser asCompilerNode);
   195 		child: ($a asParser asCompilerNode);
   263 	self assert: parser parse: 'a' to: #($a).
   253 	self assert: parser parse: 'a' to: #($a).
   264 	self assert: parser parse: '' to: #().
   254 	self assert: parser parse: '' to: #().
   265 !
   255 !
   266 
   256 
   267 testCompileStarCharSetPredicate
   257 testCompileStarCharSetPredicate
   268         tree := PPCStarCharSetPredicateNode new
   258 	tree := PPCStarCharSetPredicateNode new
   269                 predicate: (PPCharSetPredicate on: [:e | e = $a ]);
   259 		predicate: (PPCharSetPredicate on: [:e | e = $a ]);
   270                 yourself.
   260 		yourself.
   271         parser := self compileTree: tree.
   261 	parser := self compileTree: tree.
   272         
   262 	
   273         self assert: parser class methodDictionary size = 2.
   263 	self assert: parser class methods size = 2.
   274         self assert: parser parse: 'aaa1' to: #( $a $a $a) end: 3.
   264 	self assert: parser parse: 'aaa1' to: #( $a $a $a) end: 3.
   275         self assert: context invocationCount = 2.
   265 	self assert: context invocationCount = 2.
   276         self assert: parser parse: 'bba' to: #() end: 0.
   266 	self assert: parser parse: 'bba' to: #() end: 0.
   277         self assert: context invocationCount = 2.
   267 	self assert: context invocationCount = 2.
   278 
   268 	
   279     "Modified: / 30-10-2014 / 23:50:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   280 !
   269 !
   281 
   270 
   282 testCompileStarMessagePredicate
   271 testCompileStarMessagePredicate
   283         tree := PPCStarMessagePredicateNode new
   272 	tree := PPCStarMessagePredicateNode new
   284                 message: #isLetter;
   273 		message: #isLetter;
   285                 yourself.
   274 		yourself.
   286         parser := self compileTree: tree.
   275 	parser := self compileTree: tree.
   287         
   276 	
   288         self assert: parser class methodDictionary size = 2.
   277 	self assert: parser class methods size = 2.
   289         self assert: parser parse: 'abc1' to: #( $a $b $c) end: 3.
   278 	self assert: parser parse: 'abc1' to: #( $a $b $c) end: 3.
   290         self assert: context invocationCount = 2.
   279 	self assert: context invocationCount = 2.
   291         
   280 	
   292         self assert: parser parse: '123a' to: #() end: 0.
   281 	self assert: parser parse: '123a' to: #() end: 0.
   293         self assert: context invocationCount = 2.
   282 	self assert: context invocationCount = 2.
   294 
   283 	
   295     "Modified: / 30-10-2014 / 23:50:13 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   296 !
   284 !
   297 
   285 
   298 testCompileSymbolAction
   286 testCompileSymbolAction
   299 	tree := PPCSymbolActionNode new
   287 	tree := PPCSymbolActionNode new
   300 		block: #second;
   288 		block: #second;
   351 	self assert: context lwRestoreCount = 1.
   339 	self assert: context lwRestoreCount = 1.
   352 	
   340 	
   353 !
   341 !
   354 
   342 
   355 testCompileTokenStarMessagePredicate
   343 testCompileTokenStarMessagePredicate
   356         
   344 	
   357         tree := PPCTokenStarMessagePredicateNode new message: #isLetter.
   345 	tree := PPCTokenStarMessagePredicateNode new message: #isLetter.
   358         parser := self compileTree: tree params: {#guards -> false}.
   346 	parser := self compileTree: tree params: {#guards -> false}.
   359         
   347 	
   360         self assert: parser class methodDictionary size = 2.
   348 	self assert: parser class methods size = 2.
   361         
   349 	
   362         self assert: parser parse: 'foo' to: parser.
   350 	self assert: parser parse: 'foo' to: parser.
   363         self assert: context invocationCount = 2.
   351 	self assert: context invocationCount = 2.
   364         self assert: context lwRememberCount  = 0.
   352 	self assert: context lwRememberCount  = 0.
   365         self assert: context lwRestoreCount  = 0.
   353 	self assert: context lwRestoreCount  = 0.
   366         self assert: context rememberCount = 0.
   354 	self assert: context rememberCount = 0.
   367         
   355 	
   368         self assert: parser parse: 'foo123' to: parser end: 3.
   356 	self assert: parser parse: 'foo123' to: parser end: 3.
   369 
       
   370     "Modified: / 30-10-2014 / 23:50:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   371 ! !
   357 ! !
   372 
   358 
   373 !PPCNodeCompilingTest methodsFor:'tests - guard'!
   359 !PPCNodeCompilingTest methodsFor:'tests - guard'!
   374 
   360 
   375 testSequenceTokenGuard
   361 testSequenceTokenGuard