xo-reader: misc bugfixes + logging [wip, utests not passing]
This commit is contained in:
parent
22d4c6c601
commit
ac4ee7d6b8
14 changed files with 175 additions and 17 deletions
|
|
@ -56,6 +56,11 @@ namespace xo {
|
|||
n_defexprstatetype,
|
||||
};
|
||||
|
||||
extern const char * defexprstatetype_descr(defexprstatetype x);
|
||||
|
||||
std::ostream &
|
||||
operator<<(std::ostream & os, defexprstatetype x);
|
||||
|
||||
/** @class define_xs
|
||||
* @brief state to provide parsing of a define-expression
|
||||
**/
|
||||
|
|
|
|||
|
|
@ -65,7 +65,10 @@ namespace xo {
|
|||
|
||||
inline std::ostream &
|
||||
operator<< (std::ostream & os, const envframestack * x) {
|
||||
x->print(os);
|
||||
if (x)
|
||||
x->print(os);
|
||||
else
|
||||
os << "nullptr";
|
||||
return os;
|
||||
}
|
||||
} /*namespace scm*/
|
||||
|
|
|
|||
|
|
@ -189,6 +189,15 @@ namespace xo {
|
|||
x.print(os);
|
||||
return os;
|
||||
}
|
||||
|
||||
inline std::ostream &
|
||||
operator<< (std::ostream & os, const exprstate * x) {
|
||||
if (x)
|
||||
x->print(os);
|
||||
else
|
||||
os << "nullptr";
|
||||
return os;
|
||||
};
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "exprstate.hpp"
|
||||
#include "xo/indentlog/print/vector.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
|
|
|
|||
|
|
@ -19,6 +19,15 @@ namespace xo {
|
|||
n_parenexprstatetype,
|
||||
};
|
||||
|
||||
extern const char *
|
||||
parenexprstatetype_descr(parenexprstatetype x);
|
||||
|
||||
inline std::ostream &
|
||||
operator<< (std::ostream & os, parenexprstatetype x) {
|
||||
os << parenexprstatetype_descr(x);
|
||||
return os;
|
||||
}
|
||||
|
||||
/** @class paren_xs
|
||||
* @brief state machine for handling parentheses in expressions
|
||||
**/
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ namespace xo {
|
|||
public:
|
||||
using Expression = xo::ast::Expression;
|
||||
using Variable = xo::ast::Variable;
|
||||
using token_type = token<char>;
|
||||
|
||||
public:
|
||||
parserstatemachine(exprstatestack * p_stack,
|
||||
|
|
@ -42,6 +43,18 @@ namespace xo {
|
|||
void push_envframe(envframe x);
|
||||
void pop_envframe();
|
||||
|
||||
// ----- parsing outputs -----
|
||||
|
||||
void on_symbol(const std::string & symbol);
|
||||
|
||||
// ---- parsing inputs -----
|
||||
|
||||
void on_leftbrace_token(const token_type & tk);
|
||||
void on_rightbrace_token(const token_type & tk);
|
||||
|
||||
/** write human-readable representation on @p os **/
|
||||
void print(std::ostream & os) const;
|
||||
|
||||
public:
|
||||
/** stack of incomplete parser work.
|
||||
* generally speaking, push when to start new work for nested content;
|
||||
|
|
@ -55,6 +68,12 @@ namespace xo {
|
|||
**/
|
||||
rp<Expression> * p_emit_expr_;
|
||||
};
|
||||
|
||||
inline std::ostream &
|
||||
operator<<(std::ostream & os, const parserstatemachine & x) {
|
||||
x.print(os);
|
||||
return os;
|
||||
}
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,33 @@
|
|||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
// ----- defexprstatetype -----
|
||||
|
||||
const char *
|
||||
defexprstatetype_descr(defexprstatetype x) {
|
||||
switch(x) {
|
||||
case defexprstatetype::invalid: return "invalid";
|
||||
case defexprstatetype::def_0: return "def_0";
|
||||
case defexprstatetype::def_1: return "def_1";
|
||||
case defexprstatetype::def_2: return "def_2";
|
||||
case defexprstatetype::def_3: return "def_3";
|
||||
case defexprstatetype::def_4: return "def_4";
|
||||
case defexprstatetype::def_5: return "def_5";
|
||||
case defexprstatetype::def_6: return "def_6";
|
||||
case defexprstatetype::n_defexprstatetype: break;
|
||||
}
|
||||
|
||||
return "???defexprstatetype";
|
||||
}
|
||||
|
||||
std::ostream &
|
||||
operator<<(std::ostream & os, defexprstatetype x) {
|
||||
os << defexprstatetype_descr(x);
|
||||
return os;
|
||||
}
|
||||
|
||||
// ----- define_xs -----
|
||||
|
||||
std::unique_ptr<define_xs>
|
||||
define_xs::make() {
|
||||
return std::make_unique<define_xs>(define_xs(DefineExprAccess::make_empty()));
|
||||
|
|
@ -17,6 +44,9 @@ namespace xo {
|
|||
void
|
||||
define_xs::start(parserstatemachine * p_psm)
|
||||
{
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
|
||||
p_psm->push_exprstate(define_xs::make());
|
||||
p_psm->top_exprstate().on_def_token(token_type::def(), p_psm);
|
||||
}
|
||||
|
|
@ -31,6 +61,11 @@ namespace xo {
|
|||
define_xs::on_expr(ref::brw<Expression> expr,
|
||||
parserstatemachine * p_psm)
|
||||
{
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
|
||||
log && log("defxs_type", defxs_type_);
|
||||
|
||||
if (this->defxs_type_ == defexprstatetype::def_5) {
|
||||
/* have all the ingredients to create an expression
|
||||
* representing a definition
|
||||
|
|
@ -59,6 +94,11 @@ namespace xo {
|
|||
define_xs::on_symbol(const std::string & symbol_name,
|
||||
parserstatemachine * p_psm)
|
||||
{
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
|
||||
log && log("defxs_type", defxs_type_);
|
||||
|
||||
if (this->defxs_type_ == defexprstatetype::def_1) {
|
||||
this->defxs_type_ = defexprstatetype::def_2;
|
||||
this->def_expr_->assign_lhs_name(symbol_name);
|
||||
|
|
@ -72,6 +112,11 @@ namespace xo {
|
|||
define_xs::on_typedescr(TypeDescr td,
|
||||
parserstatemachine * p_psm)
|
||||
{
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
|
||||
log && log("defxs_type", defxs_type_);
|
||||
|
||||
if (this->defxs_type_ == defexprstatetype::def_3) {
|
||||
this->defxs_type_ = defexprstatetype::def_4;
|
||||
this->cvt_expr_ = ConvertExprAccess::make(td /*dest_type*/,
|
||||
|
|
@ -92,7 +137,7 @@ namespace xo {
|
|||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
|
||||
//constexpr const char * self_name = "define_xs::on_def_token";
|
||||
log && log("defxs_type", defxs_type_);
|
||||
|
||||
if (this->defxs_type_ == defexprstatetype::def_0) {
|
||||
this->defxs_type_ = defexprstatetype::def_1;
|
||||
|
|
@ -110,7 +155,7 @@ namespace xo {
|
|||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
|
||||
//constexpr const char * self_name = "define_xs::on_colon_token";
|
||||
log && log("defxs_type", defxs_type_);
|
||||
|
||||
if (this->defxs_type_ == defexprstatetype::def_2) {
|
||||
this->defxs_type_ = defexprstatetype::def_3;
|
||||
|
|
@ -128,7 +173,7 @@ namespace xo {
|
|||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
|
||||
//constexpr const char * self_name = "exprstate::on_semicolon";
|
||||
log && log("defxs_type", defxs_type_);
|
||||
|
||||
if (this->defxs_type_ == defexprstatetype::def_6) {
|
||||
rp<Expression> expr = this->def_expr_;
|
||||
|
|
@ -150,6 +195,8 @@ namespace xo {
|
|||
|
||||
constexpr const char * self_name = "exprstate::on_singleassign";
|
||||
|
||||
log && log("defxs_type", defxs_type_);
|
||||
|
||||
/*
|
||||
* def foo = 1 ;
|
||||
* def foo : f64 = 1 ;
|
||||
|
|
@ -203,7 +250,9 @@ namespace xo {
|
|||
void
|
||||
define_xs::print(std::ostream & os) const {
|
||||
os << "<define_xs"
|
||||
<< xtag("type", exs_type_);
|
||||
//<< xtag("type", exs_type_)
|
||||
<< xtag("defxs_type", defxs_type_);
|
||||
|
||||
if (def_expr_)
|
||||
os << xtag("def_expr", def_expr_);
|
||||
if (cvt_expr_)
|
||||
|
|
|
|||
|
|
@ -105,7 +105,7 @@ namespace xo {
|
|||
|
||||
/* do not call .on_expr(), since '}' cancelled */
|
||||
|
||||
p_psm->top_exprstate().on_rightbrace_token(tk, p_psm);
|
||||
p_psm->on_rightbrace_token(tk);
|
||||
} else {
|
||||
exprstate::on_rightbrace_token(tk, p_psm);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,9 +33,7 @@ namespace xo {
|
|||
*/
|
||||
std::unique_ptr<exprstate> self = p_psm->pop_exprstate();
|
||||
|
||||
|
||||
p_psm->top_exprstate().on_symbol(tk.text(), p_psm);
|
||||
return;
|
||||
p_psm->on_symbol(tk.text());
|
||||
}
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
|
|
|||
|
|
@ -271,6 +271,7 @@ namespace xo {
|
|||
scope log(XO_DEBUG(c_debug_flag));
|
||||
log && log(xtag("tk", tk));
|
||||
log && log(xtag("state", *this));
|
||||
log && log(xtag("psm", *p_psm));
|
||||
|
||||
switch (tk.tk_type()) {
|
||||
|
||||
|
|
@ -405,6 +406,7 @@ namespace xo {
|
|||
void
|
||||
exprstate::print(std::ostream & os) const {
|
||||
os << "<exprstate"
|
||||
<< xtag("this", (void*)this)
|
||||
<< xtag("type", exs_type_);
|
||||
os << ">";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ namespace xo {
|
|||
exprstatestack::push_exprstate(std::unique_ptr<exprstate> exs) {
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag),
|
||||
xtag("exs", *exs));
|
||||
xtag("exs", exs.get()));
|
||||
|
||||
std::size_t z = stack_.size();
|
||||
|
||||
|
|
@ -59,7 +59,7 @@ namespace xo {
|
|||
|
||||
for (std::size_t i = 0, z = stack_.size(); i < z; ++i) {
|
||||
os << " [" << z-i-1 << "] "
|
||||
<< stack_[i]
|
||||
<< stack_[i].get()
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -39,8 +39,7 @@ namespace xo {
|
|||
const rp<Expression> & rhs,
|
||||
parserstatemachine * p_psm)
|
||||
{
|
||||
p_psm->push_exprstate(let1_xs::make(std::move(lhs_name),
|
||||
std::move(rhs)));
|
||||
p_psm->push_exprstate(let1_xs::make(lhs_name, rhs));
|
||||
|
||||
expect_expr_xs::start(true /*allow_defs*/,
|
||||
true /*cxl_on_rightbrace*/,
|
||||
|
|
@ -111,7 +110,7 @@ namespace xo {
|
|||
* -- remember we pushed let1_xs to handle an embedded def-expr
|
||||
* in a sequence
|
||||
*/
|
||||
p_psm->top_exprstate().on_rightbrace_token(tk, p_psm);
|
||||
p_psm->on_rightbrace_token(tk);
|
||||
}
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
|
|
|||
|
|
@ -8,8 +8,22 @@
|
|||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
const char *
|
||||
parenexprstatetype_descr(parenexprstatetype x)
|
||||
{
|
||||
switch(x) {
|
||||
case parenexprstatetype::invalid: return "invalid";
|
||||
case parenexprstatetype::lparen_0: return "lparen_0";
|
||||
case parenexprstatetype::lparen_1: return "lparen_1";
|
||||
case parenexprstatetype::n_parenexprstatetype: break;
|
||||
}
|
||||
|
||||
return "???parenexprstatetype";
|
||||
}
|
||||
|
||||
paren_xs::paren_xs()
|
||||
: parenxs_type_{parenexprstatetype::lparen_0}
|
||||
: exprstate(exprstatetype::parenexpr),
|
||||
parenxs_type_{parenexprstatetype::lparen_0}
|
||||
{}
|
||||
|
||||
std::unique_ptr<paren_xs>
|
||||
|
|
@ -221,13 +235,15 @@ namespace xo {
|
|||
void
|
||||
paren_xs::print(std::ostream & os) const {
|
||||
os << "<paren_xs"
|
||||
<< xtag("type", exs_type_);
|
||||
//<< xtag("type", exs_type_);
|
||||
<< xtag("parenxs_type", parenxs_type_);
|
||||
|
||||
if (gen_expr_)
|
||||
os << xtag("gen_expr", gen_expr_);
|
||||
|
||||
os << ">";
|
||||
}
|
||||
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
|
|
|||
|
|
@ -47,6 +47,54 @@ namespace xo {
|
|||
|
||||
p_env_stack_->pop_envframe();
|
||||
}
|
||||
|
||||
void
|
||||
parserstatemachine::on_symbol(const std::string & x)
|
||||
{
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
|
||||
log && log(xtag("x", x),
|
||||
xtag("psm", *this));
|
||||
|
||||
this->p_stack_
|
||||
->top_exprstate().on_symbol(x, this);
|
||||
}
|
||||
|
||||
void
|
||||
parserstatemachine::on_leftbrace_token(const token_type & tk)
|
||||
{
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
|
||||
log && log(xtag("tk", tk),
|
||||
xtag("psm", *this));
|
||||
|
||||
this->p_stack_
|
||||
->top_exprstate().on_leftbrace_token(tk, this);
|
||||
}
|
||||
|
||||
void
|
||||
parserstatemachine::on_rightbrace_token(const token_type & tk)
|
||||
{
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag));
|
||||
|
||||
log && log(xtag("tk", tk),
|
||||
xtag("psm", *this));
|
||||
|
||||
this->p_stack_
|
||||
->top_exprstate().on_rightbrace_token(tk, this);
|
||||
}
|
||||
|
||||
void
|
||||
parserstatemachine::print(std::ostream & os) const {
|
||||
os << "<psm";
|
||||
os << xtag("stack", p_stack_);
|
||||
os << xtag("env_stack", p_env_stack_);
|
||||
os << xtag("emit_expr", p_emit_expr_);
|
||||
os << ">";
|
||||
}
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue