2016-09-21 19:02:13 +00:00
|
|
|
#include "cubescript/cubescript.hh"
|
2016-08-15 01:19:59 +00:00
|
|
|
#include "cs_util.hh"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
namespace cscript {
|
|
|
|
|
|
|
|
static inline void p_skip_white(ostd::ConstCharRange &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(
|
|
|
|
const ostd::ConstCharRange &v, ostd::ConstCharRange *end
|
|
|
|
) {
|
|
|
|
if (!end) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*end = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline CsInt p_hexd_to_int(char c) {
|
|
|
|
if ((c >= 48) && (c <= 57)) { /* 0-9 */
|
|
|
|
return c - '0';
|
|
|
|
}
|
|
|
|
if ((c >= 65) && (c <= 70)) { /* A-F */
|
2016-08-15 01:44:48 +00:00
|
|
|
return (c - 'A') + 10;
|
2016-08-15 01:19:59 +00:00
|
|
|
}
|
|
|
|
if ((c >= 97) && (c <= 102)) { /* a-f */
|
2016-08-15 01:44:48 +00:00
|
|
|
return (c - 'a') + 10;
|
2016-08-15 01:19:59 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-15 01:49:24 +00:00
|
|
|
static inline bool p_check_neg(ostd::ConstCharRange &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;
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:55:22 +00:00
|
|
|
CsInt cs_parse_int(ostd::ConstCharRange input, ostd::ConstCharRange *end) {
|
2016-08-15 01:19:59 +00:00
|
|
|
ostd::ConstCharRange orig = input;
|
|
|
|
p_skip_white(input);
|
|
|
|
if (input.empty()) {
|
|
|
|
p_set_end(orig, end);
|
|
|
|
return CsInt(0);
|
|
|
|
}
|
2016-08-15 01:49:24 +00:00
|
|
|
bool neg = p_check_neg(input);
|
2016-08-15 01:19:59 +00:00
|
|
|
CsInt ret = 0;
|
|
|
|
ostd::ConstCharRange past = input;
|
|
|
|
if (input.size() >= 2) {
|
|
|
|
ostd::ConstCharRange pfx = input.slice(0, 2);
|
|
|
|
if ((pfx == "0x") || (pfx == "0X")) {
|
2016-08-15 17:55:22 +00:00
|
|
|
input += 2;
|
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")) {
|
2016-08-15 17:55:22 +00:00
|
|
|
input += 2;
|
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:
|
|
|
|
if (past.equals_front(input)) {
|
|
|
|
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'>
|
2016-08-15 01:19:59 +00:00
|
|
|
static inline bool p_read_exp(ostd::ConstCharRange &input, CsInt &fn) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
CsInt 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(
|
2016-08-15 01:19:59 +00:00
|
|
|
ostd::ConstCharRange input, ostd::ConstCharRange *end, CsFloat &ret
|
|
|
|
) {
|
2016-08-17 16:08:14 +00:00
|
|
|
auto read_digits = [&input](double r, CsInt &n) {
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
CsInt 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) {
|
2016-08-17 16:10:55 +00:00
|
|
|
ret = CsFloat(ldexp(r, fn * 4));
|
2016-08-17 16:08:14 +00:00
|
|
|
} else {
|
|
|
|
ret = CsFloat(r * pow(10, fn));
|
2016-08-15 01:19:59 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:55:22 +00:00
|
|
|
CsFloat cs_parse_float(ostd::ConstCharRange input, ostd::ConstCharRange *end) {
|
2016-08-15 01:19:59 +00:00
|
|
|
ostd::ConstCharRange orig = input;
|
|
|
|
p_skip_white(input);
|
|
|
|
if (input.empty()) {
|
|
|
|
p_set_end(orig, end);
|
|
|
|
return CsFloat(0);
|
|
|
|
}
|
2016-08-15 01:49:24 +00:00
|
|
|
bool neg = p_check_neg(input);
|
2016-08-15 01:19:59 +00:00
|
|
|
CsFloat ret = CsFloat(0);
|
|
|
|
if (input.size() >= 2) {
|
|
|
|
ostd::ConstCharRange pfx = input.slice(0, 2);
|
2016-08-15 01:49:24 +00:00
|
|
|
if ((pfx == "0x") || (pfx == "0X")) {
|
2016-08-15 17:55:22 +00:00
|
|
|
input += 2;
|
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;
|
|
|
|
}
|
|
|
|
|
2016-09-21 19:02:13 +00:00
|
|
|
namespace util {
|
2016-10-10 18:14:16 +00:00
|
|
|
OSTD_EXPORT ostd::ConstCharRange parse_string(
|
2016-10-11 19:15:23 +00:00
|
|
|
CsState &cs, ostd::ConstCharRange str, ostd::Size &nlines
|
2016-09-26 00:26:02 +00:00
|
|
|
) {
|
2016-10-11 19:15:23 +00:00
|
|
|
ostd::Size nl = 0;
|
|
|
|
nlines = nl;
|
2016-10-10 18:14:16 +00:00
|
|
|
if (str.empty() || (*str != '\"')) {
|
|
|
|
return str;
|
|
|
|
}
|
2016-09-26 00:26:02 +00:00
|
|
|
ostd::ConstCharRange orig = str;
|
|
|
|
++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 != '\"')) {
|
|
|
|
throw CsErrorException(
|
|
|
|
cs, "unfinished string '%s'", ostd::slice_until(orig, str)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return str + 1;
|
2016-09-21 19:02:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 18:57:47 +00:00
|
|
|
OSTD_EXPORT ostd::ConstCharRange parse_word(
|
|
|
|
CsState &cs, ostd::ConstCharRange str
|
|
|
|
) {
|
2016-09-21 19:02:13 +00:00
|
|
|
for (;;) {
|
|
|
|
str = ostd::find_one_of(str, ostd::ConstCharRange("\"/;()[] \t\r\n"));
|
|
|
|
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 '[':
|
2016-09-29 18:57:47 +00:00
|
|
|
str = parse_word(cs, str + 1);
|
2016-09-21 19:02:13 +00:00
|
|
|
if (str.empty() || (*str != ']')) {
|
2016-09-29 18:57:47 +00:00
|
|
|
throw CsErrorException(cs, "missing \"]\"");
|
2016-09-21 19:02:13 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '(':
|
2016-09-29 18:57:47 +00:00
|
|
|
str = parse_word(cs, str + 1);
|
2016-09-21 19:02:13 +00:00
|
|
|
if (str.empty() || (*str != ')')) {
|
2016-09-29 18:57:47 +00:00
|
|
|
throw CsErrorException(cs, "missing \")\"");
|
2016-09-21 19:02:13 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ']':
|
|
|
|
case ')':
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
++str;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListParser::skip() {
|
|
|
|
for (;;) {
|
|
|
|
while (!input.empty()) {
|
|
|
|
char c = *input;
|
|
|
|
if ((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n')) {
|
|
|
|
++input;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((input.size() < 2) || (input[0] != '/') || (input[1] != '/')) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
input = ostd::find(input, '\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ListParser::parse() {
|
|
|
|
skip();
|
|
|
|
if (input.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch (*input) {
|
|
|
|
case '"':
|
|
|
|
quote = input;
|
2016-10-10 18:14:16 +00:00
|
|
|
input = parse_string(p_state, input);
|
2016-09-21 19:02:13 +00:00
|
|
|
quote = ostd::slice_until(quote, input);
|
2016-09-26 00:26:02 +00:00
|
|
|
item = quote.slice(1, quote.size() - 1);
|
2016-09-21 19:02:13 +00:00
|
|
|
break;
|
|
|
|
case '(':
|
|
|
|
case '[': {
|
|
|
|
quote = input;
|
|
|
|
++input;
|
|
|
|
item = input;
|
|
|
|
char btype = *quote;
|
|
|
|
int brak = 1;
|
|
|
|
for (;;) {
|
|
|
|
input = ostd::find_one_of(
|
|
|
|
input, ostd::ConstCharRange("\"/;()[]")
|
|
|
|
);
|
|
|
|
if (input.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
char c = *input;
|
|
|
|
++input;
|
|
|
|
switch (c) {
|
|
|
|
case '"':
|
2016-10-10 18:14:16 +00:00
|
|
|
input = parse_string(p_state, input);
|
2016-09-21 19:02:13 +00:00
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
if (!input.empty() && (*input == '/')) {
|
|
|
|
input = ostd::find(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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endblock:
|
|
|
|
item = ostd::slice_until(item, input);
|
|
|
|
item.pop_back();
|
|
|
|
quote = ostd::slice_until(quote, input);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ')':
|
|
|
|
case ']':
|
|
|
|
return false;
|
|
|
|
default: {
|
2016-09-29 18:57:47 +00:00
|
|
|
ostd::ConstCharRange e = parse_word(p_state, input);
|
2016-09-21 19:02:13 +00:00
|
|
|
quote = item = ostd::slice_until(input, e);
|
|
|
|
input = e;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
skip();
|
|
|
|
if (!input.empty() && (*input == ';')) {
|
|
|
|
++input;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CsString ListParser::element() {
|
|
|
|
CsString s;
|
|
|
|
s.reserve(item.size());
|
|
|
|
if (!quote.empty() && (*quote == '"')) {
|
|
|
|
auto writer = s.iter_cap();
|
|
|
|
util::unescape_string(writer, item);
|
|
|
|
writer.put('\0');
|
|
|
|
} else {
|
|
|
|
memcpy(s.data(), item.data(), item.size());
|
|
|
|
s[item.size()] = '\0';
|
|
|
|
}
|
|
|
|
s.advance(item.size());
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2016-09-25 13:26:38 +00:00
|
|
|
ostd::Size list_length(CsState &cs, ostd::ConstCharRange s) {
|
|
|
|
ListParser p(cs, s);
|
2016-09-21 19:02:13 +00:00
|
|
|
ostd::Size ret = 0;
|
|
|
|
while (p.parse()) {
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ostd::Maybe<CsString> list_index(
|
2016-09-25 13:26:38 +00:00
|
|
|
CsState &cs, ostd::ConstCharRange s, ostd::Size idx
|
2016-09-21 19:02:13 +00:00
|
|
|
) {
|
2016-09-25 13:26:38 +00:00
|
|
|
ListParser p(cs, s);
|
2016-09-21 19:02:13 +00:00
|
|
|
for (ostd::Size i = 0; i < idx; ++i) {
|
|
|
|
if (!p.parse()) {
|
|
|
|
return ostd::nothing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!p.parse()) {
|
|
|
|
return ostd::nothing;
|
|
|
|
}
|
|
|
|
return ostd::move(p.element());
|
|
|
|
}
|
|
|
|
|
|
|
|
CsVector<CsString> list_explode(
|
2016-09-25 13:26:38 +00:00
|
|
|
CsState &cs, ostd::ConstCharRange s, ostd::Size limit
|
2016-09-21 19:02:13 +00:00
|
|
|
) {
|
|
|
|
CsVector<CsString> ret;
|
2016-09-25 13:26:38 +00:00
|
|
|
ListParser p(cs, s);
|
2016-09-21 19:02:13 +00:00
|
|
|
while ((ret.size() < limit) && p.parse()) {
|
|
|
|
ret.push(ostd::move(p.element()));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} /* namespace util */
|
|
|
|
|
2016-08-15 01:19:59 +00:00
|
|
|
} /* namespace cscript */
|