Initialize Tizen 2.3
[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 redefine cond_action_stmt
86                 'action [id] '{ [al_expr] '} [NL]
87         |       'action [id] '{ [ruby_expr] '} [NL]
88 end redefine
89
90 function initDecl1 VarDecl [al_variable_decl]
91         deconstruct VarDecl
92                 'bool Id [id] ';
93         replace [repeat ruby_lang_stmt]
94         by
95                 Id '= 'false ';
96 end function
97
98 function initDecl2 VarDecl [al_variable_decl]
99         deconstruct VarDecl
100                 'char Id [id] ';
101         replace [repeat ruby_lang_stmt]
102         by
103                 Id '= ''c' ';
104 end function
105
106 function initDecl3 VarDecl [al_variable_decl]
107         deconstruct VarDecl
108                 'int Id [id] ';
109         replace [repeat ruby_lang_stmt]
110         by
111                 Id '= '0 ';
112 end function
113
114 function initDecl4 VarDecl [al_variable_decl]
115         deconstruct VarDecl
116                 'ptr Id [id] ';
117         replace [repeat ruby_lang_stmt]
118         by
119                 Id '= '-1 ';
120 end function
121
122 function initDecl5 VarDecl [al_variable_decl]
123         deconstruct VarDecl
124                 Type [al_type_decl] Id [id] Union [union] ';
125         replace [repeat ruby_lang_stmt]
126         by
127                 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] ';
128 end function
129
130
131 function alStmtToRuby1 AlStmt [action_lang_stmt]
132         deconstruct AlStmt
133                 VarDecl [al_variable_decl]
134         deconstruct VarDecl
135                 Type [al_type_decl] Id [id] OptUnion [opt union] ';
136         replace [repeat ruby_lang_stmt]
137         by
138                 _ [initDecl1 VarDecl] [initDecl2 VarDecl] 
139                         [initDecl3 VarDecl] [initDecl4 VarDecl]
140                         [initDecl5 VarDecl]
141 end function
142
143 function alTermToRuby
144         replace [al_term]
145                 'first_token_char
146         by
147                 'data '[ts]
148 end function
149
150 function alExprExtendToRuby AlExprExtend [repeat al_expr_extend]
151         deconstruct AlExprExtend
152                 Op [al_expr_op] Term [al_term] Rest [repeat al_expr_extend]
153         construct RubyRest [repeat ruby_expr_extend]
154                 _ [alExprExtendToRuby Rest]
155         replace [repeat ruby_expr_extend]
156         by
157                 Op Term [alTermToRuby] RubyRest
158 end function
159
160 % Note: this doesn't go into the ( al_expr ) form of al_term.
161 function alExprToRuby AlExpr [al_expr]
162         deconstruct AlExpr
163                 ALTerm [al_term] AlExprExtend [repeat al_expr_extend]
164         construct RubyExprExtend [repeat ruby_expr_extend]
165                 _ [alExprExtendToRuby AlExprExtend]
166         construct Result [opt ruby_expr]
167                 ALTerm [alTermToRuby] RubyExprExtend
168         replace [opt ruby_expr]
169         by
170                 Result 
171 end function
172
173 function alStmtToRuby2 AlStmt [action_lang_stmt]
174         deconstruct AlStmt
175                 AlExpr [al_expr] ';
176         construct OptRubyExpr [opt ruby_expr]
177                 _ [alExprToRuby AlExpr]
178         deconstruct OptRubyExpr
179                 RubyExpr [ruby_expr]
180         replace [repeat ruby_lang_stmt]
181         by
182                 RubyExpr ';
183 end function
184
185 function liftBlock
186         replace [repeat ruby_lang_stmt]
187                 'do Block [repeat ruby_lang_stmt] 'end
188         by
189                 Block
190 end function
191
192 function alOptElseRuby AlOptElse [opt al_else]
193         deconstruct AlOptElse
194                 'else 
195                         AlSubStmt [action_lang_stmt]
196         construct AlSubStmts [repeat action_lang_stmt]
197                 AlSubStmt
198         construct RubySubStmts [repeat ruby_lang_stmt]
199                 _ [alToRuby AlSubStmts]
200         deconstruct RubySubStmts
201                 RubySubStmt [ruby_lang_stmt]
202         replace [opt ruby_else]
203         by
204                 'else 
205                         RubySubStmts [liftBlock]
206 end function
207
208 function alStmtToRuby3 AlStmt [action_lang_stmt]
209         deconstruct AlStmt
210                 'if '( AlExpr [al_expr] ')
211                         AlSubStmt [action_lang_stmt]
212                 AlOptElse [opt al_else]
213         construct OptRubyExpr [opt ruby_expr]
214                 _ [alExprToRuby AlExpr]
215         deconstruct OptRubyExpr
216                 RubyExpr [ruby_expr]
217         construct AlSubStmts [repeat action_lang_stmt]
218                 AlSubStmt
219         construct RubySubStmts [repeat ruby_lang_stmt]
220                 _ [alToRuby AlSubStmts]
221         construct OptRubyElse [opt ruby_else]
222                 _ [alOptElseRuby AlOptElse]
223         replace [repeat ruby_lang_stmt]
224         by
225                 'if RubyExpr
226                         RubySubStmts [liftBlock]
227                 OptRubyElse
228                 'end
229 end function
230
231 function alStmtToRuby4a AlStmt [action_lang_stmt]
232         deconstruct AlStmt
233                 'printi Id [id] ';
234         replace [repeat ruby_lang_stmt]
235         by
236                 'print '( Id ') ';
237 end function
238
239 function alStmtToRuby4b AlStmt [action_lang_stmt]
240         deconstruct AlStmt
241                 'prints String [stringlit] ';
242         replace [repeat ruby_lang_stmt]
243         by
244                 'print '( String ') ';
245 end function
246
247 function alStmtToRuby4c AlStmt [action_lang_stmt]
248         deconstruct AlStmt
249                 'printb Id [id] ';
250         replace [repeat ruby_lang_stmt]
251         by
252                 '_a = Id '[0..pos-1] ';
253                 'print '( '_a '. 'pack '( '"c*" ')  ') ';
254 end function
255
256 function alStmtToRuby4d AlStmt [action_lang_stmt]
257         deconstruct AlStmt
258                 'print_token ';
259         replace [repeat ruby_lang_stmt]
260         by
261                 '_m = 'data '[ts..te-1] ';
262                 'print '( '_m '. 'pack '( '"c*" ') ') ';
263 end function
264
265 function alStmtToRuby5 AlStmt [action_lang_stmt]
266         deconstruct AlStmt
267                 '{ AlSubStmts [repeat action_lang_stmt] '}
268         construct RubySubStmts [repeat ruby_lang_stmt]
269                 _ [alToRuby AlSubStmts]
270         replace [repeat ruby_lang_stmt]
271         by
272                 'do RubySubStmts 'end
273 end function
274
275 function alStmtToRuby6 AlStmt [action_lang_stmt]
276         deconstruct AlStmt
277                 RagelStmt [al_ragel_stmt]
278         replace [repeat ruby_lang_stmt]
279         by
280                 RagelStmt
281 end function
282
283 rule fixCharLit
284         replace $ [al_term]
285                 CharLit [charlit]
286         construct BaseId [id]
287                 'id
288         construct Id [id]
289                 BaseId [unquote CharLit]
290         construct EmptyString [stringlit]
291                 '""
292         construct Repl [stringlit]
293                 EmptyString [quote Id]
294         by
295                 Repl '[0]
296 end rule
297
298
299 function alToRuby AlStmts [repeat action_lang_stmt]
300         deconstruct AlStmts
301                 FirstStmt [action_lang_stmt] Rest [repeat action_lang_stmt]
302         construct RubyFirst [repeat ruby_lang_stmt]
303                 _ 
304                         [alStmtToRuby1 FirstStmt]
305                         [alStmtToRuby2 FirstStmt]
306                         [alStmtToRuby3 FirstStmt]
307                         [alStmtToRuby4a FirstStmt]
308                         [alStmtToRuby4b FirstStmt]
309                         [alStmtToRuby4c FirstStmt]
310                         [alStmtToRuby4d FirstStmt]
311                         [alStmtToRuby5 FirstStmt]
312                         [alStmtToRuby6 FirstStmt]
313                         [fixCharLit]
314         construct RubyRest [repeat ruby_lang_stmt]
315                 _ [alToRuby Rest]
316         replace [repeat ruby_lang_stmt]
317         by
318                 RubyFirst [. RubyRest]
319 end function
320
321 rule actionTransRuby
322         replace [al_host_block]
323                 '{ AlStmts [repeat action_lang_stmt] '}
324         construct RubyStmts [repeat ruby_lang_stmt]
325                 _ [alToRuby AlStmts]
326         by
327                 '{ RubyStmts '}
328 end rule
329
330 rule condTransRuby
331         replace [cond_action_stmt]
332                 'action Id [id] '{ AlExpr [al_expr] '}
333         construct OptRubyExpr [opt ruby_expr]
334                 _ [alExprToRuby AlExpr]
335         deconstruct OptRubyExpr
336                 RubyExpr [ruby_expr]
337         by
338                 'action Id '{ RubyExpr '}
339 end rule
340
341 rule lowercaseMachine
342         replace $ [machine_stmt]
343                 'machine Id [id] ';
344         by
345                 'machine Id [tolower] ';
346 end rule
347
348 function langTransRuby
349         replace [program]
350                 Definitions [repeat action_lang_stmt]
351                 '%%
352                 Initializations [repeat action_lang_stmt]
353                 RagelDef [ragel_def]
354         construct RubyDefinitions [repeat ruby_lang_stmt]
355                 _ [alToRuby Definitions]
356         construct RubyInitializations [repeat ruby_lang_stmt]
357                 _ [alToRuby Initializations]
358         construct NewRagelDef [ragel_def]
359                 RagelDef [actionTransRuby] [condTransRuby] [lowercaseMachine]
360         import ArrayInits [ruby_statements]
361                 ArrayInitStmts [repeat ruby_lang_stmt]
362         by
363                 RubyDefinitions
364                 '%%
365                 ArrayInitStmts [. RubyInitializations]
366                 NewRagelDef
367 end function
368
369 function main
370         replace [program]
371                 P [program]
372         export ArrayInits [ruby_statements]
373                 _
374         by
375                 P [langTransRuby] 
376 end function