diff options
Diffstat (limited to 'src/core/parser')
| -rw-r--r-- | src/core/parser/ParserStack.cpp | 30 | ||||
| -rw-r--r-- | src/core/parser/ParserStack.hpp | 79 | 
2 files changed, 82 insertions, 27 deletions
diff --git a/src/core/parser/ParserStack.cpp b/src/core/parser/ParserStack.cpp index 691b9d0..caf2116 100644 --- a/src/core/parser/ParserStack.cpp +++ b/src/core/parser/ParserStack.cpp @@ -26,13 +26,30 @@  namespace ousia {  namespace parser { +/* A default handler */ + +class DefaultHandler : public Handler { +public: +	using Handler::Handler; + +	void start(Variant::mapType &args) override {} + +	void end() override {} +}; + +static Handler *createDefaultHandler(const HandlerData &handlerData) +{ +	return new DefaultHandler{handlerData}; +} +  /* Class Handler */  void Handler::data(const std::string &data, int field)  {  	for (auto &c : data) {  		if (!Utils::isWhitespace(c)) { -			throw LoggableException{"No data allowed here."}; +			logger().error("Expected command but found character data."); +			return;  		}  	}  } @@ -49,7 +66,13 @@ HandlerInstance HandlerDescriptor::create(const ParserContext &ctx,                                            bool isChild,                                            Variant::mapType &args) const  { -	Handler *h = ctor(ctx, name, targetState, parentState, isChild); +	Handler *h; +	HandlerData data{ctx, name, targetState, parentState, isChild}; +	if (ctor) { +		h = ctor(data); +	} else { +		h = createDefaultHandler(data); +	}  	// Canonicalize the arguments  	arguments.validateMap(args, ctx.logger, true); @@ -120,7 +143,8 @@ void ParserStack::start(std::string name, Variant::mapType &args)  	}  	// Instantiate the handler and call its start function -	stack.emplace(descr->create(ctx, name, curState, isChild, args)); +	stack.emplace( +	    descr->create(ctx, name, curState, isChild, args));  }  void ParserStack::start(std::string name, const Variant::mapType &args) diff --git a/src/core/parser/ParserStack.hpp b/src/core/parser/ParserStack.hpp index d9de39e..43d6529 100644 --- a/src/core/parser/ParserStack.hpp +++ b/src/core/parser/ParserStack.hpp @@ -55,15 +55,9 @@ static const State STATE_ALL = -2;  static const State STATE_NONE = -1;  /** - * 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. + * Struct collecting all the data that is being passed to a Handler instance.   */ -class Handler { -private: -	Rooted<Node> node; - -public: +struct HandlerData {  	/**  	 * Reference to the ParserContext instance that should be used to resolve  	 * references to nodes in the Graph. @@ -92,7 +86,7 @@ public:  	const bool isChild;  	/** -	 * Constructor of the Handler class. +	 * 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. @@ -101,26 +95,57 @@ public:  	 * @param isChild specifies whether this handler was called not for the  	 * command that was specified in the state machine but a child command.  	 */ -	Handler(const ParserContext &ctx, std::string name, State state, -	        State parentState, bool isChild) +	HandlerData(const ParserContext &ctx, std::string name, State state, +	            State parentState, bool isChild)  	    : ctx(ctx),  	      name(std::move(name)),  	      state(state),  	      parentState(parentState),  	      isChild(isChild){}; +}; +/** + * 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:  	/** -	 * Virtual destructor. +	 * Structure containing the internal handler data.  	 */ -	virtual ~Handler(){}; +	const HandlerData handlerData; +public:  	/** -	 * Returns the node instance that was created by the handler. +	 * Constructor of the Handler class.  	 * -	 * @return the Node instance created by the handler. May be nullptr if no -	 * Node was created. +	 * @param data is a structure containing all data being passed to the +	 * handler.  	 */ -	Rooted<Node> getNode() { return node; } +	Handler(const HandlerData &handlerData) : handlerData(handlerData) {}; + +	/** +	 * Virtual destructor. +	 */ +	virtual ~Handler(){}; + +	 +	const std::string& name() {return handlerData.name;} + +	Scope &scope() {return handlerData.ctx.scope;} + +	Registry ®istry() {return handlerData.ctx.registry;} + +	Manager &manager() { return handlerData.ctx.manager; } + +	Logger &logger() { return handlerData.ctx.logger; } + +	State state() {return handlerData.state; } + +	State parentState() { return handlerData.parentState; } + +	bool isChild() { return handlerData.isChild; }  	/**  	 * Called when the command that was specified in the constructor is @@ -128,7 +153,7 @@ public:  	 *  	 * @param args is a map from strings to variants (argument name and value).  	 */ -	virtual void start(const Variant::mapType &args) = 0; +	virtual void start(Variant::mapType &args) = 0;  	/**  	 * Called whenever the command for which this handler is defined ends. @@ -158,10 +183,12 @@ public:  /**   * 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 ParserContext &ctx, -                                        std::string name, State state, -                                        State parentState, bool isChild); +using HandlerConstructor = Handler *(*)(const HandlerData &handlerData);  struct HandlerDescriptor; @@ -177,6 +204,10 @@ struct HandlerInstance {  	 */  	std::shared_ptr<Handler> handler; +	/** +	 * Pointer pointing at the descriptor from which the handler instance was +	 * derived. +	 */  	const HandlerDescriptor *descr;  	HandlerInstance(Handler *handler, const HandlerDescriptor *descr) @@ -309,7 +340,7 @@ public:  	 */  	State currentState()  	{ -		return stack.empty() ? STATE_NONE : stack.top().handler->state; +		return stack.empty() ? STATE_NONE : stack.top().handler->state();  	}  	/** @@ -320,7 +351,7 @@ public:  	 */  	std::string currentName()  	{ -		return stack.empty() ? std::string{} : stack.top().handler->name; +		return stack.empty() ? std::string{} : stack.top().handler->name();  	}  	/** @@ -370,7 +401,7 @@ public:  	 *  	 * @return a reference to the parser context.  	 */ -	ParserContext& getContext() {return ctx;} +	ParserContext &getContext() { return ctx; }  };  }  }  | 
