compiler/tests/PPCLL1Test.st
changeset 465 f729f6cd3c76
parent 463 d4014e0a47a0
parent 464 f6d77fee9811
child 466 ac2d987a03d3
equal deleted inserted replaced
463:d4014e0a47a0 465:f729f6cd3c76
     1 "{ Package: 'stx:goodies/petitparser/compiler/tests' }"
       
     2 
       
     3 "{ NameSpace: Smalltalk }"
       
     4 
       
     5 PPAbstractParserTest subclass:#PPCLL1Test
       
     6 	instanceVariableNames:'parser result context node compiler id node2 id2 id1 node1 node3
       
     7 		arguments configuration'
       
     8 	classVariableNames:''
       
     9 	poolDictionaries:''
       
    10 	category:'PetitCompiler-Tests-Core'
       
    11 !
       
    12 
       
    13 !PPCLL1Test methodsFor:'as yet unclassified'!
       
    14 
       
    15 assert: p parse: whatever
       
    16     ^ result := super assert: p parse: whatever.
       
    17 !
       
    18 
       
    19 cleanClass
       
    20     | parserClass |
       
    21     parserClass := (Smalltalk at: arguments name ifAbsent: [nil]).
       
    22     parserClass notNil ifTrue:[ 
       
    23         self flag: 'uncomment'.
       
    24 "		parserClass removeFromSystem"
       
    25     ].
       
    26 !
       
    27 
       
    28 context	
       
    29     ^ context := PPCProfilingContext new
       
    30 !
       
    31 
       
    32 parse: whatever
       
    33     ^ result := super parse: whatever.
       
    34 !
       
    35 
       
    36 setUp
       
    37     arguments := PPCArguments default
       
    38         profile: true;
       
    39         yourself.
       
    40         
       
    41     configuration := PPCLL1Configuration new
       
    42         arguments: arguments;
       
    43         yourself.
       
    44         
       
    45     self cleanClass.
       
    46 !
       
    47 
       
    48 tearDown
       
    49     self cleanClass
       
    50 !
       
    51 
       
    52 testChoiceOrder
       
    53     parser := (
       
    54         'a' asParser token, 'b' asParser token / 
       
    55         'a' asParser token) 
       
    56         compileWithConfiguration: configuration.
       
    57     
       
    58     self assert: parser parse: 'ab'.
       
    59     self assert: result first inputValue = 'a'.
       
    60     self assert: result second inputValue = 'b'.
       
    61 
       
    62     self assert: parser parse: 'a'.
       
    63     self assert: result inputValue = 'a'.
       
    64 
       
    65     self assert: parser fail: '_'.
       
    66     
       
    67 !
       
    68 
       
    69 testChoiceOrder2
       
    70     | p1 p2 |
       
    71     p1 := 'a' asParser token, 'b' asParser token.
       
    72     p2 := 'b' asParser token / 'a' asParser token.
       
    73     
       
    74     parser := p1 / p2	compileWithConfiguration: configuration.
       
    75     
       
    76     self assert: parser parse: 'ab'.
       
    77     self assert: result first inputValue = 'a'.
       
    78     self assert: result second inputValue = 'b'.
       
    79 
       
    80     self assert: parser parse: 'a'.
       
    81     self assert: result inputValue = 'a'.
       
    82 
       
    83     self assert: parser parse: 'b'.
       
    84     self assert: result inputValue = 'b'.
       
    85 
       
    86     self assert: parser fail: 'c'.
       
    87     
       
    88 !
       
    89 
       
    90 testChoiceOrder3
       
    91     | p1 p2 a1 a2 |
       
    92     a1 := 'a' asParser token name: 't1'; yourself.
       
    93     a2 := 'a' asParser token name: 't2'; yourself.
       
    94     
       
    95     p1 := a1, 'b' asParser token.
       
    96     p2 := a2.
       
    97     
       
    98     parser := p1 / p2	compileWithConfiguration: configuration.
       
    99     
       
   100     self assert: parser parse: 'ab'.
       
   101     self assert: result first inputValue = 'a'.
       
   102     self assert: result second inputValue = 'b'.
       
   103 
       
   104     self assert: parser parse: 'a'.
       
   105     self assert: result inputValue = 'a'.
       
   106 
       
   107     self assert: parser fail: 'b'.
       
   108     
       
   109 !
       
   110 
       
   111 testChoiceOrder4
       
   112     | p1 p2 a1 a2 |
       
   113     a1 := 'a' asParser token name: 't1'; yourself.
       
   114     a2 := 'a' asParser token name: 't2'; yourself.
       
   115     
       
   116     p1 := a1, 'b' asParser token.
       
   117     p2 := 'b' asParser token / a2.
       
   118     
       
   119     parser := p1 / p2	compileWithConfiguration: configuration.
       
   120     
       
   121     self assert: parser parse: 'ab'.
       
   122     self assert: result first inputValue = 'a'.
       
   123     self assert: result second inputValue = 'b'.
       
   124 
       
   125     self assert: parser parse: 'a'.
       
   126     self assert: result inputValue = 'a'.
       
   127 
       
   128     self assert: parser parse: 'b'.
       
   129     self assert: result inputValue = 'b'.
       
   130 
       
   131     self assert: parser fail: 'c'.
       
   132     
       
   133 !
       
   134 
       
   135 testCompileChoice
       
   136     parser := ('foo' asParser / 'bar' asParser) compileWithConfiguration: configuration.
       
   137     
       
   138     self assert: parser parse: 'foo' to: 'foo'.
       
   139     self assert: parser parse: 'bar' to: 'bar'.
       
   140     self assert: parser fail: '_'.
       
   141     
       
   142 !
       
   143 
       
   144 testCompileChoice2
       
   145     parser := ('foo' asParser token trim / 'bar' asParser token trim) compileWithConfiguration: configuration.
       
   146     
       
   147     self assert: parser parse: 'foo'.
       
   148     self assert: result inputValue = 'foo'.
       
   149     self assert: parser parse: 'bar'.
       
   150     self assert: result inputValue = 'bar'.
       
   151     self assert: parser fail: '_'.
       
   152     
       
   153 !
       
   154 
       
   155 testCompileLiteral
       
   156     parser := 'foo' asParser token compileWithConfiguration: configuration.
       
   157     
       
   158     self assert: parser parse: 'foo'.
       
   159     self assert: result inputValue = 'foo'.
       
   160     self assert: parser fail: 'boo'.
       
   161 !
       
   162 
       
   163 testCompileSequence
       
   164     parser := ('foo' asParser token), ('bar' asParser token) 
       
   165         compileWithConfiguration: configuration.
       
   166     
       
   167     self assert: parser parse: 'foobar'.
       
   168     self assert: result first inputValue = 'foo'.
       
   169     self assert: result second inputValue = 'bar'.
       
   170 !
       
   171 
       
   172 testCompileTokenComplex2
       
   173     |  a b argumentsWith  |
       
   174     "based on the PPSmalltlakGrammar>>blockArgumentsWith"
       
   175     a := $| asParser smalltalkToken
       
   176         yourself.
       
   177     b := $] asParser smalltalkToken
       
   178         yourself.		
       
   179     argumentsWith := (a / b and ==> [:t | ]) wrapped
       
   180         name: 'argumentsWith'; 
       
   181         yourself.
       
   182 
       
   183     parser := argumentsWith compileWithConfiguration: configuration.
       
   184     self assert: parser parse: '|'.
       
   185 
       
   186     parser := argumentsWith compileWithConfiguration: configuration.
       
   187     self assert: parser parse: ']'.
       
   188 !
       
   189 
       
   190 testCompileTokenComplex3
       
   191     | choice1 choice2 a1 b1 a2 b2 tricky |
       
   192     a1 := $| asParser token
       
   193         yourself.
       
   194     b1 := $] asParser token
       
   195         yourself.		
       
   196     choice1 := (a1 / b1) wrapped
       
   197         name: 'choice1'; 
       
   198         yourself.
       
   199 
       
   200     a2 := $| asParser token
       
   201         yourself.
       
   202     b2 := $] asParser token
       
   203         yourself.		
       
   204     choice2 := (a2 / b2) wrapped
       
   205         name: 'choice1'; 
       
   206         yourself.
       
   207     
       
   208     tricky := (a1 asParser, choice1) / (b2 asParser, choice2).
       
   209 
       
   210     parser := tricky compileWithConfiguration: configuration.
       
   211     self assert: parser parse: '||'.
       
   212 
       
   213     parser := tricky compileWithConfiguration: configuration.
       
   214     self assert: parser parse: '|]'.
       
   215 
       
   216     parser := tricky compileWithConfiguration: configuration.
       
   217     self assert: parser parse: ']|'.
       
   218 
       
   219     parser := tricky compileWithConfiguration: configuration.
       
   220     self assert: parser parse: ']]'.
       
   221 !
       
   222 
       
   223 testCompileTrim
       
   224     parser := 'foo' asParser token trim end compileWithConfiguration: configuration.
       
   225     
       
   226     self assert: parser parse: 'foo'.
       
   227     self assert: result inputValue = 'foo'.
       
   228 
       
   229     self assert: parser parse: 'foo  '.
       
   230     self assert: result inputValue = 'foo'.
       
   231 
       
   232 
       
   233     self assert: parser parse: '  foo'.
       
   234     self assert: result inputValue = 'foo'.
       
   235 
       
   236     self assert: parser fail: 'boo'.
       
   237 !
       
   238 
       
   239 testTokenName
       
   240     | token |
       
   241     token := 'foo' asParser token name: 'fooToken'; yourself.
       
   242     parser := token plus
       
   243         compileWithConfiguration: configuration.
       
   244 
       
   245     self assert: parser parse: 'foofoo'.
       
   246     self assert: result first inputValue = 'foo'.
       
   247     self assert: result second inputValue = 'foo'.
       
   248     self assert: (parser class methodDictionary includesKey: #fooToken).
       
   249 ! !
       
   250