summaryrefslogtreecommitdiff
path: root/test/ragel.d/nfa1.rl
blob: 284f12281d9b3ea8f060824dbeb44c34ec56c3c2 (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
/*
 * @LANG: c
 * @FILTER: LC_ALL=C sort
 */

#include <string.h>
#include <stdio.h>

int neg ;
long value ;

struct nfa_bp_rec
{
	long state;
	char *p;
	int pop;
};

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

%%{
	machine atoi;

	action begin {neg = 0;
		value = 0;
	}

	action see_neg {
		neg = 1;
	}

	action add_digit {
		value = value * 10 + ( int ) ( fc - 48 );
	}

	action finish {
		if ( neg ) { value = -1 * value; }
	}

	action print1 {
		printf( "%ld", value );
		printf( "%s", "\n" );
		fnext *atoi_error;
	}

	action print2 {
		printf( "saw 80808080\n" );
		fnext *atoi_error;
	}

	atoi = (
		('-'@see_neg | '+')? (digit @add_digit)+
	) >begin %finish;

	main1 = atoi '\n' @print1;
	main2 = [0-9]* '00000000' [0-9]* '\n' @print2;

	main |= (5, 0) main1 | main2;
}%%

%% write data;
int cs;
int blen;
char buffer[1024];

void init()
{
	value = 0;
	neg = 0;
	%% write init;
}

void exec( char *data, int len )
{
	char *p = data;
	char *pe = data + len;
	char *eof = pe;
	%% write exec;
}

void finish( )
{
	if ( cs >= atoi_first_final )
		printf( "-> accept\n\n" );
	else
		printf( "-> fail\n\n" );
}

char *inp[] = {
	"1\n",
	"12\n",
	"1002000000002\n",
	"222222\n",
	"+2123\n",
	"-12321\n",
	"-99\n",
	"213 3213\n",
	"--123\n",
	" -3000\n",
};

int inplen = 10;

int main( )
{
	int i;
	for ( i = 0; i < inplen; i++ ) {
		init();
		exec( inp[i], strlen(inp[i]) );
		finish();
	}
	return 0;
}

##### OUTPUT #####










-12321
-99
-> fail
-> fail
-> fail
-> fail
-> fail
-> fail
-> fail
-> fail
-> fail
-> fail
1
1002000000002
12
2123
222222
saw 80808080