yolobs-studio/libobs/util/lexer.h

281 lines
6.5 KiB
C
Raw Permalink Normal View History

2016-02-23 23:16:51 +00:00
/*
* 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;
2019-09-22 21:19:10 +00:00
dst->len = 0;
2016-02-23 23:16:51 +00:00
}
static inline void strref_set(struct strref *dst, const char *array, size_t len)
{
dst->array = array;
2019-09-22 21:19:10 +00:00
dst->len = len;
2016-02-23 23:16:51 +00:00
}
static inline void strref_copy(struct strref *dst, const struct strref *src)
{
dst->array = src->array;
2019-09-22 21:19:10 +00:00
dst->len = src->len;
2016-02-23 23:16:51 +00:00
}
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,
2019-09-22 21:19:10 +00:00
const struct strref *str2);
2016-02-23 23:16:51 +00:00
EXPORT int strref_cmpi_strref(const struct strref *str1,
2019-09-22 21:19:10 +00:00
const struct strref *str2);
2016-02-23 23:16:51 +00:00
/* ------------------------------------------------------------------------- */
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)
{
2019-09-22 21:19:10 +00:00
return (ch1 == '\r' && ch2 == '\n') || (ch1 == '\n' && ch2 == '\r');
2016-02-23 23:16:51 +00:00
}
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,
2019-09-22 21:19:10 +00:00
struct base_token *src)
2016-02-23 23:16:51 +00:00
{
memcpy(dst, src, sizeof(struct base_token));
}
/* ------------------------------------------------------------------------- */
2019-09-22 21:19:10 +00:00
#define LEX_ERROR 0
2016-02-23 23:16:51 +00:00
#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++)
2019-09-22 21:19:10 +00:00
error_item_free(array + i);
2016-02-23 23:16:51 +00:00
}
/* ------------------------------------------------------------------------- */
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,
2019-09-22 21:19:10 +00:00
size_t idx)
2016-02-23 23:16:51 +00:00
{
2019-09-22 21:19:10 +00:00
return ed->errors.array + idx;
2016-02-23 23:16:51 +00:00
}
EXPORT char *error_data_buildstring(struct error_data *ed);
EXPORT void error_data_add(struct error_data *ed, const char *file,
2019-09-22 21:19:10 +00:00
uint32_t row, uint32_t column, const char *msg,
int level);
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
static inline size_t error_data_type_count(struct error_data *ed, int type)
2016-02-23 23:16:51 +00:00
{
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);
2019-09-22 21:19:10 +00:00
lex->text = bstrdup(text);
2016-02-23 23:16:51 +00:00
lex->offset = lex->text;
}
static inline void lexer_start_move(struct lexer *lex, char *text)
{
lexer_free(lex);
2019-09-22 21:19:10 +00:00
lex->text = text;
2016-02-23 23:16:51 +00:00
lex->offset = lex->text;
}
static inline void lexer_reset(struct lexer *lex)
{
lex->offset = lex->text;
}
2019-09-22 21:19:10 +00:00
enum ignore_whitespace { PARSE_WHITESPACE, IGNORE_WHITESPACE };
2016-02-23 23:16:51 +00:00
EXPORT bool lexer_getbasetoken(struct lexer *lex, struct base_token *t,
2019-09-22 21:19:10 +00:00
enum ignore_whitespace iws);
2016-02-23 23:16:51 +00:00
EXPORT void lexer_getstroffset(const struct lexer *lex, const char *str,
2019-09-22 21:19:10 +00:00
uint32_t *row, uint32_t *col);
2016-02-23 23:16:51 +00:00
#ifdef __cplusplus
}
#endif