2017-06-20 19:21:39 +00:00
|
|
|
#include <cubescript/cubescript.hh>
|
2016-08-15 01:19:59 +00:00
|
|
|
#include "cs_util.hh"
|
2021-03-15 23:44:25 +00:00
|
|
|
#include "cs_vm.hh"
|
2016-08-15 01:19:59 +00:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
namespace cscript {
|
|
|
|
|
2017-02-16 18:07:22 +00:00
|
|
|
static inline void p_skip_white(ostd::string_range &v) {
|
2016-08-15 17:57:31 +00:00
|
|
|
while (!v.empty() && isspace(*v)) {
|
2016-08-15 17:55:22 +00:00
|
|
|
++v;
|
2016-08-15 01:19:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void p_set_end(
|
2017-02-16 18:07:22 +00:00
|
|
|
const ostd::string_range &v, ostd::string_range *end
|
2016-08-15 01:19:59 +00:00
|
|
|
) {
|
|
|
|
if (!end) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*end = v;
|
|
|
|
}
|
|
|
|
|
2016-11-08 22:12:02 +00:00
|
|
|
/* this function assumes the input is definitely a hex digit */
|
2017-02-13 17:10:40 +00:00
|
|
|
static inline cs_int p_hexd_to_int(char c) {
|
2016-11-08 22:12:02 +00:00
|
|
|
if (c >= 97) { /* a-f */
|
2016-08-15 01:44:48 +00:00
|
|
|
return (c - 'a') + 10;
|
2016-11-08 22:12:02 +00:00
|
|
|
} else if (c >= 65) { /* A-F */
|
|
|
|
return (c - 'A') + 10;
|
2016-08-15 01:19:59 +00:00
|
|
|
}
|
2016-11-08 22:12:02 +00:00
|
|
|
/* 0-9 */
|
|
|
|
return c - '0';
|
2016-08-15 01:19:59 +00:00
|
|
|
}
|
|
|
|
|
2017-02-16 18:07:22 +00:00
|
|
|
static inline bool p_check_neg(ostd::string_range &input) {
|
2016-08-15 17:55:22 +00:00
|
|
|
bool neg = (*input == '-');
|
|
|
|
if (neg || (*input == '+')) {
|
|
|
|
++input;
|
2016-08-15 01:49:24 +00:00
|
|
|
}
|
|
|
|
return neg;
|
|
|
|
}
|
|
|
|
|
2017-02-16 18:07:22 +00:00
|
|
|
cs_int cs_parse_int(ostd::string_range input, ostd::string_range *end) {
|
|
|
|
ostd::string_range orig = input;
|
2016-08-15 01:19:59 +00:00
|
|
|
p_skip_white(input);
|
|
|
|
if (input.empty()) {
|
|
|
|
p_set_end(orig, end);
|
2017-02-13 17:10:40 +00:00
|
|
|
return cs_int(0);
|
2016-08-15 01:19:59 +00:00
|
|
|
}
|
2016-08-15 01:49:24 +00:00
|
|
|
bool neg = p_check_neg(input);
|
2017-02-13 17:10:40 +00:00
|
|
|
cs_int ret = 0;
|
2017-02-16 18:07:22 +00:00
|
|
|
ostd::string_range past = input;
|
2016-08-15 01:19:59 +00:00
|
|
|
if (input.size() >= 2) {
|
2017-02-16 18:07:22 +00:00
|
|
|
ostd::string_range pfx = input.slice(0, 2);
|
2016-08-15 01:19:59 +00:00
|
|
|
if ((pfx == "0x") || (pfx == "0X")) {
|
2017-03-31 23:03:22 +00:00
|
|
|
input = input.slice(2, input.size());
|
2016-08-15 01:44:48 +00:00
|
|
|
past = input;
|
2016-08-15 17:55:22 +00:00
|
|
|
while (!past.empty() && isxdigit(*past)) {
|
|
|
|
ret = ret * 16 + p_hexd_to_int(*past);
|
|
|
|
++past;
|
2016-08-15 01:44:48 +00:00
|
|
|
}
|
2016-08-15 01:19:59 +00:00
|
|
|
goto done;
|
|
|
|
} else if ((pfx == "0b") || (pfx == "0B")) {
|
2017-03-31 23:03:22 +00:00
|
|
|
input = input.slice(2, input.size());
|
2016-08-15 01:44:48 +00:00
|
|
|
past = input;
|
2016-08-15 17:55:22 +00:00
|
|
|
while (!past.empty() && ((*past == '0') || (*past == '1'))) {
|
|
|
|
ret = ret * 2 + (*past - '0');
|
|
|
|
++past;
|
2016-08-15 01:44:48 +00:00
|
|
|
}
|
2016-08-15 01:19:59 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
2016-08-15 17:55:22 +00:00
|
|
|
while (!past.empty() && isdigit(*past)) {
|
|
|
|
ret = ret * 10 + (*past - '0');
|
|
|
|
++past;
|
2016-08-15 01:44:48 +00:00
|
|
|
}
|
2016-08-15 01:19:59 +00:00
|
|
|
done:
|
2017-03-31 01:34:09 +00:00
|
|
|
if (&past[0] == &input[0]) {
|
2016-08-15 01:19:59 +00:00
|
|
|
p_set_end(orig, end);
|
|
|
|
} else {
|
|
|
|
p_set_end(past, end);
|
|
|
|
}
|
2016-08-15 01:49:24 +00:00
|
|
|
if (neg) {
|
|
|
|
return -ret;
|
|
|
|
}
|
|
|
|
return ret;
|
2016-08-15 01:19:59 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 16:08:14 +00:00
|
|
|
template<bool Hex, char e1 = Hex ? 'p' : 'e', char e2 = Hex ? 'P' : 'E'>
|
2017-02-16 18:07:22 +00:00
|
|
|
static inline bool p_read_exp(ostd::string_range &input, cs_int &fn) {
|
2016-08-15 01:19:59 +00:00
|
|
|
if (input.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-08-15 17:55:22 +00:00
|
|
|
if ((*input != e1) && (*input != e2)) {
|
2016-08-15 01:19:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-08-15 17:55:22 +00:00
|
|
|
++input;
|
2016-08-15 01:19:59 +00:00
|
|
|
if (input.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-15 01:49:24 +00:00
|
|
|
bool neg = p_check_neg(input);
|
2016-08-15 17:55:22 +00:00
|
|
|
if (input.empty() || !isdigit(*input)) {
|
2016-08-15 01:19:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-13 17:10:40 +00:00
|
|
|
cs_int exp = 0;
|
2016-08-15 17:55:22 +00:00
|
|
|
while (!input.empty() && isdigit(*input)) {
|
|
|
|
exp = exp * 10 + (*input - '0');
|
|
|
|
++input;
|
2016-08-15 01:19:59 +00:00
|
|
|
}
|
|
|
|
if (neg) {
|
|
|
|
exp = -exp;
|
|
|
|
}
|
|
|
|
fn += exp;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-17 16:08:14 +00:00
|
|
|
template<bool Hex>
|
|
|
|
static inline bool parse_gen_float(
|
2017-02-16 18:07:22 +00:00
|
|
|
ostd::string_range input, ostd::string_range *end, cs_float &ret
|
2016-08-15 01:19:59 +00:00
|
|
|
) {
|
2017-02-13 17:10:40 +00:00
|
|
|
auto read_digits = [&input](double r, cs_int &n) {
|
2016-08-17 16:08:14 +00: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');
|
|
|
|
}
|
2016-08-15 01:19:59 +00:00
|
|
|
++n;
|
2016-08-15 17:55:22 +00:00
|
|
|
++input;
|
2016-08-15 01:19:59 +00:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
};
|
2017-02-13 17:10:40 +00:00
|
|
|
cs_int wn = 0, fn = 0;
|
2016-08-17 16:08:14 +00:00
|
|
|
double r = read_digits(0.0, wn);
|
2016-08-15 17:55:22 +00:00
|
|
|
if (!input.empty() && (*input == '.')) {
|
|
|
|
++input;
|
2016-08-17 16:08:14 +00:00
|
|
|
r = read_digits(r, fn);
|
2016-08-15 01:19:59 +00:00
|
|
|
}
|
|
|
|
if (!wn && !fn) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-17 16:10:55 +00:00
|
|
|
fn = -fn;
|
2016-08-15 01:19:59 +00:00
|
|
|
p_set_end(input, end); /* we have a valid number until here */
|
2016-08-17 16:08:14 +00:00
|
|
|
if (p_read_exp<Hex>(input, fn)) {
|
2016-08-15 01:19:59 +00:00
|
|
|
p_set_end(input, end);
|
|
|
|
}
|
2016-08-17 16:08:14 +00:00
|
|
|
if (Hex) {
|
2017-02-13 17:10:40 +00:00
|
|
|
ret = cs_float(ldexp(r, fn * 4));
|
2016-08-17 16:08:14 +00:00
|
|
|
} else {
|
2017-02-13 17:10:40 +00:00
|
|
|
ret = cs_float(r * pow(10, fn));
|
2016-08-15 01:19:59 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-16 18:07:22 +00:00
|
|
|
cs_float cs_parse_float(ostd::string_range input, ostd::string_range *end) {
|
|
|
|
ostd::string_range orig = input;
|
2016-08-15 01:19:59 +00:00
|
|
|
p_skip_white(input);
|
|
|
|
if (input.empty()) {
|
|
|
|
p_set_end(orig, end);
|
2017-02-13 17:10:40 +00:00
|
|
|
return cs_float(0);
|
2016-08-15 01:19:59 +00:00
|
|
|
}
|
2016-08-15 01:49:24 +00:00
|
|
|
bool neg = p_check_neg(input);
|
2017-02-13 17:10:40 +00:00
|
|
|
cs_float ret = cs_float(0);
|
2016-08-15 01:19:59 +00:00
|
|
|
if (input.size() >= 2) {
|
2017-02-16 18:07:22 +00:00
|
|
|
ostd::string_range pfx = input.slice(0, 2);
|
2016-08-15 01:49:24 +00:00
|
|
|
if ((pfx == "0x") || (pfx == "0X")) {
|
2017-03-31 23:03:22 +00:00
|
|
|
input = input.slice(2, input.size());
|
2016-08-17 16:08:14 +00:00
|
|
|
if (!parse_gen_float<true>(input, end, ret)) {
|
2016-08-15 01:19:59 +00:00
|
|
|
p_set_end(orig, end);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-08-15 01:49:24 +00:00
|
|
|
goto done;
|
2016-08-15 01:19:59 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-17 16:08:14 +00:00
|
|
|
if (!parse_gen_float<false>(input, end, ret)) {
|
2016-08-15 01:19:59 +00:00
|
|
|
p_set_end(orig, end);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-08-15 01:49:24 +00:00
|
|
|
done:
|
2016-08-15 01:19:59 +00:00
|
|
|
if (neg) {
|
|
|
|
return -ret;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-15 23:44:25 +00:00
|
|
|
/* string manager */
|
|
|
|
|
|
|
|
inline cs_strref_state *get_ref_state(char const *ptr) {
|
|
|
|
return const_cast<cs_strref_state *>(
|
|
|
|
reinterpret_cast<cs_strref_state const *>(ptr)
|
|
|
|
) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char const *cs_strman::add(ostd::string_range str) {
|
2021-03-17 01:47:34 +00:00
|
|
|
auto it = counts.find(str);
|
2021-03-15 23:44:25 +00:00
|
|
|
/* already present: just increment ref */
|
2021-03-17 01:47:34 +00:00
|
|
|
if (it != counts.end()) {
|
|
|
|
auto *st = it->second;
|
2021-03-15 23:44:25 +00:00
|
|
|
/* having a null pointer is the same as non-existence */
|
|
|
|
if (st) {
|
|
|
|
++st->refcount;
|
|
|
|
return reinterpret_cast<char const *>(st + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* not present: allocate brand new data */
|
|
|
|
auto ss = str.size();
|
2021-03-19 00:00:11 +00:00
|
|
|
auto strp = alloc_buf(ss);
|
|
|
|
/* write string data, it's already pre-terminated */
|
2021-03-15 23:44:25 +00:00
|
|
|
memcpy(strp, str.data(), ss);
|
2021-03-17 01:47:34 +00:00
|
|
|
/* store it */
|
2021-03-19 00:00:11 +00:00
|
|
|
counts.emplace(ostd::string_range{strp, strp + ss}, get_ref_state(strp));
|
2021-03-15 23:44:25 +00:00
|
|
|
return strp;
|
|
|
|
}
|
|
|
|
|
|
|
|
char const *cs_strman::ref(char const *ptr) {
|
|
|
|
auto *ss = get_ref_state(ptr);
|
|
|
|
++ss->refcount;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2021-03-19 00:00:11 +00:00
|
|
|
char const *cs_strman::steal(char *ptr) {
|
|
|
|
auto *ss = get_ref_state(ptr);
|
|
|
|
auto sr = ostd::string_range{ptr, ptr + ss->length};
|
|
|
|
/* much like add(), but we already have memory */
|
|
|
|
auto it = counts.find(sr);
|
|
|
|
if (it != counts.end()) {
|
|
|
|
auto *st = it->second;
|
|
|
|
if (st) {
|
|
|
|
++st->refcount;
|
|
|
|
/* the buffer is superfluous now */
|
|
|
|
cstate->alloc(ss, ss->length + sizeof(cs_strref_state) + 1, 0);
|
|
|
|
return reinterpret_cast<char const *>(st + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ss->refcount = 1;
|
|
|
|
counts.emplace(sr, ss);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2021-03-15 23:44:25 +00:00
|
|
|
void cs_strman::unref(char const *ptr) {
|
|
|
|
auto *ss = get_ref_state(ptr);
|
|
|
|
if (!--ss->refcount) {
|
|
|
|
/* refcount zero, so ditch it
|
|
|
|
* this path is a little slow...
|
|
|
|
*/
|
|
|
|
auto sr = ostd::string_range{ptr, ptr + ss->length};
|
|
|
|
auto it = counts.find(sr);
|
|
|
|
if (it == counts.end()) {
|
|
|
|
/* internal error: this should *never* happen */
|
2021-03-17 01:47:34 +00:00
|
|
|
throw cs_internal_error{"no refcount"};
|
2021-03-15 23:44:25 +00:00
|
|
|
}
|
2021-03-17 01:47:34 +00:00
|
|
|
/* we're freeing the key */
|
|
|
|
counts.erase(it);
|
2021-03-15 23:44:25 +00:00
|
|
|
/* dealloc */
|
|
|
|
cstate->alloc(ss, ss->length + sizeof(cs_strref_state) + 1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char const *cs_strman::find(ostd::string_range str) const {
|
|
|
|
auto it = counts.find(str);
|
|
|
|
if (it == counts.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return reinterpret_cast<char const *>(it->second + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ostd::string_range cs_strman::get(char const *ptr) const {
|
|
|
|
auto *ss = get_ref_state(ptr);
|
|
|
|
return ostd::string_range{ptr, ptr + ss->length};
|
|
|
|
}
|
|
|
|
|
2021-03-19 00:00:11 +00:00
|
|
|
char *cs_strman::alloc_buf(std::size_t len) const {
|
|
|
|
auto mem = cstate->alloc(nullptr, 0, len + sizeof(cs_strref_state) + 1);
|
|
|
|
if (!mem) {
|
|
|
|
throw cs_internal_error{"allocation failed"};
|
|
|
|
}
|
|
|
|
/* write length and initial refcount */
|
|
|
|
auto *sst = static_cast<cs_strref_state *>(mem);
|
|
|
|
sst->length = len;
|
|
|
|
sst->refcount = 1;
|
|
|
|
/* pre-terminate */
|
|
|
|
auto *strp = reinterpret_cast<char *>(sst + 1);
|
|
|
|
strp[len] = '\0';
|
|
|
|
/* now the user can fill it */
|
|
|
|
return strp;
|
|
|
|
};
|
|
|
|
|
2021-03-19 01:28:30 +00:00
|
|
|
/* strref */
|
|
|
|
|
|
|
|
cs_strref::cs_strref(cs_shared_state &cs, ostd::string_range str):
|
|
|
|
p_state{&cs}
|
|
|
|
{
|
|
|
|
p_str = cs.strman->add(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
cs_strref::cs_strref(cs_state &cs, ostd::string_range str):
|
|
|
|
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 */
|
|
|
|
cs_strref::cs_strref(char const *p, cs_shared_state &cs):
|
|
|
|
p_state{&cs}
|
|
|
|
{
|
|
|
|
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 ostd::string_range() const {
|
|
|
|
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 19:02:13 +00:00
|
|
|
namespace util {
|
2017-02-16 18:07:22 +00:00
|
|
|
OSTD_EXPORT ostd::string_range parse_string(
|
|
|
|
cs_state &cs, ostd::string_range str, size_t &nlines
|
2016-09-26 00:26:02 +00:00
|
|
|
) {
|
2017-01-25 01:09:50 +00:00
|
|
|
size_t nl = 0;
|
2016-10-11 19:15:23 +00:00
|
|
|
nlines = nl;
|
2016-10-10 18:14:16 +00:00
|
|
|
if (str.empty() || (*str != '\"')) {
|
|
|
|
return str;
|
|
|
|
}
|
2017-02-16 18:07:22 +00:00
|
|
|
ostd::string_range orig = str;
|
2016-09-26 00:26:02 +00:00
|
|
|
++str;
|
2016-10-11 19:15:23 +00:00
|
|
|
++nl;
|
2016-09-21 19:02:13 +00:00
|
|
|
while (!str.empty()) {
|
|
|
|
switch (*str) {
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
case '\"':
|
2016-09-26 00:26:02 +00:00
|
|
|
goto end;
|
2016-09-21 19:02:13 +00:00
|
|
|
case '^':
|
2016-10-11 19:15:23 +00:00
|
|
|
case '\\': {
|
|
|
|
bool needn = (*str == '\\');
|
2016-09-21 19:02:13 +00:00
|
|
|
++str;
|
2016-10-11 19:15:23 +00:00
|
|
|
if (str.empty()) {
|
|
|
|
goto end;
|
2016-09-21 19:02:13 +00:00
|
|
|
}
|
2016-10-11 19:15:23 +00:00
|
|
|
if ((*str == '\r') || (*str == '\n')) {
|
2016-09-24 01:31:41 +00:00
|
|
|
char c = *str;
|
|
|
|
++str;
|
2016-10-11 19:15:23 +00:00
|
|
|
++nl;
|
2016-09-24 01:31:41 +00:00
|
|
|
if (!str.empty() && (c == '\r') && (*str == '\n')) {
|
|
|
|
++str;
|
|
|
|
}
|
2016-10-11 19:15:23 +00:00
|
|
|
} else if (needn) {
|
|
|
|
goto end;
|
|
|
|
} else {
|
|
|
|
++str;
|
2016-09-24 01:31:41 +00:00
|
|
|
}
|
|
|
|
continue;
|
2016-10-11 19:15:23 +00:00
|
|
|
}
|
2016-09-21 19:02:13 +00:00
|
|
|
}
|
|
|
|
++str;
|
|
|
|
}
|
2016-09-26 00:26:02 +00:00
|
|
|
end:
|
2016-10-11 19:15:23 +00:00
|
|
|
nlines = nl;
|
2016-09-26 00:26:02 +00:00
|
|
|
if (str.empty() || (*str != '\"')) {
|
2017-02-13 17:10:40 +00:00
|
|
|
throw cs_error(
|
2017-06-15 18:44:09 +00:00
|
|
|
cs, "unfinished string '%s'", orig.slice(0, &str[0] - &orig[0])
|
2016-09-26 00:26:02 +00:00
|
|
|
);
|
|
|
|
}
|
2017-03-31 23:03:22 +00:00
|
|
|
str.pop_front();
|
|
|
|
return str;
|
2016-09-21 19:02:13 +00:00
|
|
|
}
|
|
|
|
|
2017-02-16 18:07:22 +00:00
|
|
|
OSTD_EXPORT ostd::string_range parse_word(
|
|
|
|
cs_state &cs, ostd::string_range str
|
2016-09-29 18:57:47 +00:00
|
|
|
) {
|
2016-09-21 19:02:13 +00:00
|
|
|
for (;;) {
|
2017-02-16 18:07:22 +00:00
|
|
|
str = ostd::find_one_of(str, ostd::string_range("\"/;()[] \t\r\n"));
|
2016-09-21 19:02:13 +00:00
|
|
|
if (str.empty()) {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
switch (*str) {
|
|
|
|
case '"':
|
|
|
|
case ';':
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
return str;
|
|
|
|
case '/':
|
2016-09-21 23:07:43 +00:00
|
|
|
if ((str.size() > 1) && (str[1] == '/')) {
|
2016-09-21 19:02:13 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '[':
|
2017-03-31 23:03:22 +00:00
|
|
|
str.pop_front();
|
|
|
|
str = parse_word(cs, str);
|
2016-09-21 19:02:13 +00:00
|
|
|
if (str.empty() || (*str != ']')) {
|
2017-02-13 17:10:40 +00:00
|
|
|
throw cs_error(cs, "missing \"]\"");
|
2016-09-21 19:02:13 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '(':
|
2017-03-31 23:03:22 +00:00
|
|
|
str.pop_front();
|
|
|
|
str = parse_word(cs, str);
|
2016-09-21 19:02:13 +00:00
|
|
|
if (str.empty() || (*str != ')')) {
|
2017-02-13 17:10:40 +00:00
|
|
|
throw cs_error(cs, "missing \")\"");
|
2016-09-21 19:02:13 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ']':
|
|
|
|
case ')':
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
++str;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
2021-03-18 22:53:16 +00:00
|
|
|
} /* namespace util */
|
2016-09-21 19:02:13 +00:00
|
|
|
|
2021-03-18 22:53:16 +00:00
|
|
|
OSTD_EXPORT bool list_parse(cs_list_parse_state &ps, cs_state &cs) {
|
|
|
|
list_find_item(ps);
|
|
|
|
if (ps.input.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch (*ps.input) {
|
|
|
|
case '"':
|
|
|
|
ps.quoted_item = ps.input;
|
|
|
|
ps.input = util::parse_string(cs, ps.input);
|
|
|
|
ps.quoted_item = ps.quoted_item.slice(
|
|
|
|
0, &ps.input[0] - &ps.quoted_item[0]
|
|
|
|
);
|
|
|
|
ps.item = ps.quoted_item.slice(1, ps.quoted_item.size() - 1);
|
|
|
|
break;
|
|
|
|
case '(':
|
|
|
|
case '[': {
|
|
|
|
ps.quoted_item = ps.input;
|
|
|
|
++ps.input;
|
|
|
|
ps.item = ps.input;
|
|
|
|
char btype = *ps.quoted_item;
|
|
|
|
int brak = 1;
|
|
|
|
for (;;) {
|
|
|
|
ps.input = ostd::find_one_of(
|
|
|
|
ps.input, ostd::string_range("\"/;()[]")
|
|
|
|
);
|
|
|
|
if (ps.input.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
char c = *ps.input;
|
|
|
|
++ps.input;
|
|
|
|
switch (c) {
|
|
|
|
case '"':
|
|
|
|
ps.input = util::parse_string(cs, ps.input);
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
if (!ps.input.empty() && (*ps.input == '/')) {
|
|
|
|
ps.input = ostd::find(ps.input, '\n');
|
|
|
|
}
|
|
|
|
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 19:02:13 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-18 22:53:16 +00:00
|
|
|
endblock:
|
|
|
|
ps.item = ps.item.slice(0, &ps.input[0] - &ps.item[0]);
|
|
|
|
ps.item.pop_back();
|
|
|
|
ps.quoted_item = ps.quoted_item.slice(
|
|
|
|
0, &ps.input[0] - &ps.quoted_item[0]
|
|
|
|
);
|
|
|
|
break;
|
2016-09-21 19:02:13 +00:00
|
|
|
}
|
2021-03-18 22:53:16 +00:00
|
|
|
case ')':
|
|
|
|
case ']':
|
2016-09-21 19:02:13 +00:00
|
|
|
return false;
|
2021-03-18 22:53:16 +00:00
|
|
|
default: {
|
|
|
|
ostd::string_range e = util::parse_word(cs, ps.input);
|
|
|
|
ps.quoted_item = ps.item = ps.input.slice(0, &e[0] - &ps.input[0]);
|
|
|
|
ps.input = e;
|
|
|
|
break;
|
2016-09-21 19:02:13 +00:00
|
|
|
}
|
2021-03-18 22:53:16 +00:00
|
|
|
}
|
|
|
|
list_find_item(ps);
|
|
|
|
if (!ps.input.empty() && (*ps.input == ';')) {
|
|
|
|
++ps.input;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
OSTD_EXPORT std::size_t list_count(cs_list_parse_state &ps, cs_state &cs) {
|
|
|
|
size_t ret = 0;
|
|
|
|
while (list_parse(ps, cs)) {
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
OSTD_EXPORT cs_strref list_get_item(cs_list_parse_state &ps, cs_state &cs) {
|
|
|
|
if (!ps.quoted_item.empty() && (*ps.quoted_item == '"')) {
|
|
|
|
auto app = ostd::appender<cs_string>();
|
|
|
|
util::unescape_string(app, ps.item);
|
|
|
|
return cs_strref{cs, app.get()};
|
|
|
|
}
|
|
|
|
return cs_strref{cs, ps.item};
|
|
|
|
}
|
|
|
|
|
|
|
|
OSTD_EXPORT void list_find_item(cs_list_parse_state &ps) {
|
|
|
|
for (;;) {
|
|
|
|
while (!ps.input.empty()) {
|
|
|
|
char c = *ps.input;
|
|
|
|
if ((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n')) {
|
|
|
|
++ps.input;
|
|
|
|
} else {
|
2016-09-21 19:02:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 22:53:16 +00:00
|
|
|
if ((ps.input.size() < 2) || (ps.input[0] != '/') || (ps.input[1] != '/')) {
|
|
|
|
break;
|
2016-10-24 00:33:04 +00:00
|
|
|
}
|
2021-03-18 22:53:16 +00:00
|
|
|
ps.input = ostd::find(ps.input, '\n');
|
2016-10-24 00:33:04 +00:00
|
|
|
}
|
2021-03-18 22:53:16 +00:00
|
|
|
}
|
2016-09-21 19:02:13 +00:00
|
|
|
|
2021-03-18 23:30:54 +00:00
|
|
|
OSTD_EXPORT cs_strref value_list_concat(
|
|
|
|
cs_state &cs, cs_value_r vals, ostd::string_range sep
|
|
|
|
) {
|
|
|
|
auto app = ostd::appender<cs_string>();
|
|
|
|
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:
|
|
|
|
case cs_value_type::STRING: {
|
|
|
|
cs_value v{vals[i]};
|
|
|
|
ostd::range_put_all(app, cs_value{vals[i]}.force_str());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == (vals.size() - 1)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ostd::range_put_all(app, sep);
|
|
|
|
}
|
|
|
|
return cs_strref{cs, ostd::iter(app.get())};
|
|
|
|
}
|
|
|
|
|
2016-08-15 01:19:59 +00:00
|
|
|
} /* namespace cscript */
|