/usr/include/obs/util/lexer.h is in libobs-dev 21.0.2+dfsg1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | /*
* Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#pragma once
#include "c99defs.h"
#include "dstr.h"
#include "darray.h"
#ifdef __cplusplus
extern "C" {
#endif
/* ------------------------------------------------------------------------- */
/* string reference (string segment within an already existing array) */
struct strref {
const char *array;
size_t len;
};
static inline void strref_clear(struct strref *dst)
{
dst->array = NULL;
dst->len = 0;
}
static inline void strref_set(struct strref *dst, const char *array, size_t len)
{
dst->array = array;
dst->len = len;
}
static inline void strref_copy(struct strref *dst, const struct strref *src)
{
dst->array = src->array;
dst->len = src->len;
}
static inline void strref_add(struct strref *dst, const struct strref *t)
{
if (!dst->array)
strref_copy(dst, t);
else
dst->len += t->len;
}
static inline bool strref_is_empty(const struct strref *str)
{
return !str || !str->array || !str->len || !*str->array;
}
EXPORT int strref_cmp(const struct strref *str1, const char *str2);
EXPORT int strref_cmpi(const struct strref *str1, const char *str2);
EXPORT int strref_cmp_strref(const struct strref *str1,
const struct strref *str2);
EXPORT int strref_cmpi_strref(const struct strref *str1,
const struct strref *str2);
/* ------------------------------------------------------------------------- */
EXPORT bool valid_int_str(const char *str, size_t n);
EXPORT bool valid_float_str(const char *str, size_t n);
static inline bool valid_int_strref(const struct strref *str)
{
return valid_int_str(str->array, str->len);
}
static inline bool valid_float_strref(const struct strref *str)
{
return valid_float_str(str->array, str->len);
}
static inline bool is_whitespace(char ch)
{
return ch == ' ' || ch == '\r' || ch == '\t' || ch == '\n';
}
static inline bool is_newline(char ch)
{
return ch == '\r' || ch == '\n';
}
static inline bool is_space_or_tab(const char ch)
{
return ch == ' ' || ch == '\t';
}
static inline bool is_newline_pair(char ch1, char ch2)
{
return (ch1 == '\r' && ch2 == '\n') ||
(ch1 == '\n' && ch2 == '\r');
}
static inline int newline_size(const char *array)
{
if (strncmp(array, "\r\n", 2) == 0 || strncmp(array, "\n\r", 2) == 0)
return 2;
else if (*array == '\r' || *array == '\n')
return 1;
return 0;
}
/* ------------------------------------------------------------------------- */
/*
* A "base" token is one of four things:
* 1.) A sequence of alpha characters
* 2.) A sequence of numeric characters
* 3.) A single whitespace character if whitespace is not ignored
* 4.) A single character that does not fall into the above 3 categories
*/
enum base_token_type {
BASETOKEN_NONE,
BASETOKEN_ALPHA,
BASETOKEN_DIGIT,
BASETOKEN_WHITESPACE,
BASETOKEN_OTHER,
};
struct base_token {
struct strref text;
enum base_token_type type;
bool passed_whitespace;
};
static inline void base_token_clear(struct base_token *t)
{
memset(t, 0, sizeof(struct base_token));
}
static inline void base_token_copy(struct base_token *dst,
struct base_token *src)
{
memcpy(dst, src, sizeof(struct base_token));
}
/* ------------------------------------------------------------------------- */
#define LEX_ERROR 0
#define LEX_WARNING 1
struct error_item {
char *error;
const char *file;
uint32_t row, column;
int level;
};
static inline void error_item_init(struct error_item *ei)
{
memset(ei, 0, sizeof(struct error_item));
}
static inline void error_item_free(struct error_item *ei)
{
bfree(ei->error);
error_item_init(ei);
}
static inline void error_item_array_free(struct error_item *array, size_t num)
{
size_t i;
for (i = 0; i < num; i++)
error_item_free(array+i);
}
/* ------------------------------------------------------------------------- */
struct error_data {
DARRAY(struct error_item) errors;
};
static inline void error_data_init(struct error_data *data)
{
da_init(data->errors);
}
static inline void error_data_free(struct error_data *data)
{
error_item_array_free(data->errors.array, data->errors.num);
da_free(data->errors);
}
static inline const struct error_item *error_data_item(struct error_data *ed,
size_t idx)
{
return ed->errors.array+idx;
}
EXPORT char *error_data_buildstring(struct error_data *ed);
EXPORT void error_data_add(struct error_data *ed, const char *file,
uint32_t row, uint32_t column, const char *msg, int level);
static inline size_t error_data_type_count(struct error_data *ed,
int type)
{
size_t count = 0, i;
for (i = 0; i < ed->errors.num; i++) {
if (ed->errors.array[i].level == type)
count++;
}
return count;
}
static inline bool error_data_has_errors(struct error_data *ed)
{
size_t i;
for (i = 0; i < ed->errors.num; i++)
if (ed->errors.array[i].level == LEX_ERROR)
return true;
return false;
}
/* ------------------------------------------------------------------------- */
struct lexer {
char *text;
const char *offset;
};
static inline void lexer_init(struct lexer *lex)
{
memset(lex, 0, sizeof(struct lexer));
}
static inline void lexer_free(struct lexer *lex)
{
bfree(lex->text);
lexer_init(lex);
}
static inline void lexer_start(struct lexer *lex, const char *text)
{
lexer_free(lex);
lex->text = bstrdup(text);
lex->offset = lex->text;
}
static inline void lexer_start_move(struct lexer *lex, char *text)
{
lexer_free(lex);
lex->text = text;
lex->offset = lex->text;
}
static inline void lexer_reset(struct lexer *lex)
{
lex->offset = lex->text;
}
enum ignore_whitespace {
PARSE_WHITESPACE,
IGNORE_WHITESPACE
};
EXPORT bool lexer_getbasetoken(struct lexer *lex, struct base_token *t,
enum ignore_whitespace iws);
EXPORT void lexer_getstroffset(const struct lexer *lex, const char *str,
uint32_t *row, uint32_t *col);
#ifdef __cplusplus
}
#endif
|