xo-reader2 stack: expand symbol table to store typedefs

+ typedef utest
+ misc qol policy choices
This commit is contained in:
Roland Conybeare 2026-03-11 07:49:14 -05:00
commit 76af3ff3b5
42 changed files with 1050 additions and 110 deletions

View file

@ -0,0 +1,186 @@
/** @file DDeftypeSsm.hpp
*
* @author Roland Conybeare, Mar 2026
**/
#pragma once
#include "DSyntaxStateMachine.hpp"
#include "syntaxstatetype.hpp"
#include <xo/facet/obj.hpp>
#include <string_view>
namespace xo {
namespace scm {
/**
* @pre
*
* deftype foo :: f64 ;
* ^ ^ ^ ^ ^ ^
* | | | | | (done)
* | | | | def_4
* | | | def_3
* | | def_2
* | def_1
* def_0
*
* def_0 --on_deftype_token()--> def_1 [expect symbol]
* def_1 --on_symbol_token()--> def_2 [expect ::]
* def_2 --on_doublecolon_token()--> def_3 [start ExpectTypeSsm]
* def_3 --on_parsed_type()--> def_4 [++ symbol table]
* def_4 --on_semicolon_token()--> (done) [pop]
*
* @endpre
**/
class DeftypeXst {
public:
enum class code {
invalid = -1,
def_0,
def_1,
def_2,
def_3,
def_4,
N,
};
explicit DeftypeXst(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_;
};
std::ostream &
operator<<(std::ostream & os, DeftypeXst x);
/** @class DDeftypeSsm
* @brief state machine for parsing a deftype expression
**/
class DDeftypeSsm : public DSyntaxStateMachine<DDeftypeSsm> {
public:
using Super = DSyntaxStateMachine<DDeftypeSsm>;
using TypeDescr = xo::reflect::TypeDescr;
using AAllocator = xo::mm::AAllocator;
using DArena = xo::mm::DArena;
using ppindentinfo = xo::print::ppindentinfo;
public:
/** @defgroup scm-deftypessm-ctors constructors **/
///@{
/** constructor; using @p def_expr for initial expression scaffold **/
explicit DDeftypeSsm();
/** Create instance using memory from @p parser_mm **/
static DDeftypeSsm * _make(DArena & parser_mm);
/** create fop referring to new DDeftypeSsm **/
static obj<ASyntaxStateMachine,DDeftypeSsm> make(DArena & parser_mm);
/** start nested parser for a define-expression,
* on top of parser state machine @p p_psm
* Use @p parser_mm to allocate syntax state machines,
* and @p expr_mm to allocate expressions
**/
static void start(DArena & parser_mm,
//obj<AAllocator> expr_mm,
ParserStateMachine * p_psm);
///@}
/** @defgroup scm-definessm-access-methods **/
///@{
/** identify this nested state machine **/
static const char * ssm_classname() { return "DDeftypeSsm"; }
/** internal state **/
DeftypeXst deftypestate() const noexcept { return deftype_xst_; }
///@}
/** @defgroup scm-definessm-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 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);
/** update state for this syntax on incoming @c deftype token @p tk,
* overall parser state in @p p_psm
**/
void on_deftype_token(const Token & tk,
ParserStateMachine * p_psm);
/** update state for this syntax on incoming double-colon token @p tk,
* overall parser state in @p p_psm
**/
void on_doublecolon_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);
/** update state for this syntax after type @p type emitted by nested
* state machine, with overall parser state in @p p_psm
**/
void on_parsed_type(obj<AType> type,
ParserStateMachine * p_psm);
#ifdef NOT_YET
/** 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);
#endif
/** update state for this syntax after parsing semicolon token @p tk,
* overall parser state in @p p_psm.
* if state def_4 completes deftype statement.
**/
void on_semicolon_token(const Token & tk,
ParserStateMachine * p_psm);
///@}
/** @defgroup scm-define-printable-facet printable facet methods **/
///@{
/** pretty-printer support **/
bool pretty(const ppindentinfo & ppii) const;
///@}
private:
/** @defgroup scm-deftypessm-member-vars **/
///@{
/** identify deftype ssm state **/
DeftypeXst deftype_xst_;
/** lhs symbol for type definition **/
const DUniqueString * lhs_symbol_ = nullptr;
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end DDefineSsm.hpp */

View file

@ -30,14 +30,14 @@ namespace xo {
using ppindentinfo = xo::print::ppindentinfo;
public:
DExpectTypeSsm();
explicit DExpectTypeSsm(bool corrected);
static DExpectTypeSsm * _make(DArena & parser_mm);
static DExpectTypeSsm * _make(DArena & parser_mm, bool corrected);
/** create fop referring to new DExpectTypeSsm **/
static obj<ASyntaxStateMachine,DExpectTypeSsm> make(DArena & parser_mm);
static obj<ASyntaxStateMachine,DExpectTypeSsm> make(DArena & parser_mm, bool corrected);
static void start(ParserStateMachine * p_psm);
static void start(bool corrected, ParserStateMachine * p_psm);
static const char * ssm_classname() { return "DExpectTypeSsm"; }
@ -72,6 +72,12 @@ namespace xo {
///@}
private:
/** temporary shim.
* if true, construct obj<AType>
* if false, construct TypeDescr
**/
bool corrected_ = false;
};
} /*namespace scm*/
} /*namespace xo*/

View file

@ -32,7 +32,6 @@ namespace xo {
*
* @endpre
**/
class QuoteXst {
public:
enum class code {

View file

@ -48,6 +48,7 @@ namespace xo {
void illegal_type(obj<AType> type,
ParserStateMachine * p_psm)
{
// starting with c++23 can use "this auto&& self" instead
Derived & self = static_cast<Derived &>(*this);
p_psm->illegal_input_on_type(Derived::ssm_classname(),
@ -55,6 +56,18 @@ namespace xo {
self.get_expect_str());
}
/** Explicit error path **/
void illegal_parsed_symbol(std::string_view sym,
ParserStateMachine * p_psm)
{
// starting with c++23 can use "this auto&& self" instead
Derived & self = static_cast<Derived &>(*this);
p_psm->illegal_input_on_symbol(Derived::ssm_classname(),
sym,
self.get_expect_str());
}
/** Explicit error path **/
void illegal_quoted_literal(obj<AGCObject> lit,
ParserStateMachine * p_psm)
@ -78,12 +91,7 @@ namespace xo {
void on_parsed_symbol(std::string_view sym,
ParserStateMachine * p_psm)
{
// starting with c++23 can use "this auto&& self" instead
Derived & self = reinterpret_cast<Derived&>(*this);
p_psm->illegal_input_on_symbol(Derived::ssm_classname(),
sym,
self.get_expect_str());
this->illegal_parsed_symbol(sym, p_psm);
}
/** Default implementation for required SyntaxStateMachine facet method

View file

@ -84,6 +84,11 @@ namespace xo {
**/
void on_def_token(const Token & tk, ParserStateMachine * p_psm);
/** update state for this syntax on incoming @c deftype token @p tk,
* with overall parser state in @p p_psm
**/
void on_deftype_token(const Token & tk, ParserStateMachine * p_psm);
/** update state for this syntax on incoming lamdba token @p tk,
* overall parser state in @p p_psm
**/

View file

@ -0,0 +1,12 @@
/** @file DeftypeSsm.hpp
*
* @author Roland Conybeare, Mar 2026
**/
#pragma once
#include "DDeftypeSsm.hpp"
#include "ssm/ISyntaxStateMachine_DDeftypeSsm.hpp"
#include "ssm/IPrintable_DDeftypeSsm.hpp"
/* end DeftypeSsm.hpp */

View file

@ -24,7 +24,7 @@ namespace xo {
.header_{},
.debug_flag_ = false };
/** configuration for hash map for global symbol table
/** configuration for hash map for global symbol table (variables)
*
* reminder: ownership chain
* SchematikaReader
@ -32,9 +32,25 @@ namespace xo {
* ->ParserStateMachine
* ->DGlobalSymtab
**/
ArenaHashMapConfig symtab_config_ { .name_ = "global-symtab",
.hint_max_capacity_ = 64*1024,
.debug_flag_ = false };
ArenaHashMapConfig symtab_var_config_ {
.name_ = "global-vars",
.hint_max_capacity_ = 64*1024,
.debug_flag_ = false
};
/** configuration for hash map for global symbol table (types)
*
* reminder: ownership chain
* SchematikaReader
* ->SchematikaParser
* ->ParserStateMachine
* ->DGlobalSymtab
**/
ArenaHashMapConfig symtab_types_config_ {
.name_ = "global-types",
.hint_max_capacity_ = 32*1024,
.debug_flag_ = false
};
/** max capacity for unique string table **/
size_t max_stringtable_capacity_ = 4096;

View file

@ -47,8 +47,10 @@ namespace xo {
public:
/**
* @p config arena configuration for parser state
* @p symtab_config configuration for global symtab
* @p symtab_var_config configuration for global symtab variables
* (maps separate dedicated memory)
* @p symtab_type_config configuration for global symtab types
* (maps to separate dedicated memory)
* @p max_stringtable_capacity
* hard max size for unique stringtable
* @p expr_alloc allocator for schematika expressions.
@ -59,7 +61,8 @@ namespace xo {
* same as @p expr_alloc.
**/
ParserStateMachine(const ArenaConfig & config,
const ArenaHashMapConfig & symtab_config,
const ArenaHashMapConfig & symtab_var_config,
const ArenaHashMapConfig & symtab_type_config,
size_type max_stringtable_capacity,
obj<AAllocator> expr_alloc,
obj<AAllocator> aux_alloc);
@ -151,6 +154,9 @@ namespace xo {
**/
void on_parsed_typedescr(TypeDescr td);
/** respond to type emitted by nested ssm **/
void on_parsed_type(obj<AType> type);
/** update state to consume param (name, type) emitted by
* nested (expired) parsing state
**/

View file

@ -37,7 +37,7 @@ namespace xo {
.header_{},
.debug_flag_ = false };
/** configuration for hash map for global symbol table
/** configuration for hash map for global symbol table (variables)
*
* reminder: ownership chain
* SchematikaReader
@ -45,9 +45,25 @@ namespace xo {
* ->ParserStateMachine
* ->DGlobalSymtab
**/
ArenaHashMapConfig symtab_config_ { .name_ = "global-symtab",
.hint_max_capacity_ = 64*1024,
.debug_flag_ = false };
ArenaHashMapConfig symtab_var_config_ {
.name_ = "global-vars",
.hint_max_capacity_ = 64*1024,
.debug_flag_ = false,
};
/** configuration for hash map for global symbol table (types)
*
* reminder: ownership chain
* SchematikaReader
* ->SchematikaParser
* ->ParserStateMachine
* ->DGlobalSymtab
**/
ArenaHashMapConfig symtab_types_config_ {
.name_ = "global-types",
.hint_max_capacity_ = 32*1024,
.debug_flag_ = false,
};
/** debug flag for schematika parser **/
bool parser_debug_flag_ = false;

View file

@ -0,0 +1,62 @@
/** @file IPrintable_DDeftypeSsm.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IPrintable_DDeftypeSsm.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_repr.hpp.j2]
* 3. idl for facet methods
* [idl/IPrintable_DDeftypeSsm.json5]
**/
#pragma once
#include "Printable.hpp"
#include <xo/printable2/Printable.hpp>
#include <xo/printable2/detail/IPrintable_Xfer.hpp>
#include "DDeftypeSsm.hpp"
namespace xo { namespace scm { class IPrintable_DDeftypeSsm; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::print::APrintable,
xo::scm::DDeftypeSsm>
{
using ImplType = xo::print::IPrintable_Xfer
<xo::scm::DDeftypeSsm,
xo::scm::IPrintable_DDeftypeSsm>;
};
}
}
namespace xo {
namespace scm {
/** @class IPrintable_DDeftypeSsm
**/
class IPrintable_DDeftypeSsm {
public:
/** @defgroup scm-printable-ddeftypessm-type-traits **/
///@{
using ppindentinfo = xo::print::APrintable::ppindentinfo;
using Copaque = xo::print::APrintable::Copaque;
using Opaque = xo::print::APrintable::Opaque;
///@}
/** @defgroup scm-printable-ddeftypessm-methods **/
///@{
// const methods
/** Pretty-printing support for this object.
See [xo-indentlog/xo/indentlog/pretty.hpp] **/
static bool pretty(const DDeftypeSsm & self, const ppindentinfo & ppii);
// non-const methods
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end */

View file

@ -0,0 +1,84 @@
/** @file ISyntaxStateMachine_DDeftypeSsm.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/ISyntaxStateMachine_DDeftypeSsm.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_repr.hpp.j2]
* 3. idl for facet methods
* [idl/ISyntaxStateMachine_DDeftypeSsm.json5]
**/
#pragma once
#include "SyntaxStateMachine.hpp"
#include "SyntaxStateMachine.hpp"
#include "ssm/ISyntaxStateMachine_Xfer.hpp"
#include "DDeftypeSsm.hpp"
namespace xo { namespace scm { class ISyntaxStateMachine_DDeftypeSsm; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::scm::ASyntaxStateMachine,
xo::scm::DDeftypeSsm>
{
using ImplType = xo::scm::ISyntaxStateMachine_Xfer
<xo::scm::DDeftypeSsm,
xo::scm::ISyntaxStateMachine_DDeftypeSsm>;
};
}
}
namespace xo {
namespace scm {
/** @class ISyntaxStateMachine_DDeftypeSsm
**/
class ISyntaxStateMachine_DDeftypeSsm {
public:
/** @defgroup scm-syntaxstatemachine-ddeftypessm-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-ddeftypessm-methods **/
///@{
// const methods
/** identify a type of syntax state machine **/
static syntaxstatetype ssm_type(const DDeftypeSsm & self) noexcept;
/** text describing expected/allowed input to this ssm in current state **/
static std::string_view get_expect_str(const DDeftypeSsm & self) noexcept;
// non-const methods
/** operate state machine for incoming token @p tk **/
static void on_token(DDeftypeSsm & self, const Token & tk, ParserStateMachine * p_psm);
/** update stat machine for incoming parsed symbol @p sym **/
static void on_parsed_symbol(DDeftypeSsm & self, std::string_view sym, ParserStateMachine * p_psm);
/** operate state machine for incoming type description @p td **/
static void on_parsed_typedescr(DDeftypeSsm & self, TypeDescr td, ParserStateMachine * p_psm);
/** update state machine for type emitted by nested ssm **/
static void on_parsed_type(DDeftypeSsm & self, obj<AType> type, ParserStateMachine * p_psm);
/** operate state machine for formal emitted by nested ssm **/
static void on_parsed_formal(DDeftypeSsm & 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(DDeftypeSsm & 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(DDeftypeSsm & self, DArray * arglist, ParserStateMachine * p_psm);
/** update state machine for nested parsed expression @p expr **/
static void on_parsed_expression(DDeftypeSsm & 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(DDeftypeSsm & self, obj<AExpression> expr, const Token & tk, ParserStateMachine * p_psm);
/** update state machine for nested quoted literal @p lit **/
static void on_quoted_literal(DDeftypeSsm & self, obj<AGCObject> lit, ParserStateMachine * p_psm);
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end */

View file

@ -21,6 +21,9 @@ namespace xo {
/** handle define-expression. See @ref DDefineSsm **/
defexpr,
/** handle deftype-expression. See @ref DDeftypeSsm **/
deftypeexpr,
/** handle lambda-expression. See @ref DLambdaSsm **/
lambdaexpr,