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