xo-reader2: bugfix: detecting toplevel for upsert global var

This commit is contained in:
Roland Conybeare 2026-02-16 19:07:37 -05:00
commit 9fd5bebae9
4 changed files with 45 additions and 5 deletions

View file

@ -678,6 +678,8 @@ namespace xo {
Super::on_parsed_expression_with_token(expr, tk, p_psm);
}
// ----- printable facet -----
bool
DDefineSsm::pretty(const ppindentinfo & ppii) const
{

View file

@ -6,6 +6,8 @@
#include "ParserStateMachine.hpp"
#include "ParserStack.hpp"
#include "SyntaxStateMachine.hpp"
#include "ToplevelSeqSsm.hpp"
#include "DefineSsm.hpp"
#include <xo/object2/array/IPrintable_DArray.hpp>
#include <xo/printable2/Printable.hpp>
#include <xo/alloc2/arena/IAllocator_DArena.hpp>
@ -39,16 +41,52 @@ namespace xo {
bool
ParserStateMachine::is_at_toplevel() const noexcept
{
return ((stack_ == nullptr)
|| (stack_->parent() == nullptr));
/* top-level alwyas has DToplevelSeqSsm */
ParserStack * s = stack_;
if (s) {
auto def = obj<ASyntaxStateMachine,DDefineSsm>::from(s->top());
if (def) {
/* carve-out for top-level DefineSsm: report 'at top-level' when
* that top-level DefineSsm is on the stack, so we detect
* this condition inside DefineSsm's event handling
*/
s = stack_->parent();
}
if (s && s->parent() == nullptr) {
auto top = obj<ASyntaxStateMachine,DToplevelSeqSsm>::from(s->top());
return top;
}
} else {
/** this isn't a normal operating state, still need a batch/interactive toplevel seq.
* just the same seems better to call it top-level
**/
return true;
}
return false;
}
bool
ParserStateMachine::has_incomplete_expr() const noexcept
{
scope log(XO_DEBUG(debug_flag_));
// don't count toplevel expression
return !(this->is_at_toplevel());
ParserStack * s = stack_;
if (s) {
auto top = obj<ASyntaxStateMachine,DToplevelSeqSsm>::from(s->top());
return !top;
} else {
return false;
}
}
obj<ASyntaxStateMachine>

View file

@ -41,7 +41,7 @@ namespace xo {
bool
SchematikaParser::has_incomplete_expr() const
{
return !(this->is_at_toplevel());
return psm_.has_incomplete_expr();
}
obj<ASyntaxStateMachine>

View file

@ -232,7 +232,7 @@ namespace xo {
{
const auto & testname = Catch::getResultCapture().getCurrentTestName();
constexpr bool c_debug_flag = false;
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
ParserFixture fixture(testname, c_debug_flag);