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 ++++++++++++++++++++++++++++++-- src/core/parser/ParserStack.hpp | 14 ++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) (limited to 'src') 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; diff --git a/src/core/parser/ParserStack.hpp b/src/core/parser/ParserStack.hpp index 7ad3da1..8561d42 100644 --- a/src/core/parser/ParserStack.hpp +++ b/src/core/parser/ParserStack.hpp @@ -270,6 +270,20 @@ public: ParserStack(ParserContext &ctx, const std::multimap &states); + /** + * Tries to reconstruct the parser state from the Scope instance of the + * ParserContext given in the constructor. This functionality is needed for + * including files,as the Parser of the included file needs to be brought to + + an equivalent state as the one in the including file. + * + * @param scope is the ParserScope instance from which the ParserState + * should be reconstructed. + * @param logger is the logger instance to which error messages should be + * written. + * @return true if the operation was sucessful, false otherwise. + */ + bool deduceState(); + /** * Returns the state the ParserStack instance currently is in. * -- cgit v1.2.3