xo-reader: refactor: + parserstatemachine; use for def_expr

This commit is contained in:
Roland Conybeare 2024-08-19 00:44:34 -04:00
commit 034dac7dfd
11 changed files with 69 additions and 18 deletions

View file

@ -70,7 +70,7 @@ namespace xo {
static const define_xs * from(const exprstate * x) { return dynamic_cast<const define_xs *>(x); }
static void start(exprstatestack * p_stack);
static void start(parserstatemachine * p_psm);
defexprstatetype defxs_type() const { return defxs_type_; }
@ -84,7 +84,7 @@ namespace xo {
exprstatestack * p_stack,
rp<Expression> * p_emit_expr) override;
virtual void on_def_token(const token_type & tk,
exprstatestack * p_stack) override;
parserstatemachine * p_psm) override;
virtual void on_colon_token(const token_type & tk,
exprstatestack * p_stack) override;
virtual void on_semicolon_token(const token_type & tk,

View file

@ -24,7 +24,7 @@ namespace xo {
// ----- token input methods -----
virtual void on_def_token(const token_type & tk,
exprstatestack * p_stack) override;
parserstatemachine * p_psm) override;
virtual void on_symbol_token(const token_type & tk,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr) override;

View file

@ -63,6 +63,7 @@ namespace xo {
return os;
}
class parserstatemachine;
class exprstatestack;
class formal_arg;
@ -120,7 +121,7 @@ namespace xo {
/** handle incoming 'def' token **/
virtual void on_def_token(const token_type & tk,
exprstatestack * p_stack);
parserstatemachine * p_psm);
/** handle incoming 'lambda' token **/
virtual void on_lambda_token(const token_type & tk,
exprstatestack * p_stack,

View file

@ -45,7 +45,7 @@ namespace xo {
rp<Expression> * /*p_emit_expr*/) override;
virtual void on_def_token(const token_type & tk,
exprstatestack * p_stack) override;
parserstatemachine * p_psm) override;
virtual void on_symbol_token(const token_type & tk,
exprstatestack * p_stack,
rp<Expression> * p_emit_expr) override;

View file

@ -0,0 +1,36 @@
/* file parserstatemachine.hpp
*
* author: Roland Conybeare, Aug 2024
*/
#pragma once
#include "exprstate.hpp"
namespace xo {
namespace scm {
/** @class parserstatemachine
* @brief public parser state.
*
* Schematica parser state; sent to subsidiary single-feature state machines.
* For example entry points for the lambda feature (@ref lambda_xs)
* will accept a non-const parserstatemachine pointer argument
**/
class parserstatemachine {
public:
using Expression = xo::ast::Expression;
public:
parserstatemachine(exprstatestack * p_stack,
rp<Expression> * p_emit_expr)
: p_stack_{p_stack}, p_emit_expr_{p_emit_expr} {}
public:
exprstatestack * p_stack_;
rp<Expression> * p_emit_expr_;
};
} /*namespace scm*/
} /*namespace xo*/
/* end parserstatemachine.hpp */

View file

@ -66,7 +66,7 @@ namespace xo {
rp<Expression> * /*p_emit_expr*/) override;
virtual void on_def_token(const token_type & tk,
exprstatestack * p_stack) override;
parserstatemachine * p_psm) override;
virtual void on_colon_token(const token_type & tk,
exprstatestack * p_stack) override;
virtual void on_semicolon_token(const token_type & tk,

View file

@ -1,6 +1,7 @@
/* @file define_xs.cpp */
#include "define_xs.hpp"
#include "parserstatemachine.hpp"
#include "expect_symbol_xs.hpp"
#include "expect_expr_xs.hpp"
#include "expect_type_xs.hpp"
@ -13,10 +14,12 @@ namespace xo {
}
void
define_xs::start(exprstatestack * p_stack)
define_xs::start(parserstatemachine * p_psm)
{
auto p_stack = p_psm->p_stack_;
p_stack->push_exprstate(define_xs::make());
p_stack->top_exprstate().on_def_token(token_type::def(), p_stack);
p_stack->top_exprstate().on_def_token(token_type::def(), p_psm);
}
define_xs::define_xs(rp<DefineExprAccess> def_expr)
@ -88,7 +91,7 @@ namespace xo {
void
define_xs::on_def_token(const token_type & tk,
exprstatestack * p_stack)
parserstatemachine * p_psm)
{
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag));
@ -98,9 +101,9 @@ namespace xo {
if (this->defxs_type_ == defexprstatetype::def_0) {
this->defxs_type_ = defexprstatetype::def_1;
expect_symbol_xs::start(p_stack);
expect_symbol_xs::start(p_psm->p_stack_);
} else {
exprstate::on_def_token(tk, p_stack);
exprstate::on_def_token(tk, p_psm);
}
}

View file

@ -25,14 +25,14 @@ namespace xo {
void
exprseq_xs::on_def_token(const token_type & /*tk*/,
exprstatestack * p_stack)
parserstatemachine * p_psm)
{
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag));
//constexpr const char * c_self_name = "exprseq_xs::on_def_token";
define_xs::start(p_stack);
define_xs::start(p_psm);
/* keyword 'def' introduces a definition:
* def pi : f64 = 3.14159265

View file

@ -1,6 +1,7 @@
/* @file exprstate.cpp */
#include "exprstate.hpp"
#include "parserstatemachine.hpp"
//#include "formal_arg.hpp"
#include "xo/expression/Variable.hpp"
#include "xo/indentlog/print/vector.hpp"
@ -52,7 +53,7 @@ namespace xo {
void
exprstate::on_def_token(const token_type & tk,
exprstatestack * /*p_stack*/)
parserstatemachine * /*p_psm*/)
{
this->illegal_input_error("exprstate::on_def_token", tk);
}
@ -254,10 +255,12 @@ namespace xo {
log && log(xtag("tk", tk));
log && log(xtag("state", *this));
parserstatemachine psm(p_stack, p_emit_expr);
switch (tk.tk_type()) {
case tokentype::tk_def:
this->on_def_token(tk, p_stack);
this->on_def_token(tk, &psm);
return;
case tokentype::tk_lambda:

View file

@ -2,6 +2,7 @@
#include "paren_xs.hpp"
#include "progress_xs.hpp"
#include "expect_expr_xs.hpp"
namespace xo {
namespace scm {
@ -10,10 +11,17 @@ namespace xo {
{}
std::unique_ptr<paren_xs>
paren_xs::lparen_0() {
paren_xs::make() {
return std::make_unique<paren_xs>(paren_xs());
}
void
paren_xs::start(exprstatestack * p_stack)
{
p_stack->push_exprstate(paren_xs::make());
expect_expr_xs::start(p_stack);
}
bool
paren_xs::admits_rightparen() const {
switch (parenxs_type_) {
@ -56,7 +64,7 @@ namespace xo {
void
paren_xs::on_def_token(const token_type & tk,
exprstatestack * /*p_stack*/)
parserstatemachine * /*p_stack*/)
{
constexpr const char * c_self_name = "paren_xs::on_def";

View file

@ -73,7 +73,7 @@ namespace xo {
void
progress_xs::on_def_token(const token_type & tk,
exprstatestack * /*p_stack*/)
parserstatemachine * /*p_stack*/)
{
constexpr const char * self_name = "progress_xs::on_def";