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,
67  const char *str, 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,
80  int level, const char *val1, const char *val2,
81  const char *val3);
82 
83 static inline void cf_adderror_expecting(struct cf_parser *p,
84  const char *expected)
85 {
86  cf_adderror(p, "Expected '$1'", LEX_ERROR, expected, NULL, NULL);
87 }
88 
89 static inline void cf_adderror_unexpected_eof(struct cf_parser *p)
90 {
91  cf_adderror(p, "Unexpected EOF", LEX_ERROR,
92  NULL, NULL, NULL);
93 }
94 
95 static inline void cf_adderror_syntax_error(struct cf_parser *p)
96 {
97  cf_adderror(p, "Syntax error", LEX_ERROR,
98  NULL, NULL, NULL);
99 }
100 
101 static inline bool cf_next_token(struct cf_parser *p)
102 {
103  if (p->cur_token->type != CFTOKEN_SPACETAB &&
104  p->cur_token->type != CFTOKEN_NEWLINE &&
105  p->cur_token->type != CFTOKEN_NONE)
106  p->cur_token++;
107 
108  while (p->cur_token->type == CFTOKEN_SPACETAB ||
110  p->cur_token++;
111 
112  return p->cur_token->type != CFTOKEN_NONE;
113 }
114 
115 static inline bool cf_next_valid_token(struct cf_parser *p)
116 {
117  if (!cf_next_token(p)) {
118  cf_adderror_unexpected_eof(p);
119  return false;
120  }
121 
122  return true;
123 }
124 
125 EXPORT bool cf_pass_pair(struct cf_parser *p, char in, char out);
126 
127 static inline bool cf_go_to_token(struct cf_parser *p,
128  const char *str1, const char *str2)
129 {
130  while (cf_next_token(p)) {
131  if (strref_cmp(&p->cur_token->str, str1) == 0) {
132  return true;
133  } else if (str2 && strref_cmp(&p->cur_token->str, str2) == 0) {
134  return true;
135  } else if (*p->cur_token->str.array == '{') {
136  if (!cf_pass_pair(p, '{', '}'))
137  break;
138  }
139  }
140 
141  return false;
142 }
143 
144 static inline bool cf_go_to_valid_token(struct cf_parser *p,
145  const char *str1, const char *str2)
146 {
147  if (!cf_go_to_token(p, str1, str2)) {
148  cf_adderror_unexpected_eof(p);
149  return false;
150  }
151 
152  return true;
153 }
154 
155 static inline bool cf_go_to_token_type(struct cf_parser *p,
156  enum cf_token_type type)
157 {
158  while (p->cur_token->type != CFTOKEN_NONE &&
159  p->cur_token->type != type)
160  p->cur_token++;
161 
162  return p->cur_token->type != CFTOKEN_NONE;
163 }
164 
165 static inline int cf_token_should_be(struct cf_parser *p,
166  const char *str, const char *goto1, const char *goto2)
167 {
168  if (strref_cmp(&p->cur_token->str, str) == 0)
169  return PARSE_SUCCESS;
170 
171  if (goto1) {
172  if (!cf_go_to_token(p, goto1, goto2))
173  return PARSE_EOF;
174  }
175 
176  cf_adderror_expecting(p, str);
177  return PARSE_CONTINUE;
178 }
179 
180 static inline int cf_next_token_should_be(struct cf_parser *p,
181  const char *str, const char *goto1, const char *goto2)
182 {
183  if (!cf_next_token(p)) {
184  cf_adderror_unexpected_eof(p);
185  return PARSE_EOF;
186  } else if (strref_cmp(&p->cur_token->str, str) == 0) {
187  return PARSE_SUCCESS;
188  }
189 
190  if (goto1) {
191  if (!cf_go_to_token(p, goto1, goto2))
192  return PARSE_EOF;
193  }
194 
195  cf_adderror_expecting(p, str);
196  return PARSE_CONTINUE;
197 }
198 
199 static inline bool cf_peek_token(struct cf_parser *p, struct cf_token *peek)
200 {
201  struct cf_token *cur_token = p->cur_token;
202  bool success = cf_next_token(p);
203 
204  *peek = *p->cur_token;
205  p->cur_token = cur_token;
206 
207  return success;
208 }
209 
210 static inline bool cf_peek_valid_token(struct cf_parser *p,
211  struct cf_token *peek)
212 {
213  bool success = cf_peek_token(p, peek);
214  if (!success)
215  cf_adderror_unexpected_eof(p);
216  return success;
217 }
218 
219 static inline bool cf_token_is(struct cf_parser *p, const char *val)
220 {
221  return strref_cmp(&p->cur_token->str, val) == 0;
222 }
223 
224 static inline int cf_token_is_type(struct cf_parser *p,
225  enum cf_token_type type, const char *type_expected,
226  const char *goto_token)
227 {
228  if (p->cur_token->type != type) {
229  cf_adderror_expecting(p, type_expected);
230  if (goto_token) {
231  if (!cf_go_to_valid_token(p, goto_token, NULL))
232  return PARSE_EOF;
233  }
234  return PARSE_CONTINUE;
235  }
236 
237  return PARSE_SUCCESS;
238 }
239 
240 static inline void cf_copy_token(struct cf_parser *p, char **dst)
241 {
242  *dst = bstrdup_n(p->cur_token->str.array, p->cur_token->str.len);
243 }
244 
245 static inline int cf_get_name(struct cf_parser *p, char **dst,
246  const char *name, const char *goto_token)
247 {
248  int errcode;
249 
250  errcode = cf_token_is_type(p, CFTOKEN_NAME, name, goto_token);
251  if (errcode != PARSE_SUCCESS)
252  return errcode;
253 
254  *dst = bstrdup_n(p->cur_token->str.array, p->cur_token->str.len);
255  return PARSE_SUCCESS;
256 }
257 
258 static inline int cf_next_name(struct cf_parser *p, char **dst,
259  const char *name, const char *goto_token)
260 {
261  if (!cf_next_valid_token(p))
262  return PARSE_EOF;
263 
264  return cf_get_name(p, dst, name, goto_token);
265 }
266 
267 static inline int cf_next_token_copy(struct cf_parser *p, char **dst)
268 {
269  if (!cf_next_valid_token(p))
270  return PARSE_EOF;
271 
272  cf_copy_token(p, dst);
273  return PARSE_SUCCESS;
274 }
275 
276 static inline int cf_get_name_ref(struct cf_parser *p, struct strref *dst,
277  const char *name, const char *goto_token)
278 {
279  int errcode;
280 
281  errcode = cf_token_is_type(p, CFTOKEN_NAME, name, goto_token);
282  if (errcode != PARSE_SUCCESS)
283  return errcode;
284 
285  strref_copy(dst, &p->cur_token->str);
286  return PARSE_SUCCESS;
287 }
288 
289 static inline int cf_next_name_ref(struct cf_parser *p, struct strref *dst,
290  const char *name, const char *goto_token)
291 {
292  if (!cf_next_valid_token(p))
293  return PARSE_EOF;
294 
295  return cf_get_name_ref(p, dst, name, goto_token);
296 }
297 
298 #ifdef __cplusplus
299 }
300 #endif
Definition: cf-lexer.h:39
#define LEX_ERROR
Definition: lexer.h:156
#define PARSE_SUCCESS
Definition: cf-parser.h:33
size_t len
Definition: lexer.h:32
Definition: lexer.h:186
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:53
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
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)