compiler/tests/PPCTokenizingTest.st
changeset 515 b5316ef15274
parent 464 f6d77fee9811
child 516 3b81c9e53352
child 524 f6f68d32de73
equal deleted inserted replaced
502:1e45d3c96ec5 515:b5316ef15274
    19 assert: p parse: whatever end: end
    19 assert: p parse: whatever end: end
    20     ^ result := super assert: p parse: whatever end: end
    20     ^ result := super assert: p parse: whatever end: end
    21 !
    21 !
    22 
    22 
    23 cleanClass
    23 cleanClass
    24     | parserClass |
    24     | parserClass scannerClass |
    25     parserClass := (Smalltalk at: arguments name ifAbsent: [nil]).
    25     parserClass := (Smalltalk at: arguments parserName ifAbsent: [nil]).
    26     parserClass notNil ifTrue:[ 
    26     parserClass notNil ifTrue:[ 
    27         self flag: 'uncomment'.
    27         parserClass removeFromSystem
    28 "		parserClass removeFromSystem"
    28     ].
       
    29 
       
    30     scannerClass := (Smalltalk at: arguments scannerName ifAbsent: [nil]).
       
    31     scannerClass notNil ifTrue:[ 
       
    32         scannerClass removeFromSystem
    29     ].
    33     ].
    30 !
    34 !
    31 
    35 
    32 context	
    36 context	
    33     ^ context := PPCProfilingContext new
    37     ^ context := PPCProfilingContext new
    48         
    52         
    49     self cleanClass.
    53     self cleanClass.
    50 !
    54 !
    51 
    55 
    52 tearDown
    56 tearDown
    53     self cleanClass
    57     "self cleanClass"
       
    58 !
       
    59 
       
    60 testChoice
       
    61     | p1 p2 a1 a2 |
       
    62     a1 := 'a' asParser token name: 't1'; yourself.
       
    63     a2 := 'b' asParser token name: 't2'; yourself.
       
    64     
       
    65     p1 := a1 star.
       
    66     p2 := a2.
       
    67     
       
    68     parser := p1 / p2	compileWithConfiguration: configuration.
       
    69 
       
    70     self assert: parser parse: ''.
       
    71     self assert: result isEmpty.
       
    72     
       
    73     self assert: parser parse: 'a'.
       
    74     self assert: result first inputValue = 'a'.
       
    75 
       
    76     self assert: parser parse: 'aa'.
       
    77     self assert: result first inputValue = 'a'.
       
    78     self assert: result second inputValue = 'a'.
       
    79 
       
    80     self assert: parser parse: 'b' end: 0.
       
    81     self assert: result isEmpty.
       
    82 
       
    83     self assert: parser parse: 'c' end: 0.
       
    84     
    54 !
    85 !
    55 
    86 
    56 testChoiceOrder
    87 testChoiceOrder
    57     parser := (
    88     parser := (
    58         'a' asParser token, 'b' asParser token / 
    89         'a' asParser token, 'b' asParser token / 
   201 
   232 
   202     self assert: parser fail: 'bar'.
   233     self assert: parser fail: 'bar'.
   203     
   234     
   204 !
   235 !
   205 
   236 
       
   237 testCompileEmptytoken
       
   238     | start stop epsilon |
       
   239     start := $( asParser token.
       
   240     stop := $) asParser token.
       
   241     epsilon := '' asParser token.
       
   242     
       
   243     self should: [
       
   244         (start, epsilon, stop) compileWithConfiguration: configuration.
       
   245     ] raise: Exception.
       
   246 "	
       
   247     self assert: parser parse: '()'.
       
   248     self assert: parser fail: '('.
       
   249 "
       
   250 !
       
   251 
   206 testCompileLiteral
   252 testCompileLiteral
   207     parser := 'foo' asParser token compileWithConfiguration: configuration.
   253     parser := 'foo' asParser token compileWithConfiguration: configuration.
   208     
   254     
   209     self assert: parser parse: 'foo'.
   255     self assert: parser parse: 'foo'.
   210     self assert: result inputValue = 'foo'.
   256     self assert: result inputValue = 'foo'.
   218     self assert: parser parse: 'foobar'.
   264     self assert: parser parse: 'foobar'.
   219     self assert: result first inputValue = 'foo'.
   265     self assert: result first inputValue = 'foo'.
   220     self assert: result second inputValue = 'bar'.
   266     self assert: result second inputValue = 'bar'.
   221 !
   267 !
   222 
   268 
       
   269 testCompileSequence2
       
   270     parser := ('foo' asParser trimmingToken), ('bar' asParser trimmingToken) 
       
   271         compileWithConfiguration: configuration.
       
   272     
       
   273     self assert: parser parse: 'foobar'.
       
   274     self assert: result first inputValue = 'foo'.
       
   275     self assert: result second inputValue = 'bar'.
       
   276 
       
   277     self assert: parser parse: 'foo  bar'.
       
   278     self assert: result first inputValue = 'foo'.
       
   279     self assert: result second inputValue = 'bar'.
       
   280 
       
   281     self assert: parser parse: '  foo  bar'.
       
   282     self assert: result first inputValue = 'foo'.
       
   283     self assert: result second inputValue = 'bar'.
       
   284 !
       
   285 
       
   286 testCompileSequence3
       
   287     parser := 	('foo' asParser trimmingToken), 
       
   288                     ('bar' asParser trimmingToken), 
       
   289                     ('baz' asParser trimmingToken)
       
   290         compileWithConfiguration: configuration.
       
   291     
       
   292     self assert: parser parse: 'foobarbaz'.
       
   293     self assert: result first inputValue = 'foo'.
       
   294     self assert: result second inputValue = 'bar'.
       
   295 
       
   296     self assert: parser parse: ' foo  bar  baz  '.
       
   297     self assert: result first inputValue = 'foo'.
       
   298     self assert: result second inputValue = 'bar'.
       
   299     self assert: result third inputValue = 'baz'.
       
   300 !
       
   301 
   223 testCompileStar
   302 testCompileStar
   224     parser := 'foo' asParser token star compileWithConfiguration: configuration.
   303     parser := 'foo' asParser token star compileWithConfiguration: configuration.
   225     
   304     
   226     self assert: parser parse: 'foo'.
   305     self assert: parser parse: 'foo'.
   227     self assert: result first inputValue = 'foo'.
   306     self assert: result first inputValue = 'foo'.
   232 
   311 
   233 testCompileStar2
   312 testCompileStar2
   234     parser := ('foo' asParser token, 'bar' asParser token) star compileWithConfiguration: configuration.
   313     parser := ('foo' asParser token, 'bar' asParser token) star compileWithConfiguration: configuration.
   235     
   314     
   236     self assert: parser parse: 'foobar'.
   315     self assert: parser parse: 'foobar'.
   237     self assert: context tokenReads size = 3.
   316     self assert: context tokenReads size = 1.
   238             
   317             
   239     self assert: parser parse: 'bar' end: 0.
   318     self assert: parser parse: 'bar' end: 0.
   240     self assert: result isEmpty.
   319     self assert: result isEmpty.
   241     self assert: context tokenReads size = 1.
   320     self assert: context tokenReads size = 1.
   242         
   321         
   254         yourself.
   333         yourself.
   255 
   334 
   256     parser := argumentsWith compileWithConfiguration: configuration.
   335     parser := argumentsWith compileWithConfiguration: configuration.
   257     self assert: parser parse: '|'.
   336     self assert: parser parse: '|'.
   258 
   337 
   259     parser := argumentsWith compileWithConfiguration: configuration.
       
   260     self assert: parser parse: ']'.
   338     self assert: parser parse: ']'.
   261 !
   339 !
   262 
   340 
   263 testCompileTokenComplex3
   341 testCompileTokenComplex3
   264     | choice1 choice2 a1 b1 a2 b2 tricky |
   342     | choice1 choice2 a1 b1 a2 b2 tricky |
   281     tricky := (a1 asParser, choice1) / (b2 asParser, choice2).
   359     tricky := (a1 asParser, choice1) / (b2 asParser, choice2).
   282 
   360 
   283     parser := tricky compileWithConfiguration: configuration.
   361     parser := tricky compileWithConfiguration: configuration.
   284     self assert: parser parse: '||'.
   362     self assert: parser parse: '||'.
   285 
   363 
   286     parser := tricky compileWithConfiguration: configuration.
       
   287     self assert: parser parse: '|]'.
   364     self assert: parser parse: '|]'.
   288 
   365 
   289     parser := tricky compileWithConfiguration: configuration.
       
   290     self assert: parser parse: ']|'.
   366     self assert: parser parse: ']|'.
   291 
   367 
   292     parser := tricky compileWithConfiguration: configuration.
       
   293     self assert: parser parse: ']]'.
   368     self assert: parser parse: ']]'.
       
   369 !
       
   370 
       
   371 testCompileTokenComplex4
       
   372     |  symbol symbolLiteralArray symbolLiteral arrayItem  arrayLiteral |
       
   373     "based on symbolLiteral symbolLiteralArray in SmalltalkGrammar"
       
   374     
       
   375     symbol := PPDelegateParser new.
       
   376     symbol setParser: 'foo' asParser.
       
   377     symbol name: 'symbol'.
       
   378     
       
   379     symbolLiteralArray := PPDelegateParser new.
       
   380     symbolLiteralArray setParser: symbol token.
       
   381     symbolLiteralArray name: 'symbolLiteralArray'.
       
   382     
       
   383     symbolLiteral := PPDelegateParser new.
       
   384     symbolLiteral setParser: $# asParser token, symbol token ==> [:e | e].
       
   385     symbolLiteral name: 'symbolLiteral'.
       
   386     
       
   387     arrayLiteral := PPDelegateParser new.
       
   388     arrayLiteral setParser: '#(' asParser token, symbolLiteralArray, ')' asParser token.
       
   389     arrayLiteral name: 'arrayLiteral'.
       
   390 
       
   391     arrayItem := arrayLiteral / symbolLiteral.
       
   392 
       
   393     parser := arrayItem compileWithConfiguration: configuration.
       
   394 
       
   395     self assert: parser parse: '#(foo)'.
       
   396     self assert: parser parse: '#foo'.
   294 !
   397 !
   295 
   398 
   296 testCompileTrim
   399 testCompileTrim
   297     parser := 'foo' asParser token trim end compileWithConfiguration: configuration.
   400     parser := 'foo' asParser token trim end compileWithConfiguration: configuration.
   298     
   401     
   318     parser := token plus
   421     parser := token plus
   319         compileWithConfiguration: configuration.
   422         compileWithConfiguration: configuration.
   320 
   423 
   321     self assert: parser parse: 'a'.
   424     self assert: parser parse: 'a'.
   322     self assert: result first inputValue = 'a'.
   425     self assert: result first inputValue = 'a'.
   323     self assert: context invocations size = 5.
   426     self assert: context tokenReads size = 1.
       
   427 
       
   428     self flag: 'add the assertion here?'.
       
   429 "	self assert: context invocations size = 5."
   324 !
   430 !
   325 
   431 
   326 testTokenCharacter2
   432 testTokenCharacter2
   327     | token |
   433     | token |
   328     token := $a asParser token.
   434     token := $a asParser token.
   331 
   437 
   332     self assert: parser parse: 'aaa'.
   438     self assert: parser parse: 'aaa'.
   333     self assert: result first inputValue = 'a'.
   439     self assert: result first inputValue = 'a'.
   334     self assert: result second inputValue = 'a'.
   440     self assert: result second inputValue = 'a'.
   335     self assert: result third inputValue = 'a'.
   441     self assert: result third inputValue = 'a'.
   336     self assert: context invocations size = 7.
   442     
       
   443     self assert: context tokenReads size = 1.
       
   444     self flag: 'Add the assertion here?'.
       
   445 "	self assert: context invocations size = 7."
   337 !
   446 !
   338 
   447 
   339 testTokenName
   448 testTokenName
   340     | token |
   449     | token |
   341     token := 'foo' asParser token name: 'fooToken'; yourself.
   450     token := 'foo' asParser token name: 'fooToken'; yourself.
   362         compileWithConfiguration: configuration.
   471         compileWithConfiguration: configuration.
   363 
   472 
   364     self assert: parser parse: ' foo '.
   473     self assert: parser parse: ' foo '.
   365     self assert: result first inputValue = 'foo'.
   474     self assert: result first inputValue = 'foo'.
   366 
   475 
   367     self assert: (context invocations select: [:e | e = #consumeWhitespace ]) size = 2.
   476     self assert: (context invocations select: [:e | e = #consumeWhitespace ]) size = 3.
   368 !
   477 !
   369 
   478 
   370 testWhitespace2
   479 testWhitespace2
   371     | token ws trimmingToken |
   480     | token ws trimmingToken |
   372     configuration arguments inline: false.
   481     configuration arguments inline: false.
   382 
   491 
   383     self assert: parser parse: ' foo foo '.
   492     self assert: parser parse: ' foo foo '.
   384     self assert: result first inputValue = 'foo'.
   493     self assert: result first inputValue = 'foo'.
   385     self assert: result second inputValue = 'foo'.
   494     self assert: result second inputValue = 'foo'.
   386 
   495 
   387     self assert: (context invocations select: [:e | e = #consumeWhitespace ]) size = 3.
   496     self assert: (context invocations select: [:e | e = #consumeWhitespace ]) size = 4.
       
   497 !
       
   498 
       
   499 testWhitespace3
       
   500     | token ws trimmingToken |
       
   501     configuration arguments inline: false.
       
   502         
       
   503     token := 'foo' asParser token.
       
   504     ws := #blank asParser star name: 'consumeWhitespace'; yourself.
       
   505     trimmingToken := ((ws, token, ws) ==> #second) 
       
   506         propertyAt: 'trimmingToken' put: true; 
       
   507         yourself.
       
   508     
       
   509     parser := trimmingToken plus
       
   510         compileWithConfiguration: configuration.
       
   511 
       
   512     self assert: parser parse: ' foo  foo  foo  '.
       
   513     self assert: result first inputValue = 'foo'.
       
   514     self assert: result second inputValue = 'foo'.
       
   515     self assert: result third inputValue = 'foo'.
       
   516 
       
   517     self assert: (context invocations select: [:e | e = #consumeWhitespace ]) size = 5.
   388 ! !
   518 ! !
   389 
   519