tizen 2.3.1 release
[external/ragel.git] / test / langtrans_csharp.txl
1 include "testcase.txl"
2
3 keys
4         'bool 'new
5 end keys
6
7
8 define csharp_statements
9                 [repeat csharp_lang_stmt]
10 end define
11
12 define csharp_lang_stmt
13                 [al_ragel_stmt]
14         |       [csharp_variable_decl]
15         |       [csharp_expr_stmt]
16         |       [csharp_if_stmt]
17         |       [EX] '{ [IN] [NL] [csharp_statements] [EX] '} [IN] [NL]
18 end define
19
20 define csharp_variable_decl
21                 [csharp_type_decl] [opt union] [id] '; [NL]
22 end define
23
24 define csharp_type_decl
25                 [al_type_decl]
26         |       'bool
27         |       'String
28 end define
29
30 define csharp_expr_stmt
31                 [csharp_expr] '; [NL]
32 end define
33
34 define csharp_expr
35                 [csharp_term] [repeat csharp_expr_extend]
36 end define
37
38 define csharp_expr_extend
39                 [al_expr_op] [csharp_term]
40 end define
41
42 define csharp_term
43                 [al_term]
44         |       [id] [repeat csharp_dot_id]
45         |       [id] [repeat csharp_dot_id] '( [csharp_args] ')
46         |       'new [csharp_type_decl] [union]
47         |       'new [csharp_type_decl] '( [csharp_args] ') 
48 end define
49
50 define csharp_dot_id
51                 '. [id]
52 end define
53
54 define csharp_args
55                 [list csharp_expr] 
56 end define
57
58 define csharp_sign
59                 '- | '+
60 end define
61
62 define csharp_if_stmt
63                 'if '( [csharp_expr] ') [NL] [IN]
64                         [csharp_lang_stmt] [EX]
65                 [opt csharp_else]
66 end define
67
68 define csharp_else
69                 'else [NL] [IN]
70                         [csharp_lang_stmt] [EX]
71 end define
72
73 define csharp_lang
74                 [csharp_statements]
75                 '%% [NL]
76                 [csharp_statements]
77                 [ragel_def]
78 end define
79
80 define program
81                 [lang_indep]
82         |       [csharp_lang]
83 end define
84
85 redefine al_host_block
86                 '{ [NL] [IN] [al_statements] [EX] '} [NL]
87         |       '{ [NL] [IN] [csharp_statements] [EX] '} [NL]
88 end define
89
90 redefine cond_action_stmt
91                 'action [id] '{ [al_expr] '} [NL]
92         |       'action [id] '{ [csharp_expr] '} [NL]
93 end redefine
94
95
96 function clearUnion Type [csharp_type_decl] Id [id] 
97         replace [opt union]
98                 Union [union]
99         import ArrayInits [csharp_statements]
100                 Stmts [repeat csharp_lang_stmt]
101         export ArrayInits 
102                 Id '= 'new Type Union '; Stmts
103         by
104                 '[]
105 end function
106
107 rule ptrTypes
108         replace [al_type_decl]
109                 'ptr
110         by
111                 'int
112 end rule
113
114 function alStmtToCSharp1 AlStmt [action_lang_stmt]
115         deconstruct AlStmt
116                 VarDecl [al_variable_decl]
117         deconstruct VarDecl
118                 Type [al_type_decl] Id [id] OptUnion [opt union] ';
119         construct CSharpType [csharp_type_decl]
120                 Type
121         construct Result [csharp_variable_decl]
122                 CSharpType [ptrTypes] OptUnion [clearUnion CSharpType Id] Id ';
123         replace [repeat csharp_lang_stmt]
124         by
125                 Result
126 end function
127
128 function alTermToCSharp
129         replace [al_term]
130                 'first_token_char
131         by
132                 'data '[ts]
133 end function
134
135 function alExprExtendToCSharp AlExprExtend [repeat al_expr_extend]
136         deconstruct AlExprExtend
137                 Op [al_expr_op] Term [al_term] Rest [repeat al_expr_extend]
138         construct CSharpRest [repeat csharp_expr_extend]
139                 _ [alExprExtendToCSharp Rest]
140         replace [repeat csharp_expr_extend]
141         by
142                 Op Term [alTermToCSharp] CSharpRest
143 end function
144
145 function alExprToCSharp AlExpr [al_expr]
146         deconstruct AlExpr
147                 ALTerm [al_term] AlExprExtend [repeat al_expr_extend]
148         construct CSharpExprExtend [repeat csharp_expr_extend]
149                 _ [alExprExtendToCSharp AlExprExtend]
150         construct Result [opt csharp_expr]
151                 ALTerm [alTermToCSharp] CSharpExprExtend
152         replace [opt csharp_expr]
153         by
154                 Result 
155 end function
156
157 function alStmtToCSharp2 AlStmt [action_lang_stmt]
158         deconstruct AlStmt
159                 AlExpr [al_expr] ';
160         construct OptCSharpExpr [opt csharp_expr]
161                 _ [alExprToCSharp AlExpr]
162         deconstruct OptCSharpExpr
163                 CSharpExpr [csharp_expr]
164         replace [repeat csharp_lang_stmt]
165         by
166                 CSharpExpr ';
167 end function
168
169 function alOptElseCSharp AlOptElse [opt al_else]
170         deconstruct AlOptElse
171                 'else 
172                         AlSubStmt [action_lang_stmt]
173         construct AlSubStmts [repeat action_lang_stmt]
174                 AlSubStmt
175         construct CSharpSubStmts [repeat csharp_lang_stmt]
176                 _ [alToCSharp AlSubStmts]
177         deconstruct CSharpSubStmts
178                 CSharpSubStmt [csharp_lang_stmt]
179         replace [opt csharp_else]
180         by
181                 'else 
182                         CSharpSubStmt
183 end function
184
185 function alStmtToCSharp3 AlStmt [action_lang_stmt]
186         deconstruct AlStmt
187                 'if '( AlExpr [al_expr] ')
188                         AlSubStmt [action_lang_stmt]
189                 AlOptElse [opt al_else]
190         construct OptCSharpExpr [opt csharp_expr]
191                 _ [alExprToCSharp AlExpr]
192         deconstruct OptCSharpExpr
193                 CSharpExpr [csharp_expr]
194         construct AlSubStmts [repeat action_lang_stmt]
195                 AlSubStmt
196         construct CSharpSubStmts [repeat csharp_lang_stmt]
197                 _ [alToCSharp AlSubStmts]
198         deconstruct CSharpSubStmts
199                 CSharpSubStmt [csharp_lang_stmt]
200         construct OptCSharpElse [opt csharp_else]
201                 _ [alOptElseCSharp AlOptElse]
202         replace [repeat csharp_lang_stmt]
203         by
204                 'if '( CSharpExpr ')
205                         CSharpSubStmt
206                 OptCSharpElse
207 end function
208
209 function alStmtToCSharp4a AlStmt [action_lang_stmt]
210         deconstruct AlStmt
211                 'printi Id [id] ';
212         replace [repeat csharp_lang_stmt]
213         by
214                 'Console '. 'Write '( Id ');
215 end function
216
217 function alStmtToCSharp4b AlStmt [action_lang_stmt]
218         deconstruct AlStmt
219                 'prints String [stringlit] ';
220         replace [repeat csharp_lang_stmt]
221         by
222                 'Console '. 'Write '( String ');
223 end function
224
225 function alStmtToCSharp4c AlStmt [action_lang_stmt]
226         deconstruct AlStmt
227                 'printb Id [id] ';
228         replace [repeat csharp_lang_stmt]
229         by
230                 '_s '= 'new 'String '( Id ', '0 ', 'pos ') ';
231                 'Console '. 'Write '( '_s ');
232 end function
233
234 function alStmtToCSharp4d AlStmt [action_lang_stmt]
235         deconstruct AlStmt
236                 'print_token ';
237         replace [repeat csharp_lang_stmt]
238         by
239                 '_s '= 'new 'String '( 'data ', 'ts ', 'te '- 'ts ') ';
240                 'Console '. 'Write '( '_s ');
241 end function
242
243 function alStmtToCSharp5 AlStmt [action_lang_stmt]
244         deconstruct AlStmt
245                 '{ AlSubStmts [repeat action_lang_stmt] '}
246         construct CSharpSubStmts [repeat csharp_lang_stmt]
247                 _ [alToCSharp AlSubStmts]
248         replace [repeat csharp_lang_stmt]
249         by
250                 '{ CSharpSubStmts '}
251 end function
252
253 function alStmtToCSharp6 AlStmt [action_lang_stmt]
254         deconstruct AlStmt
255                 RagelStmt [al_ragel_stmt]
256         replace [repeat csharp_lang_stmt]
257         by
258                 RagelStmt
259 end function
260
261
262 function alToCSharp AlStmts [repeat action_lang_stmt]
263         deconstruct AlStmts
264                 FirstStmt [action_lang_stmt] Rest [repeat action_lang_stmt]
265         construct CSharpFirst [repeat csharp_lang_stmt]
266                 _ 
267                         [alStmtToCSharp1 FirstStmt]
268                         [alStmtToCSharp2 FirstStmt]
269                         [alStmtToCSharp3 FirstStmt]
270                         [alStmtToCSharp4a FirstStmt]
271                         [alStmtToCSharp4b FirstStmt]
272                         [alStmtToCSharp4c FirstStmt]
273                         [alStmtToCSharp4d FirstStmt]
274                         [alStmtToCSharp5 FirstStmt]
275                         [alStmtToCSharp6 FirstStmt]
276         construct CSharpRest [repeat csharp_lang_stmt]
277                 _ [alToCSharp Rest]
278         replace [repeat csharp_lang_stmt]
279         by
280                 CSharpFirst [. CSharpRest]
281 end function
282
283 rule actionTransCSharp
284         replace [al_host_block]
285                 '{ AlStmts [repeat action_lang_stmt] '}
286         construct CSharpStmts [repeat csharp_lang_stmt]
287                 _ [alToCSharp AlStmts]
288         by
289                 '{ CSharpStmts '}
290 end rule
291
292 rule condTransCSharp
293         replace [cond_action_stmt]
294                 'action Id [id] '{ AlExpr [al_expr] '}
295         construct OptCSharpExpr [opt csharp_expr]
296                 _ [alExprToCSharp AlExpr]
297         deconstruct OptCSharpExpr
298                 CSharpExpr [csharp_expr]
299         by
300                 'action Id '{ CSharpExpr '}
301 end rule
302
303 rule machineName
304         replace $ [machine_stmt]
305                 'machine _ [id] ';
306         import TXLargs [repeat stringlit]
307                 Arg1 [stringlit] _ [repeat stringlit]
308         construct ClassName [id]
309                 _ [unquote Arg1]
310         by
311                 'machine ClassName ';
312 end rule
313
314 function langTransCSharp
315         replace [program]
316                 Definitions [repeat action_lang_stmt]
317                 '%%
318                 Initializations [repeat action_lang_stmt]
319                 RagelDef [ragel_def]
320         construct CSharpDefinitions [repeat csharp_lang_stmt]
321                 _ [alToCSharp Definitions]
322         construct CSharpInitializations [repeat csharp_lang_stmt]
323                 _ [alToCSharp Initializations]
324         construct NewRagelDef [ragel_def]
325                 RagelDef [actionTransCSharp] [condTransCSharp] [machineName]
326         import ArrayInits [csharp_statements]
327                 ArrayInitStmts [repeat csharp_lang_stmt]
328         by
329                 CSharpDefinitions
330                 '%%
331                 ArrayInitStmts [. CSharpInitializations]
332                 NewRagelDef
333 end function
334
335 function main
336         replace [program]
337                 P [program]
338         export ArrayInits [csharp_statements]
339                 _
340         by
341                 P [langTransCSharp] 
342 end function