From 74099cacab8979b8deddf3614787a1913f3647e9 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Fri, 9 Aug 2024 13:57:42 -0400 Subject: [PATCH] xo-reader: uncopy: with + exprstate.illegal_input_error() --- include/xo/reader/exprstate.hpp | 7 ++++++ src/reader/define_xs.cpp | 27 +++++++------------- src/reader/exprstate.cpp | 34 ++++++++++++++----------- src/reader/paren_xs.cpp | 44 +++++++++++---------------------- src/reader/progress_xs.cpp | 30 ++++++++-------------- 5 files changed, 61 insertions(+), 81 deletions(-) diff --git a/include/xo/reader/exprstate.hpp b/include/xo/reader/exprstate.hpp index 467d2eab..c775a5b9 100644 --- a/include/xo/reader/exprstate.hpp +++ b/include/xo/reader/exprstate.hpp @@ -156,6 +156,13 @@ namespace xo { exprstatestack * p_stack, rp * 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: /** explicit subtype: identifies derived class **/ exprstatetype exs_type_; diff --git a/src/reader/define_xs.cpp b/src/reader/define_xs.cpp index d7bccf82..95fde120 100644 --- a/src/reader/define_xs.cpp +++ b/src/reader/define_xs.cpp @@ -382,7 +382,7 @@ namespace xo { } void - define_xs::on_singleassign_token(const token_type & /*tk*/, + define_xs::on_singleassign_token(const token_type & tk, exprstatestack * p_stack) { constexpr bool c_debug_flag = true; @@ -390,11 +390,8 @@ namespace xo { constexpr const char * self_name = "exprstate::on_singleassign"; - if (!this->admits_singleassign()) - { - throw std::runtime_error(tostr(self_name, - ": unexpected equals for parsing state", - xtag("state", *this))); + if (!this->admits_singleassign()) { + this->illegal_input_error(self_name, tk); } if ((this->defxs_type_ == defexprstatetype::def_1) @@ -409,7 +406,7 @@ namespace xo { } void - define_xs::on_leftparen_token(const token_type & /*tk*/, + define_xs::on_leftparen_token(const token_type & tk, exprstatestack * /*p_stack*/, rp * /*p_emit_expr*/) { @@ -420,16 +417,14 @@ namespace xo { if (!this->admits_leftparen()) { - throw std::runtime_error(tostr(self_name, - ": unexpected leftparen '(' for parsing state", - xtag("state", *this))); + this->illegal_input_error(self_name, tk); } assert(false); /* inserting this during refactor...? */ } void - define_xs::on_rightparen_token(const token_type & /*tk*/, + define_xs::on_rightparen_token(const token_type & tk, exprstatestack * /*p_stack*/, rp * /*p_emit_expr*/) { @@ -440,16 +435,14 @@ namespace xo { if (!this->admits_rightparen()) { - throw std::runtime_error(tostr(self_name, - ": unexpected rightparen ')' for parsing state", - xtag("state", *this))); + this->illegal_input_error(self_name, tk); } assert(false); /* inserting this during refactor..? */ } void - define_xs::on_f64_token(const token_type & /*tk*/, + define_xs::on_f64_token(const token_type & tk, exprstatestack * /*p_stack*/, rp * /*p_emit_expr*/) { @@ -460,9 +453,7 @@ namespace xo { if (!this->admits_f64()) { - throw std::runtime_error(tostr(self_name, - ": unexpected floating-point literal for parsing state", - xtag("state", *this))); + this->illegal_input_error(self_name, tk); } assert(false); diff --git a/src/reader/exprstate.cpp b/src/reader/exprstate.cpp index 5227135c..d6242730 100644 --- a/src/reader/exprstate.cpp +++ b/src/reader/exprstate.cpp @@ -320,19 +320,18 @@ namespace xo { } void - exprstate::on_def_token(const token_type & /*tk*/, - exprstatestack * p_stack) { + exprstate::on_def_token(const token_type & tk, + exprstatestack * p_stack) + { constexpr bool c_debug_flag = true; 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 */ if (!this->admits_definition()) { - throw std::runtime_error(tostr(self_name, - ": unexpected keyword 'def' for parsing state", - xtag("state", *this))); + this->illegal_input_error(c_self_name, tk); } p_stack->push_exprstate(define_xs::def_0()); @@ -358,20 +357,16 @@ namespace xo { 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()) { - throw std::runtime_error - (tostr(self_name, - ": unexpected symbol-token for parsing state", - xtag("symbol", tk), - xtag("state", *this))); + this->illegal_input_error(c_self_name, tk); } switch (this->exs_type_) { case exprstatetype::expect_toplevel_expression_sequence: throw std::runtime_error - (tostr(self_name, + (tostr(c_self_name, ": unexpected symbol-token at top-level", " (expecting decl|def)", xtag("symbol", tk))); @@ -442,7 +437,7 @@ namespace xo { if (!td) { throw std::runtime_error - (tostr(self_name, + (tostr(c_self_name, ": unknown type name", " (expecting f64|f32|i16|i32|i64)", xtag("typename", tk.text()))); @@ -835,6 +830,17 @@ namespace xo { 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 ----- exprstate & diff --git a/src/reader/paren_xs.cpp b/src/reader/paren_xs.cpp index 20d7573d..6d464178 100644 --- a/src/reader/paren_xs.cpp +++ b/src/reader/paren_xs.cpp @@ -73,14 +73,12 @@ namespace xo { } void - paren_xs::on_def_token(const token_type & /*tk*/, + paren_xs::on_def_token(const token_type & tk, exprstatestack * /*p_stack*/) { constexpr const char * c_self_name = "paren_xs::on_def"; - throw std::runtime_error(tostr(c_self_name, - ": unexpected keyword 'def' for parsing state", - xtag("state", *this))); + this->illegal_input_error(c_self_name, tk); } void @@ -110,66 +108,56 @@ namespace xo { } void - paren_xs::on_colon_token(const token_type & /*tk*/, + paren_xs::on_colon_token(const token_type & tk, exprstatestack * /*p_stack*/) { constexpr const char * c_self_name = "paren_xs::on_colon"; - throw std::runtime_error(tostr(c_self_name, - ": unexpected colon for parsing state", - xtag("state", *this))); + this->illegal_input_error(c_self_name, tk); } void - paren_xs::on_semicolon_token(const token_type & /*tk*/, + paren_xs::on_semicolon_token(const token_type & tk, exprstatestack * /*p_stack*/, rp * /*p_emit_expr*/) { constexpr const char * c_self_name = "paren_xs::on_semicolon"; - throw std::runtime_error(tostr(c_self_name, - ": unexpected semicolon for parsing state", - xtag("state", *this))); + this->illegal_input_error(c_self_name, tk); } void - paren_xs::on_singleassign_token(const token_type & /*tk*/, + paren_xs::on_singleassign_token(const token_type & tk, exprstatestack * /*p_stack*/) { constexpr const char * c_self_name = "paren_xs::on_singleassign"; - throw std::runtime_error(tostr(c_self_name, - ": unexpected equals for parsing state", - xtag("state", *this))); + this->illegal_input_error(c_self_name, tk); } void - paren_xs::on_leftparen_token(const token_type & /*tk*/, + paren_xs::on_leftparen_token(const token_type & tk, exprstatestack * /*p_stack*/, rp * /*p_emit_expr*/) { constexpr const char * c_self_name = "paren_xs::on_leftparen"; - throw std::runtime_error(tostr(c_self_name, - ": unexpected leftparen '(' for parsing state", - xtag("state", *this))); + this->illegal_input_error(c_self_name, tk); } void - paren_xs::on_rightparen_token(const token_type & /*tk*/, + paren_xs::on_rightparen_token(const token_type & tk, exprstatestack * p_stack, rp * p_emit_expr) { constexpr bool c_debug_flag = true; 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()) { - throw std::runtime_error(tostr(self_name, - ": unexpected rightparen ')' for parsing state", - xtag("state", *this))); + this->illegal_input_error(c_self_name, tk); } if (this->parenxs_type_ == parenexprstatetype::lparen_1) { @@ -182,7 +170,7 @@ namespace xo { } void - paren_xs::on_f64_token(const token_type & /*tk*/, + paren_xs::on_f64_token(const token_type & tk, exprstatestack * /*p_stack*/, rp * /*p_emit_expr*/) { @@ -193,9 +181,7 @@ namespace xo { if (!this->admits_f64()) { - throw std::runtime_error(tostr(c_self_name, - ": unexpected floating-point literal for parsing state", - xtag("state", *this))); + this->illegal_input_error(c_self_name, tk); } assert(false); diff --git a/src/reader/progress_xs.cpp b/src/reader/progress_xs.cpp index 19df5489..a355229e 100644 --- a/src/reader/progress_xs.cpp +++ b/src/reader/progress_xs.cpp @@ -43,15 +43,13 @@ namespace xo { progress_xs::admits_f64() const { return false; } void - progress_xs::on_def_token(const token_type & /*tk*/, + progress_xs::on_def_token(const token_type & tk, exprstatestack * /*p_stack*/) { constexpr const char * self_name = "progress_xs::on_def"; /* nothing here - admits_definition unconditionally false */ - throw std::runtime_error(tostr(self_name, - ": unexpected keyword 'def' for parsing state", - xtag("state", *this))); + this->illegal_input_error(self_name, tk) ; } void @@ -84,14 +82,12 @@ namespace xo { } void - progress_xs::on_colon_token(const token_type & /*tk*/, + progress_xs::on_colon_token(const token_type & tk, exprstatestack * /*p_stack*/) { constexpr const char * self_name = "progress_xs::on_colon"; - throw std::runtime_error(tostr(self_name, - ": unexpected colon for parsing state", - xtag("state", *this))); + this->illegal_input_error(self_name, tk); } void @@ -128,18 +124,16 @@ namespace xo { } void - progress_xs::on_singleassign_token(const token_type & /*tk*/, + progress_xs::on_singleassign_token(const token_type & tk, exprstatestack * /*p_stack*/) { constexpr const char * self_name = "progress_xs::on_singleassign"; - throw std::runtime_error(tostr(self_name, - ": unexpected equals for parsing state", - xtag("state", *this))); + this->illegal_input_error(self_name, tk); } void - progress_xs::on_leftparen_token(const token_type & /*tk*/, + progress_xs::on_leftparen_token(const token_type & tk, exprstatestack * /*p_stack*/, rp * /*p_emit_expr*/) { @@ -148,9 +142,7 @@ namespace xo { constexpr const char * self_name = "exprstate::on_leftparen"; - throw std::runtime_error(tostr(self_name, - ": unexpected leftparen '(' for parsing state", - xtag("state", *this))); + this->illegal_input_error(self_name, tk); } void @@ -194,7 +186,7 @@ namespace xo { } void - progress_xs::on_f64_token(const token_type & /*tk*/, + progress_xs::on_f64_token(const token_type & tk, exprstatestack * /*p_stack*/, rp * /*p_emit_expr*/) { @@ -203,9 +195,7 @@ namespace xo { constexpr const char * self_name = "progress_xs::on_f64"; - throw std::runtime_error(tostr(self_name, - ": unexpected floating-point literal for parsing state", - xtag("state", *this))); + this->illegal_input_error(self_name, tk); } void