Added a language-independent test based on cppscan3.rl. Added the necessary
[external/ragel.git] / test / langtrans_ruby.txl
1 include "testcase.txl"
2
3 keys
4         'boolean 'new
5 end keys
6
7
8 define ruby_statements
9                 [repeat ruby_lang_stmt]
10 end define
11
12 define ruby_lang_stmt
13                 [al_ragel_stmt]
14         |       [ruby_expr_stmt]
15         |       [ruby_if_stmt]
16         |       [EX] 'do [IN] [NL] [ruby_statements] [EX] 'end [IN] [NL]
17 end define
18
19 define ruby_type_decl
20                 [al_type_decl]
21         |       'boolean
22 end define
23
24 define ruby_expr_stmt
25                 [ruby_expr] '; [NL]
26 end define
27
28 define ruby_expr
29                 [ruby_term] [repeat ruby_expr_extend]
30 end define
31
32 define ruby_expr_extend
33                 [al_expr_op] [ruby_term]
34 end define
35
36 define ruby_term
37                 [al_term]
38         |       [stringlit] [union]
39         |       [id] [repeat ruby_dot_id]
40         |       [SPOFF] [id] [repeat ruby_dot_id] '( [SPON] [ruby_args] ')
41         |       [union]
42 end define
43
44 define ruby_dot_id
45                 '. [id]
46 end define
47
48 define ruby_args
49                 [list ruby_expr] 
50 end define
51
52 define ruby_sign
53                 '- | '+
54 end define
55
56 define ruby_if_stmt
57                 'if [ruby_expr] [NL] [IN]
58                         [ruby_statements] [EX]
59                 [opt ruby_else]
60                 'end [NL]
61 end define
62
63 define ruby_else
64                 'else [NL] [IN]
65                         [ruby_statements] [EX]
66 end define
67
68 define ruby_lang
69                 [ruby_statements]
70                 '%% [NL]
71                 [ruby_statements]
72                 [ragel_def]
73 end define
74
75 define program
76                 [lang_indep]
77         |       [ruby_lang]
78 end define
79
80 redefine al_host_block
81                 '{ [NL] [IN] [al_statements] [EX] '} [NL]
82         |       '{ [NL] [IN] [ruby_statements] [EX] '} [NL]
83 end define
84
85 function initDecl1 VarDecl [al_variable_decl]
86         deconstruct VarDecl
87                 'bool Id [id] ';
88         replace [repeat ruby_lang_stmt]
89         by
90                 Id '= 'false ';
91 end function
92
93 function initDecl2 VarDecl [al_variable_decl]
94         deconstruct VarDecl
95                 'char Id [id] ';
96         replace [repeat ruby_lang_stmt]
97         by
98                 Id '= ''c' ';
99 end function
100
101 function initDecl3 VarDecl [al_variable_decl]
102         deconstruct VarDecl
103                 'int Id [id] ';
104         replace [repeat ruby_lang_stmt]
105         by
106                 Id '= '0 ';
107 end function
108
109 function initDecl4 VarDecl [al_variable_decl]
110         deconstruct VarDecl
111                 'ptr Id [id] ';
112         replace [repeat ruby_lang_stmt]
113         by
114                 Id '= '-1 ';
115 end function
116
117 function initDecl5 VarDecl [al_variable_decl]
118         deconstruct VarDecl
119                 Type [al_type_decl] Id [id] Union [union] ';
120         replace [repeat ruby_lang_stmt]
121         by
122                 Id '= '[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] ';
123 end function
124
125
126 function alStmtToRuby1 AlStmt [action_lang_stmt]
127         deconstruct AlStmt
128                 VarDecl [al_variable_decl]
129         deconstruct VarDecl
130                 Type [al_type_decl] Id [id] OptUnion [opt union] ';
131         replace [repeat ruby_lang_stmt]
132         by
133                 _ [initDecl1 VarDecl] [initDecl2 VarDecl] 
134                         [initDecl3 VarDecl] [initDecl4 VarDecl]
135                         [initDecl5 VarDecl]
136 end function
137
138 function alTermToRuby
139         replace [al_term]
140                 'first_token_char
141         by
142                 'data '[tokstart]
143 end function
144
145 function alExprExtendToRuby AlExprExtend [repeat al_expr_extend]
146         deconstruct AlExprExtend
147                 Op [al_expr_op] Term [al_term] Rest [repeat al_expr_extend]
148         construct RubyRest [repeat ruby_expr_extend]
149                 _ [alExprExtendToRuby Rest]
150         replace [repeat ruby_expr_extend]
151         by
152                 Op Term [alTermToRuby] RubyRest
153 end function
154
155 % Note: this doesn't go into the ( al_expr ) form of al_term.
156 function alExprToRuby AlExpr [al_expr]
157         deconstruct AlExpr
158                 ALTerm [al_term] AlExprExtend [repeat al_expr_extend]
159         construct RubyExprExtend [repeat ruby_expr_extend]
160                 _ [alExprExtendToRuby AlExprExtend]
161         construct Result [opt ruby_expr]
162                 ALTerm [alTermToRuby] RubyExprExtend
163         replace [opt ruby_expr]
164         by
165                 Result 
166 end function
167
168 function alStmtToRuby2 AlStmt [action_lang_stmt]
169         deconstruct AlStmt
170                 AlExpr [al_expr] ';
171         construct OptRubyExpr [opt ruby_expr]
172                 _ [alExprToRuby AlExpr]
173         deconstruct OptRubyExpr
174                 RubyExpr [ruby_expr]
175         replace [repeat ruby_lang_stmt]
176         by
177                 RubyExpr ';
178 end function
179
180 function liftBlock
181         replace [repeat ruby_lang_stmt]
182                 'do Block [repeat ruby_lang_stmt] 'end
183         by
184                 Block
185 end function
186
187 function alOptElseRuby AlOptElse [opt al_else]
188         deconstruct AlOptElse
189                 'else 
190                         AlSubStmt [action_lang_stmt]
191         construct AlSubStmts [repeat action_lang_stmt]
192                 AlSubStmt
193         construct RubySubStmts [repeat ruby_lang_stmt]
194                 _ [alToRuby AlSubStmts]
195         deconstruct RubySubStmts
196                 RubySubStmt [ruby_lang_stmt]
197         replace [opt ruby_else]
198         by
199                 'else 
200                         RubySubStmts [liftBlock]
201 end function
202
203 function alStmtToRuby3 AlStmt [action_lang_stmt]
204         deconstruct AlStmt
205                 'if '( AlExpr [al_expr] ')
206                         AlSubStmt [action_lang_stmt]
207                 AlOptElse [opt al_else]
208         construct OptRubyExpr [opt ruby_expr]
209                 _ [alExprToRuby AlExpr]
210         deconstruct OptRubyExpr
211                 RubyExpr [ruby_expr]
212         construct AlSubStmts [repeat action_lang_stmt]
213                 AlSubStmt
214         construct RubySubStmts [repeat ruby_lang_stmt]
215                 _ [alToRuby AlSubStmts]
216         construct OptRubyElse [opt ruby_else]
217                 _ [alOptElseRuby AlOptElse]
218         replace [repeat ruby_lang_stmt]
219         by
220                 'if RubyExpr
221                         RubySubStmts [liftBlock]
222                 OptRubyElse
223                 'end
224 end function
225
226 function alStmtToRuby4a AlStmt [action_lang_stmt]
227         deconstruct AlStmt
228                 'printi Id [id] ';
229         replace [repeat ruby_lang_stmt]
230         by
231                 'print '( Id ') ';
232 end function
233
234 function alStmtToRuby4b AlStmt [action_lang_stmt]
235         deconstruct AlStmt
236                 'prints String [stringlit] ';
237         replace [repeat ruby_lang_stmt]
238         by
239                 'print '( String ') ';
240 end function
241
242 function alStmtToRuby4c AlStmt [action_lang_stmt]
243         deconstruct AlStmt
244                 'printb Id [id] ';
245         replace [repeat ruby_lang_stmt]
246         by
247                 '_a = Id '[0..pos-1] ';
248                 'print '( '_a '. 'pack '( '"c*" ')  ') ';
249 end function
250
251 function alStmtToRuby4d AlStmt [action_lang_stmt]
252         deconstruct AlStmt
253                 'print_token ';
254         replace [repeat ruby_lang_stmt]
255         by
256                 'print '( 'data '[tokstart..tokend-1] ') ';
257 end function
258
259 function alStmtToRuby5 AlStmt [action_lang_stmt]
260         deconstruct AlStmt
261                 '{ AlSubStmts [repeat action_lang_stmt] '}
262         construct RubySubStmts [repeat ruby_lang_stmt]
263                 _ [alToRuby AlSubStmts]
264         replace [repeat ruby_lang_stmt]
265         by
266                 'do RubySubStmts 'end
267 end function
268
269 function alStmtToRuby6 AlStmt [action_lang_stmt]
270         deconstruct AlStmt
271                 RagelStmt [al_ragel_stmt]
272         replace [repeat ruby_lang_stmt]
273         by
274                 RagelStmt
275 end function
276
277 rule fixCharLit
278         replace $ [al_term]
279                 CharLit [charlit]
280         construct BaseId [id]
281                 'id
282         construct Id [id]
283                 BaseId [unquote CharLit]
284         construct EmptyString [stringlit]
285                 '""
286         construct Repl [stringlit]
287                 EmptyString [quote Id]
288         by
289                 Repl '[0]
290 end rule
291
292
293 function alToRuby AlStmts [repeat action_lang_stmt]
294         deconstruct AlStmts
295                 FirstStmt [action_lang_stmt] Rest [repeat action_lang_stmt]
296         construct RubyFirst [repeat ruby_lang_stmt]
297                 _ 
298                         [alStmtToRuby1 FirstStmt]
299                         [alStmtToRuby2 FirstStmt]
300                         [alStmtToRuby3 FirstStmt]
301                         [alStmtToRuby4a FirstStmt]
302                         [alStmtToRuby4b FirstStmt]
303                         [alStmtToRuby4c FirstStmt]
304                         [alStmtToRuby4d FirstStmt]
305                         [alStmtToRuby5 FirstStmt]
306                         [alStmtToRuby6 FirstStmt]
307                         [fixCharLit]
308         construct RubyRest [repeat ruby_lang_stmt]
309                 _ [alToRuby Rest]
310         replace [repeat ruby_lang_stmt]
311         by
312                 RubyFirst [. RubyRest]
313 end function
314
315 rule actionTransRuby
316         replace [al_host_block]
317                 '{ AlStmts [repeat action_lang_stmt] '}
318         construct RubyStmts [repeat ruby_lang_stmt]
319                 _ [alToRuby AlStmts]
320         by
321                 '{ RubyStmts '}
322 end rule
323
324 rule lowercaseMachine
325         replace $ [machine_stmt]
326                 'machine Id [id] ';
327         by
328                 'machine Id [tolower] ';
329 end rule
330
331 function langTransRuby
332         replace [program]
333                 Definitions [repeat action_lang_stmt]
334                 '%%
335                 Initializations [repeat action_lang_stmt]
336                 RagelDef [ragel_def]
337         construct RubyDefinitions [repeat ruby_lang_stmt]
338                 _ [alToRuby Definitions]
339         construct RubyInitializations [repeat ruby_lang_stmt]
340                 _ [alToRuby Initializations]
341         construct NewRagelDef [ragel_def]
342                 RagelDef [actionTransRuby] [lowercaseMachine]
343         import ArrayInits [ruby_statements]
344                 ArrayInitStmts [repeat ruby_lang_stmt]
345         by
346                 RubyDefinitions
347                 '%%
348                 ArrayInitStmts [. RubyInitializations]
349                 NewRagelDef
350 end function
351
352 function main
353         replace [program]
354                 P [program]
355         export ArrayInits [ruby_statements]
356                 _
357         by
358                 P [langTransRuby] 
359 end function