Added two new statements: prepush and postpop. These are code blocks that are
[external/ragel.git] / test / recdescent2.rl
1 /*
2  * @LANG: java
3  */
4
5 class recdescent2
6 {
7         %%{
8                 machine recdescent;
9
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                 }
18
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                 }
27
28                 action item_start { item = p; }
29
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                 }
37
38                 action call_main
39                 {
40                         System.out.print( "calling main\n" );
41                         fcall main;
42                 }
43
44                 action return_main
45                 {
46                         if ( top == 0 ) {
47                                 System.out.print( "STRAY CLOSE\n" );
48                                 fbreak;
49                         }
50
51                         System.out.print( "returning from main\n" );
52                         fhold;
53                         fret;
54                 }
55
56                 id = [a-zA-Z_]+;
57                 number = [0-9]+;
58                 ws = [ \t\n]+;
59
60                 main := ( 
61                         ws |
62                         ( number | id ) >item_start %item_finish |
63
64                         '{' @call_main '}' |
65
66                         '}' @return_main
67                 )**;
68         }%%
69
70         %% write data;
71
72         static void test( char data[] )
73         {
74                 int cs, p = 0, pe = data.length, item = 0;
75                 int stack[] = new int[1024];
76                 int stack_size = 1;
77                 int top;
78
79                 %% write init;
80                 %% write exec;
81                 %% write eof;
82
83                 if ( cs == recdescent_error )
84                         System.out.println( "SCANNER ERROR" );
85         }
86
87         public static void main( String args[] )
88         {
89                 test( "88 foo { 99 {{{{}}}}{ } }".toCharArray() );
90                 test( "76 } sadf".toCharArray() );
91         }
92 }
93
94 /* _____OUTPUT_____
95 item: 88
96 item: foo
97 calling main
98 item: 99
99 calling main
100 growing stack
101 calling main
102 growing stack
103 calling main
104 calling main
105 growing stack
106 returning from main
107 returning from main
108 returning from main
109 returning from main
110 shrinking stack
111 calling main
112 returning from main
113 returning from main
114 shrinking stack
115 item: 76
116 STRAY CLOSE
117 */