diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/parser/ParserStack.cpp | 32 | ||||
-rw-r--r-- | src/core/parser/ParserStack.hpp | 14 |
2 files changed, 44 insertions, 2 deletions
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 <sstream> -#include "ParserStack.hpp" - #include <core/common/Utils.hpp> #include <core/common/Exceptions.hpp> #include <core/model/Project.hpp> +#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<const ParserState *> 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<const ParserState *> 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<std::string> ParserStack::expectedCommands(const ParserState &state) { std::set<std::string> 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 @@ -271,6 +271,20 @@ public: const std::multimap<std::string, const ParserState *> &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. * * @return the state of the currently active Handler instance or STATE_NONE |