xo-reader2: #q supports literal dictionaries
This commit is contained in:
parent
90c4819ef9
commit
25fd378c78
19 changed files with 919 additions and 57 deletions
|
|
@ -7,7 +7,6 @@
|
|||
|
||||
#include "DSyntaxStateMachine.hpp"
|
||||
#include <xo/object2/ListOps.hpp>
|
||||
//#include <xo/arena/DArena.hpp>
|
||||
#include <xo/facet/obj.hpp>
|
||||
|
||||
namespace xo {
|
||||
|
|
@ -16,13 +15,14 @@ namespace xo {
|
|||
* Already in quoted-literal context
|
||||
*
|
||||
* ( quote-expr ... )
|
||||
* ^ ^ ^
|
||||
* | qlist_1a qlist_2(done)
|
||||
* qlist_0
|
||||
* ^ ^ ^ ^
|
||||
* | qlist_1a | qlist_2(done)
|
||||
* qlist_0 qlist_1a
|
||||
*
|
||||
* qlist_0 --on_leftparen_token()--> qlist_1a [push ExpectQLiteralSsm]
|
||||
* qlist_1a --on_quoted_literal()--> qlist_1a [append literal]
|
||||
* qlist_1a --on_rightparen_token()--> qlist_2(done) [report quoted list]
|
||||
* qlist_2 end state
|
||||
**/
|
||||
class QListXst {
|
||||
public:
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "DSyntaxStateMachine.hpp"
|
||||
//#include <xo/object2/DArray.hpp>
|
||||
//#include <xo/arena/DArena.hpp>
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
|
|
@ -46,17 +44,23 @@ namespace xo {
|
|||
static const char * ssm_classname() { return "DExpectQLiteralSsm"; }
|
||||
|
||||
/** update state for f64 token @p tk, with overall parser state in @p p_psm.
|
||||
* delegates to parent ssm via @ref on_quoted_literal
|
||||
* completes syntax + delegates to parent ssm via @ref on_quoted_literal
|
||||
**/
|
||||
void on_f64_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state for i64 token @p tk, with overall parser state in @p p_psm.
|
||||
* delegates to parent ssm via @ref on_quoted_literal
|
||||
* completes syntax + delegates to parent ssm via @ref on_quoted_literal
|
||||
**/
|
||||
void on_i64_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state for string token @p tk, with overall parser state in @p p_psm.
|
||||
* completes syntax + delegates to parent ssm via @ref on_quoted_literal
|
||||
**/
|
||||
void on_string_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state on incoming token @p tk,
|
||||
* with overall parser state in @p p_psm.
|
||||
*
|
||||
|
|
@ -89,6 +93,14 @@ namespace xo {
|
|||
void on_rightbracket_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state on incoming leftbrace token @p tk,
|
||||
* with overall parser state in @p p_psm.
|
||||
*
|
||||
* Forward in-place to ExpectQDictSsm
|
||||
**/
|
||||
void on_leftbrace_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
///@}
|
||||
/** @defgroup scm-expectformalarglistssm-ssm-facet syntaxstatemachine facet methods **/
|
||||
///@{
|
||||
|
|
|
|||
12
include/xo/reader2/ExpectQDictSsm.hpp
Normal file
12
include/xo/reader2/ExpectQDictSsm.hpp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
/** @file ExpectQDictSsm.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Mar 2026
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "expect_qdict/DExpectQDictSsm.hpp"
|
||||
#include "expect_qdict/ISyntaxStateMachine_DExpectQDictSsm.hpp"
|
||||
#include "expect_qdict/IPrintable_DExpectQDictSsm.hpp"
|
||||
|
||||
/* end ExpectQDictSsm.hpp */
|
||||
178
include/xo/reader2/expect_qdict/DExpectQDictSsm.hpp
Normal file
178
include/xo/reader2/expect_qdict/DExpectQDictSsm.hpp
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
/** @file DExpectQDictSsm.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Mar 2026
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "DSyntaxStateMachine.hpp"
|
||||
#include <xo/object2/Dictionary.hpp>
|
||||
#include <xo/facet/obj.hpp>
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/**
|
||||
* When this syntax is active, parser is already in a quoted-literal context
|
||||
*
|
||||
* { key(1) : value(1) ; ... ; }
|
||||
* ^ ^ ^ ^ ^ ^ ^ ^
|
||||
* | | | qdict_1c | | | qdict_2(done)
|
||||
* | | qdict_1b | | qdict_1d
|
||||
* | qdict_1a | qdict_1a
|
||||
* qdict_0 qdict_1d
|
||||
*
|
||||
* qdict_0 --on_leftbrace_token()--> qdict_1a [push ExpectSymbolSsm]
|
||||
* qdict_1a --on_parsed_symbol()--> qdict_1b [remember key]
|
||||
* qdict_1a --onrightbace_token()--> qdictZ(done)
|
||||
* qdict_1b --on_colon_token()--> qdict_1c [push ExpectQLiteralSsm]
|
||||
* qdict_1c --on_quoted_literal()--> qdict_1d [remember value]
|
||||
* qdict_1d --on_semicolon_token()--> qdict_1a [loop]
|
||||
* qdict_1d --on_rightbrace_token()--> qdict_2(done)
|
||||
**/
|
||||
class QDictXst {
|
||||
public:
|
||||
enum class code {
|
||||
invalid = -1,
|
||||
|
||||
qdict_0,
|
||||
qdict_1a,
|
||||
qdict_1b,
|
||||
qdict_1c,
|
||||
qdict_1d,
|
||||
qdict_2,
|
||||
|
||||
N
|
||||
};
|
||||
|
||||
explicit QDictXst(code x) : code_{x} {}
|
||||
|
||||
/** @return string representation for enum @p x **/
|
||||
static const char * _descr(code x);
|
||||
|
||||
code code() const noexcept { return code_; }
|
||||
|
||||
enum code code_;
|
||||
};
|
||||
|
||||
inline std::ostream &
|
||||
operator<< (std::ostream & os, QDictXst x) {
|
||||
os << QDictXst::_descr(x.code_);
|
||||
return os;
|
||||
}
|
||||
|
||||
class DExpectQDictSsm : public DSyntaxStateMachine<DExpectQDictSsm> {
|
||||
public:
|
||||
using Super = DSyntaxStateMachine<DExpectQDictSsm>;
|
||||
using DArena = xo::mm::DArena;
|
||||
using ppindentinfo = xo::print::ppindentinfo;
|
||||
|
||||
public:
|
||||
/** @defgroup scm-expectqdictssm-ctors constructors **/
|
||||
///@{
|
||||
|
||||
DExpectQDictSsm();
|
||||
|
||||
/** create instance using memory from @parser_mm **/
|
||||
static obj<ASyntaxStateMachine,DExpectQDictSsm> make(DArena & parser_mm);
|
||||
/** create instance using memory from @parser_mm **/
|
||||
static DExpectQDictSsm * _make(DArena & parser_mm);
|
||||
|
||||
/** start nested syntax for a quoted dictionary **/
|
||||
static void start(ParserStateMachine * p_psm);
|
||||
|
||||
///@}
|
||||
/** @defgroup scm-expectqdictssm-methods general methods **/
|
||||
///@{
|
||||
|
||||
static const char * ssm_classname() { return "DExpectQDictSsm"; }
|
||||
|
||||
/** update state on incoming leftbrace token @p tk,
|
||||
* with overall parser state in @p p_psm
|
||||
*
|
||||
* in qdict_0 advance state to qdict_1a; otherwise error
|
||||
**/
|
||||
void on_leftbrace_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state on parsed symbol @p sym emitted by nested ssm,
|
||||
* with overall parser state in @p p_psm
|
||||
*
|
||||
* in qdict_1a capture key string + advance to qdict_1b; otherwise error
|
||||
**/
|
||||
void on_symbol_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state on incoming colon token @p tk,
|
||||
* with overall parser state in @p p_psm
|
||||
*
|
||||
* in qdict_1b advance to qdict_1c + push ExpectQLiteralSsm; otherwise error
|
||||
**/
|
||||
void on_colon_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state in on incoming semicolon token @p tk,
|
||||
* with overall parser state in @p p_psm.
|
||||
*
|
||||
* in qdict_1d advance to qdict_1a (ready for another (key,value) pair); otherwise error
|
||||
**/
|
||||
void on_semicolon_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state on incoming rightbrace token @p tk,
|
||||
* with overall parser state in @p p_psm
|
||||
*
|
||||
* in qdict_1a complete syntax + report literal to parent ssm; otherwise error
|
||||
**/
|
||||
void on_rightbrace_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
///@}
|
||||
/** @defgroup scm-expectqdictssm-ssm-facet syntaxstatemachine facet methods **/
|
||||
///@{
|
||||
|
||||
/** identifies the ssm implemented here **/
|
||||
syntaxstatetype ssm_type() const noexcept;
|
||||
|
||||
/** mnemonic *for expected input (for this ssm) in current state **/
|
||||
std::string_view get_expect_str() const;
|
||||
|
||||
/** update state on incoming token @p tk, with overall parser state in @p p_psm **/
|
||||
void on_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
/** update state on quoted literal @p lit, with overall parser state in @p p_psm
|
||||
*
|
||||
* in qdict_1c capture (key,value) pair into dictionary + advance to qdict_1d; otherwise error
|
||||
**/
|
||||
void on_quoted_literal(obj<AGCObject> lit,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
///@}
|
||||
/** @defgroup scm-expectqdictssm-printable-facet printable facet methods **/
|
||||
///@{
|
||||
|
||||
bool pretty(const ppindentinfo & ppii) const;
|
||||
|
||||
///@}
|
||||
|
||||
private:
|
||||
/** @defgroup ssm-expectqdictssm-member-vars **/
|
||||
///@{
|
||||
|
||||
/** iddentifies qdict parsing state **/
|
||||
QDictXst state_;
|
||||
|
||||
/** captured key in next (key,value) pair;
|
||||
* expect to include in @ref dict_
|
||||
**/
|
||||
const DString * key_ = nullptr;
|
||||
|
||||
/** literal dictionary (assembled incrementally) **/
|
||||
DDictionary * dict_ = nullptr;
|
||||
|
||||
///@}
|
||||
};
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end DExpectQDictSsm.hpp */
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
/** @file IPrintable_DExpectQDictSsm.hpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/IPrintable_DExpectQDictSsm.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [iface_facet_repr.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/IPrintable_DExpectQDictSsm.json5]
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Printable.hpp"
|
||||
#include <xo/printable2/Printable.hpp>
|
||||
#include <xo/printable2/detail/IPrintable_Xfer.hpp>
|
||||
#include "DExpectQDictSsm.hpp"
|
||||
|
||||
namespace xo { namespace scm { class IPrintable_DExpectQDictSsm; } }
|
||||
|
||||
namespace xo {
|
||||
namespace facet {
|
||||
template <>
|
||||
struct FacetImplementation<xo::print::APrintable,
|
||||
xo::scm::DExpectQDictSsm>
|
||||
{
|
||||
using ImplType = xo::print::IPrintable_Xfer
|
||||
<xo::scm::DExpectQDictSsm,
|
||||
xo::scm::IPrintable_DExpectQDictSsm>;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** @class IPrintable_DExpectQDictSsm
|
||||
**/
|
||||
class IPrintable_DExpectQDictSsm {
|
||||
public:
|
||||
/** @defgroup scm-printable-dexpectqdictssm-type-traits **/
|
||||
///@{
|
||||
using ppindentinfo = xo::print::APrintable::ppindentinfo;
|
||||
using Copaque = xo::print::APrintable::Copaque;
|
||||
using Opaque = xo::print::APrintable::Opaque;
|
||||
///@}
|
||||
/** @defgroup scm-printable-dexpectqdictssm-methods **/
|
||||
///@{
|
||||
// const methods
|
||||
/** Pretty-printing support for this object.
|
||||
See [xo-indentlog/xo/indentlog/pretty.hpp] **/
|
||||
static bool pretty(const DExpectQDictSsm & self, const ppindentinfo & ppii);
|
||||
|
||||
// non-const methods
|
||||
///@}
|
||||
};
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end */
|
||||
|
|
@ -0,0 +1,84 @@
|
|||
/** @file ISyntaxStateMachine_DExpectQDictSsm.hpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/ISyntaxStateMachine_DExpectQDictSsm.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [iface_facet_repr.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/ISyntaxStateMachine_DExpectQDictSsm.json5]
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "SyntaxStateMachine.hpp"
|
||||
#include "SyntaxStateMachine.hpp"
|
||||
#include "ssm/ISyntaxStateMachine_Xfer.hpp"
|
||||
#include "DExpectQDictSsm.hpp"
|
||||
|
||||
namespace xo { namespace scm { class ISyntaxStateMachine_DExpectQDictSsm; } }
|
||||
|
||||
namespace xo {
|
||||
namespace facet {
|
||||
template <>
|
||||
struct FacetImplementation<xo::scm::ASyntaxStateMachine,
|
||||
xo::scm::DExpectQDictSsm>
|
||||
{
|
||||
using ImplType = xo::scm::ISyntaxStateMachine_Xfer
|
||||
<xo::scm::DExpectQDictSsm,
|
||||
xo::scm::ISyntaxStateMachine_DExpectQDictSsm>;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** @class ISyntaxStateMachine_DExpectQDictSsm
|
||||
**/
|
||||
class ISyntaxStateMachine_DExpectQDictSsm {
|
||||
public:
|
||||
/** @defgroup scm-syntaxstatemachine-dexpectqdictssm-type-traits **/
|
||||
///@{
|
||||
using TypeDescr = xo::scm::ASyntaxStateMachine::TypeDescr;
|
||||
using AGCObject = xo::scm::ASyntaxStateMachine::AGCObject;
|
||||
using Copaque = xo::scm::ASyntaxStateMachine::Copaque;
|
||||
using Opaque = xo::scm::ASyntaxStateMachine::Opaque;
|
||||
///@}
|
||||
/** @defgroup scm-syntaxstatemachine-dexpectqdictssm-methods **/
|
||||
///@{
|
||||
// const methods
|
||||
/** identify a type of syntax state machine **/
|
||||
static syntaxstatetype ssm_type(const DExpectQDictSsm & self) noexcept;
|
||||
/** text describing expected/allowed input to this ssm in current state **/
|
||||
static std::string_view get_expect_str(const DExpectQDictSsm & self) noexcept;
|
||||
|
||||
// non-const methods
|
||||
/** operate state machine for incoming token @p tk **/
|
||||
static void on_token(DExpectQDictSsm & self, const Token & tk, ParserStateMachine * p_psm);
|
||||
/** update stat machine for incoming parsed symbol @p sym **/
|
||||
static void on_parsed_symbol(DExpectQDictSsm & self, std::string_view sym, ParserStateMachine * p_psm);
|
||||
/** operate state machine for incoming type description @p td **/
|
||||
static void on_parsed_typedescr(DExpectQDictSsm & self, TypeDescr td, ParserStateMachine * p_psm);
|
||||
/** update state machine for type emitted by nested ssm **/
|
||||
static void on_parsed_type(DExpectQDictSsm & self, obj<AType> type, ParserStateMachine * p_psm);
|
||||
/** operate state machine for formal emitted by nested ssm **/
|
||||
static void on_parsed_formal(DExpectQDictSsm & self, const DUniqueString * param_name, TypeDescr param_type, ParserStateMachine * p_psm);
|
||||
/** operate state machine for formal emitted by nested ssm **/
|
||||
static void on_parsed_formal_with_token(DExpectQDictSsm & self, const DUniqueString * param_name, TypeDescr param_type, const Token & tk, ParserStateMachine * p_psm);
|
||||
/** consume formal arglist emitted by nested ssm **/
|
||||
static void on_parsed_formal_arglist(DExpectQDictSsm & self, DArray * arglist, ParserStateMachine * p_psm);
|
||||
/** update state machine for nested parsed expression @p expr **/
|
||||
static void on_parsed_expression(DExpectQDictSsm & self, obj<AExpression> expr, ParserStateMachine * p_psm);
|
||||
/** update state machine @p p_psm for incoming parsed expression @p expr followed by token @p tk **/
|
||||
static void on_parsed_expression_with_token(DExpectQDictSsm & self, obj<AExpression> expr, const Token & tk, ParserStateMachine * p_psm);
|
||||
/** update state machine for nested quoted literal @p lit **/
|
||||
static void on_quoted_literal(DExpectQDictSsm & self, obj<AGCObject> lit, ParserStateMachine * p_psm);
|
||||
///@}
|
||||
};
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end */
|
||||
|
|
@ -75,6 +75,9 @@ namespace xo {
|
|||
/** expecting a quoted array. See @ref DExpectQArraySsm **/
|
||||
expect_qarray,
|
||||
|
||||
/** expecting a quoted dictionary. See @ref DExpectQDictSsm **/
|
||||
expect_qdict,
|
||||
|
||||
/** comes last, counts number of valid enums **/
|
||||
N
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue