xo-reader: + expect_formal_xs [wip - not used]

This commit is contained in:
Roland Conybeare 2024-08-16 21:55:23 -04:00
commit 6d5387eef7
8 changed files with 282 additions and 12 deletions

View file

@ -56,18 +56,18 @@ namespace xo {
exprstatestack * p_stack,
rp<Expression> * p_emit_expr) override;
virtual void on_typedescr(TypeDescr td,
exprstatestack * /*p_stack*/,
rp<Expression> * /*p_emit_expr*/) override;
exprstatestack * p_stack,
rp<Expression> * p_emit_expr) override;
virtual void on_colon_token(const token_type & tk,
exprstatestack * p_stack) override;
virtual void on_semicolon_token(const token_type & tk,
exprstatestack * p_stack,
rp<Expression> * /*p_emit_expr*/) override;
rp<Expression> * p_emit_expr) override;
virtual void on_singleassign_token(const token_type & tk,
exprstatestack * p_stack) override;
virtual void on_rightparen_token(const token_type & tk,
exprstatestack * p_stack,
rp<Expression> * /*p_emit_expr*/) override;
rp<Expression> * p_emit_expr) override;
virtual void on_f64_token(const token_type & tk,
exprstatestack * p_stack,
rp<Expression> * /*p_emit_expr*/) override;

View file

@ -0,0 +1,80 @@
/* file expect_formal_xs.hpp
*
* author: Roland Conybeare, Aug 2024
*/
#pragma once
#include "exprstate.hpp"
#include "formal_arg.hpp"
namespace xo {
namespace scm {
/**
* name : type
* ^ ^ ^
* | | formal_2
* | formal_1
* formal_0
*
**/
enum class formalstatetype {
invalid = -1,
formal_0,
formal_1,
formal_2,
n_formalstatetype,
};
extern const char *
formalstatetype_descr(formalstatetype x);
inline std::ostream &
operator<< (std::ostream & os, formalstatetype x) {
os << formalstatetype_descr(x);
return os;
}
/** @class expect_formal_xs
* @brief parser state-machine for a typed formal parameter
**/
class expect_formal_xs : public exprstate {
public:
expect_formal_xs() = default;
virtual void on_symbol(const std::string & symbol_name,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr) override;
virtual void on_colon_token(const token_type & tk,
exprstatestack * p_stack
/*rp<Expression> * p_emit_expr*/) override;
// virtual void on_comma_token(...) override;
#ifdef PROBABLY_NOT
virtual void on_rightparen_token(const token_type & tk,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr) override;
#endif
virtual void on_typedescr(TypeDescr td,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr) override;
virtual void print(std::ostream & os) const override;
private:
/** parsing state-machine state **/
formalstatetype formalxs_type_;
/** populate with {parameter-name, parameter-type}
* as they're encountered
**/
formal_arg result_;
};
} /*namespace scm*/
} /*namespace xo*/
/* end expect_formal_xs.hpp */

View file

@ -52,7 +52,7 @@ namespace xo {
class exprstatestack;
class define_xs;
class formal_arg;
/** state associated with a partially-parsed expression.
**/
@ -91,6 +91,10 @@ namespace xo {
virtual void on_typedescr(TypeDescr td,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr);
/** update exprstate when expecting a formal parameter **/
virtual void on_formal(const formal_arg & formal,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr);
/** print human-readable representation on @p os **/
virtual void print(std::ostream & os) const;

View file

@ -0,0 +1,58 @@
/* file formal_arg.hpp
*
* author: Roland Conybeare, Aug 2024
*/
#pragma once
#include "TypeDescr.hpp"
#include "xo/indentlog/print/tag.hpp"
namespace xo {
namespace scm {
/** @class formal_arg
* @brief description of formal parameter in an argument list
*
* Terminated by an argument separator ',' or rightparen ')'
**/
class formal_arg {
public:
using TypeDescr = xo::reflect::TypeDescr;
public:
formal_arg() = default;
formal_arg(const std::string & n, TypeDescr td) : name_{n}, td_{td} {}
const std::string & name() const { return name_; }
TypeDescr td() const { return td_; }
void assign_name(const std::string & x) { name_ = x; }
void assign_td(TypeDescr x) { td_ = x; }
void print(std::ostream & os) const {
os << "<formal_arg";
if (!name_.empty())
os << xtag("name", name_);
if (td_)
os << xtag("td", td_);
os << ">";
}
private:
/** formal parameter name **/
std::string name_;
/** type description for variable @p name **/
TypeDescr td_;
};
inline std::ostream &
operator<< (std::ostream & os,
const formal_arg & x) {
x.print(os);
return os;
}
} /*namespace scm*/
} /*namespace xo*/
/* end formal_arg.hpp */

View file

@ -18,7 +18,9 @@ namespace xo {
progress_xs(rp<Expression> valex);
virtual ~progress_xs() = default;
static const progress_xs * from(const exprstate * x) { return dynamic_cast<const progress_xs *>(x); }
static const progress_xs * from(const exprstate * x) {
return dynamic_cast<const progress_xs *>(x);
}
static std::unique_ptr<progress_xs> make(rp<Expression> valex);