2017-06-20 19:21:39 +00:00
|
|
|
#include <cubescript/cubescript.hh>
|
2016-08-15 01:19:59 +00:00
|
|
|
|
2021-03-23 21:17:25 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <cctype>
|
2021-04-08 21:33:00 +00:00
|
|
|
#include <limits>
|
2021-03-30 23:10:58 +00:00
|
|
|
#include <iterator>
|
2021-03-23 21:17:25 +00:00
|
|
|
|
2021-04-08 21:33:00 +00:00
|
|
|
#include "cs_parser.hh"
|
2016-08-15 01:19:59 +00:00
|
|
|
|
2021-03-23 22:32:25 +00:00
|
|
|
namespace cubescript {
|
2016-08-15 01:19:59 +00:00
|
|
|
|
2021-03-23 01:00:11 +00:00
|
|
|
/* string/word parsers are also useful to have public */
|
|
|
|
|
2021-03-23 22:29:32 +00:00
|
|
|
LIBCUBESCRIPT_EXPORT char const *parse_string(
|
|
|
|
state &cs, std::string_view str, size_t &nlines
|
2021-03-23 01:00:11 +00:00
|
|
|
) {
|
|
|
|
size_t nl = 0;
|
|
|
|
nlines = nl;
|
|
|
|
if (str.empty() || (str.front() != '\"')) {
|
|
|
|
return str.data();
|
|
|
|
}
|
2021-03-30 23:10:58 +00:00
|
|
|
char const *beg = &str[0];
|
|
|
|
char const *end = &str[str.size()];
|
2021-03-23 01:00:11 +00:00
|
|
|
char const *orig = beg++;
|
|
|
|
++nl;
|
|
|
|
while (beg != end) {
|
|
|
|
switch (*beg) {
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
case '\"':
|
|
|
|
goto end;
|
|
|
|
case '^':
|
|
|
|
case '\\': {
|
|
|
|
bool needn = (*beg == '\\');
|
|
|
|
if (++beg == end) {
|
2016-09-26 00:26:02 +00:00
|
|
|
goto end;
|
2021-03-23 01:00:11 +00:00
|
|
|
}
|
|
|
|
if ((*beg == '\r') || (*beg == '\n')) {
|
|
|
|
char c = *beg++;
|
|
|
|
++nl;
|
|
|
|
if ((beg != end) && (c == '\r') && (*beg == '\n')) {
|
2021-03-20 01:26:37 +00:00
|
|
|
++beg;
|
2016-09-24 01:31:41 +00:00
|
|
|
}
|
2021-03-23 01:00:11 +00:00
|
|
|
} else if (needn) {
|
|
|
|
goto end;
|
|
|
|
} else {
|
|
|
|
++beg;
|
2016-10-11 19:15:23 +00:00
|
|
|
}
|
2021-03-23 01:00:11 +00:00
|
|
|
continue;
|
2016-09-21 19:02:13 +00:00
|
|
|
}
|
2021-03-23 01:00:11 +00:00
|
|
|
default:
|
|
|
|
break;
|
2016-09-21 19:02:13 +00:00
|
|
|
}
|
2021-03-23 01:00:11 +00:00
|
|
|
++beg;
|
|
|
|
}
|
2016-09-26 00:26:02 +00:00
|
|
|
end:
|
2021-03-23 01:00:11 +00:00
|
|
|
nlines = nl;
|
|
|
|
if ((beg == end) || (*beg != '\"')) {
|
2021-03-23 22:29:32 +00:00
|
|
|
throw error{
|
2021-04-12 17:53:24 +00:00
|
|
|
cs, "unfinished string '%.*s'", int(beg - orig), orig
|
2021-03-23 21:17:25 +00:00
|
|
|
};
|
2016-09-21 19:02:13 +00:00
|
|
|
}
|
2021-03-23 01:00:11 +00:00
|
|
|
return ++beg;
|
|
|
|
}
|
2016-09-21 19:02:13 +00:00
|
|
|
|
2021-03-23 22:29:32 +00:00
|
|
|
LIBCUBESCRIPT_EXPORT char const *parse_word(
|
|
|
|
state &cs, std::string_view str
|
2021-03-23 01:00:11 +00:00
|
|
|
) {
|
2021-03-30 23:10:58 +00:00
|
|
|
char const *it = &str[0];
|
|
|
|
char const *end = &str[str.size()];
|
2021-03-23 01:00:11 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
switch (*it) {
|
|
|
|
case '"':
|
|
|
|
case ';':
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
2021-03-20 01:26:37 +00:00
|
|
|
return it;
|
2021-03-23 01:00:11 +00:00
|
|
|
case '/':
|
|
|
|
if (((end - it) > 1) && (it[1] == '/')) {
|
2021-03-20 01:26:37 +00:00
|
|
|
return it;
|
2021-03-23 01:00:11 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
++it;
|
2021-04-12 17:53:24 +00:00
|
|
|
it = parse_word(cs, make_str_view(it, end));
|
2021-03-23 01:00:11 +00:00
|
|
|
if ((it == end) || (*it != ']')) {
|
2021-03-23 22:29:32 +00:00
|
|
|
throw error{cs, "missing \"]\""};
|
2021-03-23 01:00:11 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '(':
|
|
|
|
++it;
|
2021-04-12 17:53:24 +00:00
|
|
|
it = parse_word(cs, make_str_view(it, end));
|
2021-03-23 01:00:11 +00:00
|
|
|
if ((it == end) || (*it != ')')) {
|
2021-03-23 22:29:32 +00:00
|
|
|
throw error{cs, "missing \")\""};
|
2021-03-23 01:00:11 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ']':
|
|
|
|
case ')':
|
|
|
|
return it;
|
2016-09-21 19:02:13 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-23 01:00:11 +00:00
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
2021-03-23 21:17:25 +00:00
|
|
|
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;
|
|
|
|
}
|
2021-04-12 17:53:24 +00:00
|
|
|
*end = make_str_view(nbeg, nend);
|
2021-03-23 21:17:25 +00:00
|
|
|
}
|
|
|
|
/* this function assumes the input is definitely a hex digit */
|
2021-03-23 22:29:32 +00:00
|
|
|
static inline integer_type p_hexd_to_int(char c) {
|
2021-03-23 21:17:25 +00:00
|
|
|
if (c >= 97) { /* a-f */
|
|
|
|
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) {
|
|
|
|
bool neg = (*input == '-');
|
|
|
|
if (neg || (*input == '+')) {
|
|
|
|
++input;
|
|
|
|
}
|
|
|
|
return neg;
|
|
|
|
}
|
|
|
|
|
2021-03-23 22:29:32 +00:00
|
|
|
integer_type parse_int(std::string_view input, std::string_view *endstr) {
|
2021-03-30 23:10:58 +00:00
|
|
|
char const *beg = &input[0];
|
|
|
|
char const *end = &input[input.size()];
|
2021-03-23 21:17:25 +00:00
|
|
|
char const *orig = beg;
|
|
|
|
beg = p_skip_white(beg, end);
|
|
|
|
if (beg == end) {
|
|
|
|
p_set_end(orig, end, endstr);
|
2021-03-23 22:29:32 +00:00
|
|
|
return integer_type(0);
|
2021-03-23 21:17:25 +00:00
|
|
|
}
|
|
|
|
bool neg = p_check_neg(beg);
|
2021-03-23 22:29:32 +00:00
|
|
|
integer_type ret = 0;
|
2021-03-23 21:17:25 +00:00
|
|
|
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) && std::isxdigit(*past)) {
|
|
|
|
ret = ret * 16 + p_hexd_to_int(*past++);
|
|
|
|
}
|
|
|
|
goto done;
|
|
|
|
} else if ((pfx == "0b") || (pfx == "0B")) {
|
|
|
|
beg += 2;
|
|
|
|
past = beg;
|
|
|
|
while ((past != end) && ((*past == '0') || (*past == '1'))) {
|
|
|
|
ret = ret * 2 + (*past++ - '0');
|
|
|
|
}
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while ((past != end) && std::isdigit(*past)) {
|
|
|
|
ret = ret * 10 + (*past++ - '0');
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
p_set_end((past == beg) ? orig : past, end, endstr);
|
|
|
|
if (neg) {
|
|
|
|
return -ret;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<bool Hex, char e1 = Hex ? 'p' : 'e', char e2 = Hex ? 'P' : 'E'>
|
2021-03-23 22:29:32 +00:00
|
|
|
static inline bool p_read_exp(char const *&beg, char const *end, integer_type &fn) {
|
2021-03-23 21:17:25 +00:00
|
|
|
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) || !std::isdigit(*beg)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-23 22:29:32 +00:00
|
|
|
integer_type exp = 0;
|
2021-03-23 21:17:25 +00:00
|
|
|
while ((beg != end) && std::isdigit(*beg)) {
|
|
|
|
exp = exp * 10 + (*beg++ - '0');
|
|
|
|
}
|
|
|
|
if (neg) {
|
|
|
|
exp = -exp;
|
|
|
|
}
|
|
|
|
fn += exp;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<bool Hex>
|
|
|
|
static inline bool parse_gen_float(
|
2021-03-23 22:29:32 +00:00
|
|
|
char const *&beg, char const *end, std::string_view *endstr, float_type &ret
|
2021-03-23 21:17:25 +00:00
|
|
|
) {
|
2021-03-23 22:29:32 +00:00
|
|
|
auto read_digits = [&beg, end](double r, integer_type &n) {
|
2021-03-23 21:17:25 +00:00
|
|
|
while (
|
|
|
|
(beg != end) &&
|
|
|
|
(Hex ? std::isxdigit(*beg) : std::isdigit(*beg))
|
|
|
|
) {
|
|
|
|
if (Hex) {
|
|
|
|
r = r * 16.0 + double(p_hexd_to_int(*beg));
|
|
|
|
} else {
|
|
|
|
r = r * 10.0 + double(*beg - '0');
|
|
|
|
}
|
|
|
|
++n;
|
|
|
|
++beg;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
};
|
2021-03-23 22:29:32 +00:00
|
|
|
integer_type wn = 0, fn = 0;
|
2021-03-23 21:17:25 +00:00
|
|
|
double r = read_digits(0.0, wn);
|
|
|
|
if ((beg != end) && (*beg == '.')) {
|
|
|
|
++beg;
|
|
|
|
r = read_digits(r, fn);
|
|
|
|
}
|
|
|
|
if (!wn && !fn) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
if (Hex) {
|
2021-03-23 22:29:32 +00:00
|
|
|
ret = float_type(ldexp(r, fn * 4));
|
2021-03-23 21:17:25 +00:00
|
|
|
} else {
|
2021-03-23 22:29:32 +00:00
|
|
|
ret = float_type(r * pow(10, fn));
|
2021-03-23 21:17:25 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-23 22:29:32 +00:00
|
|
|
float_type parse_float(std::string_view input, std::string_view *endstr) {
|
2021-03-30 23:10:58 +00:00
|
|
|
char const *beg = &input[0];
|
|
|
|
char const *end = &input[input.size()];
|
2021-03-23 21:17:25 +00:00
|
|
|
char const *orig = beg;
|
|
|
|
beg = p_skip_white(beg, end);
|
|
|
|
if (beg == end) {
|
|
|
|
p_set_end(orig, end, endstr);
|
2021-03-23 22:29:32 +00:00
|
|
|
return float_type(0);
|
2021-03-23 21:17:25 +00:00
|
|
|
}
|
|
|
|
bool neg = p_check_neg(beg);
|
2021-03-23 22:29:32 +00:00
|
|
|
float_type ret = float_type(0);
|
2021-03-23 21:17:25 +00:00
|
|
|
if ((end - beg) >= 2) {
|
|
|
|
std::string_view pfx = std::string_view{beg, 2};
|
|
|
|
if ((pfx == "0x") || (pfx == "0X")) {
|
|
|
|
beg += 2;
|
|
|
|
if (!parse_gen_float<true>(beg, end, endstr, ret)) {
|
|
|
|
p_set_end(orig, end, endstr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!parse_gen_float<false>(beg, end, endstr, ret)) {
|
|
|
|
p_set_end(orig, end, endstr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
if (neg) {
|
|
|
|
return -ret;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_valid_name(std::string_view s) {
|
|
|
|
/* names cannot start with numbers (clashes with numeric literals) */
|
|
|
|
if (std::isdigit(s[0])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch (s[0]) {
|
|
|
|
/* more numeric literal clashes */
|
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
return std::isdigit(s[1]) || ((s[1] == '.') && std::isdigit(s[2]));
|
|
|
|
case '.':
|
|
|
|
return std::isdigit(s[1]) != 0;
|
|
|
|
/* other than that a name can be mostly anything */
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 17:32:01 +00:00
|
|
|
/* parse out a quoted string; return the raw string, without the quotes
|
|
|
|
* current parser state will be after the final quote
|
|
|
|
*/
|
2021-04-08 21:33:00 +00:00
|
|
|
std::string_view parser_state::get_str() {
|
|
|
|
size_t nl;
|
|
|
|
char const *beg = source;
|
2021-04-12 17:53:24 +00:00
|
|
|
source = parse_string(*ts.pstate, make_str_view(source, send), nl);
|
2021-04-08 21:33:00 +00:00
|
|
|
current_line += nl - 1;
|
2021-04-12 17:53:24 +00:00
|
|
|
auto ret = make_str_view(beg, source);
|
2021-04-08 21:33:00 +00:00
|
|
|
return ret.substr(1, ret.size() - 2);
|
|
|
|
}
|
|
|
|
|
2021-04-10 17:32:01 +00:00
|
|
|
/* like the above, but unescapes the string and dups it as a buffer */
|
2021-04-08 21:33:00 +00:00
|
|
|
charbuf parser_state::get_str_dup() {
|
|
|
|
charbuf buf{ts};
|
|
|
|
unescape_string(std::back_inserter(buf), get_str());
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2021-04-10 17:32:01 +00:00
|
|
|
/* a simple name, used for @foo in macro substitutions
|
|
|
|
*
|
|
|
|
* consists of an alpha character (or '_') followed
|
|
|
|
* by alphanumeric characters (or more '_')
|
|
|
|
*/
|
2021-04-08 21:33:00 +00:00
|
|
|
std::string_view parser_state::read_macro_name() {
|
|
|
|
char const *op = source;
|
|
|
|
char c = current();
|
|
|
|
if (!isalpha(c) && (c != '_')) {
|
|
|
|
return std::string_view{};
|
|
|
|
}
|
|
|
|
for (; isalnum(c) || (c == '_'); c = current()) {
|
|
|
|
next_char();
|
|
|
|
}
|
2021-04-12 17:53:24 +00:00
|
|
|
return make_str_view(op, source);
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 17:32:01 +00:00
|
|
|
/* advance the parser until we reach any of the given chars, then stop at it */
|
2021-04-08 21:33:00 +00:00
|
|
|
char parser_state::skip_until(std::string_view chars) {
|
|
|
|
char c = current();
|
|
|
|
while (c && (chars.find(c) == std::string_view::npos)) {
|
|
|
|
next_char();
|
|
|
|
c = current();
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2021-04-10 17:32:01 +00:00
|
|
|
/* advance the parser until we reach the given character, then stop at it */
|
2021-04-08 21:33:00 +00:00
|
|
|
char parser_state::skip_until(char cf) {
|
|
|
|
char c = current();
|
|
|
|
while (c && (c != cf)) {
|
|
|
|
next_char();
|
|
|
|
c = current();
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_hspace(char c) {
|
|
|
|
return (c == ' ') || (c == '\t') || (c == '\r');
|
|
|
|
}
|
|
|
|
|
|
|
|
void parser_state::skip_comments() {
|
|
|
|
for (;;) {
|
|
|
|
for (char c = current(); is_hspace(c); c = current()) {
|
|
|
|
next_char();
|
|
|
|
}
|
|
|
|
if (current() == '\\') {
|
|
|
|
char c = current(1);
|
|
|
|
if ((c != '\r') && (c != '\n')) {
|
|
|
|
throw error{*ts.pstate, "invalid line break"};
|
|
|
|
}
|
|
|
|
/* skip backslash */
|
|
|
|
next_char();
|
|
|
|
/* skip CR or LF */
|
|
|
|
next_char();
|
|
|
|
/* when CR, try also skipping LF; covers \r, \n, \r\n */
|
|
|
|
if ((c == '\r') && (current(1) == '\n')) {
|
|
|
|
next_char();
|
|
|
|
}
|
|
|
|
/* skip whitespace on new line */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((current() != '/') || (current(1) != '/')) {
|
|
|
|
return;
|
|
|
|
}
|
2021-04-10 17:32:01 +00:00
|
|
|
for (;;) {
|
|
|
|
auto c = current();
|
|
|
|
if (c && (c != '\n')) {
|
|
|
|
next_char();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string_view parser_state::get_word() {
|
|
|
|
char const *beg = source;
|
2021-04-12 17:53:24 +00:00
|
|
|
source = parse_word(*ts.pstate, make_str_view(source, send));
|
|
|
|
return make_str_view(beg, source);
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 02:40:58 +00:00
|
|
|
/* lookups that are invalid but not causing an error */
|
|
|
|
static void lookup_invalid(gen_state &gs, int ltype) {
|
2021-04-08 21:33:00 +00:00
|
|
|
switch (ltype) {
|
2021-04-10 02:40:58 +00:00
|
|
|
case VAL_POP:
|
|
|
|
break;
|
|
|
|
case VAL_NULL:
|
|
|
|
case VAL_ANY:
|
|
|
|
case VAL_WORD:
|
2021-04-08 21:33:00 +00:00
|
|
|
case VAL_COND:
|
2021-04-10 02:40:58 +00:00
|
|
|
gs.gen_val_null();
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
default:
|
2021-04-10 02:40:58 +00:00
|
|
|
gs.gen_val(ltype);
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-04-10 02:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* on success, handles non-value return types not handled by type mask */
|
|
|
|
static void lookup_done(gen_state &gs, int ltype) {
|
2021-04-08 21:33:00 +00:00
|
|
|
switch (ltype) {
|
|
|
|
case VAL_POP:
|
2021-04-10 02:40:58 +00:00
|
|
|
gs.gen_pop();
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
case VAL_CODE:
|
2021-04-10 02:40:58 +00:00
|
|
|
gs.gen_compile();
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
case VAL_COND:
|
2021-04-10 02:40:58 +00:00
|
|
|
gs.gen_compile(true);
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
case VAL_IDENT:
|
2021-04-10 02:40:58 +00:00
|
|
|
gs.gen_ident_lookup();
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-04-10 02:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* parses $foo */
|
|
|
|
void parser_state::parse_lookup(int ltype) {
|
|
|
|
charbuf lookup{gs.ts};
|
|
|
|
next_char(); /* skip $ */
|
|
|
|
switch (current()) {
|
|
|
|
/* $(...), $[...] */
|
|
|
|
case '(':
|
|
|
|
case '[':
|
2021-04-10 03:53:17 +00:00
|
|
|
if (!parse_arg(VAL_STRING)) {
|
2021-04-10 02:40:58 +00:00
|
|
|
lookup_invalid(gs, ltype);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gs.gen_lookup_ident(ltype);
|
|
|
|
lookup_done(gs, ltype);
|
|
|
|
return;
|
|
|
|
/* $$...; sub-lookup */
|
|
|
|
case '$':
|
|
|
|
parse_lookup(VAL_STRING);
|
|
|
|
gs.gen_lookup_ident(ltype);
|
|
|
|
lookup_done(gs, ltype);
|
|
|
|
return;
|
|
|
|
/* $"..."; like $foo but looser */
|
|
|
|
case '\"':
|
|
|
|
lookup = get_str_dup();
|
|
|
|
lookup.push_back('\0');
|
|
|
|
goto lookup_id;
|
|
|
|
/* any other thing, presumably a valid name */
|
|
|
|
default:
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
2021-04-10 02:40:58 +00:00
|
|
|
}
|
|
|
|
lookup.append(get_word());
|
|
|
|
if (lookup.empty()) {
|
|
|
|
lookup_invalid(gs, ltype);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lookup.push_back('\0');
|
|
|
|
lookup_id:
|
|
|
|
/* fetch an ident or prepare a fresh one */
|
|
|
|
ident &id = ts.istate->new_ident(
|
|
|
|
*ts.pstate, lookup.str_term(), IDENT_FLAG_UNKNOWN
|
|
|
|
);
|
|
|
|
switch (id.get_type()) {
|
|
|
|
case ident_type::IVAR:
|
|
|
|
switch (ltype) {
|
|
|
|
case VAL_CODE:
|
|
|
|
case VAL_IDENT:
|
|
|
|
gs.gen_lookup_ivar(id, ltype);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lookup_done(gs, ltype);
|
|
|
|
return;
|
|
|
|
case ident_type::FVAR:
|
|
|
|
switch (ltype) {
|
|
|
|
case VAL_CODE:
|
|
|
|
case VAL_IDENT:
|
|
|
|
gs.gen_lookup_fvar(id, ltype);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lookup_done(gs, ltype);
|
|
|
|
return;
|
|
|
|
case ident_type::SVAR:
|
|
|
|
switch (ltype) {
|
|
|
|
case VAL_POP:
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
gs.gen_lookup_svar(id, ltype);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lookup_done(gs, ltype);
|
|
|
|
return;
|
|
|
|
case ident_type::ALIAS:
|
|
|
|
switch (ltype) {
|
|
|
|
case VAL_POP:
|
|
|
|
return;
|
|
|
|
case VAL_COND:
|
|
|
|
gs.gen_lookup_alias(id, ltype);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
gs.gen_lookup_alias(id, ltype, VAL_STRING);
|
|
|
|
}
|
|
|
|
lookup_done(gs, ltype);
|
|
|
|
return;
|
|
|
|
case ident_type::COMMAND: {
|
|
|
|
std::uint32_t comtype = BC_INST_COM, numargs = 0;
|
|
|
|
auto fmt = static_cast<command_impl &>(id).get_args();
|
|
|
|
for (char c: fmt) {
|
|
|
|
switch (c) {
|
|
|
|
case 's':
|
|
|
|
gs.gen_val_string(std::string_view{});
|
|
|
|
numargs++;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
gs.gen_val_integer();
|
|
|
|
numargs++;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
gs.gen_val_integer(
|
|
|
|
std::numeric_limits<integer_type>::min()
|
|
|
|
);
|
|
|
|
numargs++;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
gs.gen_val_float();
|
|
|
|
numargs++;
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
gs.gen_dup(VAL_FLOAT);
|
|
|
|
numargs++;
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
case 't':
|
|
|
|
gs.gen_val_null();
|
|
|
|
numargs++;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
gs.gen_block();
|
|
|
|
numargs++;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
gs.gen_val_ident();
|
|
|
|
numargs++;
|
|
|
|
break;
|
|
|
|
case '$':
|
|
|
|
gs.gen_val_ident(id);
|
|
|
|
numargs++;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
gs.gen_val_integer(-1);
|
|
|
|
numargs++;
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
comtype = BC_INST_COM_C;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
comtype = BC_INST_COM_V;
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gs.gen_command_call(id, comtype, ltype, numargs);
|
|
|
|
gs.gen_push_result(ltype);
|
|
|
|
lookup_done(gs, ltype);
|
|
|
|
return;
|
|
|
|
}
|
2021-04-08 21:33:00 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-04-10 02:40:58 +00:00
|
|
|
lookup_invalid(gs, ltype);
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 03:32:07 +00:00
|
|
|
/* parses @... macro substitutions within block strings
|
|
|
|
*
|
|
|
|
* only called from within parse_blockarg
|
|
|
|
*/
|
2021-04-10 02:51:11 +00:00
|
|
|
bool parser_state::parse_subblock() {
|
|
|
|
charbuf lookup{ts};
|
|
|
|
switch (current()) {
|
|
|
|
/* @(...) */
|
2021-04-08 21:33:00 +00:00
|
|
|
case '(':
|
2021-04-10 03:53:17 +00:00
|
|
|
return parse_arg(VAL_ANY);
|
2021-04-10 02:51:11 +00:00
|
|
|
/* @[...]; like a variable lookup */
|
2021-04-08 21:33:00 +00:00
|
|
|
case '[':
|
2021-04-10 03:53:17 +00:00
|
|
|
if (!parse_arg(VAL_STRING)) {
|
2021-04-08 21:33:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-04-10 02:51:11 +00:00
|
|
|
gs.gen_lookup_ident();
|
|
|
|
return true;
|
|
|
|
/* @"..."; like the above but easier (no inner compiles) */
|
2021-04-08 21:33:00 +00:00
|
|
|
case '\"':
|
2021-04-10 02:51:11 +00:00
|
|
|
lookup = get_str_dup();
|
2021-04-08 21:33:00 +00:00
|
|
|
lookup.push_back('\0');
|
2021-04-10 02:51:11 +00:00
|
|
|
goto lookup_id;
|
|
|
|
/* anything else, presumably a valid name */
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lookup.append(read_macro_name());
|
|
|
|
if (lookup.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
lookup.push_back('\0');
|
|
|
|
lookup_id:
|
|
|
|
ident &id = ts.istate->new_ident(
|
|
|
|
*ts.pstate, lookup.str_term(), IDENT_FLAG_UNKNOWN
|
|
|
|
);
|
|
|
|
switch (id.get_type()) {
|
|
|
|
case ident_type::IVAR:
|
|
|
|
gs.gen_lookup_ivar(id);
|
|
|
|
return true;
|
|
|
|
case ident_type::FVAR:
|
|
|
|
gs.gen_lookup_fvar(id);
|
|
|
|
return true;
|
|
|
|
case ident_type::SVAR:
|
|
|
|
gs.gen_lookup_svar(id);
|
|
|
|
return true;
|
|
|
|
case ident_type::ALIAS:
|
|
|
|
gs.gen_lookup_alias(id);
|
|
|
|
return true;
|
|
|
|
default:
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-04-10 02:51:11 +00:00
|
|
|
gs.gen_val_string(lookup.str_term());
|
|
|
|
gs.gen_lookup_ident();
|
2021-04-08 21:33:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-10 03:32:07 +00:00
|
|
|
/* [...] argument parser */
|
|
|
|
void parser_state::parse_blockarg(int ltype) {
|
|
|
|
char const *start = source;
|
|
|
|
/* current bracket level */
|
|
|
|
std::size_t blevel = 1;
|
|
|
|
std::size_t curline = current_line;
|
|
|
|
/* number of strings to concatenate */
|
|
|
|
std::size_t concs = 0;
|
|
|
|
while (blevel > 0) {
|
|
|
|
/* skip until a significant character */
|
|
|
|
switch (skip_until("@\"/[]")) {
|
|
|
|
/* EOS */
|
2021-04-08 21:33:00 +00:00
|
|
|
case '\0':
|
2021-04-10 03:32:07 +00:00
|
|
|
throw error{*ts.pstate, "missing \"]\""};
|
|
|
|
/* inner string, parse through it */
|
2021-04-08 21:33:00 +00:00
|
|
|
case '\"':
|
2021-04-10 03:32:07 +00:00
|
|
|
get_str();
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
2021-04-10 03:32:07 +00:00
|
|
|
/* possibly a comment */
|
2021-04-08 21:33:00 +00:00
|
|
|
case '/':
|
2021-04-10 03:32:07 +00:00
|
|
|
next_char();
|
|
|
|
if (current() == '/') {
|
|
|
|
/* yup, just go until we reach a newline */
|
|
|
|
skip_until('\n');
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
break;
|
2021-04-10 03:32:07 +00:00
|
|
|
/* keep it nice and balanced */
|
2021-04-08 21:33:00 +00:00
|
|
|
case '[':
|
2021-04-10 03:32:07 +00:00
|
|
|
next_char();
|
|
|
|
++blevel;
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
case ']':
|
2021-04-10 03:32:07 +00:00
|
|
|
next_char();
|
|
|
|
--blevel;
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
2021-04-10 03:32:07 +00:00
|
|
|
/* macro substitution */
|
2021-04-08 21:33:00 +00:00
|
|
|
case '@': {
|
2021-04-10 03:32:07 +00:00
|
|
|
char const *end = source;
|
|
|
|
std::size_t alevel = 0;
|
|
|
|
while (current() == '@') {
|
|
|
|
++alevel;
|
|
|
|
next_char();
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
2021-04-10 03:32:07 +00:00
|
|
|
if (blevel > alevel) {
|
|
|
|
/* deeper block level than macro level
|
|
|
|
* we can't substitute at this point so leave it alone
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (blevel < alevel) {
|
|
|
|
/* shallower block level than macro level
|
|
|
|
* this is an error, we can't substitute this
|
|
|
|
*/
|
|
|
|
throw error{*ts.pstate, "too many @s"};
|
|
|
|
}
|
|
|
|
/* generate a block string for everything until now */
|
|
|
|
if (start != end) {
|
2021-04-12 17:53:24 +00:00
|
|
|
gs.gen_val_block(make_str_view(start, end));
|
2021-04-10 03:32:07 +00:00
|
|
|
++concs;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
2021-04-10 03:32:07 +00:00
|
|
|
if (parse_subblock()) {
|
|
|
|
++concs;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
if (concs) {
|
2021-04-10 03:32:07 +00:00
|
|
|
start = source;
|
|
|
|
curline = current_line;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-04-10 03:32:07 +00:00
|
|
|
/* actually unreachable, we handle all incl. EOS */
|
2021-04-08 21:33:00 +00:00
|
|
|
default:
|
2021-04-10 03:32:07 +00:00
|
|
|
next_char();
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-04-10 03:32:07 +00:00
|
|
|
if ((source - 1) <= start) {
|
|
|
|
/* possibly empty */
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
/* non-empty */
|
|
|
|
if (!concs) {
|
|
|
|
/* one contiguous string */
|
|
|
|
switch (ltype) {
|
|
|
|
case VAL_POP:
|
|
|
|
/* ignore */
|
|
|
|
return;
|
|
|
|
case VAL_CODE:
|
|
|
|
case VAL_COND: {
|
|
|
|
/* compile */
|
|
|
|
auto ret = gs.gen_block(
|
2021-04-12 17:53:24 +00:00
|
|
|
make_str_view(start, send), curline, VAL_NULL, ']'
|
2021-04-10 03:32:07 +00:00
|
|
|
);
|
|
|
|
source = ret.second.data();
|
|
|
|
send = source + ret.second.size();
|
|
|
|
current_line = ret.first;
|
|
|
|
return;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
2021-04-10 03:32:07 +00:00
|
|
|
case VAL_IDENT:
|
2021-04-12 17:53:24 +00:00
|
|
|
gs.gen_val_ident(make_str_view(start, source - 1));
|
2021-04-10 03:32:07 +00:00
|
|
|
return;
|
|
|
|
default:
|
2021-04-12 17:53:24 +00:00
|
|
|
gs.gen_val_block(make_str_view(start, source - 1));
|
2021-04-10 03:32:07 +00:00
|
|
|
goto done;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-12 17:53:24 +00:00
|
|
|
gs.gen_val_block(make_str_view(start, source - 1));
|
2021-04-10 03:32:07 +00:00
|
|
|
/* concat the pieces */
|
|
|
|
gs.gen_concat(++concs, false, ltype);
|
|
|
|
done:
|
|
|
|
bool got_val = (concs || ((source - 1) > start));
|
|
|
|
/* handle the result */
|
|
|
|
switch (ltype) {
|
2021-04-08 21:33:00 +00:00
|
|
|
case VAL_POP:
|
2021-04-10 03:32:07 +00:00
|
|
|
if (got_val) {
|
|
|
|
gs.gen_pop();
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VAL_COND:
|
2021-04-10 03:32:07 +00:00
|
|
|
if (!got_val) {
|
|
|
|
gs.gen_val_null();
|
2021-04-08 21:33:00 +00:00
|
|
|
} else {
|
2021-04-10 03:32:07 +00:00
|
|
|
gs.gen_compile(true);
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VAL_CODE:
|
2021-04-10 03:32:07 +00:00
|
|
|
if (!got_val) {
|
|
|
|
gs.gen_block();
|
2021-04-08 21:33:00 +00:00
|
|
|
} else {
|
2021-04-10 03:32:07 +00:00
|
|
|
gs.gen_compile();
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VAL_IDENT:
|
2021-04-10 03:32:07 +00:00
|
|
|
if (!got_val) {
|
|
|
|
gs.gen_val_ident();
|
2021-04-08 21:33:00 +00:00
|
|
|
} else {
|
2021-04-10 03:32:07 +00:00
|
|
|
gs.gen_ident_lookup();
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VAL_STRING:
|
|
|
|
case VAL_NULL:
|
|
|
|
case VAL_ANY:
|
|
|
|
case VAL_WORD:
|
2021-04-10 03:32:07 +00:00
|
|
|
if (!got_val) {
|
|
|
|
gs.gen_val_string();
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (!concs) {
|
2021-04-10 03:32:07 +00:00
|
|
|
if ((source - 1) <= start) {
|
|
|
|
gs.gen_val(ltype);
|
2021-04-08 21:33:00 +00:00
|
|
|
} else {
|
2021-04-10 03:32:07 +00:00
|
|
|
gs.gen_force(ltype);
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 03:53:17 +00:00
|
|
|
/* parses a single argument passed to anything
|
|
|
|
* this also includes left and right sides in assignments
|
|
|
|
* returns if we parsed something
|
|
|
|
*/
|
|
|
|
bool parser_state::parse_arg(int ltype, charbuf *word) {
|
|
|
|
/* not a part of the grammar */
|
|
|
|
skip_comments();
|
|
|
|
/* guess what our argument is */
|
|
|
|
switch (current()) {
|
|
|
|
/* a plain string literal: "..." */
|
2021-04-08 21:33:00 +00:00
|
|
|
case '\"':
|
2021-04-10 03:53:17 +00:00
|
|
|
switch (ltype) {
|
2021-04-08 21:33:00 +00:00
|
|
|
case VAL_POP:
|
2021-04-10 03:53:17 +00:00
|
|
|
get_str();
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
case VAL_COND: {
|
2021-04-10 03:53:17 +00:00
|
|
|
auto line = current_line;
|
|
|
|
auto s = get_str_dup();
|
2021-04-08 21:33:00 +00:00
|
|
|
if (!s.empty()) {
|
|
|
|
s.push_back('\0');
|
2021-04-10 03:53:17 +00:00
|
|
|
gs.gen_block(s.str_term(), line);
|
2021-04-08 21:33:00 +00:00
|
|
|
} else {
|
2021-04-10 03:53:17 +00:00
|
|
|
gs.gen_val_null();
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VAL_CODE: {
|
2021-04-10 03:53:17 +00:00
|
|
|
auto line = current_line;
|
|
|
|
auto s = get_str_dup();
|
2021-04-08 21:33:00 +00:00
|
|
|
s.push_back('\0');
|
2021-04-10 03:53:17 +00:00
|
|
|
gs.gen_block(s.str_term(), line);
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-04-10 03:53:17 +00:00
|
|
|
/* used to begin a statement */
|
|
|
|
case VAL_WORD: {
|
|
|
|
*word = get_str_dup();
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
2021-04-10 03:53:17 +00:00
|
|
|
}
|
2021-04-08 21:33:00 +00:00
|
|
|
case VAL_ANY:
|
|
|
|
case VAL_STRING:
|
2021-04-10 03:53:17 +00:00
|
|
|
gs.gen_val_string_unescape(get_str());
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
default: {
|
2021-04-10 03:53:17 +00:00
|
|
|
auto line = current_line;
|
|
|
|
auto s = get_str_dup();
|
2021-04-08 21:33:00 +00:00
|
|
|
s.push_back('\0');
|
2021-04-10 03:53:17 +00:00
|
|
|
gs.gen_val(ltype, s.str_term(), line);
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2021-04-10 03:53:17 +00:00
|
|
|
/* a lookup: $foo */
|
2021-04-08 21:33:00 +00:00
|
|
|
case '$':
|
2021-04-10 03:53:17 +00:00
|
|
|
parse_lookup(ltype);
|
2021-04-08 21:33:00 +00:00
|
|
|
return true;
|
2021-04-10 03:53:17 +00:00
|
|
|
/* an expression: (...) */
|
2021-04-08 21:33:00 +00:00
|
|
|
case '(': {
|
2021-04-10 03:53:17 +00:00
|
|
|
next_char();
|
|
|
|
auto start = gs.count();
|
|
|
|
parse_block(VAL_ANY, ')');
|
|
|
|
if (gs.count() > start) {
|
|
|
|
/* non-empty */
|
|
|
|
gs.gen_push_result(ltype);
|
2021-04-08 21:33:00 +00:00
|
|
|
} else {
|
2021-04-10 03:53:17 +00:00
|
|
|
gs.gen_val(ltype);
|
2021-04-08 21:33:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-04-10 03:53:17 +00:00
|
|
|
switch (ltype) {
|
2021-04-08 21:33:00 +00:00
|
|
|
case VAL_POP:
|
2021-04-10 03:53:17 +00:00
|
|
|
gs.gen_pop();
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
case VAL_COND:
|
|
|
|
case VAL_CODE:
|
2021-04-10 03:53:17 +00:00
|
|
|
gs.gen_compile(ltype == VAL_COND);
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
case VAL_IDENT:
|
2021-04-10 03:53:17 +00:00
|
|
|
gs.gen_ident_lookup();
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2021-04-10 03:53:17 +00:00
|
|
|
/* a block: [...] */
|
2021-04-08 21:33:00 +00:00
|
|
|
case '[':
|
2021-04-10 03:53:17 +00:00
|
|
|
next_char();
|
|
|
|
parse_blockarg(ltype);
|
2021-04-08 21:33:00 +00:00
|
|
|
return true;
|
2021-04-10 03:53:17 +00:00
|
|
|
/* something else, presumably a word */
|
2021-04-08 21:33:00 +00:00
|
|
|
default:
|
2021-04-10 03:53:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (ltype) {
|
|
|
|
case VAL_POP:
|
|
|
|
return !get_word().empty();
|
|
|
|
case VAL_COND:
|
|
|
|
case VAL_CODE: {
|
|
|
|
auto line = current_line;
|
|
|
|
auto s = get_word();
|
|
|
|
if (s.empty()) {
|
|
|
|
return false;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
2021-04-10 03:53:17 +00:00
|
|
|
gs.gen_block(s, line);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case VAL_WORD: {
|
|
|
|
auto s = get_word();
|
|
|
|
if (s.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
word->clear();
|
|
|
|
word->append(s);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
auto line = current_line;
|
|
|
|
auto s = get_word();
|
|
|
|
if (s.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
gs.gen_val(ltype, s, line);
|
|
|
|
return true;
|
|
|
|
}
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 22:13:10 +00:00
|
|
|
static bool parse_cmd_arg(parser_state &ps, char s, bool more, bool rep) {
|
|
|
|
switch (s) {
|
|
|
|
case 's': /* string */
|
|
|
|
if (more) {
|
|
|
|
more = ps.parse_arg(VAL_STRING);
|
|
|
|
}
|
|
|
|
if (!more && !rep) {
|
|
|
|
ps.gs.gen_val_string();
|
|
|
|
}
|
|
|
|
return more;
|
|
|
|
case 'i': /* integer */
|
|
|
|
if (more) {
|
|
|
|
more = ps.parse_arg(VAL_INT);
|
|
|
|
}
|
|
|
|
if (!more && !rep) {
|
|
|
|
ps.gs.gen_val_integer();
|
|
|
|
}
|
|
|
|
return more;
|
|
|
|
case 'b': /* integer, INT_MIN default */
|
|
|
|
if (more) {
|
|
|
|
more = ps.parse_arg(VAL_INT);
|
|
|
|
}
|
|
|
|
if (!more && !rep) {
|
|
|
|
ps.gs.gen_val_integer(std::numeric_limits<integer_type>::min());
|
|
|
|
}
|
|
|
|
return more;
|
|
|
|
case 'f': /* float */
|
|
|
|
if (more) {
|
|
|
|
more = ps.parse_arg(VAL_FLOAT);
|
|
|
|
}
|
|
|
|
if (!more && !rep) {
|
|
|
|
ps.gs.gen_val_float();
|
|
|
|
}
|
|
|
|
return more;
|
|
|
|
case 'F': /* float, prev-argument default */
|
|
|
|
if (more) {
|
|
|
|
more = ps.parse_arg(VAL_FLOAT);
|
|
|
|
}
|
|
|
|
if (!more && !rep) {
|
|
|
|
ps.gs.gen_dup(VAL_FLOAT);
|
|
|
|
}
|
|
|
|
return more;
|
|
|
|
case 't': /* any arg */
|
|
|
|
if (more) {
|
|
|
|
more = ps.parse_arg(VAL_ANY);
|
|
|
|
}
|
|
|
|
if (!more && !rep) {
|
|
|
|
ps.gs.gen_val_null();
|
|
|
|
}
|
|
|
|
return more;
|
|
|
|
case 'E': /* condition */
|
|
|
|
if (more) {
|
|
|
|
more = ps.parse_arg(VAL_COND);
|
|
|
|
}
|
|
|
|
if (!more && !rep) {
|
|
|
|
ps.gs.gen_val_null();
|
|
|
|
}
|
|
|
|
return more;
|
|
|
|
case 'e': /* code */
|
|
|
|
if (more) {
|
|
|
|
more = ps.parse_arg(VAL_CODE);
|
|
|
|
}
|
|
|
|
if (!more && !rep) {
|
|
|
|
ps.gs.gen_block();
|
|
|
|
}
|
|
|
|
return more;
|
|
|
|
case 'r': /* ident */
|
|
|
|
if (more) {
|
|
|
|
more = ps.parse_arg(VAL_IDENT);
|
|
|
|
}
|
|
|
|
if (!more && !rep) {
|
|
|
|
ps.gs.gen_val_ident();
|
|
|
|
}
|
|
|
|
return more;
|
|
|
|
default:
|
|
|
|
return more;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 05:32:44 +00:00
|
|
|
bool parser_state::parse_call_command(
|
2021-04-10 22:13:10 +00:00
|
|
|
command_impl *id, ident &self, int rettype
|
2021-04-08 21:33:00 +00:00
|
|
|
) {
|
2021-04-10 22:13:10 +00:00
|
|
|
std::uint32_t comtype = BC_INST_COM, numargs = 0, fakeargs = 0;
|
2021-04-08 21:33:00 +00:00
|
|
|
auto fmt = id->get_args();
|
2021-04-10 22:13:10 +00:00
|
|
|
bool more = true, rep = false;
|
2021-04-08 21:33:00 +00:00
|
|
|
for (auto it = fmt.begin(); it != fmt.end(); ++it) {
|
|
|
|
switch (*it) {
|
|
|
|
case 's': /* string */
|
2021-04-10 22:13:10 +00:00
|
|
|
more = parse_cmd_arg(*this, 's', more, rep);
|
|
|
|
if (more && ((it + 1) == fmt.end())) {
|
2021-04-08 21:33:00 +00:00
|
|
|
int numconc = 1;
|
|
|
|
for (;;) {
|
2021-04-10 05:32:44 +00:00
|
|
|
more = parse_arg(VAL_STRING);
|
2021-04-08 21:33:00 +00:00
|
|
|
if (!more) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
numconc++;
|
|
|
|
}
|
|
|
|
if (numconc > 1) {
|
2021-04-10 05:32:44 +00:00
|
|
|
gs.gen_concat(numconc, true, VAL_STRING);
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
2021-04-10 22:13:10 +00:00
|
|
|
} else if (!more) {
|
|
|
|
if (!rep) {
|
|
|
|
++fakeargs;
|
|
|
|
} else {
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-04-10 22:13:10 +00:00
|
|
|
++numargs;
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
case '$': /* self */
|
2021-04-10 05:32:44 +00:00
|
|
|
gs.gen_val_ident(self);
|
2021-04-10 22:13:10 +00:00
|
|
|
++numargs;
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
case 'N': /* number of arguments */
|
2021-04-10 05:32:44 +00:00
|
|
|
gs.gen_val_integer(numargs - fakeargs);
|
2021-04-10 22:13:10 +00:00
|
|
|
++numargs;
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
case 'C': /* concatenated string */
|
|
|
|
case 'V': /* varargs */
|
2021-04-10 22:13:10 +00:00
|
|
|
comtype = (*it == 'C') ? BC_INST_COM_C : BC_INST_COM_V;
|
|
|
|
if (more) {
|
|
|
|
for (;;) {
|
2021-04-10 05:32:44 +00:00
|
|
|
more = parse_arg(VAL_ANY);
|
2021-04-10 22:13:10 +00:00
|
|
|
if (!more) {
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-04-10 22:13:10 +00:00
|
|
|
++numargs;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-09 22:40:57 +00:00
|
|
|
break;
|
2021-04-10 22:13:10 +00:00
|
|
|
case '1': case '2': case '3': case '4': /* vararg repetition */
|
|
|
|
if (more) {
|
2021-04-08 21:33:00 +00:00
|
|
|
int numrep = *it - '0' + 1;
|
|
|
|
it -= numrep;
|
|
|
|
rep = true;
|
|
|
|
}
|
|
|
|
break;
|
2021-04-10 22:13:10 +00:00
|
|
|
default:
|
|
|
|
more = parse_cmd_arg(*this, *it, more, rep);
|
|
|
|
if (!more) {
|
|
|
|
if (!rep) {
|
|
|
|
++fakeargs;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++numargs;
|
|
|
|
break;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-10 05:32:44 +00:00
|
|
|
gs.gen_command_call(*id, comtype, rettype, numargs);
|
|
|
|
return more;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 05:32:44 +00:00
|
|
|
bool parser_state::parse_call_alias(alias &id) {
|
|
|
|
bool more;
|
2021-04-08 21:33:00 +00:00
|
|
|
std::uint32_t numargs = 0;
|
|
|
|
for (;;) {
|
2021-04-10 05:32:44 +00:00
|
|
|
more = parse_arg(VAL_ANY);
|
2021-04-08 21:33:00 +00:00
|
|
|
if (!more) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++numargs;
|
|
|
|
}
|
2021-04-10 05:32:44 +00:00
|
|
|
gs.gen_alias_call(id, numargs);
|
|
|
|
return more;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 05:32:44 +00:00
|
|
|
bool parser_state::parse_id_local() {
|
|
|
|
bool more;
|
2021-04-08 21:33:00 +00:00
|
|
|
std::uint32_t numargs = 0;
|
2021-04-10 05:32:44 +00:00
|
|
|
for (;;) {
|
|
|
|
more = parse_arg(VAL_IDENT);
|
|
|
|
if (!more) {
|
|
|
|
break;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
2021-04-10 05:32:44 +00:00
|
|
|
numargs++;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
2021-04-10 05:32:44 +00:00
|
|
|
gs.gen_local(numargs);
|
|
|
|
return more;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 05:32:44 +00:00
|
|
|
bool parser_state::parse_id_do(bool args, int ltype) {
|
|
|
|
bool more = parse_arg(VAL_CODE);
|
2021-04-09 22:40:57 +00:00
|
|
|
if (!more) {
|
2021-04-10 05:32:44 +00:00
|
|
|
gs.gen_result_null(ltype);
|
2021-04-09 22:40:57 +00:00
|
|
|
} else {
|
2021-04-10 05:32:44 +00:00
|
|
|
gs.gen_do(args, ltype);
|
2021-04-09 22:40:57 +00:00
|
|
|
}
|
2021-04-10 05:32:44 +00:00
|
|
|
return more;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 05:32:44 +00:00
|
|
|
bool parser_state::parse_id_if(ident &id, int ltype) {
|
|
|
|
/* condition */
|
|
|
|
bool more = parse_arg(VAL_ANY);
|
2021-04-08 21:33:00 +00:00
|
|
|
if (!more) {
|
2021-04-10 00:56:49 +00:00
|
|
|
/* no condition: expr is nothing */
|
2021-04-10 05:32:44 +00:00
|
|
|
gs.gen_result_null(ltype);
|
2021-04-08 21:33:00 +00:00
|
|
|
} else {
|
2021-04-10 05:32:44 +00:00
|
|
|
auto tpos = gs.count();
|
2021-04-10 00:56:49 +00:00
|
|
|
/* true block */
|
2021-04-10 05:32:44 +00:00
|
|
|
more = parse_arg(VAL_CODE);
|
2021-04-08 21:33:00 +00:00
|
|
|
if (!more) {
|
2021-04-10 00:56:49 +00:00
|
|
|
/* we only had condition: expr is nothing */
|
2021-04-10 05:32:44 +00:00
|
|
|
gs.gen_pop();
|
|
|
|
gs.gen_result_null(ltype);
|
2021-04-08 21:33:00 +00:00
|
|
|
} else {
|
2021-04-10 05:32:44 +00:00
|
|
|
auto fpos = gs.count();
|
2021-04-10 00:56:49 +00:00
|
|
|
/* false block */
|
2021-04-10 05:32:44 +00:00
|
|
|
more = parse_arg(VAL_CODE);
|
|
|
|
if (!gs.gen_if(tpos, more ? fpos : 0)) {
|
2021-04-10 00:56:49 +00:00
|
|
|
/* can't fully compile: use a call */
|
2021-04-10 05:32:44 +00:00
|
|
|
gs.gen_command_call(id, BC_INST_COM, ltype);
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-10 05:32:44 +00:00
|
|
|
return more;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 05:32:44 +00:00
|
|
|
bool parser_state::parse_id_and_or(ident &id, int ltype) {
|
2021-04-08 21:33:00 +00:00
|
|
|
std::uint32_t numargs = 0;
|
2021-04-10 05:32:44 +00:00
|
|
|
/* first */
|
|
|
|
bool more = parse_arg(VAL_COND);
|
2021-04-08 21:33:00 +00:00
|
|
|
if (!more) {
|
2021-04-10 00:56:49 +00:00
|
|
|
/* no first: generate true or false */
|
2021-04-10 05:32:44 +00:00
|
|
|
if (ident_p{id}.impl().p_type == ID_AND) {
|
|
|
|
gs.gen_result_true(ltype);
|
2021-04-09 22:40:57 +00:00
|
|
|
} else {
|
2021-04-10 05:32:44 +00:00
|
|
|
gs.gen_result_false(ltype);
|
2021-04-09 22:40:57 +00:00
|
|
|
}
|
2021-04-08 21:33:00 +00:00
|
|
|
} else {
|
|
|
|
numargs++;
|
2021-04-10 05:32:44 +00:00
|
|
|
std::size_t start = gs.count(), end = start;
|
2021-04-08 21:33:00 +00:00
|
|
|
for (;;) {
|
2021-04-10 00:56:49 +00:00
|
|
|
/* keep going as long as we only get blocks */
|
2021-04-10 05:32:44 +00:00
|
|
|
more = parse_arg(VAL_COND);
|
2021-04-08 21:33:00 +00:00
|
|
|
if (!more) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
numargs++;
|
2021-04-10 05:32:44 +00:00
|
|
|
if (!gs.is_block(end)) {
|
2021-04-08 21:33:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-04-10 05:32:44 +00:00
|
|
|
end = gs.count();
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
if (more) {
|
2021-04-10 00:56:49 +00:00
|
|
|
/* last parsed thing was not a block, fall back to call */
|
2021-04-08 21:33:00 +00:00
|
|
|
for (;;) {
|
2021-04-10 00:56:49 +00:00
|
|
|
/* but first, parse out the remainder of values */
|
2021-04-10 05:32:44 +00:00
|
|
|
more = parse_arg(VAL_COND);
|
2021-04-08 21:33:00 +00:00
|
|
|
if (!more) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
numargs++;
|
|
|
|
}
|
2021-04-10 05:32:44 +00:00
|
|
|
gs.gen_command_call(id, BC_INST_COM_V, ltype, numargs);
|
2021-04-08 21:33:00 +00:00
|
|
|
} else {
|
2021-04-10 00:56:49 +00:00
|
|
|
/* all blocks and nothing left */
|
2021-04-10 05:32:44 +00:00
|
|
|
gs.gen_and_or((ident_p{id}.impl().p_type != ID_AND), start);
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-10 05:32:44 +00:00
|
|
|
return more;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 17:32:01 +00:00
|
|
|
static bool finish_statement(parser_state &ps, bool more, int term) {
|
|
|
|
/* skip through any remaining args in the statement */
|
|
|
|
if (more) {
|
|
|
|
while (ps.parse_arg(VAL_POP)) {}
|
|
|
|
}
|
|
|
|
/* handle special characters */
|
|
|
|
switch (ps.skip_until(")];/\n")) {
|
|
|
|
/* EOS */
|
|
|
|
case '\0':
|
|
|
|
if (ps.current() != term) {
|
|
|
|
throw error{*ps.ts.pstate, "missing \"%c\"", char(term)};
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
/* terminating parens/brackets */
|
|
|
|
case ')':
|
|
|
|
case ']':
|
|
|
|
/* if the expected terminator, finish normally */
|
|
|
|
if (ps.current() == term) {
|
|
|
|
ps.next_char();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
throw error{*ps.ts.pstate, "unexpected \"%c\"", ps.current()};
|
|
|
|
/* potential comment */
|
|
|
|
case '/':
|
|
|
|
ps.next_char();
|
|
|
|
if (ps.current() == '/') {
|
|
|
|
ps.skip_until('\n');
|
|
|
|
}
|
|
|
|
return finish_statement(ps, false, term);
|
|
|
|
/* next statement */
|
|
|
|
default:
|
|
|
|
ps.next_char();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* advance to next statement */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parser_state::parse_call_id(ident &id, int ltype) {
|
|
|
|
switch (ident_p{id}.impl().p_type) {
|
|
|
|
case ID_ALIAS:
|
|
|
|
return parse_call_alias(static_cast<alias &>(id));
|
|
|
|
case ID_COMMAND:
|
|
|
|
return parse_call_command(
|
|
|
|
static_cast<command_impl *>(&id), id, ltype
|
|
|
|
);
|
|
|
|
case ID_LOCAL:
|
|
|
|
return parse_id_local();
|
|
|
|
case ID_DO:
|
|
|
|
return parse_id_do(false, ltype);
|
|
|
|
case ID_DOARGS:
|
|
|
|
return parse_id_do(true, ltype);
|
|
|
|
case ID_IF:
|
|
|
|
return parse_id_if(id, ltype);
|
|
|
|
case ID_BREAK:
|
|
|
|
gs.gen_break();
|
|
|
|
return true;
|
|
|
|
case ID_CONTINUE:
|
|
|
|
gs.gen_continue();
|
|
|
|
return true;
|
|
|
|
case ID_RESULT: {
|
|
|
|
bool more = parse_arg(VAL_ANY);
|
|
|
|
if (!more) {
|
|
|
|
gs.gen_result_null(ltype);
|
|
|
|
} else {
|
|
|
|
gs.gen_result(ltype);
|
|
|
|
}
|
|
|
|
return more;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
2021-04-10 17:32:01 +00:00
|
|
|
case ID_NOT: {
|
|
|
|
bool more = parse_arg(VAL_ANY);
|
|
|
|
if (!more) {
|
|
|
|
gs.gen_result_true(ltype);
|
|
|
|
} else {
|
|
|
|
gs.gen_not(ltype);
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
2021-04-10 17:32:01 +00:00
|
|
|
return more;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
2021-04-10 17:32:01 +00:00
|
|
|
case ID_AND:
|
|
|
|
case ID_OR:
|
|
|
|
return parse_id_and_or(id, ltype);
|
|
|
|
case ID_IVAR: {
|
|
|
|
auto *hid = ts.istate->cmd_ivar;
|
|
|
|
return parse_call_command(
|
|
|
|
static_cast<command_impl *>(hid), id, ltype
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case ID_FVAR: {
|
|
|
|
auto *hid = ts.istate->cmd_fvar;
|
|
|
|
return parse_call_command(
|
|
|
|
static_cast<command_impl *>(hid), id, ltype
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case ID_SVAR: {
|
|
|
|
auto *hid = ts.istate->cmd_svar;
|
|
|
|
return parse_call_command(
|
|
|
|
static_cast<command_impl *>(hid), id, ltype
|
|
|
|
);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
/* unreachable */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generates a call to an unknown entity on the stack */
|
|
|
|
static bool parse_no_id(parser_state &ps, int term) {
|
|
|
|
std::uint32_t nargs = 0;
|
|
|
|
/* the entity is already on the stack, parse out any arguments to it */
|
|
|
|
while (ps.parse_arg(VAL_ANY)) {
|
|
|
|
++nargs;
|
|
|
|
}
|
|
|
|
ps.gs.gen_call(nargs);
|
|
|
|
return finish_statement(ps, false, term);
|
|
|
|
}
|
|
|
|
|
2021-04-10 22:13:10 +00:00
|
|
|
static bool parse_assign_var(
|
|
|
|
parser_state &ps, command_impl *id, ident &var, int ltype
|
|
|
|
) {
|
|
|
|
auto fmt = id->get_args();
|
|
|
|
std::uint32_t comtype = BC_INST_COM;
|
|
|
|
std::uint32_t nargs = 0;
|
|
|
|
bool more = true, got = false, rep = false;
|
|
|
|
for (auto it = fmt.begin(); it != fmt.end(); ++it) {
|
|
|
|
switch (*it) {
|
|
|
|
case '$':
|
|
|
|
ps.gs.gen_val_ident(var);
|
|
|
|
++nargs;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
ps.gs.gen_val_integer(nargs);
|
|
|
|
++nargs;
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
case 'V':
|
|
|
|
comtype = (*it == 'C') ? BC_INST_COM_C : BC_INST_COM_V;
|
|
|
|
if (more && !got) {
|
|
|
|
more = ps.parse_arg(VAL_ANY);
|
|
|
|
if (more) {
|
|
|
|
got = true;
|
|
|
|
++nargs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '1': case '2': case '3': case '4':
|
|
|
|
if (more && !got) {
|
|
|
|
int numrep = *it - '0' + 1;
|
|
|
|
it -= numrep;
|
|
|
|
rep = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
auto gotarg = parse_cmd_arg(ps, *it, got ? false : more, rep);
|
|
|
|
if (!got) {
|
|
|
|
more = gotarg;
|
|
|
|
}
|
|
|
|
if (gotarg) {
|
|
|
|
++nargs;
|
|
|
|
got = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ps.gs.gen_command_call(*id, comtype, ltype, nargs);
|
|
|
|
return more;
|
|
|
|
}
|
|
|
|
|
2021-04-10 17:32:01 +00:00
|
|
|
bool parser_state::parse_assign(
|
|
|
|
charbuf &idname, int ltype, int term, bool &noass
|
|
|
|
) {
|
|
|
|
/* lookahead */
|
|
|
|
switch (current(1)) {
|
|
|
|
/* the = can be followed by a bunch of stuff
|
|
|
|
* some of these result in empty assignments
|
|
|
|
*/
|
|
|
|
case '/': /* a comment maybe? */
|
|
|
|
if (current(2) != '/') {
|
|
|
|
/* not a comment */
|
|
|
|
noass = true;
|
|
|
|
return true;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
2021-04-10 17:32:01 +00:00
|
|
|
[[fallthrough]];
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
case '\0': {
|
|
|
|
/* skip = */
|
|
|
|
next_char();
|
|
|
|
/* we had a name on the left hand side */
|
|
|
|
if (!idname.empty()) {
|
|
|
|
idname.push_back('\0');
|
|
|
|
/* fetch an ident or make up a fresh one (unknown alias) */
|
|
|
|
ident &id = ts.istate->new_ident(
|
|
|
|
*ts.pstate, idname.str_term(), IDENT_FLAG_UNKNOWN
|
|
|
|
);
|
|
|
|
/* check what we're assigning */
|
|
|
|
switch (id.get_type()) {
|
|
|
|
case ident_type::ALIAS: {
|
|
|
|
/* alias assignment: parse out any one argument */
|
|
|
|
bool more = parse_arg(VAL_ANY);
|
2021-04-09 22:40:57 +00:00
|
|
|
if (!more) {
|
2021-04-10 17:32:01 +00:00
|
|
|
gs.gen_val_string();
|
2021-04-09 22:40:57 +00:00
|
|
|
}
|
2021-04-10 17:32:01 +00:00
|
|
|
gs.gen_assign_alias(id);
|
|
|
|
return finish_statement(*this, more, term);
|
|
|
|
}
|
|
|
|
case ident_type::IVAR: {
|
2021-04-08 22:41:55 +00:00
|
|
|
auto *hid = ts.istate->cmd_ivar;
|
2021-04-10 22:13:10 +00:00
|
|
|
bool more = parse_assign_var(
|
|
|
|
*this, static_cast<command_impl *>(hid), id, ltype
|
2021-04-08 21:33:00 +00:00
|
|
|
);
|
2021-04-10 17:32:01 +00:00
|
|
|
return finish_statement(*this, more, term);
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
2021-04-10 17:32:01 +00:00
|
|
|
case ident_type::FVAR: {
|
2021-04-08 22:41:55 +00:00
|
|
|
auto *hid = ts.istate->cmd_fvar;
|
2021-04-10 22:13:10 +00:00
|
|
|
bool more = parse_assign_var(
|
|
|
|
*this, static_cast<command_impl *>(hid), id, ltype
|
2021-04-08 21:33:00 +00:00
|
|
|
);
|
2021-04-10 17:32:01 +00:00
|
|
|
return finish_statement(*this, more, term);
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
2021-04-10 17:32:01 +00:00
|
|
|
case ident_type::SVAR: {
|
2021-04-08 22:41:55 +00:00
|
|
|
auto *hid = ts.istate->cmd_svar;
|
2021-04-10 22:13:10 +00:00
|
|
|
bool more = parse_assign_var(
|
|
|
|
*this, static_cast<command_impl *>(hid), id, ltype
|
2021-04-08 21:33:00 +00:00
|
|
|
);
|
2021-04-10 17:32:01 +00:00
|
|
|
return finish_statement(*this, more, term);
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
2021-04-10 17:32:01 +00:00
|
|
|
default:
|
|
|
|
break;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
2021-04-10 17:32:01 +00:00
|
|
|
gs.gen_val_string(idname.str_term());
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
2021-04-10 17:32:01 +00:00
|
|
|
/* unknown thing, make it the VM's problem */
|
|
|
|
bool more = parse_arg(VAL_ANY);
|
|
|
|
if (!more) {
|
|
|
|
gs.gen_val_string();
|
|
|
|
}
|
|
|
|
gs.gen_assign();
|
|
|
|
return finish_statement(*this, more, term);
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
2021-04-10 17:32:01 +00:00
|
|
|
/* not followed by any of these: not an assignment */
|
|
|
|
default:
|
|
|
|
noass = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void parser_state::parse_block(int ltype, int term) {
|
|
|
|
charbuf idname{gs.ts};
|
|
|
|
/* the main statement parse loop */
|
|
|
|
for (;;) {
|
|
|
|
/* first, skip any comments in the way and prepare the env */
|
|
|
|
skip_comments();
|
|
|
|
idname.clear();
|
|
|
|
std::size_t curline = current_line;
|
|
|
|
bool more = true;
|
|
|
|
/* parse the left hand side of the statement */
|
|
|
|
if (!parse_arg(VAL_WORD, &idname)) {
|
|
|
|
if (!finish_statement(*this, more, term)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
continue;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
2021-04-10 17:32:01 +00:00
|
|
|
skip_comments();
|
|
|
|
/* potentially an assignment */
|
|
|
|
if (current() == '=') {
|
|
|
|
bool noass = false;
|
|
|
|
if (!parse_assign(idname, ltype, term, noass)) {
|
|
|
|
/* terminated */
|
2021-04-08 21:33:00 +00:00
|
|
|
return;
|
2021-04-10 17:32:01 +00:00
|
|
|
}
|
|
|
|
if (!noass) {
|
|
|
|
/* was actually an assignment */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* we didn't get a name to look up: treat as unknown */
|
|
|
|
if (idname.empty()) {
|
|
|
|
if (!parse_no_id(*this, term)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
idname.push_back('\0');
|
|
|
|
auto idstr = idname.str_term();
|
|
|
|
ident *id = ts.pstate->get_ident(idstr);
|
|
|
|
if (!id) {
|
|
|
|
/* no such ident exists but the name is valid, which means
|
|
|
|
* it's a syntactically ok call, make it the VM's problem
|
|
|
|
*/
|
|
|
|
if (is_valid_name(idstr)) {
|
|
|
|
/* VAL_WORD does not codegen, put the name on the stack */
|
|
|
|
gs.gen_val_string(idstr);
|
|
|
|
if (!parse_no_id(*this, term)) {
|
2021-04-08 21:33:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-04-10 17:32:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* not a valid command name: treat like an expression */
|
|
|
|
switch (ltype) {
|
|
|
|
case VAL_ANY: {
|
|
|
|
auto end = idstr;
|
|
|
|
auto val = parse_int(idstr, &end);
|
|
|
|
if (!end.empty()) {
|
|
|
|
gs.gen_val_string(idstr);
|
|
|
|
} else {
|
|
|
|
gs.gen_val_integer(val);
|
|
|
|
}
|
|
|
|
break;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
2021-04-10 17:32:01 +00:00
|
|
|
default:
|
|
|
|
gs.gen_val(ltype, idname.str_term(), int(curline));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gs.gen_result();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* the ident exists; treat like a call according to its type */
|
|
|
|
more = parse_call_id(*id, ltype);
|
|
|
|
if (!finish_statement(*this, more, term)) {
|
|
|
|
return;
|
2021-04-08 21:33:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 01:00:11 +00:00
|
|
|
/* list parser public implementation */
|
2016-09-21 19:02:13 +00:00
|
|
|
|
2021-03-23 22:29:32 +00:00
|
|
|
LIBCUBESCRIPT_EXPORT bool list_parser::parse() {
|
2021-03-20 18:34:26 +00:00
|
|
|
skip_until_item();
|
|
|
|
if (p_input_beg == p_input_end) {
|
2021-03-18 22:53:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-20 18:34:26 +00:00
|
|
|
switch (*p_input_beg) {
|
2021-03-20 01:26:37 +00:00
|
|
|
case '"': {
|
2021-03-20 18:34:26 +00:00
|
|
|
char const *qi = p_input_beg;
|
2021-03-23 22:29:32 +00:00
|
|
|
p_input_beg = parse_string(*p_state, get_input());
|
2021-03-30 23:48:56 +00:00
|
|
|
p_qbeg = qi;
|
|
|
|
p_qend = p_input_beg;
|
|
|
|
p_ibeg = p_qbeg + 1;
|
|
|
|
p_iend = p_qend - 1;
|
2021-03-18 22:53:16 +00:00
|
|
|
break;
|
2021-03-20 01:26:37 +00:00
|
|
|
}
|
2021-03-18 22:53:16 +00:00
|
|
|
case '(':
|
|
|
|
case '[': {
|
2021-03-20 18:34:26 +00:00
|
|
|
char btype = *p_input_beg;
|
2021-03-18 22:53:16 +00:00
|
|
|
int brak = 1;
|
2021-03-20 18:34:26 +00:00
|
|
|
char const *ibeg = p_input_beg++;
|
2021-03-18 22:53:16 +00:00
|
|
|
for (;;) {
|
2021-03-20 01:26:37 +00:00
|
|
|
std::string_view chrs{"\"/;()[]"};
|
2021-03-20 18:34:26 +00:00
|
|
|
p_input_beg = std::find_first_of(
|
|
|
|
p_input_beg, p_input_end, chrs.begin(), chrs.end()
|
2021-03-18 22:53:16 +00:00
|
|
|
);
|
2021-03-20 18:34:26 +00:00
|
|
|
if (p_input_beg == p_input_end) {
|
2021-03-18 22:53:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-03-20 18:34:26 +00:00
|
|
|
char c = *p_input_beg++;
|
2021-03-18 22:53:16 +00:00
|
|
|
switch (c) {
|
|
|
|
case '"':
|
2021-03-20 01:26:37 +00:00
|
|
|
/* the quote is needed in str parsing */
|
2021-03-20 18:34:26 +00:00
|
|
|
--p_input_beg;
|
2021-03-23 22:29:32 +00:00
|
|
|
p_input_beg = parse_string(*p_state, get_input());
|
2021-03-18 22:53:16 +00:00
|
|
|
break;
|
|
|
|
case '/':
|
2021-03-20 01:26:37 +00:00
|
|
|
if (
|
2021-03-20 18:34:26 +00:00
|
|
|
(p_input_beg != p_input_end) &&
|
|
|
|
(*p_input_beg == '/')
|
2021-03-20 01:26:37 +00:00
|
|
|
) {
|
2021-03-20 18:34:26 +00:00
|
|
|
p_input_beg = std::find(
|
|
|
|
p_input_beg, p_input_end, '\n'
|
2021-03-20 01:26:37 +00:00
|
|
|
);
|
2021-03-18 22:53:16 +00: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 19:02:13 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-18 22:53:16 +00:00
|
|
|
endblock:
|
2021-03-30 23:48:56 +00:00
|
|
|
p_ibeg = ibeg + 1;
|
|
|
|
p_iend = p_input_beg - 1;
|
|
|
|
p_qbeg = ibeg;
|
|
|
|
p_qend = p_input_beg;
|
2021-03-18 22:53:16 +00:00
|
|
|
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: {
|
2021-03-23 22:29:32 +00:00
|
|
|
char const *e = parse_word(*p_state, get_input());
|
2021-03-30 23:48:56 +00:00
|
|
|
p_ibeg = p_qbeg = p_input_beg;
|
|
|
|
p_iend = p_qend = e;
|
2021-03-20 18:34:26 +00:00
|
|
|
p_input_beg = e;
|
2021-03-18 22:53:16 +00:00
|
|
|
break;
|
2016-09-21 19:02:13 +00:00
|
|
|
}
|
2021-03-18 22:53:16 +00:00
|
|
|
}
|
2021-03-20 18:34:26 +00:00
|
|
|
skip_until_item();
|
|
|
|
if ((p_input_beg != p_input_end) && (*p_input_beg == ';')) {
|
|
|
|
++p_input_beg;
|
2021-03-18 22:53:16 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-23 22:29:32 +00:00
|
|
|
LIBCUBESCRIPT_EXPORT std::size_t list_parser::count() {
|
2021-03-18 22:53:16 +00:00
|
|
|
size_t ret = 0;
|
2021-03-20 18:34:26 +00:00
|
|
|
while (parse()) {
|
2021-03-18 22:53:16 +00:00
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-23 22:29:32 +00:00
|
|
|
LIBCUBESCRIPT_EXPORT string_ref list_parser::get_item() const {
|
2021-03-30 23:48:56 +00:00
|
|
|
if ((p_qbeg != p_qend) && (*p_qbeg == '"')) {
|
2021-03-26 01:59:42 +00:00
|
|
|
charbuf buf{*p_state};
|
2021-03-30 23:48:56 +00:00
|
|
|
unescape_string(std::back_inserter(buf), get_raw_item());
|
2021-03-23 22:29:32 +00:00
|
|
|
return string_ref{*p_state, buf.str()};
|
2021-03-18 22:53:16 +00:00
|
|
|
}
|
2021-03-30 23:48:56 +00:00
|
|
|
return string_ref{*p_state, get_raw_item()};
|
2021-03-18 22:53:16 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 22:29:32 +00:00
|
|
|
LIBCUBESCRIPT_EXPORT void list_parser::skip_until_item() {
|
2021-03-18 22:53:16 +00:00
|
|
|
for (;;) {
|
2021-03-20 18:34:26 +00:00
|
|
|
while (p_input_beg != p_input_end) {
|
|
|
|
char c = *p_input_beg;
|
2021-03-18 22:53:16 +00:00
|
|
|
if ((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n')) {
|
2021-03-20 18:34:26 +00:00
|
|
|
++p_input_beg;
|
2021-03-18 22:53:16 +00:00
|
|
|
} else {
|
2016-09-21 19:02:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-03-20 18:34:26 +00:00
|
|
|
if ((p_input_end - p_input_beg) < 2) {
|
2021-03-20 01:26:37 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-03-20 18:34:26 +00:00
|
|
|
if ((p_input_beg[0] != '/') || (p_input_beg[1]) != '/') {
|
2021-03-18 22:53:16 +00:00
|
|
|
break;
|
2016-10-24 00:33:04 +00:00
|
|
|
}
|
2021-03-20 18:34:26 +00:00
|
|
|
p_input_beg = std::find(p_input_beg, p_input_end, '\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-23 22:32:25 +00:00
|
|
|
} /* namespace cubescript */
|