diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-02-14 23:46:10 +0100 |
---|---|---|
committer | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-02-14 23:46:10 +0100 |
commit | 6df10a61562fa4be7c1c13e7457341571fa6139d (patch) | |
tree | 29129eb77f5679493bfa8af414be3f27c6b54b2b /src | |
parent | 22c61026bf39c32a71cc3fbe76e1454c89a27c17 (diff) |
Renamed ParserStateStack to ParserStack
Diffstat (limited to 'src')
-rw-r--r-- | src/core/parser/generic/ParserStateStack.cpp | 53 | ||||
-rw-r--r-- | src/core/parser/generic/ParserStateStack.hpp | 206 |
2 files changed, 26 insertions, 233 deletions
diff --git a/src/core/parser/generic/ParserStateStack.cpp b/src/core/parser/generic/ParserStateStack.cpp index 1265851..8c32f17 100644 --- a/src/core/parser/generic/ParserStateStack.cpp +++ b/src/core/parser/generic/ParserStateStack.cpp @@ -23,40 +23,11 @@ #include <core/model/Project.hpp> #include "ParserScope.hpp" -#include "ParserStack.hpp" +#include "ParserStateStack.hpp" namespace ousia { -/* A default handler */ - -/** - * The DefaultHandler class is used in case no element handler is specified in - * the ParserState descriptor. - */ -class DefaultHandler : public Handler { -public: - using Handler::Handler; - - void start(Variant::mapType &args) override {} - - void end() override {} - - static Handler *create(const HandlerData &handlerData) - { - return new DefaultHandler{handlerData}; - } -}; - -/* Class Handler */ - -void Handler::data(const std::string &data, int field) -{ - if (Utils::hasNonWhitepaceChar(data)) { - logger().error("Expected command but found character data."); - } -} - -/* Class ParserStack */ +/* Class ParserStateStack */ /** * Returns an Exception that should be thrown when a currently invalid command @@ -79,14 +50,14 @@ static LoggableException InvalidCommand(const std::string &name, } } -ParserStack::ParserStack( +ParserStateStack::ParserStateStack( ParserContext &ctx, const std::multimap<std::string, const ParserState *> &states) : ctx(ctx), states(states) { } -bool ParserStack::deduceState() +bool ParserStateStack::deduceState() { // Assemble all states std::vector<const ParserState *> states; @@ -113,7 +84,7 @@ bool ParserStack::deduceState() return true; } -std::set<std::string> ParserStack::expectedCommands() +std::set<std::string> ParserStateStack::expectedCommands() { const ParserState *currentState = &(this->currentState()); std::set<std::string> res; @@ -125,17 +96,17 @@ std::set<std::string> ParserStack::expectedCommands() return res; } -const ParserState &ParserStack::currentState() +const ParserState &ParserStateStack::currentState() { return stack.empty() ? ParserStates::None : stack.top()->state(); } -std::string ParserStack::currentCommandName() +std::string ParserStateStack::currentCommandName() { return stack.empty() ? std::string{} : stack.top()->name(); } -const ParserState *ParserStack::findTargetState(const std::string &name) +const ParserState *ParserStateStack::findTargetState(const std::string &name) { const ParserState *currentState = &(this->currentState()); auto range = states.equal_range(name); @@ -149,7 +120,7 @@ const ParserState *ParserStack::findTargetState(const std::string &name) return nullptr; } -void ParserStack::start(const std::string &name, Variant::mapType &args, +void ParserStateStack::start(const std::string &name, Variant::mapType &args, const SourceLocation &location) { ParserState const *targetState = findTargetState(name); @@ -180,14 +151,14 @@ void ParserStack::start(const std::string &name, Variant::mapType &args, stack.emplace(handler); } -void ParserStack::start(std::string name, const Variant::mapType &args, +void ParserStateStack::start(std::string name, const Variant::mapType &args, const SourceLocation &location) { Variant::mapType argsCopy(args); start(name, argsCopy); } -void ParserStack::end() +void ParserStateStack::end() { // Check whether the current command could be ended if (stack.empty()) { @@ -202,7 +173,7 @@ void ParserStack::end() inst->end(); } -void ParserStack::data(const std::string &data, int field) +void ParserStateStack::data(const std::string &data, int field) { // Check whether there is any command the data can be sent to if (stack.empty()) { diff --git a/src/core/parser/generic/ParserStateStack.hpp b/src/core/parser/generic/ParserStateStack.hpp index efc4e4a..68c4026 100644 --- a/src/core/parser/generic/ParserStateStack.hpp +++ b/src/core/parser/generic/ParserStateStack.hpp @@ -17,17 +17,17 @@ */ /** - * @file ParserStack.hpp + * @file ParserStateStack.hpp * - * Helper classes for document or description parsers. Contains the ParserStack - * class, which is an pushdown automaton responsible for accepting commands in - * the correct order and calling specified handlers. + * Helper classes for document or description parsers. Contains the + * ParserStateStack class, which is an pushdown automaton responsible for + * accepting commands in the correct order and calling specified handlers. * * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) */ -#ifndef _OUSIA_PARSER_STACK_HPP_ -#define _OUSIA_PARSER_STACK_HPP_ +#ifndef _OUSIA_PARSER_STATE_STACK_HPP_ +#define _OUSIA_PARSER_STATE_STACK_HPP_ #include <cstdint> @@ -48,189 +48,10 @@ namespace ousia { /** - * Struct collecting all the data that is being passed to a Handler instance. - */ -struct HandlerData { - /** - * Reference to the ParserContext instance that should be used to resolve - * references to nodes in the Graph. - */ - ParserContext &ctx; - - /** - * Contains the name of the tag that is being handled. - */ - const std::string name; - - /** - * Contains the current state of the state machine. - */ - const ParserState &state; - - /** - * Contains the state of the state machine when the parent node was handled. - */ - const ParserState &parentState; - - /** - * Current source code location. - */ - const SourceLocation location; - - /** - * Constructor of the HandlerData class. - * - * @param ctx is the parser context the handler should be executed in. - * @param name is the name of the string. - * @param state is the state this handler was called for. - * @param parentState is the state of the parent command. - * @param location is the location at which the handler is created. - */ - HandlerData(ParserContext &ctx, std::string name, const ParserState &state, - const ParserState &parentState, const SourceLocation location) - : ctx(ctx), - name(std::move(name)), - state(state), - parentState(parentState), - location(location){}; -}; - -/** - * The handler class provides a context for handling an XML tag. It has to be - * overridden and registered in the StateStack class to form handlers for - * concrete XML tags. - */ -class Handler { -private: - /** - * Structure containing the internal handler data. - */ - const HandlerData handlerData; - -public: - /** - * Constructor of the Handler class. - * - * @param data is a structure containing all data being passed to the - * handler. - */ - Handler(const HandlerData &handlerData) : handlerData(handlerData){}; - - /** - * Virtual destructor. - */ - virtual ~Handler(){}; - - /** - * Returns a reference at the ParserContext. - * - * @return a reference at the ParserContext. - */ - ParserContext &context() { return handlerData.ctx; } - - /** - * Returns the command name for which the handler was created. - * - * @return a const reference at the command name. - */ - const std::string &name() { return handlerData.name; } - - /** - * Returns a reference at the ParserScope instance. - * - * @return a reference at the ParserScope instance. - */ - ParserScope &scope() { return handlerData.ctx.getScope(); } - - /** - * Returns a reference at the Manager instance which manages all nodes. - * - * @return a referance at the Manager instance. - */ - Manager &manager() { return handlerData.ctx.getManager(); } - - /** - * Returns a reference at the Logger instance used for logging error - * messages. - * - * @return a reference at the Logger instance. - */ - Logger &logger() { return handlerData.ctx.getLogger(); } - - /** - * Returns a reference at the Project Node, representing the project into - * which the file is currently being parsed. - * - * @return a referance at the Project Node. - */ - Rooted<Project> project() { return handlerData.ctx.getProject(); } - - /** - * Reference at the ParserState descriptor for which this Handler was - * created. - * - * @return a const reference at the constructing ParserState descriptor. - */ - const ParserState &state() { return handlerData.state; } - - /** - * Reference at the ParserState descriptor of the parent state of the state - * for which this Handler was created. Set to ParserStates::None if there - * is no parent state. - * - * @return a const reference at the parent state of the constructing - * ParserState descriptor. - */ - const ParserState &parentState() { return handlerData.parentState; } - - /** - * Returns the current location in the source file. - * - * @return the current location in the source file. - */ - SourceLocation location() { return handlerData.location; } - - /** - * Called when the command that was specified in the constructor is - * instanciated. - * - * @param args is a map from strings to variants (argument name and value). - */ - virtual void start(Variant::mapType &args) = 0; - - /** - * Called whenever the command for which this handler is defined ends. - */ - virtual void end() = 0; - - /** - * Called whenever raw data (int the form of a string) is available for the - * Handler instance. In the default handler an exception is raised if the - * received data contains non-whitespace characters. - * - * @param data is a pointer at the character data that is available for the - * Handler instance. - * @param field is the field number (the interpretation of this value - * depends on the format that is being parsed). - */ - virtual void data(const std::string &data, int field); -}; - -/** - * HandlerConstructor is a function pointer type used to create concrete - * instances of the Handler class. - * - * @param handlerData is the data that should be passed to the new handler - * instance. - * @return a newly created handler instance. - */ -using HandlerConstructor = Handler *(*)(const HandlerData &handlerData); - -/** - * The ParserStack class is a pushdown automaton responsible for turning a + * The ParserStateStack class is a pushdown automaton responsible for turning a * command stream into a tree of Node instances. */ -class ParserStack { +class ParserStateStack { private: /** * Reference at the parser context. @@ -269,14 +90,15 @@ private: public: /** - * Creates a new instance of the ParserStack class. + * Creates a new instance of the ParserStateStack class. * * @param ctx is the parser context the parser stack is working on. * @param states is a map containing the command names and pointers at the * corresponding ParserState instances. */ - ParserStack(ParserContext &ctx, - const std::multimap<std::string, const ParserState *> &states); + ParserStateStack( + ParserContext &ctx, + const std::multimap<std::string, const ParserState *> &states); /** * Tries to reconstruct the parser state from the Scope instance of the @@ -293,7 +115,7 @@ public: bool deduceState(); /** - * Returns the state the ParserStack instance currently is in. + * Returns the state the ParserStateStack instance currently is in. * * @return the state of the currently active Handler instance or STATE_NONE * if no handler is on the stack. @@ -357,5 +179,5 @@ public: }; } -#endif /* _OUSIA_PARSER_STACK_HPP_ */ +#endif /* _OUSIA_PARSER_STATE_STACK_HPP_ */ |