xo-reader: refactor to simplify parser+parserstatemachine

This commit is contained in:
Roland Conybeare 2025-07-22 23:09:55 -05:00
commit 81bbc080fd
10 changed files with 85 additions and 129 deletions

View file

@ -36,7 +36,7 @@ namespace xo {
**/
void upsert(bp<Variable> target);
bp<LocalEnv> top_envframe();
bp<LocalEnv> top_envframe() const;
void push_envframe(const rp<LocalEnv> & x);
rp<LocalEnv> pop_envframe();

View file

@ -8,6 +8,7 @@
#include "exprstatestack.hpp"
#include "envframestack.hpp"
#include "parser_result.hpp"
#include "parserstatemachine.hpp"
#include <stdexcept>
namespace xo {
@ -167,17 +168,17 @@ namespace xo {
bool is_at_toplevel() const { return stack_size() == 0; }
/** for diagnostics: number of entries in parser stack **/
std::size_t stack_size() const { return xs_stack_.size(); }
std::size_t stack_size() const { return psm_.xs_stack_.size(); }
/** for diagnostics: exprstatetype at level @p i
* (taken relative to top of stack)
*
* @pre 0 <= i < stack_size
**/
exprstatetype i_exstype(std::size_t i) const {
std::size_t z = xs_stack_.size();
std::size_t z = psm_.xs_stack_.size();
if (i < z) {
return xs_stack_[i]->exs_type();
return psm_.xs_stack_[i]->exs_type();
}
/* out of bounds */
@ -185,10 +186,10 @@ namespace xo {
}
exprstate const * i_exstate(std::size_t i) const {
std::size_t z = xs_stack_.size();
std::size_t z = psm_.xs_stack_.size();
if (i < z) {
return xs_stack_[i].get();
return psm_.xs_stack_[i].get();
}
/* out of bounds */
@ -229,28 +230,8 @@ namespace xo {
void print(std::ostream & os) const;
private:
/** state recording state associated with enclosing expressions.
*
* Note: at least asof c++23, the std::stack api doesn't support access
* to members other than the top.
*
* for stack with N elements (N = stack_.size()):
* - bottom of stack is stack_[0]
* - top of stack is stack_[N-1]
**/
exprstatestack xs_stack_;
/** environment frames for lexical context.
* push a frame on each nested lambda;
* pop when lambda body goes out of scope
**/
envframestack env_stack_;
/** parser result state **/
parser_result result_;
/** enable/disable debug logging **/
bool debug_flag_;
/** state machine **/
parserstatemachine psm_;
}; /*parser*/
inline std::ostream &

View file

@ -6,6 +6,7 @@
#pragma once
#include "exprstate.hpp"
#include "exprstatestack.hpp"
#include "envframestack.hpp"
#include "parser_result.hpp"
@ -26,14 +27,8 @@ namespace xo {
using token_type = token<char>;
public:
parserstatemachine(exprstatestack * p_stack,
envframestack * p_env_stack,
parser_result * p_result,
bool debug_flag)
: p_stack_{p_stack},
p_env_stack_{p_env_stack},
p_result_{p_result},
debug_flag_{debug_flag}
explicit parserstatemachine(bool debug_flag)
: debug_flag_{debug_flag}
{}
//const parser_result & result() const { return result_; }
@ -65,7 +60,7 @@ namespace xo {
/** @return pop innermost environment frame and return it **/
rp<LocalEnv> pop_envframe();
/** @return number of stacked environment frames **/
size_t env_stack_size() const { return p_env_stack_->size(); }
size_t env_stack_size() const { return env_stack_.size(); }
// ----- parsing outputs -----
@ -92,16 +87,24 @@ namespace xo {
void print(std::ostream & os) const;
public:
/** stack of incomplete parser work.
* generally speaking, push when to start new work for nested content;
* pop when work complete
/** state recording state associated with enclosing expressions.
*
* Note: at least asof c++23, the std::stack api doesn't support access
* to members other than the top.
*
* for stack with N elements (N = stack_.size()):
* - bottom of stack is stack_[0]
* - top of stack is stack_[N-1]
**/
exprstatestack * p_stack_ = nullptr;
/** stack of environment frames, one for each enclosing lambda **/
envframestack * p_env_stack_ = nullptr;
/** parser result object **/
parser_result * p_result_ = nullptr;
/** enable debug logging **/
exprstatestack xs_stack_;
/** environment frames for lexical context.
* push a frame on each nested lambda;
* pop when lambda body goes out of scope
**/
envframestack env_stack_;
/** parser result state **/
parser_result result_;
/** enable/disable debug logging **/
bool debug_flag_ = false;
};