Scanners now ensure that a pattern's leaving actions are executed.
[ragel.git] / test / recdescent1.rl
blob1ffca2800802b66b92171a635576dae5663de356
1 /*
2  * @LANG: c
3  * Test growable stack.
4  */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
10 %%{
11         machine recdescent;
13         prepush { 
14                 if ( top == stack_size ) {
15                         printf( "growing stack\n" );
16                         stack_size = top * 2;
17                         stack = (int*)realloc( stack, sizeof(int)*stack_size );
18                 }
19         }
21         postpop { 
22                 if ( stack_size > (top * 4) ) {
23                         stack_size = top * 2;
24                         stack = (int*)realloc( stack, sizeof(int)*stack_size );
25                         printf( "shrinking stack\n" );
26                 }
27         }
29         action item_start { item = p; }
31         action item_finish
32         {
33                 printf( "item: " );
34                 fwrite( item, 1, p-item, stdout );
35                 printf( "\n" );
36         }
38         action call_main
39         {
40                 printf( "calling main\n" );
41                 fcall main;
42         }
44         action return_main
45         {
46                 if ( top == 0 ) {
47                         printf( "STRAY CLOSE\n" );
48                         fbreak;
49                 }
51                 printf( "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 void test( char *buf )
74         int cs;
75         int *stack;
76         int top, stack_size;
77         char *p, *pe, *eof, *item = 0;
79         int len = strlen( buf );
81         %% write init;
83         stack_size = 1;
84         stack = (int*)malloc( sizeof(int) * stack_size );
86         p = buf;
87         pe = buf + len;
88         eof = pe;
90         %% write exec;
92         if ( cs == recdescent_error ) {
93                 /* Machine failed before finding a token. */
94                 printf( "PARSE ERROR\n" );
95         }
98 int main()
100         test( "88 foo { 99 {{{{}}}}{ } }");
101         test( "76 } sadf");
102         return 0;
105 #ifdef _____OUTPUT_____
106 item: 88
107 item: foo
108 calling main
109 item: 99
110 calling main
111 growing stack
112 calling main
113 growing stack
114 calling main
115 calling main
116 growing stack
117 returning from main
118 returning from main
119 returning from main
120 returning from main
121 shrinking stack
122 calling main
123 returning from main
124 returning from main
125 shrinking stack
126 item: 76
127 STRAY CLOSE
128 #endif