xo-reader: refactor: + parserstatemachine; use for def_expr
This commit is contained in:
parent
6a71f718bd
commit
034dac7dfd
11 changed files with 69 additions and 18 deletions
|
|
@ -70,7 +70,7 @@ namespace xo {
|
||||||
|
|
||||||
static const define_xs * from(const exprstate * x) { return dynamic_cast<const define_xs *>(x); }
|
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_; }
|
defexprstatetype defxs_type() const { return defxs_type_; }
|
||||||
|
|
||||||
|
|
@ -84,7 +84,7 @@ namespace xo {
|
||||||
exprstatestack * p_stack,
|
exprstatestack * p_stack,
|
||||||
rp<Expression> * p_emit_expr) override;
|
rp<Expression> * p_emit_expr) override;
|
||||||
virtual void on_def_token(const token_type & tk,
|
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,
|
virtual void on_colon_token(const token_type & tk,
|
||||||
exprstatestack * p_stack) override;
|
exprstatestack * p_stack) override;
|
||||||
virtual void on_semicolon_token(const token_type & tk,
|
virtual void on_semicolon_token(const token_type & tk,
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ namespace xo {
|
||||||
// ----- token input methods -----
|
// ----- token input methods -----
|
||||||
|
|
||||||
virtual void on_def_token(const token_type & tk,
|
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,
|
virtual void on_symbol_token(const token_type & tk,
|
||||||
exprstatestack * p_stack,
|
exprstatestack * p_stack,
|
||||||
rp<Expression> * p_emit_expr) override;
|
rp<Expression> * p_emit_expr) override;
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,7 @@ namespace xo {
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class parserstatemachine;
|
||||||
class exprstatestack;
|
class exprstatestack;
|
||||||
|
|
||||||
class formal_arg;
|
class formal_arg;
|
||||||
|
|
@ -120,7 +121,7 @@ namespace xo {
|
||||||
|
|
||||||
/** handle incoming 'def' token **/
|
/** handle incoming 'def' token **/
|
||||||
virtual void on_def_token(const token_type & tk,
|
virtual void on_def_token(const token_type & tk,
|
||||||
exprstatestack * p_stack);
|
parserstatemachine * p_psm);
|
||||||
/** handle incoming 'lambda' token **/
|
/** handle incoming 'lambda' token **/
|
||||||
virtual void on_lambda_token(const token_type & tk,
|
virtual void on_lambda_token(const token_type & tk,
|
||||||
exprstatestack * p_stack,
|
exprstatestack * p_stack,
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ namespace xo {
|
||||||
rp<Expression> * /*p_emit_expr*/) override;
|
rp<Expression> * /*p_emit_expr*/) override;
|
||||||
|
|
||||||
virtual void on_def_token(const token_type & tk,
|
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,
|
virtual void on_symbol_token(const token_type & tk,
|
||||||
exprstatestack * p_stack,
|
exprstatestack * p_stack,
|
||||||
rp<Expression> * p_emit_expr) override;
|
rp<Expression> * p_emit_expr) override;
|
||||||
|
|
|
||||||
36
include/xo/reader/parserstatemachine.hpp
Normal file
36
include/xo/reader/parserstatemachine.hpp
Normal 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 */
|
||||||
|
|
@ -66,7 +66,7 @@ namespace xo {
|
||||||
rp<Expression> * /*p_emit_expr*/) override;
|
rp<Expression> * /*p_emit_expr*/) override;
|
||||||
|
|
||||||
virtual void on_def_token(const token_type & tk,
|
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,
|
virtual void on_colon_token(const token_type & tk,
|
||||||
exprstatestack * p_stack) override;
|
exprstatestack * p_stack) override;
|
||||||
virtual void on_semicolon_token(const token_type & tk,
|
virtual void on_semicolon_token(const token_type & tk,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
/* @file define_xs.cpp */
|
/* @file define_xs.cpp */
|
||||||
|
|
||||||
#include "define_xs.hpp"
|
#include "define_xs.hpp"
|
||||||
|
#include "parserstatemachine.hpp"
|
||||||
#include "expect_symbol_xs.hpp"
|
#include "expect_symbol_xs.hpp"
|
||||||
#include "expect_expr_xs.hpp"
|
#include "expect_expr_xs.hpp"
|
||||||
#include "expect_type_xs.hpp"
|
#include "expect_type_xs.hpp"
|
||||||
|
|
@ -13,10 +14,12 @@ namespace xo {
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
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->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)
|
define_xs::define_xs(rp<DefineExprAccess> def_expr)
|
||||||
|
|
@ -88,7 +91,7 @@ namespace xo {
|
||||||
|
|
||||||
void
|
void
|
||||||
define_xs::on_def_token(const token_type & tk,
|
define_xs::on_def_token(const token_type & tk,
|
||||||
exprstatestack * p_stack)
|
parserstatemachine * p_psm)
|
||||||
{
|
{
|
||||||
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));
|
||||||
|
|
@ -98,9 +101,9 @@ namespace xo {
|
||||||
if (this->defxs_type_ == defexprstatetype::def_0) {
|
if (this->defxs_type_ == defexprstatetype::def_0) {
|
||||||
this->defxs_type_ = defexprstatetype::def_1;
|
this->defxs_type_ = defexprstatetype::def_1;
|
||||||
|
|
||||||
expect_symbol_xs::start(p_stack);
|
expect_symbol_xs::start(p_psm->p_stack_);
|
||||||
} else {
|
} else {
|
||||||
exprstate::on_def_token(tk, p_stack);
|
exprstate::on_def_token(tk, p_psm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,14 +25,14 @@ namespace xo {
|
||||||
|
|
||||||
void
|
void
|
||||||
exprseq_xs::on_def_token(const token_type & /*tk*/,
|
exprseq_xs::on_def_token(const token_type & /*tk*/,
|
||||||
exprstatestack * p_stack)
|
parserstatemachine * p_psm)
|
||||||
{
|
{
|
||||||
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 * c_self_name = "exprseq_xs::on_def_token";
|
//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:
|
/* keyword 'def' introduces a definition:
|
||||||
* def pi : f64 = 3.14159265
|
* def pi : f64 = 3.14159265
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
/* @file exprstate.cpp */
|
/* @file exprstate.cpp */
|
||||||
|
|
||||||
#include "exprstate.hpp"
|
#include "exprstate.hpp"
|
||||||
|
#include "parserstatemachine.hpp"
|
||||||
//#include "formal_arg.hpp"
|
//#include "formal_arg.hpp"
|
||||||
#include "xo/expression/Variable.hpp"
|
#include "xo/expression/Variable.hpp"
|
||||||
#include "xo/indentlog/print/vector.hpp"
|
#include "xo/indentlog/print/vector.hpp"
|
||||||
|
|
@ -52,7 +53,7 @@ namespace xo {
|
||||||
|
|
||||||
void
|
void
|
||||||
exprstate::on_def_token(const token_type & tk,
|
exprstate::on_def_token(const token_type & tk,
|
||||||
exprstatestack * /*p_stack*/)
|
parserstatemachine * /*p_psm*/)
|
||||||
{
|
{
|
||||||
this->illegal_input_error("exprstate::on_def_token", tk);
|
this->illegal_input_error("exprstate::on_def_token", tk);
|
||||||
}
|
}
|
||||||
|
|
@ -254,10 +255,12 @@ namespace xo {
|
||||||
log && log(xtag("tk", tk));
|
log && log(xtag("tk", tk));
|
||||||
log && log(xtag("state", *this));
|
log && log(xtag("state", *this));
|
||||||
|
|
||||||
|
parserstatemachine psm(p_stack, p_emit_expr);
|
||||||
|
|
||||||
switch (tk.tk_type()) {
|
switch (tk.tk_type()) {
|
||||||
|
|
||||||
case tokentype::tk_def:
|
case tokentype::tk_def:
|
||||||
this->on_def_token(tk, p_stack);
|
this->on_def_token(tk, &psm);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case tokentype::tk_lambda:
|
case tokentype::tk_lambda:
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include "paren_xs.hpp"
|
#include "paren_xs.hpp"
|
||||||
#include "progress_xs.hpp"
|
#include "progress_xs.hpp"
|
||||||
|
#include "expect_expr_xs.hpp"
|
||||||
|
|
||||||
namespace xo {
|
namespace xo {
|
||||||
namespace scm {
|
namespace scm {
|
||||||
|
|
@ -10,10 +11,17 @@ namespace xo {
|
||||||
{}
|
{}
|
||||||
|
|
||||||
std::unique_ptr<paren_xs>
|
std::unique_ptr<paren_xs>
|
||||||
paren_xs::lparen_0() {
|
paren_xs::make() {
|
||||||
return std::make_unique<paren_xs>(paren_xs());
|
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
|
bool
|
||||||
paren_xs::admits_rightparen() const {
|
paren_xs::admits_rightparen() const {
|
||||||
switch (parenxs_type_) {
|
switch (parenxs_type_) {
|
||||||
|
|
@ -56,7 +64,7 @@ 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*/)
|
parserstatemachine * /*p_stack*/)
|
||||||
{
|
{
|
||||||
constexpr const char * c_self_name = "paren_xs::on_def";
|
constexpr const char * c_self_name = "paren_xs::on_def";
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,7 @@ namespace xo {
|
||||||
|
|
||||||
void
|
void
|
||||||
progress_xs::on_def_token(const token_type & tk,
|
progress_xs::on_def_token(const token_type & tk,
|
||||||
exprstatestack * /*p_stack*/)
|
parserstatemachine * /*p_stack*/)
|
||||||
{
|
{
|
||||||
constexpr const char * self_name = "progress_xs::on_def";
|
constexpr const char * self_name = "progress_xs::on_def";
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue