summaryrefslogtreecommitdiff
path: root/test/recdescent2.rl
blob: 59c45866edb7196d2e3d530007fcc5064b870685 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * @LANG: java
 */

class recdescent2
{
	%%{
		machine recdescent;

		prepush { 
			if ( top == stack_size ) {
				System.out.print( "growing stack\n" );
				stack_size = top * 2;
				// Don't actually bother to resize here, but we do print messages.
				//stack = (int*)realloc( stack, sizeof(int)*stack_size );
			}
		}

		postpop { 
			if ( stack_size > (top * 4) ) {
				stack_size = top * 2;
				// Don't actually bother to resize here, but we do print messages.
				//stack = (int*)realloc( stack, sizeof(int)*stack_size );
				System.out.print( "shrinking stack\n" );
			}
		}

		action item_start { item = p; }

		action item_finish
		{
			String item_data = new String ( data, item, p-item );
			System.out.print( "item: " );
			System.out.print( item_data );
			System.out.print( "\n" );
		}

		action call_main
		{
			System.out.print( "calling main\n" );
			fcall main;
		}

		action return_main
		{
			if ( top == 0 ) {
				System.out.print( "STRAY CLOSE\n" );
				fbreak;
			}

			System.out.print( "returning from main\n" );
			fhold;
			fret;
		}

		id = [a-zA-Z_]+;
		number = [0-9]+;
		ws = [ \t\n]+;

		main := ( 
			ws |
			( number | id ) >item_start %item_finish |

			'{' @call_main '}' |

			'}' @return_main
		)**;
	}%%

	%% write data;

	static void test( char data[] )
	{
		int cs, p = 0, pe = data.length, eof = data.length, item = 0;
		int stack[] = new int[1024];
		int stack_size = 1;
		int top;

		%% write init;
		%% write exec;

		if ( cs == recdescent_error )
			System.out.println( "SCANNER ERROR" );
	}

	public static void main( String args[] )
	{
		test( "88 foo { 99 {{{{}}}}{ } }".toCharArray() );
		test( "76 } sadf".toCharArray() );
	}
}

/* _____OUTPUT_____
item: 88
item: foo
calling main
item: 99
calling main
growing stack
calling main
growing stack
calling main
calling main
growing stack
returning from main
returning from main
returning from main
returning from main
shrinking stack
calling main
returning from main
returning from main
shrinking stack
item: 76
STRAY CLOSE
*/