-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrex.h
205 lines (170 loc) · 7.43 KB
/
grex.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
MIT License
Copyright (c) 2023 Guilherme Freitas Nemeth
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
typedef enum grex_result {
GREX_OK = 0,
GREX_NO_MATCH = 1,
GREX_RANGE_ERR = 2,
GREX_EOF = -1,
} grex_result_t;
struct grex_parser;
/// @brief Error callback for handling parser errors
typedef void (*grex_error_callback_t)(struct grex_parser*, const char* msg, void* arg);
/// @brief The parser structure
typedef struct grex_parser {
const char* input;
const char* input_end;
unsigned input_length;
unsigned parsing_offset;
grex_error_callback_t error_callback;
void* error_callback_arg;
} grex_parser_t;
/// @brief Initialize the parser with the input
/// @param p
/// @param input
/// @param length
void grex_parser_init(grex_parser_t* p, const char* input, unsigned length);
/// @brief Releases all resources used by the parser
/// @param p
void grex_parser_destroy(grex_parser_t* p);
/// @brief Reset the parser's reading head to the start of the input
/// @param p
void grex_parser_reset(grex_parser_t* p);
/// @brief Set the parser's reading head to the end of the input
/// @param p
void grex_parser_end(grex_parser_t* p);
/// @brief Sets an error callback for the parser
/// @param p
/// @param cb
/// @param arg
void grex_parser_set_error_callback(grex_parser_t* p, grex_error_callback_t cb, void* arg);
/// @brief Matches on whitespace, including line breaks and carriage returns
/// @param p
/// @return GREX_OK on success, GREX_NO_MATCH on invalid input, GREX_EOF on eof
grex_result_t grex_whitespace(grex_parser_t* p);
/// @brief Matches on whitespace, NOT including line breaks and carriage returns
/// @param p
/// @return GREX_OK on success, GREX_NO_MATCH on invalid input, GREX_EOF on eof
grex_result_t grex_whitespace_no_line(grex_parser_t* p);
/// @brief Matches on specific character
/// @param p
/// @param c
/// @return GREX_OK on success, GREX_NO_MATCH on invalid input, GREX_EOF on eof
grex_result_t grex_char(grex_parser_t* p, int c);
/// @brief Matches a set of characters
/// @param p
/// @param set
/// @return GREX_OK on success, GREX_NO_MATCH on invalid input, GREX_EOF on eof
grex_result_t grex_set(grex_parser_t* p, const char* set);
/// @brief Matches an inclusive range of characters
/// @param p
/// @param range
/// @return GREX_OK on success, GREX_NO_MATCH on invalid input, GREX_EOF on eof
grex_result_t grex_range(grex_parser_t* p, const char* range);
/// @brief Matches a sequence of characters.
/// When the function returns, the parsing head is at the start of the sequence.
/// @param p
/// @param seq
/// @return GREX_OK on success, GREX_NO_MATCH on invalid input, GREX_EOF on eof
grex_result_t grex_sequence(grex_parser_t* p, const char* seq);
/// @brief Matches a sequence of characters, from the end to the beginning.
/// When the function returns, the parsing head is at the start of the sequence.
/// @param p
/// @param seq
/// @return
grex_result_t grex_sequence_reverse(grex_parser_t* p, const char* seq);
/// @brief Keep advancing while the current character is equal
/// @param p
/// @param seq
/// @return GREX_OK on success, GREX_NO_MATCH on invalid input, GREX_EOF on eof
grex_result_t grex_while(grex_parser_t* p, unsigned c);
/// @brief Keep advancing while the current character is not equal
/// @param p
/// @param seq
/// @return GREX_OK on success, GREX_NO_MATCH on invalid input, GREX_EOF on eof
grex_result_t grex_until(grex_parser_t* p, unsigned c);
/// @brief Keep advancing while the sequence of characters matches
/// @param p
/// @param seq
/// @return GREX_OK on success, GREX_NO_MATCH on invalid input, GREX_EOF on eof
grex_result_t grex_while_sequence(grex_parser_t* p, const char* seq);
/// @brief Keep receding while the sequence of characters matches
/// @param p
/// @param seq
/// @return GREX_OK on success, GREX_NO_MATCH on invalid input, GREX_EOF on eof
grex_result_t grex_while_sequence_reverse(grex_parser_t* p, const char* seq);
/// @brief Keep advancing while the sequence of characters does not match
/// When the function returns, the parsing head is at the end of the sequence.
/// @param p
/// @param seq
/// @return GREX_OK on success, GREX_NO_MATCH on invalid input, GREX_EOF on eof
grex_result_t grex_until_sequence(grex_parser_t* p, const char* seq);
/// @brief Keep receding while the sequence of characters does not match
/// When the function returns, the parsing head is at the start of the sequence.
/// @param p
/// @param seq
/// @return GREX_OK on success, GREX_NO_MATCH on invalid input, GREX_EOF on eof
grex_result_t grex_until_sequence_reverse(grex_parser_t* p, const char* seq);
/// @brief Matches an integer number
/// @param p
/// @param value
/// @return GREX_OK on success, GREX_NO_MATCH on invalid input, GREX_EOF on eof
grex_result_t grex_integer(grex_parser_t* p, int base, long long *value);
/// @brief Matches an unsigned integer number
/// @param p
/// @param value
/// @return GREX_OK on success, GREX_NO_MATCH on invalid input, GREX_EOF on eof
grex_result_t grex_uinteger(grex_parser_t* p, int base, unsigned long long* value);
/// @brief Matches a floating-point number
/// @param p
/// @param value
/// @return GREX_OK on success, GREX_NO_MATCH on invalid input, GREX_EOF on eof
grex_result_t grex_float(grex_parser_t* p, double* value);
/// @brief Matches a C-like identifier sequence of characters
/// @param p
/// @param buf
/// @param size
/// @return GREX_OK on success, GREX_NO_MATCH on invalid input, GREX_EOF on eof
grex_result_t grex_identifier(grex_parser_t* p, char* buf, unsigned size);
/// @brief Matches either a single or double quoted string
/// @param p
/// @param buf
/// @param size
/// @return GREX_OK on success, GREX_NO_MATCH on invalid input, GREX_EOF on eof
grex_result_t grex_string(grex_parser_t* p, char* buf, unsigned size);
/// @brief Matches a single-quoted string
/// @param p
/// @param buf
/// @param size
/// @return GREX_OK on success, GREX_NO_MATCH on invalid input, GREX_EOF on eof
grex_result_t grex_single_quoted_string(grex_parser_t* p, char* buf, unsigned size);
/// @brief Matches a double-quoted string
/// @param p
/// @param buf
/// @param size
/// @return GREX_OK on success, GREX_NO_MATCH on invalid input, GREX_EOF on eof
grex_result_t grex_double_quoted_string(grex_parser_t* p, char* buf, unsigned size);
/// @brief Capture all characters until a character matches
/// @param p
/// @param c
/// @param buf
/// @param size
/// @return GREX_OK on success, GREX_NO_MATCH on invalid input, GREX_EOF on eof
grex_result_t grex_capture_until(grex_parser_t* p, int c, char* buf, unsigned size);