summaryrefslogtreecommitdiffhomepage
path: root/src/regex/tbnf_regex.c
blob: 2cdcb48ac321ba35f07679e42665363906b3e861 (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
/**************************************************************/
/*  treebnf: a tree oriented bnf library                      */
/*  Copyright (C) 2024  SysDeer Technologies, LLC             */
/*  Released under GPLv2 and GPLv3; see COPYING.TREEBNF.      */
/**************************************************************/

#include <treebnf/treebnf.h>

#include "treebnf_regex_impl.h"
#include "treebnf_visibility_impl.h"

#include "tbnf_regex_defs.h"
#include "tbnf_regex_scanfns.h"

#define TBNF_STATE_STACK_SIZE   (512)

/* init state scan table*/
static struct tbnf_scan_tbl tbnf_regex_scan_tbl__init[TBNF_REGEX_TOK_CAP] = {
	/* --> brace */
	[TBNF_REGEX_TOK_LBRACE] = {
		.tok_scan_fn    = tbnf_regex_scan_lbrace,
		.tok_state_op   = TBNF_STATE_PUSH,
		.tok_state_next = TBNF_REGEX_STATE_BRACE,
	},


	/* --> bracket */
	[TBNF_REGEX_TOK_LBRACKET_CIRCUMFLEX_RBRACKET] = {
		.tok_scan_fn    = tbnf_regex_scan_lbracket_circumflex_rbracket,
		.tok_state_op   = TBNF_STATE_PUSH,
		.tok_state_next = TBNF_REGEX_STATE_BRACKET,
	},

	[TBNF_REGEX_TOK_LBRACKET_CIRCUMFLEX] = {
		.tok_scan_fn    = tbnf_regex_scan_lbracket_circumflex,
		.tok_state_op   = TBNF_STATE_PUSH,
		.tok_state_next = TBNF_REGEX_STATE_BRACKET,
	},

	[TBNF_REGEX_TOK_LBRACKET_RBRACKET] = {
		.tok_scan_fn    = tbnf_regex_scan_lbracket_rbracket,
		.tok_state_op   = TBNF_STATE_PUSH,
		.tok_state_next = TBNF_REGEX_STATE_BRACKET,
	},

	[TBNF_REGEX_TOK_LBRACKET] = {
		.tok_scan_fn    = tbnf_regex_scan_lbracket,
		.tok_state_op   = TBNF_STATE_PUSH,
		.tok_state_next = TBNF_REGEX_STATE_BRACKET,
	},


	/* (expression) */
	[TBNF_REGEX_TOK_ESCAPED_CHAR] = {
		.tok_scan_fn    = tbnf_regex_scan_escaped_char,
		.tok_state_op   = TBNF_STATE_KEEP,
	},

	[TBNF_REGEX_TOK_CIRCUMFLEX_ASTERISK] = {
		.tok_scan_fn    = tbnf_regex_scan_circumflex_asterisk,
		.tok_state_op   = TBNF_STATE_KEEP,
	},

	[TBNF_REGEX_TOK_CIRCUMFLEX] = {
		.tok_scan_fn    = tbnf_regex_scan_circumflex,
		.tok_state_op   = TBNF_STATE_KEEP,
	},

	[TBNF_REGEX_TOK_LPAREN] = {
		.tok_scan_fn    = tbnf_regex_scan_lparen,
		.tok_state_op   = TBNF_STATE_KEEP,
	},

	[TBNF_REGEX_TOK_RPAREN] = {
		.tok_scan_fn    = tbnf_regex_scan_rparen,
		.tok_state_op   = TBNF_STATE_KEEP,
	},

	[TBNF_REGEX_TOK_ASTERISK] = {
		.tok_scan_fn    = tbnf_regex_scan_asterisk,
		.tok_state_op   = TBNF_STATE_KEEP,
	},

	[TBNF_REGEX_TOK_PERIOD] = {
		.tok_scan_fn    = tbnf_regex_scan_period,
		.tok_state_op   = TBNF_STATE_KEEP,
	},

	[TBNF_REGEX_TOK_DOLLAR] = {
		.tok_scan_fn    = tbnf_regex_scan_dollar,
		.tok_state_op   = TBNF_STATE_KEEP,
	},

	[TBNF_REGEX_TOK_VLINE] = {
		.tok_scan_fn    = tbnf_regex_scan_vline,
		.tok_state_op   = TBNF_STATE_KEEP,
	},

	[TBNF_REGEX_TOK_QMARK] = {
		.tok_scan_fn    = tbnf_regex_scan_qmark,
		.tok_state_op   = TBNF_STATE_KEEP,
	},

	[TBNF_REGEX_TOK_PLUS] = {
		.tok_scan_fn    = tbnf_regex_scan_plus,
		.tok_state_op   = TBNF_STATE_KEEP,
	},

	[TBNF_REGEX_TOK_CHAR] = {
		.tok_scan_fn    = tbnf_regex_scan_char,
		.tok_state_op   = TBNF_STATE_KEEP,
	},
};


/* brace state scan table*/
static struct tbnf_scan_tbl tbnf_regex_scan_tbl__brace[TBNF_REGEX_TOK_CAP] = {
	[TBNF_REGEX_TOK_BRACE_RBRACE] = {
		.tok_scan_fn    = tbnf_regex_scan_brace_rbrace,
		.tok_state_op   = TBNF_STATE_POP,
	},

	[TBNF_REGEX_TOK_BRACE_DIGIT] = {
		.tok_scan_fn    = tbnf_regex_scan_brace_digit,
		.tok_state_op   = TBNF_STATE_KEEP,
	},

	[TBNF_REGEX_TOK_BRACE_COMMA] = {
		.tok_scan_fn    = tbnf_regex_scan_brace_comma,
		.tok_state_op   = TBNF_STATE_KEEP,
	},
};


/* bracket state scan table*/
static struct tbnf_scan_tbl tbnf_regex_scan_tbl__bracket[TBNF_REGEX_TOK_CAP] = {
	[TBNF_REGEX_TOK_BRACKET_RBRACKET] = {
		.tok_scan_fn    = tbnf_regex_scan_bracket_rbracket,
		.tok_state_op   = TBNF_STATE_POP,
	},

	[TBNF_REGEX_TOK_BRACKET_ESCAPED_CHAR] = {
		.tok_scan_fn    = tbnf_regex_scan_bracket_escaped_char,
		.tok_state_op   = TBNF_STATE_KEEP,
	},

	[TBNF_REGEX_TOK_BRACKET_CHARACTER_CLASS] = {
		.tok_scan_fn    = tbnf_regex_scan_bracket_character_class,
		.tok_state_op   = TBNF_STATE_KEEP,
	},

	[TBNF_REGEX_TOK_BRACKET_COLLATION_SYMBOL] = {
		.tok_scan_fn    = tbnf_regex_scan_bracket_collation_symbol,
		.tok_state_op   = TBNF_STATE_KEEP,
	},

	[TBNF_REGEX_TOK_BRACKET_EQUIVALENCE_CLASS] = {
		.tok_scan_fn    = tbnf_regex_scan_bracket_equivalence_class,
		.tok_state_op   = TBNF_STATE_KEEP,
	},

	[TBNF_REGEX_TOK_BRACKET_HYPHEN] = {
		.tok_scan_fn    = tbnf_regex_scan_bracket_hyphen,
		.tok_state_op   = TBNF_STATE_KEEP,
	},

	[TBNF_REGEX_TOK_BRACKET_ERROR] = {
		.tok_scan_fn    = tbnf_regex_scan_bracket_error,
		.tok_state_op   = TBNF_STATE_KEEP,
	},

	[TBNF_REGEX_TOK_BRACKET_CHAR] = {
		.tok_scan_fn    = tbnf_regex_scan_char,
		.tok_state_op   = TBNF_STATE_KEEP,
	},
};


/* extended regex expression token scan table */
tbnf_hidden struct tbnf_scan_tbl * tbnf_regex_scan_tbl[] = {
	[TBNF_REGEX_STATE_INIT]    = tbnf_regex_scan_tbl__init,
	[TBNF_REGEX_STATE_BRACE]   = tbnf_regex_scan_tbl__brace,
	[TBNF_REGEX_STATE_BRACKET] = tbnf_regex_scan_tbl__bracket,
	[TBNF_REGEX_STATE_CAP]     = 0,
};