trigger actual errors from parser

master
Daniel Kolesa 2016-09-10 15:01:49 +02:00
parent 879737ce83
commit 323c84c3d6
3 changed files with 20 additions and 24 deletions

View File

@ -8,6 +8,15 @@
namespace cscript {
template<typename ...A>
static void cs_error_line(
CsState &cs, ostd::ConstCharRange p, ostd::ConstCharRange fmt, A &&...args
) {
ostd::Array<char, 256> buf;
auto rfmt = cs_debug_line(p, fmt, ostd::CharRange(buf.data(), buf.size()));
cs.error(rfmt, ostd::forward<A>(args)...);
}
char *cs_dup_ostr(ostd::ConstCharRange s) {
char *r = new char[s.size() + 1];
if (s.data()) {
@ -788,9 +797,8 @@ static void compileblockmain(GenState &gs, int wordtype, int prevargs) {
char c = gs.next_char();
switch (c) {
case '\0':
cs_debug_code_line(gs.cs, line, "missing \"]\"");
gs.source--;
goto done;
cs_error_line(gs.cs, line, "missing \"]\"");
return;
case '\"':
gs.source = parsestring(gs.source);
if (gs.current() == '\"') {
@ -817,7 +825,8 @@ static void compileblockmain(GenState &gs, int wordtype, int prevargs) {
if (brak > level) {
continue;
} else if (brak < level) {
cs_debug_code_line(gs.cs, line, "too many @s");
cs_error_line(gs.cs, line, "too many @s");
return;
}
if (!concs && prevargs >= MaxResults) {
gs.code.push(CsCodeEnter);
@ -843,7 +852,6 @@ static void compileblockmain(GenState &gs, int wordtype, int prevargs) {
}
}
}
done:
if (gs.source - 1 > start) {
if (!concs) {
switch (wordtype) {
@ -1679,9 +1687,10 @@ endstatement:
switch (c) {
case '\0':
if (c != brak) {
cs_debug_code_line(
cs_error_line(
gs.cs, line, "missing \"%c\"", char(brak)
);
return;
}
gs.source--;
return;
@ -1690,8 +1699,8 @@ endstatement:
if (c == brak) {
return;
}
cs_debug_code_line(gs.cs, line, "unexpected \"%c\"", c);
break;
cs_error_line(gs.cs, line, "unexpected \"%c\"", c);
return;
case '/':
if (gs.current() == '/') {
gs.source += strcspn(gs.source, "\n\0");

View File

@ -108,6 +108,9 @@ CsStackState cs_save_stack(CsState &cs) {
for (CsIdentLink *l = cs.p_callstack; l != &cs.noalias; l = l->next) {
total++;
}
if (!total) {
return CsStackState(nullptr, true);
}
CsStackStateNode *st =
new CsStackStateNode[ostd::min(total, dalias->get_value())];
CsStackStateNode *ret = st, *nd = st;

View File

@ -115,22 +115,6 @@ void cs_debug_code(CsState &cs, ostd::ConstCharRange fmt, A &&...args) {
cscript::util::print_stack(cs.get_err().iter(), st);
}
template<typename ...A>
void cs_debug_code_line(
CsState &cs, ostd::ConstCharRange p, ostd::ConstCharRange fmt, A &&...args
) {
if (cs.nodebug) {
return;
}
ostd::Array<char, 256> buf;
cs.get_err().writefln(
cs_debug_line(p, fmt, ostd::CharRange(buf.data(), buf.size())),
ostd::forward<A>(args)...
);
auto st = cs_save_stack(cs);
cscript::util::print_stack(cs.get_err().iter(), st);
}
struct GenState {
CsState &cs;
CsVector<ostd::Uint32> code;