-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtpre.h
85 lines (65 loc) · 1.87 KB
/
tpre.h
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
#ifndef _TPRE_H
#define _TPRE_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>
typedef uint8_t tpre_groupid_t;
typedef int16_t tpre_nodeid_t;
typedef uint8_t tpre_backtrack_t;
typedef struct {
uint8_t is_special;
uint8_t val;
} tpre_pattern_t;
typedef struct {
size_t pat_byte_loc;
char* message;
} tpre_err_t;
typedef struct {
tpre_err_t *items;
size_t len;
} tpre_errs_t;
typedef struct {
bool free;
tpre_nodeid_t num_nodes;
tpre_nodeid_t first_node;
tpre_groupid_t max_group;
tpre_pattern_t * i_pat;
tpre_nodeid_t * i_ok;
tpre_nodeid_t * i_err;
tpre_backtrack_t* i_backtrack;
tpre_groupid_t * i_group;
} tpre_re_t;
typedef int32_t tpre_src_loc_t;
typedef struct {
tpre_src_loc_t begin;
/** if is 0, then no match */
size_t len;
} tpre_group_t;
typedef struct {
bool found;
size_t ngroups;
tpre_group_t * groups;
} tpre_match_t;
void tpre_match_free(tpre_match_t match);
tpre_match_t tpre_match(tpre_re_t const* re, const char * str);
/** a tiny bit slower than tpre_match() */
tpre_match_t tpre_matchn(tpre_re_t const* re, const char * str, size_t strl);
/** matched_str does not have to be mull terminated because it only prints the slices of it that match */
void tpre_match_dump(tpre_match_t match, char const * matched_str, FILE* out);
/** 0 = ok; errsOut can be null */
int tpre_compile(tpre_re_t* out, char const * str, tpre_errs_t* errs_out);
void tpre_free(tpre_re_t re);
/** output is heap allocated */
uint8_t* tpre_serialize(tpre_re_t, size_t* len_out);
/** output cannot live longer than bytes; tpre_free on the output does NOT free bytes */
tpre_re_t tpre_deserialize(uint8_t* bytes);
tpre_re_t tpre_deserialize_copy(uint8_t const* bytes);
void tpre_errs_free(tpre_errs_t errs);
#ifdef __cplusplus
}
#endif
#endif