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