summaryrefslogtreecommitdiff
path: root/test/ragel.d/genrep4.rl
blob: 861de34fc499758e787b1d5bfb380eff95a0dfbd (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * @LANG: c
 */

#include <stddef.h>  /* NULL */
#include <stdint.h>  /* uint64_t */
#include <stdlib.h>  /* malloc(3) free(3) */
#include <stdbool.h> /* bool */
#include <string.h>
#include <stdio.h>

struct nfa_bp_rec
{
	long state;
	const unsigned char *p;
	int popTrans;
	long q_2;
};

struct nfa_bp_rec nfa_bp[1024];
long nfa_len = 0;
long nfa_count = 0;

long c;

struct nfa_state_rec
{
	long c;
};

struct nfa_state_rec nfa_s[1024];

void nfa_push()
{
	nfa_s[nfa_len].c = c;
}

void nfa_pop()
{
	c = nfa_s[nfa_len].c;
}

long q_2;

%%{
	machine match_any;
	alphtype unsigned char;

	action eol { p+1 == eof }

	eol = '' %when eol;

	action psh
	{
		nfa_bp[nfa_len].q_2 = q_2;
	}

	action pop
	{ ({
		q_2 = nfa_bp[nfa_len].q_2;
		1;
	}) }

	action ini 	{
		({  q_2 = 0; 1; })
	}

	action stay 	{
		({ 1; })
	}

	action repeat 	{
		({ ++q_2 < 3; })
	}

	action exit 	{
		({ ++q_2 >= 2; })
	}

	action marker
	{ ({printf("  marker\n");1;}) }

	main := 
		( '' %when(marker) 
			:nfa( ( 'a' ), psh, pop, ini, stay, repeat, exit ): ' ' ) {2}
		eol
		any @{printf("----- MATCH\n");}
	;

	write data;
}%%

int test( const char *data )
{
	int cs;
	const unsigned char *p = (const unsigned char *)data;
	const unsigned char *pe = p + strlen(data) + 1;
	const unsigned char *eof = pe;

	printf( "%s\n", data );

	%% write init;
	%% write exec;
	
	return 0;
}

int main()
{
	test( "a " );
	test( "aa " );
	test( "aaa " );
	test( "aaaa " );

	test( "a a " );
	test( "aa aa " );
	test( "aaa aaa " );
	test( "aaaa aaaa " );

	test( "a a a " );
	test( "aa aa aa " );
	test( "aaa aaa aaa " );
	test( "aaaa aaaa aaaa " );

	test( "aa a " );
	test( "aa aaa " );
	test( "aa aaaa " );

	test( "aaa a " );
	test( "aaa aa " );
	test( "aaa aaaa " );

	return 0;
}

##### OUTPUT #####
a 
  marker
aa 
  marker
  marker
aaa 
  marker
  marker
aaaa 
  marker
a a 
  marker
aa aa 
  marker
  marker
----- MATCH
aaa aaa 
  marker
  marker
----- MATCH
aaaa aaaa 
  marker
a a a 
  marker
aa aa aa 
  marker
  marker
aaa aaa aaa 
  marker
  marker
aaaa aaaa aaaa 
  marker
aa a 
  marker
  marker
aa aaa 
  marker
  marker
----- MATCH
aa aaaa 
  marker
  marker
aaa a 
  marker
  marker
aaa aa 
  marker
  marker
----- MATCH
aaa aaaa 
  marker
  marker