xo-reader: uncopy: with + exprstate.illegal_input_error()

This commit is contained in:
Roland Conybeare 2024-08-09 13:57:42 -04:00
commit 74099cacab
5 changed files with 61 additions and 81 deletions

View file

@ -156,6 +156,13 @@ namespace xo {
exprstatestack * p_stack, exprstatestack * p_stack,
rp<Expression> * p_emit_expr); rp<Expression> * p_emit_expr);
protected:
/** throw exception when next token is inconsistent with
* parsing state
**/
void illegal_input_error(const char * self_name,
const token_type & tk) const;
protected: protected:
/** explicit subtype: identifies derived class **/ /** explicit subtype: identifies derived class **/
exprstatetype exs_type_; exprstatetype exs_type_;

View file

@ -382,7 +382,7 @@ namespace xo {
} }
void void
define_xs::on_singleassign_token(const token_type & /*tk*/, define_xs::on_singleassign_token(const token_type & tk,
exprstatestack * p_stack) exprstatestack * p_stack)
{ {
constexpr bool c_debug_flag = true; constexpr bool c_debug_flag = true;
@ -390,11 +390,8 @@ namespace xo {
constexpr const char * self_name = "exprstate::on_singleassign"; constexpr const char * self_name = "exprstate::on_singleassign";
if (!this->admits_singleassign()) if (!this->admits_singleassign()) {
{ this->illegal_input_error(self_name, tk);
throw std::runtime_error(tostr(self_name,
": unexpected equals for parsing state",
xtag("state", *this)));
} }
if ((this->defxs_type_ == defexprstatetype::def_1) if ((this->defxs_type_ == defexprstatetype::def_1)
@ -409,7 +406,7 @@ namespace xo {
} }
void void
define_xs::on_leftparen_token(const token_type & /*tk*/, define_xs::on_leftparen_token(const token_type & tk,
exprstatestack * /*p_stack*/, exprstatestack * /*p_stack*/,
rp<Expression> * /*p_emit_expr*/) rp<Expression> * /*p_emit_expr*/)
{ {
@ -420,16 +417,14 @@ namespace xo {
if (!this->admits_leftparen()) if (!this->admits_leftparen())
{ {
throw std::runtime_error(tostr(self_name, this->illegal_input_error(self_name, tk);
": unexpected leftparen '(' for parsing state",
xtag("state", *this)));
} }
assert(false); /* inserting this during refactor...? */ assert(false); /* inserting this during refactor...? */
} }
void void
define_xs::on_rightparen_token(const token_type & /*tk*/, define_xs::on_rightparen_token(const token_type & tk,
exprstatestack * /*p_stack*/, exprstatestack * /*p_stack*/,
rp<Expression> * /*p_emit_expr*/) rp<Expression> * /*p_emit_expr*/)
{ {
@ -440,16 +435,14 @@ namespace xo {
if (!this->admits_rightparen()) if (!this->admits_rightparen())
{ {
throw std::runtime_error(tostr(self_name, this->illegal_input_error(self_name, tk);
": unexpected rightparen ')' for parsing state",
xtag("state", *this)));
} }
assert(false); /* inserting this during refactor..? */ assert(false); /* inserting this during refactor..? */
} }
void void
define_xs::on_f64_token(const token_type & /*tk*/, define_xs::on_f64_token(const token_type & tk,
exprstatestack * /*p_stack*/, exprstatestack * /*p_stack*/,
rp<Expression> * /*p_emit_expr*/) rp<Expression> * /*p_emit_expr*/)
{ {
@ -460,9 +453,7 @@ namespace xo {
if (!this->admits_f64()) if (!this->admits_f64())
{ {
throw std::runtime_error(tostr(self_name, this->illegal_input_error(self_name, tk);
": unexpected floating-point literal for parsing state",
xtag("state", *this)));
} }
assert(false); assert(false);

View file

@ -320,19 +320,18 @@ namespace xo {
} }
void void
exprstate::on_def_token(const token_type & /*tk*/, exprstate::on_def_token(const token_type & tk,
exprstatestack * p_stack) { exprstatestack * p_stack)
{
constexpr bool c_debug_flag = true; constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag)); scope log(XO_DEBUG(c_debug_flag));
constexpr const char * self_name = "exprstate::on_def"; constexpr const char * c_self_name = "exprstate::on_def_token";
/* lots of illegal states */ /* lots of illegal states */
if (!this->admits_definition()) if (!this->admits_definition())
{ {
throw std::runtime_error(tostr(self_name, this->illegal_input_error(c_self_name, tk);
": unexpected keyword 'def' for parsing state",
xtag("state", *this)));
} }
p_stack->push_exprstate(define_xs::def_0()); p_stack->push_exprstate(define_xs::def_0());
@ -358,20 +357,16 @@ namespace xo {
log && log(xtag("exstype", p_stack->top_exprstate().exs_type())); log && log(xtag("exstype", p_stack->top_exprstate().exs_type()));
constexpr const char * self_name = "exprstate::on_symbol"; constexpr const char * c_self_name = "exprstate::on_symbol_token";
if (!this->admits_symbol()) { if (!this->admits_symbol()) {
throw std::runtime_error this->illegal_input_error(c_self_name, tk);
(tostr(self_name,
": unexpected symbol-token for parsing state",
xtag("symbol", tk),
xtag("state", *this)));
} }
switch (this->exs_type_) { switch (this->exs_type_) {
case exprstatetype::expect_toplevel_expression_sequence: case exprstatetype::expect_toplevel_expression_sequence:
throw std::runtime_error throw std::runtime_error
(tostr(self_name, (tostr(c_self_name,
": unexpected symbol-token at top-level", ": unexpected symbol-token at top-level",
" (expecting decl|def)", " (expecting decl|def)",
xtag("symbol", tk))); xtag("symbol", tk)));
@ -442,7 +437,7 @@ namespace xo {
if (!td) { if (!td) {
throw std::runtime_error throw std::runtime_error
(tostr(self_name, (tostr(c_self_name,
": unknown type name", ": unknown type name",
" (expecting f64|f32|i16|i32|i64)", " (expecting f64|f32|i16|i32|i64)",
xtag("typename", tk.text()))); xtag("typename", tk.text())));
@ -835,6 +830,17 @@ namespace xo {
os << ">"; os << ">";
} }
void
exprstate::illegal_input_error(const char * self_name,
const token_type & tk) const
{
throw std::runtime_error
(tostr(self_name,
": unexpected input token for parsing state",
xtag("token", tk),
xtag("state", *this)));
}
// ----- exprstatestack ----- // ----- exprstatestack -----
exprstate & exprstate &

View file

@ -73,14 +73,12 @@ namespace xo {
} }
void void
paren_xs::on_def_token(const token_type & /*tk*/, paren_xs::on_def_token(const token_type & tk,
exprstatestack * /*p_stack*/) exprstatestack * /*p_stack*/)
{ {
constexpr const char * c_self_name = "paren_xs::on_def"; constexpr const char * c_self_name = "paren_xs::on_def";
throw std::runtime_error(tostr(c_self_name, this->illegal_input_error(c_self_name, tk);
": unexpected keyword 'def' for parsing state",
xtag("state", *this)));
} }
void void
@ -110,66 +108,56 @@ namespace xo {
} }
void void
paren_xs::on_colon_token(const token_type & /*tk*/, paren_xs::on_colon_token(const token_type & tk,
exprstatestack * /*p_stack*/) exprstatestack * /*p_stack*/)
{ {
constexpr const char * c_self_name = "paren_xs::on_colon"; constexpr const char * c_self_name = "paren_xs::on_colon";
throw std::runtime_error(tostr(c_self_name, this->illegal_input_error(c_self_name, tk);
": unexpected colon for parsing state",
xtag("state", *this)));
} }
void void
paren_xs::on_semicolon_token(const token_type & /*tk*/, paren_xs::on_semicolon_token(const token_type & tk,
exprstatestack * /*p_stack*/, exprstatestack * /*p_stack*/,
rp<Expression> * /*p_emit_expr*/) rp<Expression> * /*p_emit_expr*/)
{ {
constexpr const char * c_self_name = "paren_xs::on_semicolon"; constexpr const char * c_self_name = "paren_xs::on_semicolon";
throw std::runtime_error(tostr(c_self_name, this->illegal_input_error(c_self_name, tk);
": unexpected semicolon for parsing state",
xtag("state", *this)));
} }
void void
paren_xs::on_singleassign_token(const token_type & /*tk*/, paren_xs::on_singleassign_token(const token_type & tk,
exprstatestack * /*p_stack*/) exprstatestack * /*p_stack*/)
{ {
constexpr const char * c_self_name = "paren_xs::on_singleassign"; constexpr const char * c_self_name = "paren_xs::on_singleassign";
throw std::runtime_error(tostr(c_self_name, this->illegal_input_error(c_self_name, tk);
": unexpected equals for parsing state",
xtag("state", *this)));
} }
void void
paren_xs::on_leftparen_token(const token_type & /*tk*/, paren_xs::on_leftparen_token(const token_type & tk,
exprstatestack * /*p_stack*/, exprstatestack * /*p_stack*/,
rp<Expression> * /*p_emit_expr*/) rp<Expression> * /*p_emit_expr*/)
{ {
constexpr const char * c_self_name = "paren_xs::on_leftparen"; constexpr const char * c_self_name = "paren_xs::on_leftparen";
throw std::runtime_error(tostr(c_self_name, this->illegal_input_error(c_self_name, tk);
": unexpected leftparen '(' for parsing state",
xtag("state", *this)));
} }
void void
paren_xs::on_rightparen_token(const token_type & /*tk*/, paren_xs::on_rightparen_token(const token_type & tk,
exprstatestack * p_stack, exprstatestack * p_stack,
rp<Expression> * p_emit_expr) rp<Expression> * p_emit_expr)
{ {
constexpr bool c_debug_flag = true; constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag)); scope log(XO_DEBUG(c_debug_flag));
constexpr const char * self_name = "paren_xs::on_rightparen"; constexpr const char * c_self_name = "paren_xs::on_rightparen";
if (!this->admits_rightparen()) if (!this->admits_rightparen())
{ {
throw std::runtime_error(tostr(self_name, this->illegal_input_error(c_self_name, tk);
": unexpected rightparen ')' for parsing state",
xtag("state", *this)));
} }
if (this->parenxs_type_ == parenexprstatetype::lparen_1) { if (this->parenxs_type_ == parenexprstatetype::lparen_1) {
@ -182,7 +170,7 @@ namespace xo {
} }
void void
paren_xs::on_f64_token(const token_type & /*tk*/, paren_xs::on_f64_token(const token_type & tk,
exprstatestack * /*p_stack*/, exprstatestack * /*p_stack*/,
rp<Expression> * /*p_emit_expr*/) rp<Expression> * /*p_emit_expr*/)
{ {
@ -193,9 +181,7 @@ namespace xo {
if (!this->admits_f64()) if (!this->admits_f64())
{ {
throw std::runtime_error(tostr(c_self_name, this->illegal_input_error(c_self_name, tk);
": unexpected floating-point literal for parsing state",
xtag("state", *this)));
} }
assert(false); assert(false);

View file

@ -43,15 +43,13 @@ namespace xo {
progress_xs::admits_f64() const { return false; } progress_xs::admits_f64() const { return false; }
void void
progress_xs::on_def_token(const token_type & /*tk*/, progress_xs::on_def_token(const token_type & tk,
exprstatestack * /*p_stack*/) exprstatestack * /*p_stack*/)
{ {
constexpr const char * self_name = "progress_xs::on_def"; constexpr const char * self_name = "progress_xs::on_def";
/* nothing here - admits_definition unconditionally false */ /* nothing here - admits_definition unconditionally false */
throw std::runtime_error(tostr(self_name, this->illegal_input_error(self_name, tk) ;
": unexpected keyword 'def' for parsing state",
xtag("state", *this)));
} }
void void
@ -84,14 +82,12 @@ namespace xo {
} }
void void
progress_xs::on_colon_token(const token_type & /*tk*/, progress_xs::on_colon_token(const token_type & tk,
exprstatestack * /*p_stack*/) exprstatestack * /*p_stack*/)
{ {
constexpr const char * self_name = "progress_xs::on_colon"; constexpr const char * self_name = "progress_xs::on_colon";
throw std::runtime_error(tostr(self_name, this->illegal_input_error(self_name, tk);
": unexpected colon for parsing state",
xtag("state", *this)));
} }
void void
@ -128,18 +124,16 @@ namespace xo {
} }
void void
progress_xs::on_singleassign_token(const token_type & /*tk*/, progress_xs::on_singleassign_token(const token_type & tk,
exprstatestack * /*p_stack*/) exprstatestack * /*p_stack*/)
{ {
constexpr const char * self_name = "progress_xs::on_singleassign"; constexpr const char * self_name = "progress_xs::on_singleassign";
throw std::runtime_error(tostr(self_name, this->illegal_input_error(self_name, tk);
": unexpected equals for parsing state",
xtag("state", *this)));
} }
void void
progress_xs::on_leftparen_token(const token_type & /*tk*/, progress_xs::on_leftparen_token(const token_type & tk,
exprstatestack * /*p_stack*/, exprstatestack * /*p_stack*/,
rp<Expression> * /*p_emit_expr*/) rp<Expression> * /*p_emit_expr*/)
{ {
@ -148,9 +142,7 @@ namespace xo {
constexpr const char * self_name = "exprstate::on_leftparen"; constexpr const char * self_name = "exprstate::on_leftparen";
throw std::runtime_error(tostr(self_name, this->illegal_input_error(self_name, tk);
": unexpected leftparen '(' for parsing state",
xtag("state", *this)));
} }
void void
@ -194,7 +186,7 @@ namespace xo {
} }
void void
progress_xs::on_f64_token(const token_type & /*tk*/, progress_xs::on_f64_token(const token_type & tk,
exprstatestack * /*p_stack*/, exprstatestack * /*p_stack*/,
rp<Expression> * /*p_emit_expr*/) rp<Expression> * /*p_emit_expr*/)
{ {
@ -203,9 +195,7 @@ namespace xo {
constexpr const char * self_name = "progress_xs::on_f64"; constexpr const char * self_name = "progress_xs::on_f64";
throw std::runtime_error(tostr(self_name, this->illegal_input_error(self_name, tk);
": unexpected floating-point literal for parsing state",
xtag("state", *this)));
} }
void void