diff options
Diffstat (limited to 'src/core/parser/stack/Handler.hpp')
-rw-r--r-- | src/core/parser/stack/Handler.hpp | 285 |
1 files changed, 179 insertions, 106 deletions
diff --git a/src/core/parser/stack/Handler.hpp b/src/core/parser/stack/Handler.hpp index baf31c9..67fde06 100644 --- a/src/core/parser/stack/Handler.hpp +++ b/src/core/parser/stack/Handler.hpp @@ -1,6 +1,6 @@ /* Ousía - Copyright (C) 2014 Benjamin Paaßen, Andreas Stöckel + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -16,6 +16,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ +/** + * @file Handler.hpp + * + * Contains the definition of the Handler class, used for representing Handlers + * for certain syntactic elements. + * + * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) + */ + #ifndef _OUSIA_PARSER_STACK_HANDLER_HPP_ #define _OUSIA_PARSER_STACK_HANDLER_HPP_ @@ -24,6 +33,8 @@ #include <core/common/Location.hpp> #include <core/common/Variant.hpp> #include <core/common/Whitespace.hpp> +#include <core/common/Token.hpp> +#include <core/model/Node.hpp> #include <core/model/Syntax.hpp> namespace ousia { @@ -42,6 +53,13 @@ class HandlerCallbacks; class State; /** + * Enum describing the type of the Handler instance -- a document handler may + * be created for handling a simple command, a token or an annotation start and + * end. + */ +enum class HandlerType { COMMAND, ANNOTATION_START, ANNOTATION_END, TOKEN }; + +/** * Class collecting all the data that is being passed to a Handler * instance. */ @@ -61,19 +79,21 @@ public: HandlerCallbacks &callbacks; /** - * Contains the name of the command that is being handled. + * Contains the current state of the state machine. */ - std::string name; + const State &state; /** - * Contains the current state of the state machine. + * Token containing the name of the command that is being handled, the + * location of the element in the source code or the token id of the token + * that is being handled. */ - const State &state; + Token token; /** - * Current source code location. + * Type describing for which purpose the HandlerData instance was created. */ - SourceLocation location; + HandlerType type; /** * Constructor of the HandlerData class. @@ -81,13 +101,13 @@ public: * @param ctx is the parser context the handler should be executed in. * @param callbacks is an instance of Callbacks used to notify * the parser about certain state changes. - * @param name is the name of the string. * @param state is the state this handler was called for. - * @param location is the location at which the handler is created. + * @param token contains name, token id and location of the command that is + * being handled. + * @param type describes the purpose of the Handler instance at hand. */ HandlerData(ParserContext &ctx, HandlerCallbacks &callbacks, - const std::string &name, const State &state, - const SourceLocation &location); + const State &state, const Token &token, HandlerType type); }; /** @@ -118,50 +138,6 @@ protected: Handler(const HandlerData &handlerData); /** - * Returns a reference at the ParserContext. - * - * @return a reference at the ParserContext. - */ - ParserContext &context(); - - /** - * Returns a reference at the ParserScope instance. - * - * @return a reference at the ParserScope instance. - */ - ParserScope &scope(); - - /** - * Returns a reference at the Manager instance which manages all nodes. - * - * @return a referance at the Manager instance. - */ - Manager &manager(); - - /** - * Returns a reference at the Logger instance used for logging error - * messages. - * - * @return a reference at the Logger instance. - */ - Logger &logger(); - - /** - * Returns the location of the element in the source file, for which this - * Handler was created. - * - * @return the location of the Handler in the source file. - */ - const SourceLocation &location() const; - - /** - * Returns the command name for which the handler was created. - * - * @return a const reference at the command name. - */ - const std::string &name() const; - - /** * Calls the corresponding function in the HandlerCallbacks instance. This * method registers the given tokens as tokens that are generally available, * tokens must be explicitly enabled using the "pushTokens" and "popTokens" @@ -233,23 +209,101 @@ protected: public: /** + * Enum representing the type of the annotation a Handle instance handles. + * It may either handle the start of an annotation or the end of an + * annotation. + */ + enum class AnnotationType { START, END }; + + /** + * Enum type representing the possible outcomes of the endToken() method. + */ + enum class EndTokenResult { ENDED_THIS, ENDED_HIDDEN, ENDED_NONE }; + + /** * Virtual destructor. */ virtual ~Handler(); /** - * Returns the command name for which the handler was created. + * Returns a reference at the ParserContext. + * + * @return a reference at the ParserContext. + */ + ParserContext &context(); + + /** + * Returns a reference at the ParserScope instance. * - * @return a const reference at the command name. + * @return a reference at the ParserScope instance. */ - const std::string &getName() const; + ParserScope &scope(); /** - * Reference at the State descriptor for which this Handler was created. + * Returns a reference at the Manager instance which manages all nodes. + * + * @return a referance at the Manager instance. + */ + Manager &manager(); + + /** + * Returns a reference at the Logger instance used for logging error + * messages. + * + * @return a reference at the Logger instance. + */ + Logger &logger(); + + /** + * Returns the name of the command or annotation the handler is currently + * handling. In case the command is currently handling a token, the name + * corresponds to the token string sequence. + * + * @return the name of the command or the string sequence of the token that + * is being handled by this handler. + */ + const std::string &name() const; + + /** + * Returns the token id of the token that is currently being handled by the + * handler. In case the handler currently handles a command or annotation, + * the token id is set to Tokens::Data. + * + * @return the current token id or Tokens::Data if no token is being + * handled. + */ + TokenId tokenId() const; + + /** + * Returns a reference at the Token instance, containing either the token + * that is currently being handled or the name of the command and annotation + * and their location. + * + * @return a const reference at the internal token instance. + */ + const Token &token() const; + + /** + * Returns the location of the element in the source file, for which this + * Handler was created. + * + * @return the location of the Handler in the source file. + */ + const SourceLocation &location() const; + + /** + * Returns the type describing the purpose for which the handler instance + * was created. + */ + HandlerType type() const; + + /** + * Returns a reference at the State descriptor for which this Handler was + * created. * * @return a const reference at the constructing State descriptor. */ - const State &getState() const; + const State &state() const; /** * Sets the internal logger to the given logger instance. @@ -273,14 +327,62 @@ public: const SourceLocation &getLocation() const; /** - * Called when the command that was specified in the constructor is - * instanciated. + * Called whenever the handler should handle the start of a command. This + * method (or any other of the "start" methods) is called exactly once, + * after the constructor. The name of the command that is started here can + * be accessed using the name() method. + * + * @param args is a map from strings to variants (argument name and value). + * @return true if the handler was successful in starting an element with + * the given name represents, false otherwise. + */ + virtual bool startCommand(Variant::mapType &args) = 0; + + /** + * Called whenever the handler should handle the start of an annotation. + * This method (or any other of the "start" methods) is called exactly once, + * after the constructor. This method is only called if the + * "supportsAnnotations" flag of the State instance referencing this Handler + * is set to true. The name of the command that is started here can be + * accessed using the name() method. * * @param args is a map from strings to variants (argument name and value). - * @return true if the handler was successful in starting the element it - * represents, false otherwise. + * @param type specifies whether this handler should handle the start of an + * annotation or the end of an annotation. */ - virtual bool start(Variant::mapType &args) = 0; + virtual bool startAnnotation(Variant::mapType &args, + AnnotationType annotationType) = 0; + + /** + * Called whenever the handler should handle the start of a token. This + * method (or any other of the "start" methods) is called exactly once, + * after the constructor. This method is only called if the "supportsTokens" + * flag of the State instance referencing this Handler is set to true. The + * token id of the token that is should be handled can be accessed using the + * tokenId() method. + * + * @param node is the node for which this token was registered. + */ + virtual bool startToken(Handle<Node> node) = 0; + + /** + * Called whenever a token is marked as "end" token and this handler happens + * to be the currently active handler. This operation may have three + * outcomes: + * <ol> + * <li>The token marks the end of the complete handler and the calling + * code should call the "end" method.</li> + * <li>The token marks the end of some element that is unknown the calling + * code. So the operation itself was a success, but the calling code + * should not call the "end" method. + * <li>The token did not anything in this context. Basically this shuold + * never happen, but who knows.</li> + * </ol> + * + * @param id is the Token for which the handler should be started. + * @param node is the node for which this token was registered. + */ + virtual EndTokenResult endToken(const Token &token, Handle<Node> node) = 0; /** * Called before the command for which this handler is defined ends (is @@ -310,35 +412,6 @@ public: virtual void fieldEnd() = 0; /** - * Called whenever an annotation starts while this handler is active. The - * function should return true if starting the annotation was successful, - * false otherwise. - * - * @param className is a string variant containing the name of the - * annotation class and the location of the name in the source code. - * @param args is a map from strings to variants (argument name and value). - * @return true if the mentioned annotation could be started here, false - * if an error occurred. - */ - virtual bool annotationStart(const Variant &className, - Variant::mapType &args) = 0; - - /** - * Called whenever an annotation ends while this handler is active. The - * function should return true if ending the annotation was successful, - * false otherwise. - * - * @param className is a string variant containing the name of the - * annotation class and the location of the class name in the source code. - * @param elementName is a string variant containing the name of the - * annotation class and the location of the element name in the source code. - * @return true if the mentioned annotation could be started here, false if - * an error occurred. - */ - virtual bool annotationEnd(const Variant &className, - const Variant &elementName) = 0; - - /** * Called whenever raw data (int the form of a string) is available for the * Handler instance. Should return true if the data could be handled, false * otherwise. The actual data variant must be retrieved using the "text()" @@ -368,14 +441,14 @@ protected: using Handler::Handler; public: - bool start(Variant::mapType &args) override; + bool startCommand(Variant::mapType &args) override; + bool startAnnotation(Variant::mapType &args, + AnnotationType annotationType) override; + bool startToken(Handle<Node> node) override; + EndTokenResult endToken(const Token &token, Handle<Node> node) override; void end() override; bool fieldStart(bool &isDefault, size_t fieldIdx) override; void fieldEnd() override; - bool annotationStart(const Variant &className, - Variant::mapType &args) override; - bool annotationEnd(const Variant &className, - const Variant &elementName) override; bool data() override; /** @@ -394,14 +467,14 @@ protected: using Handler::Handler; public: - bool start(Variant::mapType &args) override; + bool startCommand(Variant::mapType &args) override; + bool startAnnotation(Variant::mapType &args, + AnnotationType annotationType) override; + bool startToken(Handle<Node> node) override; + EndTokenResult endToken(const Token &token, Handle<Node> node) override; void end() override; bool fieldStart(bool &isDefault, size_t fieldIdx) override; void fieldEnd() override; - bool annotationStart(const Variant &className, - Variant::mapType &args) override; - bool annotationEnd(const Variant &className, - const Variant &elementName) override; bool data() override; }; @@ -452,9 +525,9 @@ protected: virtual void doHandle(const Variant &fieldData, Variant::mapType &args) = 0; public: - bool start(Variant::mapType &args) override; - void end() override; + bool startCommand(Variant::mapType &args) override; bool data() override; + void end() override; }; } } |