diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-01-02 12:12:00 +0100 |
---|---|---|
committer | Andreas Stöckel <andreas@somweyr.de> | 2015-01-02 12:12:00 +0100 |
commit | 7bca3d7e34ca5c2b1e939a001229c04fe6b6bde0 (patch) | |
tree | a9ee906cecaf2e32fd05dadf1b07f34d51d1a9cf /src/core | |
parent | a68ee5ed18d9f6744ce99890b8e0249dff3bd070 (diff) |
Added argument validation to parser stack
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/parser/ParserStack.cpp | 18 | ||||
-rw-r--r-- | src/core/parser/ParserStack.hpp | 35 |
2 files changed, 43 insertions, 10 deletions
diff --git a/src/core/parser/ParserStack.cpp b/src/core/parser/ParserStack.cpp index 5e801ee..ff2647d 100644 --- a/src/core/parser/ParserStack.cpp +++ b/src/core/parser/ParserStack.cpp @@ -37,6 +37,11 @@ void Handler::data(const std::string &data, int field) } } +void Handler::child(std::shared_ptr<Handler> handler) +{ + // Do nothing here +} + /* Class HandlerDescriptor */ HandlerInstance HandlerDescriptor::create(const ParserContext &ctx, @@ -45,7 +50,14 @@ HandlerInstance HandlerDescriptor::create(const ParserContext &ctx, const Variant &args) const { Handler *h = ctor(ctx, name, targetState, parentState, isChild); - h->start(args); + + // Canonicalize the arguments + Variant arguments = args; + if (argsType != nullptr) { + argsType->build(arguments, ctx.logger); + } + + h->start(arguments); return HandlerInstance(h, this); } @@ -67,8 +79,8 @@ static LoggableException invalidCommand(const std::string &name, std::string{"Expected "} + (expected.size() == 1 ? std::string{"\""} : std::string{"one of \""}) + - Utils::join(expected, "\", \"") + std::string{"\", but got \""} + name + - std::string{"\""}}; + Utils::join(expected, "\", \"") + std::string{"\", but got \""} + + name + std::string{"\""}}; } } diff --git a/src/core/parser/ParserStack.hpp b/src/core/parser/ParserStack.hpp index 233f4f9..93180b4 100644 --- a/src/core/parser/ParserStack.hpp +++ b/src/core/parser/ParserStack.hpp @@ -38,6 +38,8 @@ #include <vector> #include <core/common/Variant.hpp> +#include <core/common/Logger.hpp> +#include <core/model/Typesystem.hpp> #include "Parser.hpp" @@ -61,9 +63,6 @@ class Handler { private: Rooted<Node> node; -protected: - void setNode(Handle<Node> node) { this->node = node; } - public: /** * Reference to the ParserContext instance that should be used to resolve @@ -132,7 +131,7 @@ public: virtual void start(const Variant &args) = 0; /** - * Called whenever the command for which this handler + * Called whenever the command for which this handler is defined ends. */ virtual void end() = 0; @@ -153,7 +152,7 @@ public: * * @param handler is a reference at the child Handler instance. */ - virtual void child(std::shared_ptr<Handler> handler){}; + virtual void child(std::shared_ptr<Handler> handler); }; /** @@ -212,12 +211,34 @@ struct HandlerDescriptor { */ const bool arbitraryChildren; + /** + * Pointer pointing at a StructType describing the layout of the given when + * a new Handler instance is instantiated. + */ + const Rooted<model::StructType> argsType; + + /** + * Constructor of the HandlerDescriptor class. + * + * @param parentStates is a set of states in which a new handler of this + * type may be instantiated. + * @param ctor is a function pointer pointing at a function that + * instantiates the acutal Handler instance. + * @param targetState is the state the ParserStack switches to after + * instantiating an in instance of the described Handler instances. + * @param arbitraryChildren allows the Handler instance to handle any child + * node. + * @param argsType is a struct type describing the arguments that can be + * passed to the Handler or nullptr if no check should be performed. + */ HandlerDescriptor(std::set<State> parentStates, HandlerConstructor ctor, - State targetState, bool arbitraryChildren = false) + State targetState, bool arbitraryChildren = false, + Handle<model::StructType> argsType = nullptr) : parentStates(std::move(parentStates)), ctor(ctor), targetState(targetState), - arbitraryChildren(arbitraryChildren) + arbitraryChildren(arbitraryChildren), + argsType(argsType) { } |