f0815a8971ed8f9766b9685c74315898f0a29fb8
[sbp.git] / tests / regression.tc
1 //testcase {
2 //    input  "x";
3 //    output "a1:{x}";
4 //
5 //    s ::= a        => a1
6 //    a ::= s        => s1
7 //    a ::= ^"x"
8 //}
9 //
10 //testcase {
11 //    input  "x";
12 //    output "x";
13 //    output "s2:{s2:{s0 s0} x}";
14 //    output "s2:{s0 x}";
15 //
16 //
17 //    s ::= s s      => s2
18 //    s ::= ^"x"
19 //    s ::= ()       => s0
20 //}
21
22 tibcase {
23     input 
24 "
25   hello 
26     there
27     how
28       { are }
29 ";
30     output "yep:{are}";
31
32     s        ::= { Hello } => "yep"
33     w        ::= " " | "\n"
34     ws      !::= w**
35     Hello    ::= "hello" ws { "there" "how" { "are" } }
36 }
37
38
39 testcase {
40     input "ab c";
41     output "1:{{a b} {c}}";
42
43     s   ::= ids
44     ids ::= id (" " ids &~ id ~[]*) => "1"
45           | id (    ids &~ id ~[]*) => "2"
46           | id
47     id  ::= [a-z]++
48 }
49
50 testcase {
51     input "ab c";
52
53     output "2:{{a} 1:{{b} {c}}}";
54     output "1:{{a b} {c}}";
55
56     s   ::= ids
57     ids ::= id " " ids => "1"
58           | id     ids => "2"
59           | id
60     id  ::= [a-z]+
61 }
62
63 testcase {
64     input "aaabbbccc";
65     output "ab";
66
67     s   ::= ab & dc
68     ab  ::= a b       => ab
69     dc  ::= d c       => dc
70     a   ::= "a" a     | ()
71     b   ::= "b" b "c" | ()
72     c   ::= "c" c     | ()
73     d   ::= "a" d "b" | ()
74 }
75
76 testcase {
77     input "aaabbbbccc";
78
79     s   ::= ab & dc
80     ab !::= a b       => ab
81     dc !::= d c       => dc
82     a   ::= "a" a     | ()
83     b   ::= "b" b "c" | ()
84     c   ::= "c" c     | ()
85     d   ::= "a" d "b" | ()
86 }
87
88 testcase {
89     input "12111211";
90     output "ac:{{2 1 2 1}}";
91     //output "a:{{2 1 2 1}}";
92     //output "c:{{c:{1 1} c:{1 1}}}";
93
94     s ::= ab => "ab"
95         | ac => "ac"
96         | bc => "bc"
97         //| a  =>  "a"
98         //| b  =>  "b"
99         //| c  =>  "c"
100     ab ::= a & b
101     ac ::= a & c
102     bc ::= b & c
103     a ::= ("1" x)*
104     b ::= (x "2" => "b")*
105     c ::= (x "2" x "1" => "c")*
106     x ::= [123]
107 }
108
109 testcase {
110     input  "qxbambambam";
111     output "bam:{a bam:{a bam:{a x:{x}}}}";
112
113     s ::= "q" z
114     z ::= a z ^"bam"
115     z ::= ^"x"
116     a ::= ()       => "a"
117 }
118
119 testcase {
120     input  "baaaa";
121     output "s2:{b0 a:{a:{epsilon}}}";
122     output "b:{a:{a:{epsilon}} epsilon}";
123     s ::= b t        => "s2"
124         | ^"b" t b
125     t ::= ^"a" t "a"
126         | ()         => "epsilon"
127     b ::= "b"        => "b0"
128         | ()         => "epsilon"
129 }
130
131 testcase {
132     input  "qaq";
133     output "q:{a:{s1:{epsilon}}}";
134
135     s ::= ^"q" x "q"
136     x ::= ^"a" a
137     x ::= ()        => "epsilon"
138     a ::= x         => "s1"
139 }
140
141 testcase {
142     input "baa";
143     output "s1:{a2:{a2:{a0 b0} b0}}";
144
145     s ::= "b" a     => "s1"
146     a ::= "a" a b   => "a2"
147         | ()        => "a0"
148     b ::= ()        => "b0"
149 }
150
151 testcase {
152     input  "aaa";
153
154     output "s3:{s3:{epsilon a0 epsilon epsilon} epsilon epsilon epsilon}";
155     output "s3:{s3:{epsilon epsilon epsilon epsilon} a0 epsilon epsilon}";
156     output "s3:{s3:{epsilon epsilon a0 epsilon} epsilon epsilon epsilon}";
157     output "s3:{s3:{epsilon epsilon epsilon a0} epsilon epsilon epsilon}";
158     output "s3:{epsilon epsilon a0 a0}";
159     output "s3:{s3:{s3:{epsilon epsilon epsilon epsilon} epsilon epsilon epsilon} epsilon epsilon epsilon}";
160     output "s3:{s3:{epsilon epsilon epsilon epsilon} epsilon epsilon a0}";
161     output "s3:{s3:{epsilon epsilon epsilon epsilon} epsilon a0 epsilon}";
162     output "s3:{epsilon a0 epsilon a0}";
163     output "s3:{epsilon a0 a0 epsilon}";
164
165     s ::= "a" s a a a => "s3"
166         | ()          => "epsilon"
167     a ::= "a"         => "a0"
168         | ()          => "epsilon"
169 }
170
171 testcase {
172     input "aa";
173     output "poo:{poo:{poox poox} poox}";
174     output "poo:{poox poo:{poox poox}}";
175     s ::= s s "a"  => "poo"
176         | ()       => "poox"
177 }
178
179 testcase {
180     input "baa";
181     output "s:{aa:{aa:{a b} b}}";
182     s ::= "b" a      => "s"
183     a ::= "a" a b    => "aa"
184     a ::= ()         => "a"
185     b ::= ()         => "b"
186 }
187
188 testcase {
189     input "aaab";
190     output "sx:{b aa:{aa:{b b} b}}";
191     s ::= b d "a" "b"  => "sx"
192     s ::= "a" d "a" d  => "sy"
193     d ::= "a" a b      => "aa"
194     a ::= "a" b b      => "aa"
195     a ::= ()           => "a"
196     b ::= ()           => "b"
197 }
198
199 testcase {
200     input "a+(b*c)";
201     output "+:{{a} *:{{b} {c}}}";
202
203     s   ::= r
204     r   ::= id
205           | r ^"*" r
206           | r ^"+" r
207           | "(" r ")"
208     id  ::= [a-z]++
209 }
210
211 testcase {
212     input "a+b-d*c";
213     output "plus:{stringify:{{a}} minus:{stringify:{{b}} times:{stringify:{{d}} stringify:{{c}}}}}";
214     output "times:{plus:{stringify:{{a}} minus:{stringify:{{b}} stringify:{{d}}}} stringify:{{c}}}";
215     output "plus:{stringify:{{a}} times:{minus:{stringify:{{b}} stringify:{{d}}} stringify:{{c}}}}";
216     output "times:{minus:{plus:{stringify:{{a}} stringify:{{b}}} stringify:{{d}}} stringify:{{c}}}";
217     output "minus:{plus:{stringify:{{a}} stringify:{{b}}} times:{stringify:{{d}} stringify:{{c}}}}";
218     w  ::= " "
219     l  ::= id
220     s  ::= l "=" q  => "assign"
221          | q
222     q  ::= id
223          | l "=" q       => "assign"
224          | q "-" q       => "minus"
225          | q "+" q       => "plus"
226          | q "*" q       => "times"
227          | "(" q ")"
228     id   ::= idl++       => "stringify"
229     idl  ::= [a-d]
230 }
231
232 testcase {
233     input "a*b*c";
234     output "times:{stringify:{{a}} times:{stringify:{{b}} stringify:{{c}}}}";
235     w  ::= " "
236     l  ::= id
237     s  ::= l "=" r  => "assign"
238          | r
239     r  ::= l
240          | l "=" r       => "assign"
241          | r "+" r       => "plus"
242          | (r) "*" r       => "times"
243          | "(" r ")"
244          | r r           => "times"
245     id   ::= idl++       => "stringify"
246     idl  ::= [a-d]
247 }
248
249 testcase {
250     input "a+b*c";
251     output "plus:{stringify:{{a}} times:{stringify:{{b}} stringify:{{c}}}}";
252     w  ::= " "
253     l  ::= id
254     s  ::= l "=" r  => "assign"
255          | r
256     r  ::= l
257          | l "=" r       => "assign"
258          | r "+" r       => "plus"
259          > r "*" r       => "times"
260          | "(" r ")"
261          | r r           => "times"
262     id   ::= idl++       => "stringify"
263     idl  ::= [a-d]
264 }
265
266 testcase {
267   input "aa bb";
268   output "{q:{{a a}} q:{{b b}}}";
269
270   s  ::= q */ ws
271   ws ::= " "*
272   q  ::= [a-z]++ => "q"
273 }
274
275 testcase {
276
277     input "
278
279
280  while x>0
281    while y>0
282     foo()
283      bar()
284
285  while x>0
286    while y>0
287     foo()
288    bar()
289
290
291 ";
292     output "smt:{while:{>:{{x} {0}} while:{>:{{y} {0}} sbb:{{f o o} {b a r}}}}}";
293     output "smt:{while:{>:{{x} {0}} sbb:{while:{>:{{y} {0}} {f o o}} {b a r}}}}";
294
295 indent  !::= ww
296 outdent !::= " "  outdent " "
297            | " "  (~[]*)  "\n"
298
299 any      !::= ~[]*
300 s         ::= any "\n\n" ww statement ww "\n\n" any => smt
301 ww       !::= sp*
302 ws       !::= sp**
303 sp        ::= " "
304
305 block     ::= "\n" indent  blockBody
306            &~ "\n" outdent ~[\ ] ~[]*
307
308 blockBody ::= statement
309             > statement blockBody /ws => "sbb"
310
311 statement ::= call
312             | ^"while" expr block /ws
313
314 expr      ::= ident
315             | call
316             | expr ^">" expr   /ws
317             | num
318
319 call      ::= expr "()"        /ws
320
321 num       ::= [0-9]++
322
323 ident     ::= [a-z]++ &~ keyword
324 keyword   ::= "if" | "then" | "else" | "while"
325
326 w         ::= " " | "\n" | "\r"
327 ws        ::= w*
328
329
330 }