Open Broadcaster Software
Free, open source software for live streaming and recording
cf-parser.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #pragma once
18 
19 #include "cf-lexer.h"
20 
21 /*
22  * C-family parser
23  *
24  * Handles preprocessing/lexing/errors when parsing a file, and provides a
25  * set of parsing functions to be able to go through all the resulting tokens
26  * more easily.
27  */
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #define PARSE_SUCCESS 0
34 #define PARSE_CONTINUE -1
35 #define PARSE_BREAK -2
36 #define PARSE_UNEXPECTED_CONTINUE -3
37 #define PARSE_UNEXPECTED_BREAK -4
38 #define PARSE_EOF -5
39 
40 struct cf_parser {
41  struct cf_lexer lex;
44 
46 };
47 
48 static inline void cf_parser_init(struct cf_parser *parser)
49 {
50  cf_lexer_init(&parser->lex);
51  cf_preprocessor_init(&parser->pp);
52  error_data_init(&parser->error_list);
53 
54  parser->cur_token = NULL;
55 }
56 
57 static inline void cf_parser_free(struct cf_parser *parser)
58 {
59  cf_lexer_free(&parser->lex);
60  cf_preprocessor_free(&parser->pp);
61  error_data_free(&parser->error_list);
62 
63  parser->cur_token = NULL;
64 }
65 
66 static inline bool cf_parser_parse(struct cf_parser *parser, const char *str,
67  const char *file)
68 {
69  if (!cf_lexer_lex(&parser->lex, str, file))
70  return false;
71 
72  if (!cf_preprocess(&parser->pp, &parser->lex, &parser->error_list))
73  return false;
74 
75  parser->cur_token = cf_preprocessor_get_tokens(&parser->pp);
76  return true;
77 }
78 
79 EXPORT void cf_adderror(struct cf_parser *parser, const char *error, int level,
80  const char *val1, const char *val2, const char *val3);
81 
82 static inline void cf_adderror_expecting(struct cf_parser *p,
83  const char *expected)
84 {
85  cf_adderror(p, "Expected '$1'", LEX_ERROR, expected, NULL, NULL);
86 }
87 
88 static inline void cf_adderror_unexpected_eof(struct cf_parser *p)
89 {
90  cf_adderror(p, "Unexpected EOF", LEX_ERROR, NULL, NULL, NULL);
91 }
92 
93 static inline void cf_adderror_syntax_error(struct cf_parser *p)
94 {
95  cf_adderror(p, "Syntax error", LEX_ERROR, NULL, NULL, NULL);
96 }
97 
98 static inline bool cf_next_token(struct cf_parser *p)
99 {
100  if (p->cur_token->type != CFTOKEN_SPACETAB &&
101  p->cur_token->type != CFTOKEN_NEWLINE &&
102  p->cur_token->type != CFTOKEN_NONE)
103  p->cur_token++;
104 
105  while (p->cur_token->type == CFTOKEN_SPACETAB ||
107  p->cur_token++;
108 
109  return p->cur_token->type != CFTOKEN_NONE;
110 }
111 
112 static inline bool cf_next_valid_token(struct cf_parser *p)
113 {
114  if (!cf_next_token(p)) {
115  cf_adderror_unexpected_eof(p);
116  return false;
117  }
118 
119  return true;
120 }
121 
122 EXPORT bool cf_pass_pair(struct cf_parser *p, char in, char out);
123 
124 static inline bool cf_go_to_token(struct cf_parser *p, const char *str1,
125  const char *str2)
126 {
127  while (cf_next_token(p)) {
128  if (strref_cmp(&p->cur_token->str, str1) == 0) {
129  return true;
130  } else if (str2 && strref_cmp(&p->cur_token->str, str2) == 0) {
131  return true;
132  } else if (*p->cur_token->str.array == '{') {
133  if (!cf_pass_pair(p, '{', '}'))
134  break;
135  }
136  }
137 
138  return false;
139 }
140 
141 static inline bool cf_go_to_valid_token(struct cf_parser *p, const char *str1,
142  const char *str2)
143 {
144  if (!cf_go_to_token(p, str1, str2)) {
145  cf_adderror_unexpected_eof(p);
146  return false;
147  }
148 
149  return true;
150 }
151 
152 static inline bool cf_go_to_token_type(struct cf_parser *p,
153  enum cf_token_type type)
154 {
155  while (p->cur_token->type != CFTOKEN_NONE && p->cur_token->type != type)
156  p->cur_token++;
157 
158  return p->cur_token->type != CFTOKEN_NONE;
159 }
160 
161 static inline int cf_token_should_be(struct cf_parser *p, const char *str,
162  const char *goto1, const char *goto2)
163 {
164  if (strref_cmp(&p->cur_token->str, str) == 0)
165  return PARSE_SUCCESS;
166 
167  if (goto1) {
168  if (!cf_go_to_token(p, goto1, goto2))
169  return PARSE_EOF;
170  }
171 
172  cf_adderror_expecting(p, str);
173  return PARSE_CONTINUE;
174 }
175 
176 static inline int cf_next_token_should_be(struct cf_parser *p, const char *str,
177  const char *goto1, const char *goto2)
178 {
179  if (!cf_next_token(p)) {
180  cf_adderror_unexpected_eof(p);
181  return PARSE_EOF;
182  } else if (strref_cmp(&p->cur_token->str, str) == 0) {
183  return PARSE_SUCCESS;
184  }
185 
186  if (goto1) {
187  if (!cf_go_to_token(p, goto1, goto2))
188  return PARSE_EOF;
189  }
190 
191  cf_adderror_expecting(p, str);
192  return PARSE_CONTINUE;
193 }
194 
195 static inline bool cf_peek_token(struct cf_parser *p, struct cf_token *peek)
196 {
197  struct cf_token *cur_token = p->cur_token;
198  bool success = cf_next_token(p);
199 
200  *peek = *p->cur_token;
201  p->cur_token = cur_token;
202 
203  return success;
204 }
205 
206 static inline bool cf_peek_valid_token(struct cf_parser *p,
207  struct cf_token *peek)
208 {
209  bool success = cf_peek_token(p, peek);
210  if (!success)
211  cf_adderror_unexpected_eof(p);
212  return success;
213 }
214 
215 static inline bool cf_token_is(struct cf_parser *p, const char *val)
216 {
217  return strref_cmp(&p->cur_token->str, val) == 0;
218 }
219 
220 static inline int cf_token_is_type(struct cf_parser *p, enum cf_token_type type,
221  const char *type_expected,
222  const char *goto_token)
223 {
224  if (p->cur_token->type != type) {
225  cf_adderror_expecting(p, type_expected);
226  if (goto_token) {
227  if (!cf_go_to_valid_token(p, goto_token, NULL))
228  return PARSE_EOF;
229  }
230  return PARSE_CONTINUE;
231  }
232 
233  return PARSE_SUCCESS;
234 }
235 
236 static inline void cf_copy_token(struct cf_parser *p, char **dst)
237 {
238  *dst = bstrdup_n(p->cur_token->str.array, p->cur_token->str.len);
239 }
240 
241 static inline int cf_get_name(struct cf_parser *p, char **dst, const char *name,
242  const char *goto_token)
243 {
244  int errcode;
245 
246  errcode = cf_token_is_type(p, CFTOKEN_NAME, name, goto_token);
247  if (errcode != PARSE_SUCCESS)
248  return errcode;
249 
250  *dst = bstrdup_n(p->cur_token->str.array, p->cur_token->str.len);
251  return PARSE_SUCCESS;
252 }
253 
254 static inline int cf_next_name(struct cf_parser *p, char **dst,
255  const char *name, const char *goto_token)
256 {
257  if (!cf_next_valid_token(p))
258  return PARSE_EOF;
259 
260  return cf_get_name(p, dst, name, goto_token);
261 }
262 
263 static inline int cf_next_token_copy(struct cf_parser *p, char **dst)
264 {
265  if (!cf_next_valid_token(p))
266  return PARSE_EOF;
267 
268  cf_copy_token(p, dst);
269  return PARSE_SUCCESS;
270 }
271 
272 static inline int cf_get_name_ref(struct cf_parser *p, struct strref *dst,
273  const char *name, const char *goto_token)
274 {
275  int errcode;
276 
277  errcode = cf_token_is_type(p, CFTOKEN_NAME, name, goto_token);
278  if (errcode != PARSE_SUCCESS)
279  return errcode;
280 
281  strref_copy(dst, &p->cur_token->str);
282  return PARSE_SUCCESS;
283 }
284 
285 static inline int cf_next_name_ref(struct cf_parser *p, struct strref *dst,
286  const char *name, const char *goto_token)
287 {
288  if (!cf_next_valid_token(p))
289  return PARSE_EOF;
290 
291  return cf_get_name_ref(p, dst, name, goto_token);
292 }
293 
294 #ifdef __cplusplus
295 }
296 #endif
Definition: cf-lexer.h:39
#define LEX_ERROR
Definition: lexer.h:155
#define PARSE_SUCCESS
Definition: cf-parser.h:33
size_t len
Definition: lexer.h:32
Definition: lexer.h:185
Definition: cf-lexer.h:42
#define PARSE_CONTINUE
Definition: cf-parser.h:34
struct cf_lexer lex
Definition: cf-parser.h:41
struct cf_preprocessor pp
Definition: cf-parser.h:42
EXPORT bool cf_preprocess(struct cf_preprocessor *pp, struct cf_lexer *lex, struct error_data *ed)
Definition: cf-lexer.h:47
enum cf_token_type type
Definition: cf-lexer.h:51
Definition: cf-lexer.h:38
struct strref str
Definition: cf-lexer.h:49
cf_token_type
Definition: cf-lexer.h:37
#define EXPORT
Definition: c99defs.h:37
EXPORT bool cf_pass_pair(struct cf_parser *p, char in, char out)
EXPORT void cf_adderror(struct cf_parser *parser, const char *error, int level, const char *val1, const char *val2, const char *val3)
Definition: cf-lexer.h:170
Definition: cf-lexer.h:41
struct cf_token * cur_token
Definition: cf-parser.h:45
EXPORT void cf_lexer_init(struct cf_lexer *lex)
struct error_data error_list
Definition: cf-parser.h:43
Definition: cf-lexer.h:85
#define success(stat, call)
Definition: mac-helpers.h:13
EXPORT void cf_lexer_free(struct cf_lexer *lex)
Definition: cf-parser.h:40
EXPORT bool cf_lexer_lex(struct cf_lexer *lex, const char *str, const char *file)
EXPORT int strref_cmp(const struct strref *str1, const char *str2)
const char * array
Definition: lexer.h:31
EXPORT void cf_preprocessor_init(struct cf_preprocessor *pp)
#define PARSE_EOF
Definition: cf-parser.h:38
Definition: lexer.h:30
EXPORT void cf_preprocessor_free(struct cf_preprocessor *pp)