summaryrefslogtreecommitdiff
path: root/test/statechart1.rl
blob: 9f1ce494a3b682740435b4d946de9a3cb8d93656 (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
/*
 * @LANG: c
 */

/*
 * Test in and out state actions.
 */

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

struct state_chart
{
	int cs;
};

%%{
	machine state_chart;
	variable curstate fsm->cs;

	action a { printf("a"); }
	action b { printf("b"); }
	action hexa { printf("a"); }
	action hexb { printf("b"); }

	hex_a = '0x' '0'* '61' @hexa;
	hex_b = '0x' '0'* '62' @hexb;

	a = 'a' @a | hex_a;
	b = 'b' @b | hex_b;
	ws = ' '+;

	mach = 
		start: ( 
			a -> st1 |
			b -> st2 | 
			zlen -> final 
		),
		st1: ( 
			a -> st1 | 
			ws -> start |
			zlen -> final 
		),
		st2: ( 
			b -> st2 |
			ws -> start |
			zlen -> final
		);
	
	main := ( mach '\n' )*;
}%%

%% write data;

void state_chart_init( struct state_chart *fsm )
{
	%% write init;
}

void state_chart_execute( struct state_chart *fsm, const char *_data, int _len )
{
	const char *p = _data;
	const char *pe = _data+_len;

	%% write exec;
}

int state_chart_finish( struct state_chart *fsm )
{
	%% write eof;

	if ( fsm->cs == state_chart_error )
		return -1;
	if ( fsm->cs >= state_chart_first_final )
		return 1;
	return 0;
}

struct state_chart sc;

void test( char *buf )
{
	int len = strlen( buf );
	state_chart_init( &sc );
	state_chart_execute( &sc, buf, len );
	state_chart_finish( &sc );
	printf("\n");
}

int main()
{
	test(
		"aa0x0061aa b\n"
		"bbb0x62b 0x61 0x000062\n"
	);

	return 0;
}

#ifdef _____OUTPUT_____
aaaaabbbbbbab
#endif