Added missing operators.
[ragel.git] / test / recdescent2.rl
blob59c45866edb7196d2e3d530007fcc5064b870685
1 /*
2  * @LANG: java
3  */
5 class recdescent2
7         %%{
8                 machine recdescent;
10                 prepush { 
11                         if ( top == stack_size ) {
12                                 System.out.print( "growing stack\n" );
13                                 stack_size = top * 2;
14                                 // Don't actually bother to resize here, but we do print messages.
15                                 //stack = (int*)realloc( stack, sizeof(int)*stack_size );
16                         }
17                 }
19                 postpop { 
20                         if ( stack_size > (top * 4) ) {
21                                 stack_size = top * 2;
22                                 // Don't actually bother to resize here, but we do print messages.
23                                 //stack = (int*)realloc( stack, sizeof(int)*stack_size );
24                                 System.out.print( "shrinking stack\n" );
25                         }
26                 }
28                 action item_start { item = p; }
30                 action item_finish
31                 {
32                         String item_data = new String ( data, item, p-item );
33                         System.out.print( "item: " );
34                         System.out.print( item_data );
35                         System.out.print( "\n" );
36                 }
38                 action call_main
39                 {
40                         System.out.print( "calling main\n" );
41                         fcall main;
42                 }
44                 action return_main
45                 {
46                         if ( top == 0 ) {
47                                 System.out.print( "STRAY CLOSE\n" );
48                                 fbreak;
49                         }
51                         System.out.print( "returning from main\n" );
52                         fhold;
53                         fret;
54                 }
56                 id = [a-zA-Z_]+;
57                 number = [0-9]+;
58                 ws = [ \t\n]+;
60                 main := ( 
61                         ws |
62                         ( number | id ) >item_start %item_finish |
64                         '{' @call_main '}' |
66                         '}' @return_main
67                 )**;
68         }%%
70         %% write data;
72         static void test( char data[] )
73         {
74                 int cs, p = 0, pe = data.length, eof = data.length, item = 0;
75                 int stack[] = new int[1024];
76                 int stack_size = 1;
77                 int top;
79                 %% write init;
80                 %% write exec;
82                 if ( cs == recdescent_error )
83                         System.out.println( "SCANNER ERROR" );
84         }
86         public static void main( String args[] )
87         {
88                 test( "88 foo { 99 {{{{}}}}{ } }".toCharArray() );
89                 test( "76 } sadf".toCharArray() );
90         }
93 /* _____OUTPUT_____
94 item: 88
95 item: foo
96 calling main
97 item: 99
98 calling main
99 growing stack
100 calling main
101 growing stack
102 calling main
103 calling main
104 growing stack
105 returning from main
106 returning from main
107 returning from main
108 returning from main
109 shrinking stack
110 calling main
111 returning from main
112 returning from main
113 shrinking stack
114 item: 76
115 STRAY CLOSE