Open Broadcaster Software
Free, open source software for live streaming and recording
lexer.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 "c99defs.h"
20 #include "dstr.h"
21 #include "darray.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* ------------------------------------------------------------------------- */
28 /* string reference (string segment within an already existing array) */
29 
30 struct strref {
31  const char *array;
32  size_t len;
33 };
34 
35 static inline void strref_clear(struct strref *dst)
36 {
37  dst->array = NULL;
38  dst->len = 0;
39 }
40 
41 static inline void strref_set(struct strref *dst, const char *array, size_t len)
42 {
43  dst->array = array;
44  dst->len = len;
45 }
46 
47 static inline void strref_copy(struct strref *dst, const struct strref *src)
48 {
49  dst->array = src->array;
50  dst->len = src->len;
51 }
52 
53 static inline void strref_add(struct strref *dst, const struct strref *t)
54 {
55  if (!dst->array)
56  strref_copy(dst, t);
57  else
58  dst->len += t->len;
59 }
60 
61 static inline bool strref_is_empty(const struct strref *str)
62 {
63  return !str || !str->array || !str->len || !*str->array;
64 }
65 
66 EXPORT int strref_cmp(const struct strref *str1, const char *str2);
67 EXPORT int strref_cmpi(const struct strref *str1, const char *str2);
68 EXPORT int strref_cmp_strref(const struct strref *str1,
69  const struct strref *str2);
70 EXPORT int strref_cmpi_strref(const struct strref *str1,
71  const struct strref *str2);
72 
73 /* ------------------------------------------------------------------------- */
74 
75 EXPORT bool valid_int_str(const char *str, size_t n);
76 EXPORT bool valid_float_str(const char *str, size_t n);
77 
78 static inline bool valid_int_strref(const struct strref *str)
79 {
80  return valid_int_str(str->array, str->len);
81 }
82 
83 static inline bool valid_float_strref(const struct strref *str)
84 {
85  return valid_float_str(str->array, str->len);
86 }
87 
88 static inline bool is_whitespace(char ch)
89 {
90  return ch == ' ' || ch == '\r' || ch == '\t' || ch == '\n';
91 }
92 
93 static inline bool is_newline(char ch)
94 {
95  return ch == '\r' || ch == '\n';
96 }
97 
98 static inline bool is_space_or_tab(const char ch)
99 {
100  return ch == ' ' || ch == '\t';
101 }
102 
103 static inline bool is_newline_pair(char ch1, char ch2)
104 {
105  return (ch1 == '\r' && ch2 == '\n') || (ch1 == '\n' && ch2 == '\r');
106 }
107 
108 static inline int newline_size(const char *array)
109 {
110  if (strncmp(array, "\r\n", 2) == 0 || strncmp(array, "\n\r", 2) == 0)
111  return 2;
112  else if (*array == '\r' || *array == '\n')
113  return 1;
114 
115  return 0;
116 }
117 
118 /* ------------------------------------------------------------------------- */
119 
120 /*
121  * A "base" token is one of four things:
122  * 1.) A sequence of alpha characters
123  * 2.) A sequence of numeric characters
124  * 3.) A single whitespace character if whitespace is not ignored
125  * 4.) A single character that does not fall into the above 3 categories
126  */
127 
134 };
135 
136 struct base_token {
137  struct strref text;
140 };
141 
142 static inline void base_token_clear(struct base_token *t)
143 {
144  memset(t, 0, sizeof(struct base_token));
145 }
146 
147 static inline void base_token_copy(struct base_token *dst,
148  struct base_token *src)
149 {
150  memcpy(dst, src, sizeof(struct base_token));
151 }
152 
153 /* ------------------------------------------------------------------------- */
154 
155 #define LEX_ERROR 0
156 #define LEX_WARNING 1
157 
158 struct error_item {
159  char *error;
160  const char *file;
161  uint32_t row, column;
162  int level;
163 };
164 
165 static inline void error_item_init(struct error_item *ei)
166 {
167  memset(ei, 0, sizeof(struct error_item));
168 }
169 
170 static inline void error_item_free(struct error_item *ei)
171 {
172  bfree(ei->error);
173  error_item_init(ei);
174 }
175 
176 static inline void error_item_array_free(struct error_item *array, size_t num)
177 {
178  size_t i;
179  for (i = 0; i < num; i++)
180  error_item_free(array + i);
181 }
182 
183 /* ------------------------------------------------------------------------- */
184 
185 struct error_data {
186  DARRAY(struct error_item) errors;
187 };
188 
189 static inline void error_data_init(struct error_data *data)
190 {
191  da_init(data->errors);
192 }
193 
194 static inline void error_data_free(struct error_data *data)
195 {
196  error_item_array_free(data->errors.array, data->errors.num);
197  da_free(data->errors);
198 }
199 
200 static inline const struct error_item *error_data_item(struct error_data *ed,
201  size_t idx)
202 {
203  return ed->errors.array + idx;
204 }
205 
206 EXPORT char *error_data_buildstring(struct error_data *ed);
207 
208 EXPORT void error_data_add(struct error_data *ed, const char *file,
209  uint32_t row, uint32_t column, const char *msg,
210  int level);
211 
212 static inline size_t error_data_type_count(struct error_data *ed, int type)
213 {
214  size_t count = 0, i;
215  for (i = 0; i < ed->errors.num; i++) {
216  if (ed->errors.array[i].level == type)
217  count++;
218  }
219 
220  return count;
221 }
222 
223 static inline bool error_data_has_errors(struct error_data *ed)
224 {
225  size_t i;
226  for (i = 0; i < ed->errors.num; i++)
227  if (ed->errors.array[i].level == LEX_ERROR)
228  return true;
229 
230  return false;
231 }
232 
233 /* ------------------------------------------------------------------------- */
234 
235 struct lexer {
236  char *text;
237  const char *offset;
238 };
239 
240 static inline void lexer_init(struct lexer *lex)
241 {
242  memset(lex, 0, sizeof(struct lexer));
243 }
244 
245 static inline void lexer_free(struct lexer *lex)
246 {
247  bfree(lex->text);
248  lexer_init(lex);
249 }
250 
251 static inline void lexer_start(struct lexer *lex, const char *text)
252 {
253  lexer_free(lex);
254  lex->text = bstrdup(text);
255  lex->offset = lex->text;
256 }
257 
258 static inline void lexer_start_move(struct lexer *lex, char *text)
259 {
260  lexer_free(lex);
261  lex->text = text;
262  lex->offset = lex->text;
263 }
264 
265 static inline void lexer_reset(struct lexer *lex)
266 {
267  lex->offset = lex->text;
268 }
269 
271 
272 EXPORT bool lexer_getbasetoken(struct lexer *lex, struct base_token *t,
273  enum ignore_whitespace iws);
274 
275 EXPORT void lexer_getstroffset(const struct lexer *lex, const char *str,
276  uint32_t *row, uint32_t *col);
277 
278 #ifdef __cplusplus
279 }
280 #endif
lexer_getbasetoken
EXPORT bool lexer_getbasetoken(struct lexer *lex, struct base_token *t, enum ignore_whitespace iws)
lexer::offset
const char * offset
Definition: lexer.h:237
lexer_getstroffset
EXPORT void lexer_getstroffset(const struct lexer *lex, const char *str, uint32_t *row, uint32_t *col)
strref
Definition: lexer.h:30
valid_float_str
EXPORT bool valid_float_str(const char *str, size_t n)
BASETOKEN_ALPHA
@ BASETOKEN_ALPHA
Definition: lexer.h:130
base_token::type
enum base_token_type type
Definition: lexer.h:138
error_data_buildstring
EXPORT char * error_data_buildstring(struct error_data *ed)
error_item::file
const char * file
Definition: lexer.h:160
error_item::row
uint32_t row
Definition: lexer.h:161
error_item::level
int level
Definition: lexer.h:162
bfree
EXPORT void bfree(void *ptr)
EXPORT
#define EXPORT
Definition: c99defs.h:37
error_item::column
uint32_t column
Definition: lexer.h:161
da_free
#define da_free(v)
Definition: darray.h:467
lexer::text
char * text
Definition: lexer.h:236
strref_cmp
EXPORT int strref_cmp(const struct strref *str1, const char *str2)
base_token::text
struct strref text
Definition: lexer.h:137
dstr.h
BASETOKEN_WHITESPACE
@ BASETOKEN_WHITESPACE
Definition: lexer.h:132
strref::len
size_t len
Definition: lexer.h:32
c99defs.h
error_item
Definition: lexer.h:158
base_token_type
base_token_type
Definition: lexer.h:128
BASETOKEN_OTHER
@ BASETOKEN_OTHER
Definition: lexer.h:133
da_init
#define da_init(v)
Definition: darray.h:465
base_token
Definition: lexer.h:136
error_item::error
char * error
Definition: lexer.h:159
strref_cmp_strref
EXPORT int strref_cmp_strref(const struct strref *str1, const struct strref *str2)
BASETOKEN_DIGIT
@ BASETOKEN_DIGIT
Definition: lexer.h:131
error_data
Definition: lexer.h:185
PARSE_WHITESPACE
@ PARSE_WHITESPACE
Definition: lexer.h:270
valid_int_str
EXPORT bool valid_int_str(const char *str, size_t n)
base_token::passed_whitespace
bool passed_whitespace
Definition: lexer.h:139
strref_cmpi
EXPORT int strref_cmpi(const struct strref *str1, const char *str2)
strref::array
const char * array
Definition: lexer.h:31
LEX_ERROR
#define LEX_ERROR
Definition: lexer.h:155
strref_cmpi_strref
EXPORT int strref_cmpi_strref(const struct strref *str1, const struct strref *str2)
lexer
Definition: lexer.h:235
ignore_whitespace
ignore_whitespace
Definition: lexer.h:270
darray.h
IGNORE_WHITESPACE
@ IGNORE_WHITESPACE
Definition: lexer.h:270
error_data::DARRAY
DARRAY(struct error_item) errors
error_data_add
EXPORT void error_data_add(struct error_data *ed, const char *file, uint32_t row, uint32_t column, const char *msg, int level)
BASETOKEN_NONE
@ BASETOKEN_NONE
Definition: lexer.h:129