From 7060df78f37f0d75e32212f24e2327069f5dea94 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Tue, 3 Feb 2015 02:28:30 +0100 Subject: Added "deduceState" function --- src/core/parser/ParserStack.cpp | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'src/core/parser/ParserStack.cpp') diff --git a/src/core/parser/ParserStack.cpp b/src/core/parser/ParserStack.cpp index 51a7a13..cc875cb 100644 --- a/src/core/parser/ParserStack.cpp +++ b/src/core/parser/ParserStack.cpp @@ -18,12 +18,13 @@ #include -#include "ParserStack.hpp" - #include #include #include +#include "ParserScope.hpp" +#include "ParserStack.hpp" + namespace ousia { /* A default handler */ @@ -88,6 +89,33 @@ ParserStack::ParserStack( { } +bool ParserStack::deduceState() +{ + // Assemble all states + std::vector states; + for (const auto &e : this->states) { + states.push_back(e.second); + } + + // Fetch the type signature of the scope and derive all possible states, + // abort if no unique parser state was found + std::vector possibleStates = + ParserStateDeductor(ctx.getScope().getStackTypeSignature(), states) + .deduce(); + if (possibleStates.size() != 1) { + ctx.getLogger().error( + "Error while including file: Cannot deduce parser state."); + return false; + } + + // Switch to this state by creating a dummy handler + const ParserState *state = possibleStates[0]; + Handler *handler = + DefaultHandler::create({ctx, "", *state, *state, SourceLocation{}}); + stack.emplace(handler); + return true; +} + std::set ParserStack::expectedCommands(const ParserState &state) { std::set res; -- cgit v1.2.3