xo-reader2: + ExpectSymbolSsm + SyntaxStateMachine.on_parsed_symbol
This commit is contained in:
parent
e252a9f4e7
commit
83ef04c250
28 changed files with 537 additions and 52 deletions
|
|
@ -6,7 +6,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "ParserStateMachine.hpp"
|
||||
#include "SyntaxStateMachine.hpp"
|
||||
//#include "SyntaxStateMachine.hpp"
|
||||
#include "syntaxstatetype.hpp"
|
||||
#include <xo/facet/obj.hpp>
|
||||
|
||||
|
|
@ -110,6 +110,12 @@ namespace xo {
|
|||
void on_if_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state for this syntax after parsing a symbol @p sym;
|
||||
* overall parser state in @p p_psm
|
||||
**/
|
||||
void on_parsed_symbol(std::string_view sym,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
///@}
|
||||
|
||||
private:
|
||||
|
|
|
|||
91
xo-reader2/include/xo/reader2/DExpectSymbolSsm.hpp
Normal file
91
xo-reader2/include/xo/reader2/DExpectSymbolSsm.hpp
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
/* file DExpectSymbolSsm.hpp
|
||||
*
|
||||
* author: Roland Conybeare, Aug 2024
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "ParserStateMachine.hpp"
|
||||
//#include "SyntaxStateMachine.hpp"
|
||||
#include "syntaxstatetype.hpp"
|
||||
#include <xo/facet/obj.hpp>
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** @class DExpectSymbolSsm
|
||||
* @brief state machine to expect + capture a symbol
|
||||
*
|
||||
* For example:
|
||||
* - lhs in a define-expression
|
||||
**/
|
||||
class DExpectSymbolSsm {
|
||||
public:
|
||||
using DArena = xo::mm::DArena;
|
||||
|
||||
public:
|
||||
DExpectSymbolSsm();
|
||||
|
||||
/** create instance using memory from @p parser_mm **/
|
||||
static DExpectSymbolSsm * make(DArena & parser_mm);
|
||||
|
||||
/** start nested parser expecting a symbol,
|
||||
* on top of parser state machine @p p_psm.
|
||||
* On success will deliver symbol by invoking
|
||||
* .on_symbol(sym, p_psm)
|
||||
* to the state machine on top of the stack
|
||||
* as of when this start() method invoked
|
||||
**/
|
||||
static void start(DArena & parser_mm,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state for this syntax on incoming token @p tk,
|
||||
* with overall parser state in @p p_psm
|
||||
**/
|
||||
static void on_symbol_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** @defgroup scm-expectsymbol-ssm-facet syntaxstatemachine facet methods **/
|
||||
///@{
|
||||
|
||||
/** identifies the ssm implemented here **/
|
||||
syntaxstatetype ssm_type() const noexcept;
|
||||
|
||||
/** text describing expected/allowed input to this ssm in current state.
|
||||
* Intended to drive error mesages
|
||||
**/
|
||||
std::string_view get_expect_str() const noexcept;
|
||||
|
||||
/** update state for this syntax on incoming token @p tk,
|
||||
* overall parser state in @p p_psm
|
||||
**/
|
||||
void on_def_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state for this syntax on incoming token @p tk,
|
||||
* overall parser state in @p p_psm
|
||||
**/
|
||||
void on_if_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state for this syntax after parsing a symbol @p sym;
|
||||
* overall parser state in @p p_psm.
|
||||
*
|
||||
* NOTE:
|
||||
* might not be obvious that this is unreachable.
|
||||
* DExpectSymbolSsm converts a symbol token,
|
||||
* and delivers it to parent ssm using this entry point.
|
||||
* This method would only be called if consecutive
|
||||
* DExpectSymbolSsm instances on parser stack;
|
||||
* which scenario never occurs in Schematika syntax
|
||||
**/
|
||||
void on_parsed_symbol(std::string_view sym,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
|
||||
///@}
|
||||
|
||||
};
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end DExpectSymbolSsm.hpp */
|
||||
|
|
@ -66,6 +66,12 @@ namespace xo {
|
|||
**/
|
||||
void on_if_token(const Token & tk, ParserStateMachine * p_psm);
|
||||
|
||||
/** update state for this syntax on parsed symbol @p sym
|
||||
* from immediately-downstream ssm.
|
||||
* overall parser state in @p p_psm
|
||||
**/
|
||||
void on_parsed_symbol(std::string_view sym, ParserStateMachine * p_psm);
|
||||
|
||||
///@}
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "SyntaxStateMachine.hpp"
|
||||
#include <xo/alloc2/Allocator.hpp>
|
||||
#include <xo/arena/DArena.hpp>
|
||||
#include <xo/facet/obj.hpp>
|
||||
|
||||
namespace xo {
|
||||
|
|
@ -20,22 +20,31 @@ namespace xo {
|
|||
**/
|
||||
class ParserStack {
|
||||
public:
|
||||
using AAllocator = xo::mm::AAllocator;
|
||||
using DArena = xo::mm::DArena;
|
||||
|
||||
public:
|
||||
ParserStack(obj<ASyntaxStateMachine> ssm, ParserStack * parent);
|
||||
ParserStack(DArena::Checkpoint ckp,
|
||||
obj<ASyntaxStateMachine> ssm,
|
||||
ParserStack * parent);
|
||||
|
||||
/** create new top of stack for syntax @p ssm, using memory from @p mm.
|
||||
* previous stack given by @p parent
|
||||
**/
|
||||
static ParserStack * push(ParserStack * stack,
|
||||
obj<AAllocator> mm,
|
||||
DArena & mm,
|
||||
obj<ASyntaxStateMachine> ssm);
|
||||
|
||||
/** unwind effect of last call to @ref push **/
|
||||
static ParserStack * pop(ParserStack * stack,
|
||||
DArena & mm);
|
||||
|
||||
DArena::Checkpoint ckp() const noexcept { return ckp_; }
|
||||
obj<ASyntaxStateMachine> top() const noexcept { return ssm_; }
|
||||
ParserStack * parent() const noexcept { return parent_; }
|
||||
|
||||
private:
|
||||
/** stack pointer: top of stack just before this instance created **/
|
||||
DArena::Checkpoint ckp_;
|
||||
/** top of parsing stack: always non-null **/
|
||||
obj<ASyntaxStateMachine> ssm_;
|
||||
/** remainder of parsing stack excluding top **/
|
||||
|
|
|
|||
|
|
@ -62,6 +62,9 @@ namespace xo {
|
|||
/** push syntax @p ssm onto @ref stack_ **/
|
||||
void push_ssm(obj<ASyntaxStateMachine> ssm);
|
||||
|
||||
/** pop syntax state machine from top of @ref stack_ **/
|
||||
void pop_ssm();
|
||||
|
||||
/** reset result to none **/
|
||||
void reset_result();
|
||||
|
||||
|
|
@ -73,6 +76,9 @@ namespace xo {
|
|||
/** @defgroup scm-parserstatemachine-inputmethods input methods **/
|
||||
///@{
|
||||
|
||||
/** update state to respond to prsed symbol @p sym **/
|
||||
void on_parsed_symbol(std::string_view sym);
|
||||
|
||||
/** update state to respond to input token @p tk.
|
||||
* record output (if any) in @ref result_
|
||||
**/
|
||||
|
|
@ -99,12 +105,19 @@ namespace xo {
|
|||
|
||||
/** report illegal input from syntax state machine @p ssm_name
|
||||
* recognized on input token @p tk. @p expect_str describes
|
||||
* expected input in that state
|
||||
* expected input in current ssm state
|
||||
**/
|
||||
void illegal_input_on_token(std::string_view ssm_name,
|
||||
const Token & tk,
|
||||
std::string_view expect_str);
|
||||
|
||||
/** report illegal input from syntax state machine @p ssm_name
|
||||
* receiving parsed symbol @p sym. @p expect_str describes
|
||||
* expected input in current ssm state
|
||||
**/
|
||||
void illegal_input_on_symbol(std::string_view ssm_name,
|
||||
std::string_view sym,
|
||||
std::string_view expect_str);
|
||||
///@}
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -54,8 +54,10 @@ public:
|
|||
virtual std::string_view get_expect_str(Copaque data) const noexcept = 0;
|
||||
|
||||
// nonconst methods
|
||||
/** update state machine for incoming define-keyworkd-token @p tk **/
|
||||
virtual void on_def_token(Opaque data, const Token & tk, ParserStateMachine * ps_psm) = 0;
|
||||
/** update stat machine for incoming parsed symbol @p sym **/
|
||||
virtual void on_parsed_symbol(Opaque data, std::string_view sym, ParserStateMachine * p_psm) = 0;
|
||||
/** update state machine for incoming define-keyword-token @p tk **/
|
||||
virtual void on_def_token(Opaque data, const Token & tk, ParserStateMachine * p_psm) = 0;
|
||||
/** update state machine for incoming if-keyword-token @p tk **/
|
||||
virtual void on_if_token(Opaque data, const Token & tk, ParserStateMachine * p_psm) = 0;
|
||||
///@}
|
||||
|
|
|
|||
|
|
@ -59,6 +59,7 @@ namespace scm {
|
|||
[[noreturn]] std::string_view get_expect_str(Copaque) const noexcept override { _fatal(); }
|
||||
|
||||
// nonconst methods
|
||||
[[noreturn]] void on_parsed_symbol(Opaque, std::string_view, ParserStateMachine *) override;
|
||||
[[noreturn]] void on_def_token(Opaque, const Token &, ParserStateMachine *) override;
|
||||
[[noreturn]] void on_if_token(Opaque, const Token &, ParserStateMachine *) override;
|
||||
|
||||
|
|
|
|||
|
|
@ -53,8 +53,10 @@ namespace xo {
|
|||
static std::string_view get_expect_str(const DDefineSsm & self) noexcept;
|
||||
|
||||
// non-const methods
|
||||
/** update state machine for incoming define-keyworkd-token @p tk **/
|
||||
static void on_def_token(DDefineSsm & self, const Token & tk, ParserStateMachine * ps_psm);
|
||||
/** update stat machine for incoming parsed symbol @p sym **/
|
||||
static void on_parsed_symbol(DDefineSsm & self, std::string_view sym, ParserStateMachine * p_psm);
|
||||
/** update state machine for incoming define-keyword-token @p tk **/
|
||||
static void on_def_token(DDefineSsm & self, const Token & tk, ParserStateMachine * p_psm);
|
||||
/** update state machine for incoming if-keyword-token @p tk **/
|
||||
static void on_if_token(DDefineSsm & self, const Token & tk, ParserStateMachine * p_psm);
|
||||
///@}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,68 @@
|
|||
/** @file ISyntaxStateMachine_DExpectSymbolSsm.hpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [/Users/roland/proj/xo-umbrella2/xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/ISyntaxStateMachine_DExpectSymbolSsm.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [iface_facet_repr.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/ISyntaxStateMachine_DExpectSymbolSsm.json5]
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "SyntaxStateMachine.hpp"
|
||||
#include "SyntaxStateMachine.hpp"
|
||||
#include "ssm/ISyntaxStateMachine_Xfer.hpp"
|
||||
#include "DExpectSymbolSsm.hpp"
|
||||
|
||||
namespace xo { namespace scm { class ISyntaxStateMachine_DExpectSymbolSsm; } }
|
||||
|
||||
namespace xo {
|
||||
namespace facet {
|
||||
template <>
|
||||
struct FacetImplementation<xo::scm::ASyntaxStateMachine,
|
||||
xo::scm::DExpectSymbolSsm>
|
||||
{
|
||||
using ImplType = xo::scm::ISyntaxStateMachine_Xfer
|
||||
<xo::scm::DExpectSymbolSsm,
|
||||
xo::scm::ISyntaxStateMachine_DExpectSymbolSsm>;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** @class ISyntaxStateMachine_DExpectSymbolSsm
|
||||
**/
|
||||
class ISyntaxStateMachine_DExpectSymbolSsm {
|
||||
public:
|
||||
/** @defgroup scm-syntaxstatemachine-dexpectsymbolssm-type-traits **/
|
||||
///@{
|
||||
using Copaque = xo::scm::ASyntaxStateMachine::Copaque;
|
||||
using Opaque = xo::scm::ASyntaxStateMachine::Opaque;
|
||||
///@}
|
||||
/** @defgroup scm-syntaxstatemachine-dexpectsymbolssm-methods **/
|
||||
///@{
|
||||
// const methods
|
||||
/** identify a type of syntax state machine **/
|
||||
static syntaxstatetype ssm_type(const DExpectSymbolSsm & self) noexcept;
|
||||
/** text describing expected/allowed input to this ssm in current state **/
|
||||
static std::string_view get_expect_str(const DExpectSymbolSsm & self) noexcept;
|
||||
|
||||
// non-const methods
|
||||
/** update stat machine for incoming parsed symbol @p sym **/
|
||||
static void on_parsed_symbol(DExpectSymbolSsm & self, std::string_view sym, ParserStateMachine * p_psm);
|
||||
/** update state machine for incoming define-keyword-token @p tk **/
|
||||
static void on_def_token(DExpectSymbolSsm & self, const Token & tk, ParserStateMachine * p_psm);
|
||||
/** update state machine for incoming if-keyword-token @p tk **/
|
||||
static void on_if_token(DExpectSymbolSsm & self, const Token & tk, ParserStateMachine * p_psm);
|
||||
///@}
|
||||
};
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end */
|
||||
|
|
@ -53,8 +53,10 @@ namespace xo {
|
|||
static std::string_view get_expect_str(const DExprSeqState & self) noexcept;
|
||||
|
||||
// non-const methods
|
||||
/** update state machine for incoming define-keyworkd-token @p tk **/
|
||||
static void on_def_token(DExprSeqState & self, const Token & tk, ParserStateMachine * ps_psm);
|
||||
/** update stat machine for incoming parsed symbol @p sym **/
|
||||
static void on_parsed_symbol(DExprSeqState & self, std::string_view sym, ParserStateMachine * p_psm);
|
||||
/** update state machine for incoming define-keyword-token @p tk **/
|
||||
static void on_def_token(DExprSeqState & self, const Token & tk, ParserStateMachine * p_psm);
|
||||
/** update state machine for incoming if-keyword-token @p tk **/
|
||||
static void on_if_token(DExprSeqState & self, const Token & tk, ParserStateMachine * p_psm);
|
||||
///@}
|
||||
|
|
|
|||
|
|
@ -50,8 +50,11 @@ namespace scm {
|
|||
}
|
||||
|
||||
// non-const methods
|
||||
void on_def_token(Opaque data, const Token & tk, ParserStateMachine * ps_psm) override {
|
||||
return I::on_def_token(_dcast(data), tk, ps_psm);
|
||||
void on_parsed_symbol(Opaque data, std::string_view sym, ParserStateMachine * p_psm) override {
|
||||
return I::on_parsed_symbol(_dcast(data), sym, p_psm);
|
||||
}
|
||||
void on_def_token(Opaque data, const Token & tk, ParserStateMachine * p_psm) override {
|
||||
return I::on_def_token(_dcast(data), tk, p_psm);
|
||||
}
|
||||
void on_if_token(Opaque data, const Token & tk, ParserStateMachine * p_psm) override {
|
||||
return I::on_if_token(_dcast(data), tk, p_psm);
|
||||
|
|
|
|||
|
|
@ -55,8 +55,11 @@ public:
|
|||
}
|
||||
|
||||
// non-const methods (still const in router!)
|
||||
void on_def_token(const Token & tk, ParserStateMachine * ps_psm) {
|
||||
return O::iface()->on_def_token(O::data(), tk, ps_psm);
|
||||
void on_parsed_symbol(std::string_view sym, ParserStateMachine * p_psm) {
|
||||
return O::iface()->on_parsed_symbol(O::data(), sym, p_psm);
|
||||
}
|
||||
void on_def_token(const Token & tk, ParserStateMachine * p_psm) {
|
||||
return O::iface()->on_def_token(O::data(), tk, p_psm);
|
||||
}
|
||||
void on_if_token(const Token & tk, ParserStateMachine * p_psm) {
|
||||
return O::iface()->on_if_token(O::data(), tk, p_psm);
|
||||
|
|
|
|||
|
|
@ -21,6 +21,9 @@ namespace xo {
|
|||
/** toplevel of some translation unit. See @ref DExprSeqState **/
|
||||
expect_toplevel_expression_sequence,
|
||||
|
||||
/** expecting a s symbol. See @ref DExpectSymbolSsm **/
|
||||
expect_symbol,
|
||||
|
||||
/** handle define-expression. See @ref DDefineSsm **/
|
||||
defexpr,
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue