int rw_cs_factor( Factor: indep::factor ) { if match Factor [`first_token_char] { send Out "data\[ts\]" } else if match Factor [tk_ident `[ expr `]] { send Out "[$Factor.tk_ident]\[ [rw_cs_expr(Factor.expr)] \] } else if match Factor [tk_ident `( expr `)] { send Out "[$Factor.tk_ident]( [rw_cs_expr(Factor.expr)] ) } elsif match Factor [`< type `> `( expr `)] { send Out "( [rw_cs_type(Factor.type)] ) ( [rw_cs_expr(Factor.expr)] ) } elsif match Factor [`( expr `)] { send Out "( [rw_cs_expr(Factor.expr)] ) } elsif match Factor ['true'] { send Out '1' } elsif match Factor ['false'] { send Out '0' } elsif match Factor [`buffer] { send Out "new String( buffer, 0, blen ) } else { send Out [$Factor] } } void rw_cs_type( Type: indep::type ) { if match Type [`int] { send Out "int" } elsif match Type [`bool] { send Out "int" } elsif match Type [`char] { send Out "char" } elsif match Type [`ptr] { send Out "int" } elsif match Type [`byte] { send Out "unsigned char" } } void rw_cs_abs_expr( Expr: indep::abs_expr ) { if ( Expr.Op ) { send Out "[rw_cs_abs_expr(Expr.E1)] [$Expr.Op] [rw_cs_abs_expr( Expr.E2 )]" } else { rw_cs_factor( Expr.factor ) } } void rw_cs_expr( Expr: indep::expr ) { AbsExpr: indep::abs_expr = indep::abs_comparative( Expr.comparative ) rw_cs_abs_expr( AbsExpr ) } void rw_cs_opt_array( OptArr: indep::opt_arr ) { if OptArr.expr send Out "\[[rw_cs_expr( OptArr.expr )]\]" } int rw_cs_var_decl( VarDecl: indep::var_decl ) { if ( VarDecl.opt_arr.expr ) { send Out "[rw_cs_type( VarDecl.type )] \[\] [$VarDecl.tk_ident]" " = new [rw_cs_type( VarDecl.type )] [rw_cs_opt_array(VarDecl.opt_arr) ]; } else { send Out "[rw_cs_type( VarDecl.type )] [$VarDecl.tk_ident]; } } void rw_cs_opt_sub( OptSub: indep::opt_sub ) { if ( OptSub.expr ) { send Out "\[[rw_cs_expr(OptSub.expr)]\] } } int rw_cs_expr_stmt( ExprStmt: indep::expr_stmt ) { if match ExprStmt [tk_ident opt_sub `= expr `;] { send Out "[$ExprStmt.tk_ident rw_cs_opt_sub(ExprStmt.opt_sub)] = [rw_cs_expr(ExprStmt.expr)]; } else if match ExprStmt [expr `;] { send Out "[rw_cs_expr(ExprStmt.expr)]; } } int rw_cs_if_stmt( IfStmt: indep::if_stmt ) { send Out "if ( [rw_cs_expr( IfStmt.expr )] ) "{ " [rw_cs_stmt_list( IfStmt._repeat_stmt )] "} if ( IfStmt.opt_else._repeat_stmt ) { send Out "else { " [rw_cs_stmt_list( IfStmt.opt_else._repeat_stmt )] "} } } int rw_cs_print_stmt( Stmt: indep::print_stmt ) { if match Stmt [`print_int expr `;] { send Out "Console.Write( [rw_cs_expr(Stmt.expr)] );" } else if match Stmt [`print_buf E1: expr `, E2: expr `;] { send Out "Console.Write( new String( [rw_cs_expr(E1)], 0, [rw_cs_expr(E2)] ) );" } else if match Stmt [`print_str expr `;] { send Out "Console.Write( [rw_cs_expr( Stmt.expr )] );" } else if match Stmt [`print_token `;] { send Out "Console.Write( new String( data , ts , te - ts ) ); } } void rw_cs_buf_stmt( BufStmt: indep::buf_stmt ) { switch BufStmt case [`buf_clear `( `) `;] { send Out " blen = 0; } case [`buf_append `( `) `;] { send Out " buffer\[blen++\] = fc; } } int rw_cs_ragel_stmt( Stmt: indep::ragel_stmt ) { send Out [$Stmt] } int rw_cs_stmt( Stmt: indep::stmt ) { if match Stmt [var_decl] rw_cs_var_decl( Stmt.var_decl ) else if match Stmt [expr_stmt] rw_cs_expr_stmt( Stmt.expr_stmt ) else if match Stmt [if_stmt] rw_cs_if_stmt( Stmt.if_stmt ) else if match Stmt [print_stmt] rw_cs_print_stmt( Stmt.print_stmt ) else if match Stmt [buf_stmt] rw_cs_buf_stmt( Stmt.buf_stmt ) else if match Stmt [ragel_stmt] rw_cs_ragel_stmt( Stmt.ragel_stmt ) } void rw_cs_stmt_list( StmtList: indep::stmt* ) { for Stmt: indep::stmt in repeat( StmtList ) rw_cs_stmt( Stmt ) } int rw_cs_action_block( ActionBlock: indep::action_block ) { Out = new parser() if match ActionBlock [`{ stmt* `}] { send Out "{[rw_cs_stmt_list( ActionBlock._repeat_stmt )]} } else if match ActionBlock [`{ expr `}] { send Out "{[rw_cs_expr( ActionBlock.expr )]} } send Out [] eos } void rw_csharp( Output: stream ) { send Output "/* " * @LANG: csharp if ProhibitGenflags { send Output " * @PROHIBIT_GENFLAGS:[ProhibitGenflags] } send Output " * @GENERATED: true " */ " "using System; "// Disables lots of warnings that appear in the test suite "#pragma warning disable 0168, 0169, 0219, 0162, 0414 "namespace Test { "class Test "{ Init: indep::stmt* = RagelTree.Init for Stmt: indep::stmt in Init { if match Stmt [Decl: var_decl] { Out = new parser() rw_cs_var_decl( Decl ) send Out [] eos send Output [Out->tree] } } Section: indep::section = RagelTree.section for Action: ragel::action_block in Section { # Reparse as lang-independent code. parse IndepActionBlock: indep::action_block[$Action] if ( !IndepActionBlock ) { print( error, '\n', Action ) exit(1) } # Translate to specific language. rw_cs_action_block( IndepActionBlock ) # Reparse back to ragel action block. Action = parse ragel::action_block[$Out->tree] if ( !Action ) { print( error, '\n' ) exit(1) } } send Output ["\n" @Section "\n"] send Output "%% write data; "int cs; " "void init() "{ for Stmt: indep::stmt in Init { if match Stmt [ExprStmt: expr_stmt] { Out = new parser() rw_cs_expr_stmt( ExprStmt ) send Out [] eos send Output [Out->tree] } } send Output " %% write init; "} " "void exec( char\[\] data, int len ) "{ " int p = 0; " int pe = len; " int eof = len; " string _s; " char \[\] buffer = new char \[1024\]; " int blen = 0; " %% write exec; "} " "void finish( ) "{ " if ( cs >= [$MachineName.word]_first_final ) " Console.WriteLine( \"ACCEPT\" ); " else " Console.WriteLine( \"FAIL\" ); "} " send Output "static readonly string\[\] inp = { NR: int = 0 for InputString: indep::input_string in RagelTree { send Output [^InputString ",\n"] NR = NR + 1 } send Output "}; " send Output " "static readonly int inplen = [NR]; " "public static void Main (string\[\] args) "{ " Test machine = new Test(); " for ( int i = 0; i < inplen; i++ ) { " machine.init(); " machine.exec( inp\[i\].ToCharArray(), inp\[i\].Length ); " machine.finish(); " } "} "} "} }