libcubescript/src/cs_util.cc

386 lines
11 KiB
C++
Raw Permalink Normal View History

2017-06-20 21:21:39 +02:00
#include <cubescript/cubescript.hh>
#include "cs_util.hh"
#include <ctype.h>
#include <math.h>
namespace cscript {
2017-02-16 19:07:22 +01:00
static inline void p_skip_white(ostd::string_range &v) {
2016-08-15 19:57:31 +02:00
while (!v.empty() && isspace(*v)) {
2016-08-15 19:55:22 +02:00
++v;
}
}
static inline void p_set_end(
2017-02-16 19:07:22 +01:00
const ostd::string_range &v, ostd::string_range *end
) {
if (!end) {
return;
}
*end = v;
}
/* this function assumes the input is definitely a hex digit */
2017-02-13 18:10:40 +01:00
static inline cs_int p_hexd_to_int(char c) {
if (c >= 97) { /* a-f */
2016-08-15 03:44:48 +02:00
return (c - 'a') + 10;
} else if (c >= 65) { /* A-F */
return (c - 'A') + 10;
}
/* 0-9 */
return c - '0';
}
2017-02-16 19:07:22 +01:00
static inline bool p_check_neg(ostd::string_range &input) {
2016-08-15 19:55:22 +02:00
bool neg = (*input == '-');
if (neg || (*input == '+')) {
++input;
2016-08-15 03:49:24 +02:00
}
return neg;
}
2017-02-16 19:07:22 +01:00
cs_int cs_parse_int(ostd::string_range input, ostd::string_range *end) {
ostd::string_range orig = input;
p_skip_white(input);
if (input.empty()) {
p_set_end(orig, end);
2017-02-13 18:10:40 +01:00
return cs_int(0);
}
2016-08-15 03:49:24 +02:00
bool neg = p_check_neg(input);
2017-02-13 18:10:40 +01:00
cs_int ret = 0;
2017-02-16 19:07:22 +01:00
ostd::string_range past = input;
if (input.size() >= 2) {
2017-02-16 19:07:22 +01:00
ostd::string_range pfx = input.slice(0, 2);
if ((pfx == "0x") || (pfx == "0X")) {
2017-04-01 01:03:22 +02:00
input = input.slice(2, input.size());
2016-08-15 03:44:48 +02:00
past = input;
2016-08-15 19:55:22 +02:00
while (!past.empty() && isxdigit(*past)) {
ret = ret * 16 + p_hexd_to_int(*past);
++past;
2016-08-15 03:44:48 +02:00
}
goto done;
} else if ((pfx == "0b") || (pfx == "0B")) {
2017-04-01 01:03:22 +02:00
input = input.slice(2, input.size());
2016-08-15 03:44:48 +02:00
past = input;
2016-08-15 19:55:22 +02:00
while (!past.empty() && ((*past == '0') || (*past == '1'))) {
ret = ret * 2 + (*past - '0');
++past;
2016-08-15 03:44:48 +02:00
}
goto done;
}
}
2016-08-15 19:55:22 +02:00
while (!past.empty() && isdigit(*past)) {
ret = ret * 10 + (*past - '0');
++past;
2016-08-15 03:44:48 +02:00
}
done:
2017-03-31 03:34:09 +02:00
if (&past[0] == &input[0]) {
p_set_end(orig, end);
} else {
p_set_end(past, end);
}
2016-08-15 03:49:24 +02:00
if (neg) {
return -ret;
}
return ret;
}
2016-08-17 18:08:14 +02:00
template<bool Hex, char e1 = Hex ? 'p' : 'e', char e2 = Hex ? 'P' : 'E'>
2017-02-16 19:07:22 +01:00
static inline bool p_read_exp(ostd::string_range &input, cs_int &fn) {
if (input.empty()) {
return true;
}
2016-08-15 19:55:22 +02:00
if ((*input != e1) && (*input != e2)) {
return true;
}
2016-08-15 19:55:22 +02:00
++input;
if (input.empty()) {
return false;
}
2016-08-15 03:49:24 +02:00
bool neg = p_check_neg(input);
2016-08-15 19:55:22 +02:00
if (input.empty() || !isdigit(*input)) {
return false;
}
2017-02-13 18:10:40 +01:00
cs_int exp = 0;
2016-08-15 19:55:22 +02:00
while (!input.empty() && isdigit(*input)) {
exp = exp * 10 + (*input - '0');
++input;
}
if (neg) {
exp = -exp;
}
fn += exp;
return true;
}
2016-08-17 18:08:14 +02:00
template<bool Hex>
static inline bool parse_gen_float(
2017-02-16 19:07:22 +01:00
ostd::string_range input, ostd::string_range *end, cs_float &ret
) {
2017-02-13 18:10:40 +01:00
auto read_digits = [&input](double r, cs_int &n) {
2016-08-17 18:08:14 +02:00
while (!input.empty() && (Hex ? isxdigit(*input) : isdigit(*input))) {
if (Hex) {
r = r * 16.0 + double(p_hexd_to_int(*input));
} else {
r = r * 10.0 + double(*input - '0');
}
++n;
2016-08-15 19:55:22 +02:00
++input;
}
return r;
};
2017-02-13 18:10:40 +01:00
cs_int wn = 0, fn = 0;
2016-08-17 18:08:14 +02:00
double r = read_digits(0.0, wn);
2016-08-15 19:55:22 +02:00
if (!input.empty() && (*input == '.')) {
++input;
2016-08-17 18:08:14 +02:00
r = read_digits(r, fn);
}
if (!wn && !fn) {
return false;
}
2016-08-17 18:10:55 +02:00
fn = -fn;
p_set_end(input, end); /* we have a valid number until here */
2016-08-17 18:08:14 +02:00
if (p_read_exp<Hex>(input, fn)) {
p_set_end(input, end);
}
2016-08-17 18:08:14 +02:00
if (Hex) {
2017-02-13 18:10:40 +01:00
ret = cs_float(ldexp(r, fn * 4));
2016-08-17 18:08:14 +02:00
} else {
2017-02-13 18:10:40 +01:00
ret = cs_float(r * pow(10, fn));
}
return true;
}
2017-02-16 19:07:22 +01:00
cs_float cs_parse_float(ostd::string_range input, ostd::string_range *end) {
ostd::string_range orig = input;
p_skip_white(input);
if (input.empty()) {
p_set_end(orig, end);
2017-02-13 18:10:40 +01:00
return cs_float(0);
}
2016-08-15 03:49:24 +02:00
bool neg = p_check_neg(input);
2017-02-13 18:10:40 +01:00
cs_float ret = cs_float(0);
if (input.size() >= 2) {
2017-02-16 19:07:22 +01:00
ostd::string_range pfx = input.slice(0, 2);
2016-08-15 03:49:24 +02:00
if ((pfx == "0x") || (pfx == "0X")) {
2017-04-01 01:03:22 +02:00
input = input.slice(2, input.size());
2016-08-17 18:08:14 +02:00
if (!parse_gen_float<true>(input, end, ret)) {
p_set_end(orig, end);
return ret;
}
2016-08-15 03:49:24 +02:00
goto done;
}
}
2016-08-17 18:08:14 +02:00
if (!parse_gen_float<false>(input, end, ret)) {
p_set_end(orig, end);
return ret;
}
2016-08-15 03:49:24 +02:00
done:
if (neg) {
return -ret;
}
return ret;
}
2016-09-21 21:02:13 +02:00
namespace util {
2017-02-16 19:07:22 +01:00
OSTD_EXPORT ostd::string_range parse_string(
cs_state &cs, ostd::string_range str, size_t &nlines
2016-09-26 02:26:02 +02:00
) {
2017-01-25 02:09:50 +01:00
size_t nl = 0;
nlines = nl;
2016-10-10 20:14:16 +02:00
if (str.empty() || (*str != '\"')) {
return str;
}
2017-02-16 19:07:22 +01:00
ostd::string_range orig = str;
2016-09-26 02:26:02 +02:00
++str;
++nl;
2016-09-21 21:02:13 +02:00
while (!str.empty()) {
switch (*str) {
case '\r':
case '\n':
case '\"':
2016-09-26 02:26:02 +02:00
goto end;
2016-09-21 21:02:13 +02:00
case '^':
case '\\': {
bool needn = (*str == '\\');
2016-09-21 21:02:13 +02:00
++str;
if (str.empty()) {
goto end;
2016-09-21 21:02:13 +02:00
}
if ((*str == '\r') || (*str == '\n')) {
char c = *str;
++str;
++nl;
if (!str.empty() && (c == '\r') && (*str == '\n')) {
++str;
}
} else if (needn) {
goto end;
} else {
++str;
}
continue;
}
2016-09-21 21:02:13 +02:00
}
++str;
}
2016-09-26 02:26:02 +02:00
end:
nlines = nl;
2016-09-26 02:26:02 +02:00
if (str.empty() || (*str != '\"')) {
2017-02-13 18:10:40 +01:00
throw cs_error(
2017-06-15 20:44:09 +02:00
cs, "unfinished string '%s'", orig.slice(0, &str[0] - &orig[0])
2016-09-26 02:26:02 +02:00
);
}
2017-04-01 01:03:22 +02:00
str.pop_front();
return str;
2016-09-21 21:02:13 +02:00
}
2017-02-16 19:07:22 +01:00
OSTD_EXPORT ostd::string_range parse_word(
cs_state &cs, ostd::string_range str
) {
2016-09-21 21:02:13 +02:00
for (;;) {
2017-02-16 19:07:22 +01:00
str = ostd::find_one_of(str, ostd::string_range("\"/;()[] \t\r\n"));
2016-09-21 21:02:13 +02:00
if (str.empty()) {
return str;
}
switch (*str) {
case '"':
case ';':
case ' ':
case '\t':
case '\r':
case '\n':
return str;
case '/':
2016-09-22 01:07:43 +02:00
if ((str.size() > 1) && (str[1] == '/')) {
2016-09-21 21:02:13 +02:00
return str;
}
break;
case '[':
2017-04-01 01:03:22 +02:00
str.pop_front();
str = parse_word(cs, str);
2016-09-21 21:02:13 +02:00
if (str.empty() || (*str != ']')) {
2017-02-13 18:10:40 +01:00
throw cs_error(cs, "missing \"]\"");
2016-09-21 21:02:13 +02:00
}
break;
case '(':
2017-04-01 01:03:22 +02:00
str.pop_front();
str = parse_word(cs, str);
2016-09-21 21:02:13 +02:00
if (str.empty() || (*str != ')')) {
2017-02-13 18:10:40 +01:00
throw cs_error(cs, "missing \")\"");
2016-09-21 21:02:13 +02:00
}
break;
case ']':
case ')':
return str;
}
++str;
}
return str;
}
2018-04-25 01:49:58 +02:00
void list_parser::skip() {
2016-09-21 21:02:13 +02:00
for (;;) {
2016-10-27 00:49:16 +02:00
while (!p_input.empty()) {
char c = *p_input;
2016-09-21 21:02:13 +02:00
if ((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n')) {
2016-10-27 00:49:16 +02:00
++p_input;
2016-09-21 21:02:13 +02:00
} else {
break;
}
}
2016-10-27 00:49:16 +02:00
if ((p_input.size() < 2) || (p_input[0] != '/') || (p_input[1] != '/')) {
2016-09-21 21:02:13 +02:00
break;
}
2016-10-27 00:49:16 +02:00
p_input = ostd::find(p_input, '\n');
2016-09-21 21:02:13 +02:00
}
}
2018-04-25 01:49:58 +02:00
bool list_parser::parse() {
2016-09-21 21:02:13 +02:00
skip();
2016-10-27 00:49:16 +02:00
if (p_input.empty()) {
2016-09-21 21:02:13 +02:00
return false;
}
2016-10-27 00:49:16 +02:00
switch (*p_input) {
2016-09-21 21:02:13 +02:00
case '"':
2016-11-07 23:33:53 +01:00
p_quote = p_input;
2016-10-27 00:49:16 +02:00
p_input = parse_string(p_state, p_input);
2017-06-15 20:44:09 +02:00
p_quote = p_quote.slice(0, &p_input[0] - &p_quote[0]);
2016-11-07 23:33:53 +01:00
p_item = p_quote.slice(1, p_quote.size() - 1);
2016-09-21 21:02:13 +02:00
break;
case '(':
case '[': {
2016-11-07 23:33:53 +01:00
p_quote = p_input;
2016-10-27 00:49:16 +02:00
++p_input;
2016-11-07 23:33:53 +01:00
p_item = p_input;
char btype = *p_quote;
2016-09-21 21:02:13 +02:00
int brak = 1;
for (;;) {
2016-10-27 00:49:16 +02:00
p_input = ostd::find_one_of(
2017-02-16 19:07:22 +01:00
p_input, ostd::string_range("\"/;()[]")
2016-09-21 21:02:13 +02:00
);
2016-10-27 00:49:16 +02:00
if (p_input.empty()) {
2016-09-21 21:02:13 +02:00
return true;
}
2016-10-27 00:49:16 +02:00
char c = *p_input;
++p_input;
2016-09-21 21:02:13 +02:00
switch (c) {
case '"':
2016-10-27 00:49:16 +02:00
p_input = parse_string(p_state, p_input);
2016-09-21 21:02:13 +02:00
break;
case '/':
2016-10-27 00:49:16 +02:00
if (!p_input.empty() && (*p_input == '/')) {
p_input = ostd::find(p_input, '\n');
2016-09-21 21:02:13 +02:00
}
break;
case '(':
case '[':
brak += (c == btype);
break;
case ')':
if ((btype == '(') && (--brak <= 0)) {
goto endblock;
}
break;
case ']':
if ((btype == '[') && (--brak <= 0)) {
goto endblock;
}
break;
}
}
endblock:
2017-06-15 20:44:09 +02:00
p_item = p_item.slice(0, &p_input[0] - &p_item[0]);
2016-11-07 23:33:53 +01:00
p_item.pop_back();
2017-06-15 20:44:09 +02:00
p_quote = p_quote.slice(0, &p_input[0] - &p_quote[0]);
2016-09-21 21:02:13 +02:00
break;
}
case ')':
case ']':
return false;
default: {
2017-02-16 19:07:22 +01:00
ostd::string_range e = parse_word(p_state, p_input);
2017-06-15 20:44:09 +02:00
p_quote = p_item = p_input.slice(0, &e[0] - &p_input[0]);
2016-10-27 00:49:16 +02:00
p_input = e;
2016-09-21 21:02:13 +02:00
break;
}
}
skip();
2016-10-27 00:49:16 +02:00
if (!p_input.empty() && (*p_input == ';')) {
++p_input;
2016-09-21 21:02:13 +02:00
}
return true;
}
2018-04-25 01:49:58 +02:00
size_t list_parser::count() {
2017-01-25 02:09:50 +01:00
size_t ret = 0;
while (parse()) {
++ret;
}
return ret;
}
2016-09-21 21:02:13 +02:00
} /* namespace util */
} /* namespace cscript */