compiler/tests/PPCUniversalTest.st
changeset 538 16e8536f5cfb
parent 537 fb212e14d1f4
equal deleted inserted replaced
537:fb212e14d1f4 538:16e8536f5cfb
    39 ! !
    39 ! !
    40 
    40 
    41 !PPCUniversalTest methodsFor:'tests - compiling'!
    41 !PPCUniversalTest methodsFor:'tests - compiling'!
    42 
    42 
    43 testCompileAnd
    43 testCompileAnd
    44     parser := #digit asParser and compileUsingCompiler:compiler.
    44     parser := compiler compile: (#digit asParser and).
    45     
    45     
    46     self assert: parser parse: '1' to: $1 end: 0.
    46     self assert: parser parse: '1' to: $1 end: 0.
    47     self assert: parser fail: 'a'.
    47     self assert: parser fail: 'a'.
    48     self assert: parser fail: ''.
    48     self assert: parser fail: ''.
    49 
    49 
    59     self assert: parser parse: '
    59     self assert: parser parse: '
    60 ' to: Character cr.
    60 ' to: Character cr.
    61 !
    61 !
    62 
    62 
    63 testCompileAnyStar
    63 testCompileAnyStar
    64     parser := #any asParser star compileUsingCompiler:compiler.
    64     parser := compiler compile: (#any asParser star).
    65     
    65     
    66     
    66     
    67     self assert: parser parse: 'aaa' to: { $a. $a . $a }.
    67     self assert: parser parse: 'aaa' to: { $a. $a . $a }.
    68     self assert: context invocationCount = 1.
    68     self assert: context invocationCount = 1.
    69     self assert: parser parse: '' to: { }.	
    69     self assert: parser parse: '' to: { }.	
    70 !
    70 !
    71 
    71 
    72 testCompileBlock
    72 testCompileBlock
    73     parser := (#letter asParser) plus ==> [ :res | res collect: [:each | each asUppercase ]].
    73     parser := (#letter asParser) plus ==> [ :res | res collect: [:each | each asUppercase ]].
    74     parser := parser compileUsingCompiler:compiler.
    74     parser := compiler compile: (parser).
    75     
    75     
    76     self assert: parser parse: 'foo' to: { $F . $O . $O}.
    76     self assert: parser parse: 'foo' to: { $F . $O . $O}.
    77     self assert: parser parse: 'bar' to: { $B . $A . $R}.
    77     self assert: parser parse: 'bar' to: { $B . $A . $R}.
    78     self assert: parser fail: ''.
    78     self assert: parser fail: ''.
    79 !
    79 !
    80 
    80 
    81 testCompileCharacter
    81 testCompileCharacter
    82     parser := $a asParser compileUsingCompiler:compiler.
    82     parser := compiler compile: ($a asParser).
    83     
    83     
    84     self assert: parser parse: 'a'  to: $a.
    84     self assert: parser parse: 'a'  to: $a.
    85     self assert: parser fail: 'b'.
    85     self assert: parser fail: 'b'.
    86 
    86 
    87     parser := $# asParser compileUsingCompiler:compiler.
    87     parser := compiler compile: ($# asParser).
    88     self assert: parser parse: '#'.
    88     self assert: parser parse: '#'.
    89 !
    89 !
    90 
    90 
    91 testCompileChoice
    91 testCompileChoice
    92     parser := (#digit asParser / #letter asParser) compileUsingCompiler:compiler.
    92     parser := compiler compile: ((#digit asParser / #letter asParser)).
    93     
    93     
    94     self assert: parser parse: '1' to: $1.
    94     self assert: parser parse: '1' to: $1.
    95     self assert: parser parse: 'a' to: $a.
    95     self assert: parser parse: 'a' to: $a.
    96     self assert: parser fail: '_'.
    96     self assert: parser fail: '_'.
    97     
    97     
    98 !
    98 !
    99 
    99 
   100 testCompileChoice2
   100 testCompileChoice2
   101     parser := ('true' asParser / 'false' asParser) compileUsingCompiler:compiler.
   101     parser := compiler compile: (('true' asParser / 'false' asParser)).
   102     
   102     
   103     self assert: parser parse: 'true' to: 'true'.
   103     self assert: parser parse: 'true' to: 'true'.
   104     self assert: parser parse: 'false' to: 'false'.
   104     self assert: parser parse: 'false' to: 'false'.
   105     self assert: parser fail: 'trulse'.
   105     self assert: parser fail: 'trulse'.
   106     
   106     
   107 !
   107 !
   108 
   108 
   109 testCompileLiteral
   109 testCompileLiteral
   110     parser := 'foo' asParser compileUsingCompiler:compiler.
   110     parser := compiler compile: ('foo' asParser).
   111     
   111     
   112     self assert: parser parse: 'foo'  to: 'foo'.
   112     self assert: parser parse: 'foo'  to: 'foo'.
   113     self assert: parser parse: 'foobar'  to: 'foo' end: 3.
   113     self assert: parser parse: 'foobar'  to: 'foo' end: 3.
   114     self assert: parser fail: 'boo'.
   114     self assert: parser fail: 'boo'.
   115     
   115     
   116     parser := '#[' asParser compileUsingCompiler:compiler.
   116     parser := compiler compile: ('#[' asParser).
   117     self assert: parser parse: '#[1]' to: '#[' end: 2.
   117     self assert: parser parse: '#[1]' to: '#[' end: 2.
   118 !
   118 !
   119 
   119 
   120 testCompileLiteral2
   120 testCompileLiteral2
   121     | quote |
   121     | quote |
   122     quote := '''' asParser.
   122     quote := '''' asParser.
   123     parser := (quote , $a asParser) compileUsingCompiler:compiler.	
   123     parser := compiler compile: ((quote , $a asParser)).	
   124     self assert: parser parse: '''a'  to: {'''' . $a}.	
   124     self assert: parser parse: '''a'  to: {'''' . $a}.	
   125 !
   125 !
   126 
   126 
   127 testCompileNegate
   127 testCompileNegate
   128     parser := #letter asParser negate star, #letter asParser.
   128     parser := #letter asParser negate star, #letter asParser.
   129     parser := parser compileUsingCompiler:compiler.
   129     parser := compiler compile: (parser).
   130     
   130     
   131     self assert: parser parse: '...a' to: { { $. . $. . $. } . $a }.
   131     self assert: parser parse: '...a' to: { { $. . $. . $. } . $a }.
   132     self assert: parser parse: 'aaa' to: { {} . $a } end: 1.
   132     self assert: parser parse: 'aaa' to: { {} . $a } end: 1.
   133     self assert: parser fail: '...'.
   133     self assert: parser fail: '...'.
   134 !
   134 !
   135 
   135 
   136 testCompileNil
   136 testCompileNil
   137     parser := nil asParser compileUsingCompiler:compiler.
   137     parser := compiler compile: (nil asParser).
   138     
   138     
   139     self assert: parser parse: 'a' to: nil end: 0.
   139     self assert: parser parse: 'a' to: nil end: 0.
   140     self assert: parser parse: '' to: nil end: 0.
   140     self assert: parser parse: '' to: nil end: 0.
   141     
   141     
   142     parser := nil asParser, 'foo' asParser.
   142     parser := nil asParser, 'foo' asParser.
   143     self assert: parser parse: 'foo' to: { nil . 'foo' }
   143     self assert: parser parse: 'foo' to: { nil . 'foo' }
   144 !
   144 !
   145 
   145 
   146 testCompileNot
   146 testCompileNot
   147     parser := #digit asParser not compileUsingCompiler:compiler.
   147     parser := compiler compile: (#digit asParser not).
   148     
   148     
   149     self assert: parser parse: 'a' to: nil end: 0.
   149     self assert: parser parse: 'a' to: nil end: 0.
   150     self assert: parser fail: '1'.
   150     self assert: parser fail: '1'.
   151     self assert: parser parse: '' to: nil end: 0.
   151     self assert: parser parse: '' to: nil end: 0.
   152 
   152 
   153     parser := 'foo' asParser, $: asParser not.
   153     parser := 'foo' asParser, $: asParser not.
   154     parser := parser compileUsingCompiler:compiler.	
   154     parser := compiler compile: (parser).	
   155     self assert: parser parse: 'foo' to: { 'foo'. nil } end: 3.
   155     self assert: parser parse: 'foo' to: { 'foo'. nil } end: 3.
   156     
   156     
   157     parser := 'foo' asParser, $: asParser not, 'bar' asParser.
   157     parser := 'foo' asParser, $: asParser not, 'bar' asParser.
   158     parser := parser compileUsingCompiler:compiler.	
   158     parser := compiler compile: (parser).	
   159     self assert: parser parse: 'foobar' to: { 'foo'. nil . 'bar' } end: 6.
   159     self assert: parser parse: 'foobar' to: { 'foo'. nil . 'bar' } end: 6.
   160 !
   160 !
   161 
   161 
   162 testCompileNot2
   162 testCompileNot2
   163     parser := ($a asParser , $b asParser) not compileUsingCompiler:compiler.
   163     parser := compiler compile: (($a asParser , $b asParser) not).
   164         
   164         
   165     self assert: parser parse: '' to: nil end: 0.
   165     self assert: parser parse: '' to: nil end: 0.
   166     self assert: parser parse: 'a' to: nil end: 0.
   166     self assert: parser parse: 'a' to: nil end: 0.
   167     self assert: parser parse: 'aa' to: nil end: 0.
   167     self assert: parser parse: 'aa' to: nil end: 0.
   168     self assert: parser fail: 'ab'.
   168     self assert: parser fail: 'ab'.
   169 !
   169 !
   170 
   170 
   171 testCompileNot3
   171 testCompileNot3
   172     parser := ('foo' asParser not , 'fee' asParser) compileUsingCompiler:compiler.
   172     parser := compiler compile: (('foo' asParser not , 'fee' asParser)).
   173         
   173         
   174     self assert: parser parse: 'fee' to: #(nil 'fee').
   174     self assert: parser parse: 'fee' to: #(nil 'fee').
   175     self assert: parser fail: 'foo'.
   175     self assert: parser fail: 'foo'.
   176 !
   176 !
   177 
   177 
   178 testCompileNotLiteral
   178 testCompileNotLiteral
   179     parser := 'foo' asParser not compileUsingCompiler:compiler.
   179     parser := compiler compile: ('foo' asParser not).
   180     self assert: parser class methodDictionary size = 1.
   180     self assert: parser class methodDictionary size = 1.
   181 
   181 
   182     self assert: parser parse: 'bar' to: nil end: 0.
   182     self assert: parser parse: 'bar' to: nil end: 0.
   183         
   183         
   184     self assert: parser fail: 'foo'.
   184     self assert: parser fail: 'foo'.
   199     self assert: parser parse: 'foobar' to: { 'foo'. nil . #($b $a $r) } end: 6.
   199     self assert: parser parse: 'foobar' to: { 'foo'. nil . #($b $a $r) } end: 6.
   200     self assert: parser fail: 'foofoo'.
   200     self assert: parser fail: 'foofoo'.
   201 !
   201 !
   202 
   202 
   203 testCompileOptional
   203 testCompileOptional
   204     parser := #digit asParser optional compileUsingCompiler:compiler.
   204     parser := compiler compile: (#digit asParser optional).
   205     
   205     
   206     self assert: parser parse: '1' to: $1.
   206     self assert: parser parse: '1' to: $1.
   207     self assert: parser parse: 'a' to: nil end: 0.
   207     self assert: parser parse: 'a' to: nil end: 0.
   208     
   208     
   209     parser := (#digit asParser optional, #letter asParser) compile.
   209     parser := (#digit asParser optional, #letter asParser) compile.
   210     self assert: parser parse: '1a' to: { $1 . $a }.
   210     self assert: parser parse: '1a' to: { $1 . $a }.
   211     self assert: parser parse: 'a' to: { nil . $a }.
   211     self assert: parser parse: 'a' to: { nil . $a }.
   212 !
   212 !
   213 
   213 
   214 testCompilePlus
   214 testCompilePlus
   215     parser := #letter asParser plus compileUsingCompiler:compiler.
   215     parser := compiler compile: (#letter asParser plus).
   216     
   216     
   217     self assert: parser parse: 'lorem' to: {$l. $o. $r. $e. $m} .
   217     self assert: parser parse: 'lorem' to: {$l. $o. $r. $e. $m} .
   218     self assert: parser parse: 'a123' to: {$a} end: 1.
   218     self assert: parser parse: 'a123' to: {$a} end: 1.
   219     self assert: parser parse: 'ab123' to: {$a . $b} end: 2.
   219     self assert: parser parse: 'ab123' to: {$a . $b} end: 2.
   220 
   220 
   221     self assert: parser fail: ''.
   221     self assert: parser fail: ''.
   222     self assert: parser fail: '123'.
   222     self assert: parser fail: '123'.
   223 !
   223 !
   224 
   224 
   225 testCompilePredicate
   225 testCompilePredicate
   226     parser := #digit asParser compileUsingCompiler:compiler.
   226     parser := compiler compile: (#digit asParser).
   227     
   227     
   228     self assert: parser parse: '1' to: $1.
   228     self assert: parser parse: '1' to: $1.
   229     self assert: parser parse: '0' to: $0.
   229     self assert: parser parse: '0' to: $0.
   230     self assert: parser fail: 'a'.
   230     self assert: parser fail: 'a'.
   231 !
   231 !
   232 
   232 
   233 testCompilePredicate2
   233 testCompilePredicate2
   234     parser := #space asParser compileUsingCompiler:compiler.
   234     parser := compiler compile: (#space asParser).
   235     
   235     
   236     self assert: parser parse: ' ' to: Character space.
   236     self assert: parser parse: ' ' to: Character space.
   237     self assert: parser fail: 'a'.
   237     self assert: parser fail: 'a'.
   238 !
   238 !
   239 
   239 
   240 testCompileSequence
   240 testCompileSequence
   241     parser := (#digit asParser , #letter asParser) compileUsingCompiler:compiler.
   241     parser := compiler compile: ((#digit asParser , #letter asParser)).
   242     
   242     
   243     self assert: parser parse: '1a' to: {$1 .$a}.
   243     self assert: parser parse: '1a' to: {$1 .$a}.
   244     
   244     
   245     
   245     
   246 !
   246 !
   247 
   247 
   248 testCompileSequence2
   248 testCompileSequence2
   249     parser := (#digit asParser , #space asParser , #letter asParser) 
   249     parser := compiler compile: (#digit asParser , #space asParser , #letter asParser).
   250                     compileUsingCompiler:compiler.
   250     
   251     
   251     self assert: parser parse: '9 c' to: {$9 . Character space. $c }.   
   252     self assert: parser parse: '9 c' to: {$9 . Character space. $c }.	
       
   253     self assert: parser fail: '9c'.
   252     self assert: parser fail: '9c'.
   254     
   253 
       
   254     "Modified: / 07-09-2015 / 12:38:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   255 !
   255 !
   256 
   256 
   257 testCompileSequence3
   257 testCompileSequence3
   258     parser := (#any asParser , #any asParser , #any asParser) 
   258     parser := compiler compile: (#any asParser , #any asParser , #any asParser).
   259                     compileUsingCompiler:compiler.
   259     
   260     
   260     self assert: parser parse: 'foo' to: #($f $o $o).   
   261     self assert: parser parse: 'foo' to: #($f $o $o).	
       
   262     self assert: parser fail: 'fo'.
   261     self assert: parser fail: 'fo'.
   263     
   262 
       
   263     "Modified: / 07-09-2015 / 12:38:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   264 !
   264 !
   265 
   265 
   266 testCompileStar
   266 testCompileStar
   267     parser := #letter asParser star compileUsingCompiler:compiler.
   267     parser := compiler compile: (#letter asParser star).
   268     
   268     
   269     self assert: parser parse: 'lorem' to: {$l. $o. $r. $e. $m} .
   269     self assert: parser parse: 'lorem' to: {$l. $o. $r. $e. $m} .
   270     self assert: parser parse: '' to: {}.
   270     self assert: parser parse: '' to: {}.
   271     self assert: parser parse: '123' to: {} end: 0.
   271     self assert: parser parse: '123' to: {} end: 0.
   272     self assert: parser parse: 'ab123' to: {$a . $b} end: 2.
   272     self assert: parser parse: 'ab123' to: {$a . $b} end: 2.
   273 !
   273 !
   274 
   274 
   275 testCompileStarLiteral
   275 testCompileStarLiteral
   276     parser := 'foo' asParser star compileUsingCompiler:compiler.
   276     parser := compiler compile: ('foo' asParser star).
   277     
   277     
   278     self assert: parser parse: 'foo' to: #('foo' ) .
   278     self assert: parser parse: 'foo' to: #('foo' ) .
   279     self assert: parser parse: 'foofoo' to: #('foo' 'foo') .
   279     self assert: parser parse: 'foofoo' to: #('foo' 'foo') .
   280     self assert: parser parse: 'foofoofoo' to: #('foo' 'foo' 'foo') .
   280     self assert: parser parse: 'foofoofoo' to: #('foo' 'foo' 'foo') .
   281     self assert: parser parse: '' to: #().
   281     self assert: parser parse: '' to: #().
   282     self assert: parser parse: 'bar' to: #() end: 0.
   282     self assert: parser parse: 'bar' to: #() end: 0.
   283 !
   283 !
   284 
   284 
   285 testCompileStarPredicate
   285 testCompileStarPredicate
   286     parser := #letter asParser star compileUsingCompiler:compiler.
   286     parser := compiler compile: (#letter asParser star).
   287     
   287     
   288     self assert: parser parse: 'foo' to: #($f $o $o ) .
   288     self assert: parser parse: 'foo' to: #($f $o $o ) .
   289     self assert: parser parse: '' to: #().
   289     self assert: parser parse: '' to: #().
   290     self assert: parser parse: '123' to: #() end: 0.
   290     self assert: parser parse: '123' to: #() end: 0.
   291 !
   291 !
   292 
   292 
   293 testCompileSymbolBlock
   293 testCompileSymbolBlock
   294     parser := (#letter asParser) plus ==> #second.
   294     parser := (#letter asParser) plus ==> #second.
   295     parser := parser compileUsingCompiler:compiler.
   295     parser := compiler compile: (parser).
   296     
   296     
   297     self assert: parser parse: 'foo' to: $o.
   297     self assert: parser parse: 'foo' to: $o.
   298     self assert: parser parse: 'bar' to: $a.
   298     self assert: parser parse: 'bar' to: $a.
   299     self assert: parser fail: ''.
   299     self assert: parser fail: ''.
   300     self should: [ parser parse: 'f' ] raise: Error.
   300     self should: [ parser parse: 'f' ] raise: Error.
   301 !
   301 !
   302 
   302 
   303 testCompileTrim
   303 testCompileTrim
   304     parser := $a asParser trim compileUsingCompiler:compiler.
   304     parser := compiler compile: ($a asParser trim).
   305     
   305     
   306     self assert: parser fail: ''.
   306     self assert: parser fail: ''.
   307     self assert: parser parse: 'a' to: $a.
   307     self assert: parser parse: 'a' to: $a.
   308     self assert: parser parse: '   a' to: $a.
   308     self assert: parser parse: '   a' to: $a.
   309     self assert: parser parse: 'a    ' to: $a.
   309     self assert: parser parse: 'a    ' to: $a.
   313 testCompileTrimmingToken
   313 testCompileTrimmingToken
   314     | token1 token2 |
   314     | token1 token2 |
   315     token1 := (#letter asParser) plus trimmingToken.
   315     token1 := (#letter asParser) plus trimmingToken.
   316     token2 := (#letter asParser) plus trimmingToken.
   316     token2 := (#letter asParser) plus trimmingToken.
   317     
   317     
   318     parser := (token1 , token2) compileUsingCompiler:compiler.
   318     parser := compiler compile: ((token1 , token2)).
   319     
   319     
   320     self assert: parser parse: 'foo bar'.
   320     self assert: parser parse: 'foo bar'.
   321     self assert: parser parse: ' foo bar '.
   321     self assert: parser parse: ' foo bar '.
   322 !
   322 !
   323 
   323 
   324 testCompileTrimmingToken2
   324 testCompileTrimmingToken2
   325     | token1 token2 |
   325     | token1 token2 |
   326     token1 := (#letter asParser) plus trimmingToken.
   326     token1 := (#letter asParser) plus trimmingToken.
   327     token2 := (#letter asParser) plus trimmingToken / 'foo' asParser trimmingToken.
   327     token2 := (#letter asParser) plus trimmingToken / 'foo' asParser trimmingToken.
   328     
   328     
   329     parser := (token1 , token2) compileUsingCompiler:compiler.
   329     parser := compiler compile: ((token1 , token2)).
   330     
   330     
   331     self assert: parser parse: 'foo bar'.
   331     self assert: parser parse: 'foo bar'.
   332     self assert: parser parse: ' foo bar '.
   332     self assert: parser parse: ' foo bar '.
   333 !
   333 !
   334 
   334 
   335 testCompileTrimmingToken3
   335 testCompileTrimmingToken3
   336     | token1 token2 |
   336     | token1 token2 |
   337     token1 := ($a asParser, $b asParser) trimmingToken name: 'token1'.
   337     token1 := ($a asParser, $b asParser) trimmingToken name: 'token1'.
   338     token2 := (token1 not, $c asParser) trimmingToken name: 'token2'.
   338     token2 := (token1 not, $c asParser) trimmingToken name: 'token2'.
   339     
   339     
   340     parser := (token1 / token2) compileUsingCompiler:compiler.
   340     parser := compiler compile: ((token1 / token2)).
   341 
   341 
   342     self assert: (parser class methodDictionary includesKey: #'token1').
   342     self assert: (parser class methodDictionary includesKey: #'token1').
   343     self assert: (parser class methodDictionary includesKey: #'token1_fast').
   343     self assert: (parser class methodDictionary includesKey: #'token1_fast').
   344     
   344     
   345     self assert: parser parse: 'ab'.
   345     self assert: parser parse: 'ab'.
   353 ! !
   353 ! !
   354 
   354 
   355 !PPCUniversalTest methodsFor:'tests - extra'!
   355 !PPCUniversalTest methodsFor:'tests - extra'!
   356 
   356 
   357 testCompileSmalltalkToken
   357 testCompileSmalltalkToken
   358     parser := (#letter asParser , ((#letter asParser / #digit asParser) star)) 
   358     parser := compiler compile: (#letter asParser , ((#letter asParser / #digit asParser) star)) smalltalkToken.
   359                     smalltalkToken compileUsingCompiler:compiler.
       
   360     
   359     
   361     self assert: parser parse: 'foo'.
   360     self assert: parser parse: 'foo'.
   362     self assert: result inputValue = 'foo'.
   361     self assert: result inputValue = 'foo'.
   363     self assert: parser parse: 'a'.
   362     self assert: parser parse: 'a'.
   364     self assert: result inputValue = 'a'.
   363     self assert: result inputValue = 'a'.
   380         "and yet, another comment"
   379         "and yet, another comment"
   381 
   380 
   382         "one more to make sure :)"
   381         "one more to make sure :)"
   383     '.
   382     '.
   384     self assert: result inputValue = 'foo'.
   383     self assert: result inputValue = 'foo'.
       
   384 
       
   385     "Modified: / 07-09-2015 / 12:38:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   385 !
   386 !
   386 
   387 
   387 testCycle
   388 testCycle
   388     | p1 block |
   389     | p1 block |
   389     
   390     
   390     p1 := PPDelegateParser new.
   391     p1 := PPDelegateParser new.
   391     block := ${ asParser, p1, $} asParser / nil asParser.
   392     block := ${ asParser, p1, $} asParser / nil asParser.
   392     p1 setParser: block.
   393     p1 setParser: block.
   393     
   394     
   394     parser := block compileUsingCompiler:compiler.
   395     parser := compiler compile: (block).
   395     self assert: parser parse: '{}' to: { ${. nil . $} }.
   396     self assert: parser parse: '{}' to: { ${. nil . $} }.
   396     self assert: parser parse: '{{}}' to: { ${. { ${ . nil . $} } . $} }.
   397     self assert: parser parse: '{{}}' to: { ${. { ${ . nil . $} } . $} }.
   397     
   398     
   398 !
   399 !
   399 
   400 
   400 testSmalltalkToken
   401 testSmalltalkToken
   401     parser := (#letter asParser , (#digit asParser / #letter asParser) star) 
   402     parser := compiler compile: (#letter asParser , (#digit asParser / #letter asParser) star) smalltalkToken.
   402                     smalltalkToken compileUsingCompiler:compiler.
       
   403     
   403     
   404     self assert: parser class methodDictionary size = 5.
   404     self assert: parser class methodDictionary size = 5.
   405     self assert: parser parse: 'foo'.
   405     self assert: parser parse: 'foo'.
   406     self assert: result inputValue = 'foo'.
   406     self assert: result inputValue = 'foo'.
   407     self assert: context invocationCount = 8.
   407     self assert: context invocationCount = 8.
   408     self assert: context rememberCount = 0.
   408     self assert: context rememberCount = 0.
   409     self assert: context lwRememberCount = 0.
   409     self assert: context lwRememberCount = 0.
   410     self assert: context lwRestoreCount = 0.	
   410     self assert: context lwRestoreCount = 0.
       
   411 
       
   412     "Modified: / 07-09-2015 / 12:38:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   411 !
   413 !
   412 
   414 
   413 testSmalltalkToken2
   415 testSmalltalkToken2
   414     id := (#letter asParser, (#digit asParser / #letter asParser) star)
   416     id := (#letter asParser, (#digit asParser / #letter asParser) star)
   415         name: 'identifier';
   417         name: 'identifier';
   417         
   419         
   418     parser := (id wrapped, $: asParser) smalltalkToken 
   420     parser := (id wrapped, $: asParser) smalltalkToken 
   419         name: 'kw';
   421         name: 'kw';
   420         yourself.
   422         yourself.
   421     
   423     
   422     parser := parser compileUsingCompiler:compiler.
   424     parser := compiler compile: (parser).
   423     
   425     
   424     self assert: parser parse: 'foo:'.
   426     self assert: parser parse: 'foo:'.
   425     self assert: result inputValue = 'foo:'.
   427     self assert: result inputValue = 'foo:'.
   426 !
   428 !
   427 
   429 
   428 testToken
   430 testToken
   429     parser := (#letter asParser , (#digit asParser / #letter asParser) star) flatten 
   431     parser := compiler compile: (#letter asParser , (#digit asParser / #letter asParser) star) flatten.
   430                     compileUsingCompiler:compiler.
   432 
   431     
   433     
   432     self assert: parser parse: 'foo' to: 'foo'.
   434     self assert: parser parse: 'foo' to: 'foo'.
   433     self assert: parser parse: 'a' to: 'a'.
   435     self assert: parser parse: 'a' to: 'a'.
   434     self assert: parser parse: 'f123a' to: 'f123a'.
   436     self assert: parser parse: 'f123a' to: 'f123a'.
   435     self assert: parser fail: ''.
   437     self assert: parser fail: ''.
       
   438 
       
   439     "Modified: / 07-09-2015 / 12:38:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   436 !
   440 !
   437 
   441 
   438 testToken2
   442 testToken2
   439     parser := (#letter asParser , (#digit asParser / #letter asParser) star) token 
   443     parser := compiler compile: (#letter asParser , (#digit asParser / #letter asParser) star) token.
   440                     compileUsingCompiler:compiler.
   444 
   441     
   445     
   442     self assert: parser class methodDictionary size = 4.
   446     self assert: parser class methodDictionary size = 4.
   443     self assert: parser parse: 'foo'.
   447     self assert: parser parse: 'foo'.
   444     self assert: result inputValue = 'foo'.
   448     self assert: result inputValue = 'foo'.
   445     self assert: context invocationCount = 6.
   449     self assert: context invocationCount = 6.
   446     self assert: context rememberCount = 0.
   450     self assert: context rememberCount = 0.
   447     self assert: context lwRememberCount = 0.
   451     self assert: context lwRememberCount = 0.
   448     self assert: context lwRestoreCount = 0.	
   452     self assert: context lwRestoreCount = 0.
       
   453 
       
   454     "Modified: / 07-09-2015 / 12:37:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   449 !
   455 !
   450 
   456 
   451 testTrimmingToken
   457 testTrimmingToken
   452     parser := (#letter asParser , (#digit asParser / #letter asParser) star) 
   458     parser := compiler compile: (#letter asParser , (#digit asParser / #letter asParser) star) 
   453                     trimmingToken compileUsingCompiler:compiler.
   459                     trimmingToken.
   454 
   460 
   455     self assert: parser class methodDictionary size = 4.
   461     self assert: parser class methodDictionary size = 4.
   456     self assert: parser parse: 'foo'.
   462     self assert: parser parse: 'foo'.
   457     self assert: result inputValue = 'foo'.
   463     self assert: result inputValue = 'foo'.
   458 
   464 
   459     self assert: context invocationCount = 6.
   465     self assert: context invocationCount = 6.
   460     self assert: context rememberCount = 0.
   466     self assert: context rememberCount = 0.
   461     self assert: context lwRememberCount = 0.
   467     self assert: context lwRememberCount = 0.
   462     self assert: context lwRestoreCount = 0.	
   468     self assert: context lwRestoreCount = 0.    
   463 
   469 
   464     self assert: parser parse: ' foo '.
   470     self assert: parser parse: ' foo '.
   465     self assert: result inputValue = 'foo'.
   471     self assert: result inputValue = 'foo'.
   466 
   472 
   467 
   473 
   469     self assert: parser fail: '123'.
   475     self assert: parser fail: '123'.
   470 
   476 
   471     self assert: context invocationCount = 1.
   477     self assert: context invocationCount = 1.
   472     self assert: context rememberCount = 0.
   478     self assert: context rememberCount = 0.
   473     self assert: context lwRememberCount = 0.
   479     self assert: context lwRememberCount = 0.
   474     self assert: context lwRestoreCount = 0.	
   480     self assert: context lwRestoreCount = 0.    
   475 
   481 
   476 
   482 
   477     self assert: parser fail: ''.
   483     self assert: parser fail: ''.
       
   484 
       
   485     "Modified: / 07-09-2015 / 12:37:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   478 !
   486 !
   479 
   487 
   480 testTrimmingToken2
   488 testTrimmingToken2
   481 
   489 
   482     parser := 'foo' asParser trimmingToken , 'bar' asParser trimmingToken 
   490     parser := compiler compile:  ('foo' asParser trimmingToken , 'bar' asParser trimmingToken).
   483                     compileUsingCompiler:compiler.
   491 
   484     
   492     
   485     self assert: parser parse: 'foobar'.
   493     self assert: parser parse: 'foobar'.
   486     self assert: result first inputValue = 'foo'.
   494     self assert: result first inputValue = 'foo'.
   487     self assert: result second inputValue = 'bar'.	
   495     self assert: result second inputValue = 'bar'.      
   488     self assert: context invocationCount = 3.
   496     self assert: context invocationCount = 3.
   489 
   497 
   490     self assert: parser parse: ' foobar'.
   498     self assert: parser parse: ' foobar'.
   491     self assert: result first inputValue = 'foo'.
   499     self assert: result first inputValue = 'foo'.
   492     self assert: result second inputValue = 'bar'.	
   500     self assert: result second inputValue = 'bar'.      
   493     self assert: context invocationCount = 3.
   501     self assert: context invocationCount = 3.
   494     self assert: (context invocations anySatisfy: [ :e | e beginsWith: 'token' ]).	
   502     self assert: (context invocations anySatisfy: [ :e | e beginsWith: 'token' ]).      
   495         
   503         
   496     self assert: parser fail: 'bar'.
   504     self assert: parser fail: 'bar'.
   497     self assert: context invocationCount = 2.
   505     self assert: context invocationCount = 2.
   498 "	self assert: (context invocations noneSatisfy: [ :e | e beginsWith: 'token' ]).	"
   506 "       self assert: (context invocations noneSatisfy: [ :e | e beginsWith: 'token' ]).         "
   499     
   507 
       
   508     "Modified: / 07-09-2015 / 12:37:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   500 !
   509 !
   501 
   510 
   502 testTrimmingToken3
   511 testTrimmingToken3
   503 
   512 
   504     parser := ('foo' asParser trimmingToken / 'bar' asParser trimmingToken) 
   513     parser := compiler compile: ('foo' asParser trimmingToken / 'bar' asParser trimmingToken).
   505                     compileUsingCompiler:compiler.
       
   506     
   514     
   507     self assert: parser parse: 'foo'.
   515     self assert: parser parse: 'foo'.
   508     self assert: result inputValue = 'foo'.
   516     self assert: result inputValue = 'foo'.
   509     self assert: context invocationCount = 2.
   517     self assert: context invocationCount = 2.
   510 
   518 
   511     self assert: parser parse: ' bar'.
   519     self assert: parser parse: ' bar'.
   512     self assert: result inputValue = 'bar'.	
   520     self assert: result inputValue = 'bar'.     
   513     self assert: context invocationCount = 2.
   521     self assert: context invocationCount = 2.
   514     self assert: (context invocations anySatisfy: [ :e | e beginsWith: 'token' ]).	
   522     self assert: (context invocations anySatisfy: [ :e | e beginsWith: 'token' ]).      
   515         
   523         
   516     self assert: parser fail: 'baz'.
   524     self assert: parser fail: 'baz'.
   517     self assert: context invocationCount = 2.
   525     self assert: context invocationCount = 2.
   518     
   526     
   519     self assert: parser fail: 'zaz'.
   527     self assert: parser fail: 'zaz'.
   520     self assert: context invocationCount = 1.
   528     self assert: context invocationCount = 1.
   521     self assert: (context invocations noneSatisfy: [ :e | e beginsWith: 'token' ]).	
   529     self assert: (context invocations noneSatisfy: [ :e | e beginsWith: 'token' ]).
       
   530 
       
   531     "Modified: / 07-09-2015 / 12:37:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   522 !
   532 !
   523 
   533 
   524 testTrimmingTokenNested
   534 testTrimmingTokenNested
   525     | identifier kw |
   535     | identifier kw |
   526     kw := 'false' asParser trimmingToken name: #kw.
   536     kw := 'false' asParser trimmingToken name: #kw.
   527     identifier := (kw not, (#letter asParser, #word asParser star)) trimmingToken name: #identifier.
   537     identifier := (kw not, (#letter asParser, #word asParser star)) trimmingToken name: #identifier.
   528     
   538     
   529     parser := identifier / kw.
   539     parser := identifier / kw.
   530     parser := parser compileUsingCompiler:compiler.
   540     parser := compiler compile: (parser).
   531     self assert: parser class methodDictionary size = 5.
   541     self assert: parser class methodDictionary size = 5.
   532 
   542 
   533     self assert: parser parse: 'foo'.
   543     self assert: parser parse: 'foo'.
   534     self assert: result inputValue = 'foo'.
   544     self assert: result inputValue = 'foo'.
   535 
   545 
   541     | identifier kw |
   551     | identifier kw |
   542     kw := 'false' asParser trimmingToken name: #kw.
   552     kw := 'false' asParser trimmingToken name: #kw.
   543     identifier := (kw not, (#letter asParser, #word asParser star)) trimmingToken name: #identifier.
   553     identifier := (kw not, (#letter asParser, #word asParser star)) trimmingToken name: #identifier.
   544     
   554     
   545     parser := identifier / kw.
   555     parser := identifier / kw.
   546     parser := parser compileUsingCompiler:compiler.
   556     parser := compiler compile: (parser).
   547     self assert: parser class methodDictionary size = 5.
   557     self assert: parser class methodDictionary size = 5.
   548 
   558 
   549     self assert: parser parse: 'foo'.
   559     self assert: parser parse: 'foo'.
   550     self assert: result inputValue = 'foo'.
   560     self assert: result inputValue = 'foo'.
   551 
   561 
   557     | identifier kw |
   567     | identifier kw |
   558     kw := ('false' asParser, #word asParser not) trimmingToken name: #kw.
   568     kw := ('false' asParser, #word asParser not) trimmingToken name: #kw.
   559     identifier := (kw not, (#letter asParser, #word asParser star)) trimmingToken name: #identifier.
   569     identifier := (kw not, (#letter asParser, #word asParser star)) trimmingToken name: #identifier.
   560     
   570     
   561     parser := identifier / kw.
   571     parser := identifier / kw.
   562     parser := parser compileUsingCompiler:compiler.
   572     parser := compiler compile: (parser).
   563     self assert: parser class methodDictionary size = 8.
   573     self assert: parser class methodDictionary size = 8.
   564     self assert: (parser class methodDictionary values anySatisfy: [ :m | m selector = #kw ]).
   574     self assert: (parser class methodDictionary values anySatisfy: [ :m | m selector = #kw ]).
   565     self assert: (parser class methodDictionary values anySatisfy: [ :m | m selector = #kw_fast ]).
   575     self assert: (parser class methodDictionary values anySatisfy: [ :m | m selector = #kw_fast ]).
   566 
   576 
   567     self assert: parser parse: 'foo'.
   577     self assert: parser parse: 'foo'.
   574 ! !
   584 ! !
   575 
   585 
   576 !PPCUniversalTest methodsFor:'tests - ids'!
   586 !PPCUniversalTest methodsFor:'tests - ids'!
   577 
   587 
   578 setUp
   588 setUp
   579     options := (PPCCompilationOptions default)
   589     options := (PPCCompilationOptions new)
   580             profile:true;
   590             profile:true;
   581             debug:true;
   591             debug:true;
   582             tokenize:false;
   592             tokenize:false;
   583             yourself.
   593             yourself.
   584     compiler := PPCCompiler new.
   594     compiler := PPCCompiler new.
   585     compiler context options:options
   595     compiler context options:options
   586 
   596 
   587     "Modified: / 04-09-2015 / 16:21:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   597     "Modified: / 07-09-2015 / 10:22:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   588 ! !
   598 ! !
   589 
   599 
   590 !PPCUniversalTest class methodsFor:'documentation'!
   600 !PPCUniversalTest class methodsFor:'documentation'!
   591 
   601 
   592 version_HG
   602 version_HG