xo-reader2: support if-then-else expressions. + detailed utest
This commit is contained in:
parent
e243264511
commit
c052c5c509
23 changed files with 1354 additions and 150 deletions
|
|
@ -113,7 +113,7 @@ namespace xo {
|
|||
syntaxstatetype ssm_type() const noexcept;
|
||||
|
||||
/** text describing expected/allowed input to this ssm in current state.
|
||||
* Intended to drive error mesages
|
||||
* Intended to drive error messages
|
||||
**/
|
||||
std::string_view get_expect_str() const noexcept;
|
||||
|
||||
|
|
@ -212,6 +212,7 @@ namespace xo {
|
|||
/** @defgroup scm-define-printable-facet printable facet methods **/
|
||||
///@{
|
||||
|
||||
/** pretty-printer support **/
|
||||
bool pretty(const ppindentinfo & ppii) const;
|
||||
|
||||
///@}
|
||||
|
|
|
|||
|
|
@ -41,6 +41,34 @@ namespace xo {
|
|||
bool allow_defs() const noexcept { return allow_defs_; }
|
||||
bool cxl_on_rightbrace() const noexcept { return cxl_on_rightbrace_; }
|
||||
|
||||
///@}
|
||||
/** @defgroup scm-expectexpr-methods general methods **/
|
||||
///@{
|
||||
|
||||
/** step state machine for this syntax on incoming boolean literal token @p tkk
|
||||
* with overall parser state in @p p_psm
|
||||
**/
|
||||
void on_bool_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state for this syntax on incoming f64 token @p tk,
|
||||
* overall parser state in @p p_psm
|
||||
**/
|
||||
void on_f64_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state for this syntax on incoming i64 token @p tk,
|
||||
* overall parser state in @p p_psm
|
||||
**/
|
||||
void on_i64_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state for this syntax on incoming string token @p tk,
|
||||
* overall parser state in @p p_psm
|
||||
**/
|
||||
void on_string_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
///@}
|
||||
/** @defgroup scm-expectexpr-ssm-facet syntaxstatemachine facet methods **/
|
||||
///@{
|
||||
|
|
@ -89,30 +117,6 @@ namespace xo {
|
|||
void on_singleassign_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state for this syntax on incoming string token @p tk,
|
||||
* overall parser state in @p p_psm
|
||||
**/
|
||||
void on_string_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state for this syntax on incoming f64 token @p tk,
|
||||
* overall parser state in @p p_psm
|
||||
**/
|
||||
void on_f64_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state for this syntax on incoming i64 token @p tk,
|
||||
* overall parser state in @p p_psm
|
||||
**/
|
||||
void on_i64_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state for this syntax on incoming bool token @p tk,
|
||||
* overall parser state in @p p_psm
|
||||
**/
|
||||
void on_bool_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state for this syntax on incoming semicolon token @p tk,
|
||||
* overall parser state in @p p_psm
|
||||
**/
|
||||
|
|
|
|||
221
xo-reader2/include/xo/reader2/DIfElseSsm.hpp
Normal file
221
xo-reader2/include/xo/reader2/DIfElseSsm.hpp
Normal file
|
|
@ -0,0 +1,221 @@
|
|||
/** @file DIfElseSsm.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Jul 2025
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <xo/expression2/DIfElseExpr.hpp>
|
||||
#include "ParserStateMachine.hpp"
|
||||
#include "syntaxstatetype.hpp"
|
||||
#include <xo/expression2/detail/IExpression_DIfElseExpr.hpp>
|
||||
#include <xo/expression2/DIfElseExpr.hpp>
|
||||
#include <xo/facet/obj.hpp>
|
||||
//#include "exprstate.hpp"
|
||||
//#include "xo/indentlog/print/ppdetail_atomic.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/**
|
||||
* if test-expr then then-expr else else-expr ;
|
||||
* ^ ^ ^ ^ ^ ^ ^
|
||||
* | | | | | | |
|
||||
* | if_1 if_2 if_3 if_4 if_5 if_6
|
||||
* if_0
|
||||
*
|
||||
* if_0 --on_if_token()--> if_1
|
||||
* if_1 --on_expr()--> if_2
|
||||
* if_2 --on_then_token()--> if_3
|
||||
* if_3 --on_expr()--> if_4
|
||||
* if_4 --on_else_token()--> if_5
|
||||
* --on_semicolon_token()--> (done)
|
||||
* if_5 --on_expr()-->if_6
|
||||
* if_6 --on_semicolon_token()--> (done)
|
||||
**/
|
||||
enum class ifexprstatetype {
|
||||
invalid = -1,
|
||||
|
||||
if_0,
|
||||
if_1,
|
||||
if_2,
|
||||
if_3,
|
||||
if_4,
|
||||
if_5,
|
||||
if_6,
|
||||
|
||||
N,
|
||||
};
|
||||
|
||||
extern const char * ifexprstatetype_descr(ifexprstatetype x);
|
||||
|
||||
std::ostream &
|
||||
operator<<(std::ostream & os, ifexprstatetype x);
|
||||
|
||||
/** @class DIfElseSsm
|
||||
* @brief syntax state machine for parsing a conditional expression
|
||||
**/
|
||||
class DIfElseSsm {
|
||||
public:
|
||||
using AAllocator = xo::mm::AAllocator;
|
||||
using DArena = xo::mm::DArena;
|
||||
using TypeDescr = xo::reflect::TypeDescr;
|
||||
using ppindentinfo = xo::print::ppindentinfo;
|
||||
|
||||
public:
|
||||
/** @defgroup scm-ifelsessm-expression-ctors constructors **/
|
||||
///@{
|
||||
explicit DIfElseSsm(DIfElseExpr * ifelse_expr);
|
||||
|
||||
#ifdef NOT_YET
|
||||
/** create instance using memory from @p parser_mm
|
||||
* with initial scaffold @p ifelse_expr
|
||||
**/
|
||||
static obj<AExpression,DIfElseSsm> make(DArena & parser_mm,
|
||||
DIfElseExpr * ifelse_expr);
|
||||
#endif
|
||||
|
||||
/** create instance using memory from @p parser_mm
|
||||
* with initial scaffold @p ifelse_expr.
|
||||
**/
|
||||
static DIfElseSsm * _make(DArena & parser_mm,
|
||||
DIfElseExpr * ifelse_expr);
|
||||
|
||||
/** start nested parser for an if-else expression
|
||||
* on top of parser state machine @p p_psm.
|
||||
* Use @p parser_mm to allocate syntax state machines
|
||||
* (i.e. temporary memory needed only during parsing)
|
||||
* Use @p expr_mm to allocate expressions.
|
||||
**/
|
||||
static void start(DArena & parser_mm,
|
||||
obj<AAllocator> expr_mm,
|
||||
ParserStateMachine * p_psm);
|
||||
///@}
|
||||
/** @defgroup scm-ifelsessm-expression-methods general methods **/
|
||||
///@{
|
||||
|
||||
/** operate state machine on if-token input @p tk,
|
||||
* with overall parser state in @p p_psm
|
||||
**/
|
||||
void on_if_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** operate state machine on then-token input @p tk,
|
||||
* with overall parser state in @p p_psm
|
||||
**/
|
||||
void on_then_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** operate state machine on else-token input @p tk,
|
||||
* with overall parser state in @p p_psm
|
||||
**/
|
||||
void on_else_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** victory: report completed @ref if_expr_ to parent ssm,
|
||||
* after removing this ssm from parser stack
|
||||
**/
|
||||
void finish_and_continue(ParserStateMachine * p_psm);
|
||||
|
||||
///@}
|
||||
/** @defgroup scm-ifelsessm-expression-facet expression facet methods **/
|
||||
///@{
|
||||
|
||||
/** identifies this state machine **/
|
||||
syntaxstatetype ssm_type() const noexcept;
|
||||
|
||||
/** text describing expected/allowed input to this ssm in current state.
|
||||
* Intended to drive error messages
|
||||
**/
|
||||
std::string_view get_expect_str() const noexcept;
|
||||
|
||||
/** operate state machine for this syntax on incoming token @p tk
|
||||
* with overall parser state in @p p_psm
|
||||
**/
|
||||
void on_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** operate state machine for this syntax on incoming semicolon token @p tk
|
||||
* with overall parser state in @p p_psm
|
||||
**/
|
||||
void on_semicolon_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state for this syntax after parsing an expression @p expr,
|
||||
* overall parser state in @p p_psm.
|
||||
**/
|
||||
void on_parsed_expression(obj<AExpression> expr,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state for this syntax after parsing a type description @p td;
|
||||
* overall parser state in @p p_psm
|
||||
**/
|
||||
void on_parsed_typedescr(TypeDescr td,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state for this syntax after parsing an expression @p expr,
|
||||
* followed by semicolon,
|
||||
* with overall parser state in @p p_psm.
|
||||
**/
|
||||
void on_parsed_expression_with_semicolon(obj<AExpression> expr,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state for this syntax after parsing a symbol @p sym,
|
||||
* with overall parser state in @p p_psm
|
||||
**/
|
||||
void on_parsed_symbol(std::string_view sym,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
///@}
|
||||
/** @defgroup scm-ifelsessm-printable-facet printable facet methods **/
|
||||
///@{
|
||||
|
||||
bool pretty(const ppindentinfo & ppii) const;
|
||||
|
||||
///@}
|
||||
|
||||
#ifdef NOT_YET
|
||||
// ----- inherited from exprstate -----
|
||||
|
||||
virtual const char * get_expect_str() const override;
|
||||
|
||||
virtual void on_if_token(const token_type & tk,
|
||||
parserstatemachine * p_psm) override;
|
||||
virtual void on_then_token(const token_type & tk,
|
||||
parserstatemachine * p_psm) override;
|
||||
virtual void on_else_token(const token_type & tk,
|
||||
parserstatemachine * p_psm) override;
|
||||
virtual void on_semicolon_token(const token_type & tk,
|
||||
parserstatemachine * p_psm) override;
|
||||
virtual void on_rightbrace_token(const token_type & tk,
|
||||
parserstatemachine * p_psm) override;
|
||||
|
||||
virtual void on_expr(bp<Expression> expr,
|
||||
parserstatemachine * p_psm) override;
|
||||
virtual void on_expr_with_semicolon(bp<Expression> expr,
|
||||
parserstatemachine * p_psm) override;
|
||||
|
||||
virtual void print(std::ostream & os) const override;
|
||||
#endif
|
||||
|
||||
private:
|
||||
#ifdef NOT_YET
|
||||
static std::unique_ptr<if_else_xs> make();
|
||||
|
||||
/** exit this exprstate,
|
||||
* and deliver @ref if_expr_ to parent exprstate
|
||||
**/
|
||||
void finish_and_continue(parserstatemachine * p_psm);
|
||||
#endif
|
||||
|
||||
private:
|
||||
ifexprstatetype ifstate_ = ifexprstatetype::invalid;
|
||||
/** scaffold ifelse-expression here.
|
||||
* This will eventually be the output of this ssm
|
||||
**/
|
||||
obj<AExpression,DIfElseExpr> if_expr_;
|
||||
|
||||
};
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end DIfElseSsm.hpp */
|
||||
|
|
@ -126,6 +126,17 @@ namespace xo {
|
|||
**/
|
||||
obj<AExpression> assemble_expr(ParserStateMachine * p_psm);
|
||||
|
||||
/** @defgroup scm-progressssm-methods general methods **/
|
||||
///@{
|
||||
|
||||
void on_if_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
void on_then_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
void on_else_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
///@}
|
||||
/** @defgroup scm-progressssm-ssm-facet syntaxstatemachine facet methods **/
|
||||
/// @{
|
||||
|
||||
|
|
@ -139,8 +150,6 @@ namespace xo {
|
|||
ParserStateMachine * p_psm);
|
||||
void on_def_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
void on_if_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
void on_colon_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
void on_singleassign_token(const Token & tk,
|
||||
|
|
|
|||
62
xo-reader2/include/xo/reader2/ssm/IPrintable_DIfElseSsm.hpp
Normal file
62
xo-reader2/include/xo/reader2/ssm/IPrintable_DIfElseSsm.hpp
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
/** @file IPrintable_DIfElseSsm.hpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [/home/roland/proj/xo-umbrella2-claude1/xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/IPrintable_DIfElseSsm.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [iface_facet_repr.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/IPrintable_DIfElseSsm.json5]
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Printable.hpp"
|
||||
#include <xo/printable2/Printable.hpp>
|
||||
#include <xo/printable2/detail/IPrintable_Xfer.hpp>
|
||||
#include "DIfElseSsm.hpp"
|
||||
|
||||
namespace xo { namespace scm { class IPrintable_DIfElseSsm; } }
|
||||
|
||||
namespace xo {
|
||||
namespace facet {
|
||||
template <>
|
||||
struct FacetImplementation<xo::print::APrintable,
|
||||
xo::scm::DIfElseSsm>
|
||||
{
|
||||
using ImplType = xo::print::IPrintable_Xfer
|
||||
<xo::scm::DIfElseSsm,
|
||||
xo::scm::IPrintable_DIfElseSsm>;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** @class IPrintable_DIfElseSsm
|
||||
**/
|
||||
class IPrintable_DIfElseSsm {
|
||||
public:
|
||||
/** @defgroup scm-printable-difelsessm-type-traits **/
|
||||
///@{
|
||||
using ppindentinfo = xo::print::APrintable::ppindentinfo;
|
||||
using Copaque = xo::print::APrintable::Copaque;
|
||||
using Opaque = xo::print::APrintable::Opaque;
|
||||
///@}
|
||||
/** @defgroup scm-printable-difelsessm-methods **/
|
||||
///@{
|
||||
// const methods
|
||||
/** Pretty-printing support for this object.
|
||||
See [xo-indentlog/xo/indentlog/pretty.hpp] **/
|
||||
static bool pretty(const DIfElseSsm & self, const ppindentinfo & ppii);
|
||||
|
||||
// non-const methods
|
||||
///@}
|
||||
};
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end */
|
||||
|
|
@ -0,0 +1,73 @@
|
|||
/** @file ISyntaxStateMachine_DIfElseSsm.hpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [/home/roland/proj/xo-umbrella2-claude1/xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/ISyntaxStateMachine_DIfElseSsm.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [iface_facet_repr.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/ISyntaxStateMachine_DIfElseSsm.json5]
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "SyntaxStateMachine.hpp"
|
||||
#include "SyntaxStateMachine.hpp"
|
||||
#include "ssm/ISyntaxStateMachine_Xfer.hpp"
|
||||
#include "DIfElseSsm.hpp"
|
||||
|
||||
namespace xo { namespace scm { class ISyntaxStateMachine_DIfElseSsm; } }
|
||||
|
||||
namespace xo {
|
||||
namespace facet {
|
||||
template <>
|
||||
struct FacetImplementation<xo::scm::ASyntaxStateMachine,
|
||||
xo::scm::DIfElseSsm>
|
||||
{
|
||||
using ImplType = xo::scm::ISyntaxStateMachine_Xfer
|
||||
<xo::scm::DIfElseSsm,
|
||||
xo::scm::ISyntaxStateMachine_DIfElseSsm>;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** @class ISyntaxStateMachine_DIfElseSsm
|
||||
**/
|
||||
class ISyntaxStateMachine_DIfElseSsm {
|
||||
public:
|
||||
/** @defgroup scm-syntaxstatemachine-difelsessm-type-traits **/
|
||||
///@{
|
||||
using TypeDescr = xo::scm::ASyntaxStateMachine::TypeDescr;
|
||||
using Copaque = xo::scm::ASyntaxStateMachine::Copaque;
|
||||
using Opaque = xo::scm::ASyntaxStateMachine::Opaque;
|
||||
///@}
|
||||
/** @defgroup scm-syntaxstatemachine-difelsessm-methods **/
|
||||
///@{
|
||||
// const methods
|
||||
/** identify a type of syntax state machine **/
|
||||
static syntaxstatetype ssm_type(const DIfElseSsm & self) noexcept;
|
||||
/** text describing expected/allowed input to this ssm in current state **/
|
||||
static std::string_view get_expect_str(const DIfElseSsm & self) noexcept;
|
||||
|
||||
// non-const methods
|
||||
/** operate state machine for incoming token @p tk **/
|
||||
static void on_token(DIfElseSsm & self, const Token & tk, ParserStateMachine * p_psm);
|
||||
/** update stat machine for incoming parsed symbol @p sym **/
|
||||
static void on_parsed_symbol(DIfElseSsm & self, std::string_view sym, ParserStateMachine * p_psm);
|
||||
/** operate state machine for incoming type description @p td **/
|
||||
static void on_parsed_typedescr(DIfElseSsm & self, TypeDescr td, ParserStateMachine * p_psm);
|
||||
/** update state machine for incoming parsed expression @p expr **/
|
||||
static void on_parsed_expression(DIfElseSsm & self, obj<AExpression> expr, ParserStateMachine * p_psm);
|
||||
/** update state machine for incoming parsed expression @p expr followed by semicolon **/
|
||||
static void on_parsed_expression_with_semicolon(DIfElseSsm & self, obj<AExpression> expr, ParserStateMachine * p_psm);
|
||||
///@}
|
||||
};
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end */
|
||||
|
|
@ -33,6 +33,9 @@ namespace xo {
|
|||
/** handle define-expression. See @ref DDefineSsm **/
|
||||
defexpr,
|
||||
|
||||
/** handle ifelse-expression. See @ref DIfElseSsm **/
|
||||
ifelseexpr,
|
||||
|
||||
/** rhs expression. state exists to achieve 1-token lookahead **/
|
||||
progress,
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue