libcubescript/src/cs_util.cc

477 lines
13 KiB
C++
Raw Normal View History

2017-06-20 21:21:39 +02:00
#include <cubescript/cubescript.hh>
#include "cs_util.hh"
#include "cs_vm.hh"
2021-03-23 01:25:47 +01:00
#include "cs_strman.hh"
#include <cctype>
#include <cmath>
#include <iterator>
2021-03-20 06:52:10 +01:00
#include <algorithm>
namespace cscript {
static inline char const *p_skip_white(char const *beg, char const *end) {
while ((beg != end) && isspace(*beg)) {
++beg;
}
return beg;
}
static inline void p_set_end(
char const *nbeg, char const *nend, std::string_view *end
) {
if (!end) {
return;
}
*end = std::string_view{nbeg, std::size_t(nend - nbeg)};
}
/* 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';
}
static inline bool p_check_neg(char const *&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;
}
cs_int cs_parse_int(std::string_view input, std::string_view *endstr) {
char const *beg = input.begin();
char const *end = input.end();
char const *orig = beg;
beg = p_skip_white(beg, end);
if (beg == end) {
p_set_end(orig, end, endstr);
2017-02-13 18:10:40 +01:00
return cs_int(0);
}
bool neg = p_check_neg(beg);
2017-02-13 18:10:40 +01:00
cs_int ret = 0;
char const *past = beg;
if ((end - beg) >= 2) {
std::string_view pfx = std::string_view{beg, 2};
if ((pfx == "0x") || (pfx == "0X")) {
beg += 2;
past = beg;
while ((past != end) && isxdigit(*past)) {
ret = ret * 16 + p_hexd_to_int(*past++);
2016-08-15 03:44:48 +02:00
}
goto done;
} else if ((pfx == "0b") || (pfx == "0B")) {
beg += 2;
past = beg;
while ((past != end) && ((*past == '0') || (*past == '1'))) {
ret = ret * 2 + (*past++ - '0');
2016-08-15 03:44:48 +02:00
}
goto done;
}
}
while ((past != end) && isdigit(*past)) {
ret = ret * 10 + (*past++ - '0');
2016-08-15 03:44:48 +02:00
}
done:
p_set_end((past == beg) ? orig : past, end, endstr);
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'>
static inline bool p_read_exp(char const *&beg, char const *end, cs_int &fn) {
if (beg == end) {
return true;
}
if ((*beg != e1) && (*beg != e2)) {
return true;
}
if (++beg == end) {
return false;
}
bool neg = p_check_neg(beg);
if ((beg == end) || !isdigit(*beg)) {
return false;
}
2017-02-13 18:10:40 +01:00
cs_int exp = 0;
while ((beg != end) && isdigit(*beg)) {
exp = exp * 10 + (*beg++ - '0');
}
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(
char const *&beg, char const *end, std::string_view *endstr, cs_float &ret
) {
auto read_digits = [&beg, end](double r, cs_int &n) {
while ((beg != end) && (Hex ? isxdigit(*beg) : isdigit(*beg))) {
2016-08-17 18:08:14 +02:00
if (Hex) {
r = r * 16.0 + double(p_hexd_to_int(*beg));
2016-08-17 18:08:14 +02:00
} else {
r = r * 10.0 + double(*beg - '0');
2016-08-17 18:08:14 +02:00
}
++n;
++beg;
}
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);
if ((beg != end) && (*beg == '.')) {
++beg;
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(beg, end, endstr); /* we have a valid number until here */
if (p_read_exp<Hex>(beg, end, fn)) {
p_set_end(beg, end, endstr);
}
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;
}
cs_float cs_parse_float(std::string_view input, std::string_view *endstr) {
char const *beg = input.begin();
char const *end = input.end();
char const *orig = beg;
beg = p_skip_white(beg, end);
if (beg == end) {
p_set_end(orig, end, endstr);
2017-02-13 18:10:40 +01:00
return cs_float(0);
}
bool neg = p_check_neg(beg);
2017-02-13 18:10:40 +01:00
cs_float ret = cs_float(0);
if ((end - beg) >= 2) {
std::string_view pfx = std::string_view{beg, 2};
2016-08-15 03:49:24 +02:00
if ((pfx == "0x") || (pfx == "0X")) {
beg += 2;
if (!parse_gen_float<true>(beg, end, endstr, ret)) {
p_set_end(orig, end, endstr);
return ret;
}
2016-08-15 03:49:24 +02:00
goto done;
}
}
if (!parse_gen_float<false>(beg, end, endstr, ret)) {
p_set_end(orig, end, endstr);
return ret;
}
2016-08-15 03:49:24 +02:00
done:
if (neg) {
return -ret;
}
return ret;
}
2021-03-19 02:28:30 +01:00
/* strref */
2021-03-23 01:11:21 +01:00
cs_strref::cs_strref(cs_shared_state *cs, std::string_view str):
p_state{cs}
2021-03-19 02:28:30 +01:00
{
2021-03-23 01:11:21 +01:00
p_str = cs->strman->add(str);
2021-03-19 02:28:30 +01:00
}
cs_strref::cs_strref(cs_state &cs, std::string_view str):
2021-03-19 02:28:30 +01:00
p_state{cs.p_state}
{
p_str = p_state->strman->add(str);
}
cs_strref::cs_strref(cs_strref const &ref): p_state{ref.p_state}, p_str{ref.p_str}
{
p_state->strman->ref(p_str);
}
/* this can be used by friends to do quick cs_strref creation */
2021-03-23 01:11:21 +01:00
cs_strref::cs_strref(char const *p, cs_shared_state *cs):
p_state{cs}
2021-03-19 02:28:30 +01:00
{
p_str = p_state->strman->ref(p);
}
cs_strref::~cs_strref() {
p_state->strman->unref(p_str);
}
cs_strref &cs_strref::operator=(cs_strref const &ref) {
p_str = ref.p_str;
p_state = ref.p_state;
p_state->strman->ref(p_str);
return *this;
}
cs_strref::operator std::string_view() const {
2021-03-19 02:28:30 +01:00
return p_state->strman->get(p_str);
}
bool cs_strref::operator==(cs_strref const &s) const {
return p_str == s.p_str;
}
2016-09-21 21:02:13 +02:00
namespace util {
LIBCUBESCRIPT_EXPORT char const *parse_string(
cs_state &cs, std::string_view 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;
if (str.empty() || (str.front() != '\"')) {
return str.data();
2016-10-10 20:14:16 +02:00
}
char const *beg = str.begin();
char const *end = str.end();
char const *orig = beg++;
++nl;
while (beg != end) {
switch (*beg) {
2016-09-21 21:02:13 +02:00
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 = (*beg == '\\');
if (++beg == end) {
goto end;
2016-09-21 21:02:13 +02:00
}
if ((*beg == '\r') || (*beg == '\n')) {
char c = *beg++;
++nl;
if ((beg != end) && (c == '\r') && (*beg == '\n')) {
++beg;
}
} else if (needn) {
goto end;
} else {
++beg;
}
continue;
}
default:
break;
2016-09-21 21:02:13 +02:00
}
++beg;
2016-09-21 21:02:13 +02:00
}
2016-09-26 02:26:02 +02:00
end:
nlines = nl;
if ((beg == end) || (*beg != '\"')) {
2017-02-13 18:10:40 +01:00
throw cs_error(
cs, "unfinished string '%s'",
std::string_view{orig, std::size_t(beg - orig)}
2016-09-26 02:26:02 +02:00
);
}
return ++beg;
2016-09-21 21:02:13 +02:00
}
LIBCUBESCRIPT_EXPORT char const *parse_word(cs_state &cs, std::string_view str) {
char const *it = str.begin();
char const *end = str.end();
for (; it != end; ++it) {
std::string_view chrs{"\"/;()[] \t\r\n"};
it = std::find_first_of(it, end, chrs.begin(), chrs.end());
if (it == end) {
return it;
2016-09-21 21:02:13 +02:00
}
switch (*it) {
2016-09-21 21:02:13 +02:00
case '"':
case ';':
case ' ':
case '\t':
case '\r':
case '\n':
return it;
2016-09-21 21:02:13 +02:00
case '/':
if (((end - it) > 1) && (it[1] == '/')) {
return it;
2016-09-21 21:02:13 +02:00
}
break;
case '[':
++it;
it = parse_word(cs, std::string_view{
it, std::size_t(end - it)
});
if ((it == end) || (*it != ']')) {
2017-02-13 18:10:40 +01:00
throw cs_error(cs, "missing \"]\"");
2016-09-21 21:02:13 +02:00
}
break;
case '(':
++it;
it = parse_word(cs, std::string_view{
it, std::size_t(end - it)
});
if ((it == end) || (*it != ')')) {
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 it;
2016-09-21 21:02:13 +02:00
}
}
return it;
2016-09-21 21:02:13 +02:00
}
2021-03-18 23:53:16 +01:00
} /* namespace util */
2016-09-21 21:02:13 +02:00
2021-03-20 19:34:26 +01:00
LIBCUBESCRIPT_EXPORT bool cs_list_parser::parse() {
skip_until_item();
if (p_input_beg == p_input_end) {
2021-03-18 23:53:16 +01:00
return false;
}
2021-03-20 19:34:26 +01:00
switch (*p_input_beg) {
case '"': {
2021-03-20 19:34:26 +01:00
char const *qi = p_input_beg;
p_input_beg = util::parse_string(*p_state, get_input());
p_quoted_item = std::string_view{qi, p_input_beg};
p_item = p_quoted_item.substr(1, p_quoted_item.size() - 2);
2021-03-18 23:53:16 +01:00
break;
}
2021-03-18 23:53:16 +01:00
case '(':
case '[': {
2021-03-20 19:34:26 +01:00
char btype = *p_input_beg;
2021-03-18 23:53:16 +01:00
int brak = 1;
2021-03-20 19:34:26 +01:00
char const *ibeg = p_input_beg++;
2021-03-18 23:53:16 +01:00
for (;;) {
std::string_view chrs{"\"/;()[]"};
2021-03-20 19:34:26 +01:00
p_input_beg = std::find_first_of(
p_input_beg, p_input_end, chrs.begin(), chrs.end()
2021-03-18 23:53:16 +01:00
);
2021-03-20 19:34:26 +01:00
if (p_input_beg == p_input_end) {
2021-03-18 23:53:16 +01:00
return true;
}
2021-03-20 19:34:26 +01:00
char c = *p_input_beg++;
2021-03-18 23:53:16 +01:00
switch (c) {
case '"':
/* the quote is needed in str parsing */
2021-03-20 19:34:26 +01:00
--p_input_beg;
p_input_beg = util::parse_string(*p_state, get_input());
2021-03-18 23:53:16 +01:00
break;
case '/':
if (
2021-03-20 19:34:26 +01:00
(p_input_beg != p_input_end) &&
(*p_input_beg == '/')
) {
2021-03-20 19:34:26 +01:00
p_input_beg = std::find(
p_input_beg, p_input_end, '\n'
);
2021-03-18 23:53:16 +01:00
}
break;
case '(':
case '[':
brak += (c == btype);
break;
case ')':
if ((btype == '(') && (--brak <= 0)) {
goto endblock;
}
break;
case ']':
if ((btype == '[') && (--brak <= 0)) {
goto endblock;
}
break;
2016-09-21 21:02:13 +02:00
}
}
2021-03-18 23:53:16 +01:00
endblock:
2021-03-20 19:34:26 +01:00
p_item = std::string_view{ibeg + 1, p_input_beg - 1};
p_quoted_item = std::string_view{ibeg, p_input_beg};
2021-03-18 23:53:16 +01:00
break;
2016-09-21 21:02:13 +02:00
}
2021-03-18 23:53:16 +01:00
case ')':
case ']':
2016-09-21 21:02:13 +02:00
return false;
2021-03-18 23:53:16 +01:00
default: {
2021-03-20 19:34:26 +01:00
char const *e = util::parse_word(*p_state, get_input());
p_quoted_item = p_item = std::string_view{p_input_beg, e};
p_input_beg = e;
2021-03-18 23:53:16 +01:00
break;
2016-09-21 21:02:13 +02:00
}
2021-03-18 23:53:16 +01:00
}
2021-03-20 19:34:26 +01:00
skip_until_item();
if ((p_input_beg != p_input_end) && (*p_input_beg == ';')) {
++p_input_beg;
2021-03-18 23:53:16 +01:00
}
return true;
}
2021-03-20 19:34:26 +01:00
LIBCUBESCRIPT_EXPORT std::size_t cs_list_parser::count() {
2021-03-18 23:53:16 +01:00
size_t ret = 0;
2021-03-20 19:34:26 +01:00
while (parse()) {
2021-03-18 23:53:16 +01:00
++ret;
}
return ret;
}
2021-03-20 19:34:26 +01:00
LIBCUBESCRIPT_EXPORT cs_strref cs_list_parser::get_item() const {
if (!p_quoted_item.empty() && (p_quoted_item.front() == '"')) {
cs_charbuf buf{*p_state};
util::unescape_string(std::back_inserter(buf), p_item);
return cs_strref{*p_state, buf.str()};
2021-03-18 23:53:16 +01:00
}
2021-03-20 19:34:26 +01:00
return cs_strref{*p_state, p_item};
2021-03-18 23:53:16 +01:00
}
2021-03-20 19:34:26 +01:00
LIBCUBESCRIPT_EXPORT void cs_list_parser::skip_until_item() {
2021-03-18 23:53:16 +01:00
for (;;) {
2021-03-20 19:34:26 +01:00
while (p_input_beg != p_input_end) {
char c = *p_input_beg;
2021-03-18 23:53:16 +01:00
if ((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n')) {
2021-03-20 19:34:26 +01:00
++p_input_beg;
2021-03-18 23:53:16 +01:00
} else {
2016-09-21 21:02:13 +02:00
break;
}
}
2021-03-20 19:34:26 +01:00
if ((p_input_end - p_input_beg) < 2) {
break;
}
2021-03-20 19:34:26 +01:00
if ((p_input_beg[0] != '/') || (p_input_beg[1]) != '/') {
2021-03-18 23:53:16 +01:00
break;
}
2021-03-20 19:34:26 +01:00
p_input_beg = std::find(p_input_beg, p_input_end, '\n');
}
2021-03-18 23:53:16 +01:00
}
2016-09-21 21:02:13 +02:00
LIBCUBESCRIPT_EXPORT cs_strref value_list_concat(
2021-03-20 05:41:25 +01:00
cs_state &cs, std::span<cs_value> vals, std::string_view sep
) {
2021-03-20 06:52:10 +01:00
cs_charbuf buf{cs};
for (std::size_t i = 0; i < vals.size(); ++i) {
switch (vals[i].get_type()) {
case cs_value_type::INT:
case cs_value_type::FLOAT:
2021-03-20 19:34:26 +01:00
case cs_value_type::STRING:
std::ranges::copy(
cs_value{vals[i]}.force_str(), std::back_inserter(buf)
);
break;
default:
break;
}
if (i == (vals.size() - 1)) {
break;
}
2021-03-20 19:34:26 +01:00
std::ranges::copy(sep, std::back_inserter(buf));
}
2021-03-20 06:52:10 +01:00
return cs_strref{cs, buf.str()};
}
} /* namespace cscript */