xo-result2: + SchematikaParser

This commit is contained in:
Roland Conybeare 2026-01-18 19:25:43 -05:00
commit 6e2a2bbcd0
5 changed files with 344 additions and 0 deletions

View file

@ -4,6 +4,7 @@ set(SELF_LIB xo_reader2)
set(SELF_SRCS
init_reader2.cpp
SchematikaParser.cpp
ParserStateMachine.cpp
ParserStack.cpp
ParserResult.cpp

View file

@ -16,6 +16,13 @@ namespace xo {
using xo::facet::with_facet;
namespace scm {
ParserStateMachine::ParserStateMachine(const ArenaConfig & config)
: parser_alloc_{DArena::map(config)},
expr_alloc_{with_facet<AAllocator>::mkobj(&parser_alloc_)},
debug_flag_{config.debug_flag_}
{
}
void
ParserStateMachine::push_ssm(obj<ASyntaxStateMachine> ssm)
{

View file

@ -0,0 +1,92 @@
/** @file SchematikaParser.cpp
*
* @author Roland Conybeare, Jan 2026
**/
#include "SchematikaParser.hpp"
#include "ParserStateMachine.hpp"
#include "ParserStack.hpp"
#include "DExprSeqState.hpp"
#include <xo/indentlog/scope.hpp>
#include <stdexcept>
namespace xo {
using xo::tostr;
using xo::xtag;
namespace scm {
// ----- SchematikaParser -----
SchematikaParser::SchematikaParser(const ArenaConfig & config, bool debug_flag)
: psm_{config},
debug_flag_{debug_flag}
{
}
bool
SchematikaParser::is_at_toplevel() const {
return psm_.stack() == nullptr;
}
bool
SchematikaParser::has_incomplete_expr() const {
/* (don't count toplevel exprseq) */
ParserStack * stack = psm_.stack();
if (!stack)
return false;
return stack->parent() != nullptr;
}
void
SchematikaParser::begin_interactive_session() {
DExprSeqState::start_interactive(psm_.expr_alloc(), &psm_);
}
void
SchematikaParser::begin_translation_unit() {
DExprSeqState::start_batch(psm_.expr_alloc(), &psm_);
}
const ParserResult &
SchematikaParser::include_token(const token_type & tk)
{
scope log(XO_DEBUG(debug_flag_), xtag("tk", tk));
if (psm_.stack() == nullptr) {
throw std::runtime_error(tostr("SchematikaParser::include_token",
": parser not expecting input"
"(call parser.begin_translation_unit()..?)",
xtag("token", tk)));
}
/* stack is non-empty */
psm_.on_token(tk);
return psm_.result();
} /*include_token*/
void
SchematikaParser::reset_result()
{
psm_.reset_result();
}
void
SchematikaParser::reset_to_idle_toplevel()
{
psm_.reset_stack();
psm_.reset_result();
} /*reset_to_idle_toplevel*/
void
SchematikaParser::print(std::ostream & os) const {
os << "<SchematikaParser"
<< xtag("debug", debug_flag_)
<< xtag("has_stack", (psm_.stack() != nullptr))
<< ">" << std::endl;
}
} /*namespace scm*/
} /*namespace xo*/
/* end SchematikaParser.cpp */