compiler/tests/PPCUnivarsalGuardTest.st
changeset 538 16e8536f5cfb
parent 537 fb212e14d1f4
equal deleted inserted replaced
537:fb212e14d1f4 538:16e8536f5cfb
    20 context	
    20 context	
    21     ^ context := PPCProfilingContext new
    21     ^ context := PPCProfilingContext new
    22 !
    22 !
    23 
    23 
    24 setUp
    24 setUp
    25     options := (PPCCompilationOptions default)
    25     options := (PPCCompilationOptions new)
    26             profile:true;
    26             profile:true;
    27             tokenize:false;
    27             tokenize:false;
    28             yourself.
    28             yourself.
    29     compiler := PPCCompiler new.
    29     compiler := PPCCompiler new.
    30     compiler context options:options
    30     compiler context options:options
    31 
    31 
    32     "Modified: / 04-09-2015 / 16:21:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    32     "Modified: / 07-09-2015 / 10:22:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    33 !
    33 !
    34 
    34 
    35 tearDown
    35 tearDown
    36     | parserClass |
    36     | parserClass |
    37 
    37 
    42 ! !
    42 ! !
    43 
    43 
    44 !PPCUnivarsalGuardTest methodsFor:'tests - guard'!
    44 !PPCUnivarsalGuardTest methodsFor:'tests - guard'!
    45 
    45 
    46 testChoiceGuard
    46 testChoiceGuard
    47     parser := ('foo' asParser trimmingToken / 'bar' asParser trimmingToken 
    47     parser := compiler compile: ('foo' asParser trimmingToken / 'bar' asParser trimmingToken 
    48                     / $d asParser trimmingToken plus) compileUsingCompiler:compiler.
    48                     / $d asParser trimmingToken plus).
    49     
    49     
    50     self assert: parser parse: 'foo'.
    50     self assert: parser parse: 'foo'.
    51     self assert: result inputValue = 'foo'.	
    51     self assert: result inputValue = 'foo'.     
    52     self assert: (context invocations anySatisfy: [ :e | e beginsWith: 'token' ]).
    52     self assert: (context invocations anySatisfy: [ :e | e beginsWith: 'token' ]).
    53 
    53 
    54     self assert: parser parse: 'bar'.
    54     self assert: parser parse: 'bar'.
    55     self assert: result inputValue = 'bar'.	
    55     self assert: result inputValue = 'bar'.     
    56 
    56 
    57     self assert: parser parse: ' foo'.
    57     self assert: parser parse: ' foo'.
    58     self assert: result inputValue = 'foo'.	
    58     self assert: result inputValue = 'foo'.     
    59 
    59 
    60     self assert: parser parse: '  d'.
    60     self assert: parser parse: '  d'.
    61     self assert: result first inputValue = 'd'.	
    61     self assert: result first inputValue = 'd'.         
    62 
    62 
    63     self assert: parser fail: ''.
    63     self assert: parser fail: ''.
    64     self assert: (context invocations noneSatisfy: [ :e | e beginsWith: 'predicate' ]).
    64     self assert: (context invocations noneSatisfy: [ :e | e beginsWith: 'predicate' ]).
    65     self assert: (context invocations noneSatisfy: [ :e | e beginsWith: 'token' ]).
    65     self assert: (context invocations noneSatisfy: [ :e | e beginsWith: 'token' ]).
    66 
    66 
    67     self assert: parser fail: 'zorg'.		
    67     self assert: parser fail: 'zorg'.           
    68     self assert: (context invocations noneSatisfy: [ :e | e beginsWith: 'token' ]).
    68     self assert: (context invocations noneSatisfy: [ :e | e beginsWith: 'token' ]).
       
    69 
       
    70     "Modified: / 07-09-2015 / 12:39:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    69 !
    71 !
    70 
    72 
    71 testEmptyChoiceGuard
    73 testEmptyChoiceGuard
    72     parser := ('foo' asParser trimmingToken / 'bar' asParser trimmingToken 
    74     parser := compiler compile: ('foo' asParser trimmingToken / 'bar' asParser trimmingToken 
    73                     / $d asParser trimmingToken star) compileUsingCompiler:compiler.
    75                     / $d asParser trimmingToken star).
    74     
    76     
    75     self assert: parser parse: 'foo'.
    77     self assert: parser parse: 'foo'.
    76     self assert: result inputValue = 'foo'.	
    78     self assert: result inputValue = 'foo'.     
    77 
    79 
    78     self assert: parser parse: 'bar'.
    80     self assert: parser parse: 'bar'.
    79     self assert: result inputValue = 'bar'.	
    81     self assert: result inputValue = 'bar'.     
    80 
    82 
    81     self assert: parser parse: ' foo'.
    83     self assert: parser parse: ' foo'.
    82     self assert: result inputValue = 'foo'.	
    84     self assert: result inputValue = 'foo'.     
    83 
    85 
    84     self assert: parser parse: '  d'.
    86     self assert: parser parse: '  d'.
    85     self assert: result first inputValue = 'd'.	
    87     self assert: result first inputValue = 'd'.         
    86 
    88 
    87     self assert: parser parse: ''.
    89     self assert: parser parse: ''.
    88 
    90 
    89     self assert: parser parse: 'zorg' end: 0.	
    91     self assert: parser parse: 'zorg' end: 0.
       
    92 
       
    93     "Modified: / 07-09-2015 / 12:39:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    90 !
    94 !
    91 
    95 
    92 testGuardSmalltlakToken
    96 testGuardSmalltlakToken
    93     parser := (#letter asParser , #word asParser star) smalltalkToken 
    97     parser := compiler compile: (#letter asParser , #word asParser star) smalltalkToken.
    94                     compileUsingCompiler:compiler.
    98 
    95     
       
    96     self assert: parser parse: 'bar'.
    99     self assert: parser parse: 'bar'.
    97     self assert: (context invocations anySatisfy: [ :e | e beginsWith: 'seq' ]).
   100     self assert: (context invocations anySatisfy: [ :e | e beginsWith: 'seq' ]).
    98     
   101     
    99     self assert: parser fail: '123'.
   102     self assert: parser fail: '123'.
   100     self assert: (context invocations noneSatisfy: [ :e | e beginsWith: 'seq' ]).
   103     self assert: (context invocations noneSatisfy: [ :e | e beginsWith: 'seq' ]).
       
   104 
       
   105     "Modified (format): / 07-09-2015 / 12:39:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   101 !
   106 !
   102 
   107 
   103 testSequenceGuard
   108 testSequenceGuard
   104     parser := ((#any asParser , #any asParser) wrapped , (#any asParser , #any asParser)) 
   109     parser := compiler compile: ((#any asParser , #any asParser) wrapped , (#any asParser , #any asParser)).
   105                     compileUsingCompiler:compiler.
   110 
   106     
   111     self assert: parser parse: 'fooo' to: #(#($f $o) #($o $o)).         
   107     self assert: parser parse: 'fooo' to: #(#($f $o) #($o $o)).	
   112     self assert: parser parse: 'fo oo' to: #(#($f $o) #($  $o)) end: 4.         
   108     self assert: parser parse: 'fo oo' to: #(#($f $o) #($  $o)) end: 4.	
       
   109     self assert: parser fail: 'fo'.
   113     self assert: parser fail: 'fo'.
   110     
   114 
       
   115     "Modified: / 07-09-2015 / 12:39:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   111 !
   116 !
   112 
   117 
   113 testTrimmerGuard
   118 testTrimmerGuard
   114     parser := $a asParser trim , $b asParser compileUsingCompiler:compiler.
   119     parser := compiler compile: ($a asParser trim , $b asParser).
   115     
   120     
   116     self assert: parser parse: 'ab'.
   121     self assert: parser parse: 'ab'.
   117     self assert: parser parse: ' ab'.
   122     self assert: parser parse: ' ab'.
   118 ! !
   123 ! !
   119 
   124