From 59177921e8c81c1604e4154503a63190db66989c Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Wed, 3 Dec 2014 00:04:05 +0100 Subject: continued work on XML parser and underlying ParserStack class --- src/core/parser/ParserStack.cpp | 46 ++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 26 deletions(-) (limited to 'src/core/parser/ParserStack.cpp') diff --git a/src/core/parser/ParserStack.cpp b/src/core/parser/ParserStack.cpp index 7bc7af3..dca7f35 100644 --- a/src/core/parser/ParserStack.cpp +++ b/src/core/parser/ParserStack.cpp @@ -20,43 +20,37 @@ #include "ParserStack.hpp" +#include #include namespace ousia { namespace parser { +/* 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."}; + } + } +} + /* Class HandlerDescriptor */ HandlerInstance HandlerDescriptor::create(const ParserContext &ctx, std::string name, State parentState, - bool isChild, char **attrs) const + bool isChild, + const Variant &args) const { Handler *h = ctor(ctx, name, targetState, parentState, isChild); - h->start(attrs); + h->start(args); return HandlerInstance(h, this); } /* Class ParserStack */ -/** - * Function used internally to turn the elements of a collection into a string - * separated by the given delimiter. - */ -template -static std::string join(T es, const std::string &delim) -{ - std::stringstream res; - bool first = true; - for (auto &e : es) { - if (!first) { - res << delim; - } - res << e; - first = false; - } - return res.str(); -} - /** * Returns an Exception that should be thrown when a currently invalid command * is thrown. @@ -73,7 +67,7 @@ static LoggableException invalidCommand(const std::string &name, std::string{"Expected "} + (expected.size() == 1 ? std::string{"\""} : std::string{"one of \""}) + - join(expected, "\", \"") + std::string{"\", but got \""} + name + + Utils::join(expected, "\", \"") + std::string{"\", but got \""} + name + std::string{"\""}}; } } @@ -89,7 +83,7 @@ std::set ParserStack::expectedCommands(State state) return res; } -void ParserStack::start(std::string name, char **attrs) +void ParserStack::start(std::string name, const Variant &args) { // Fetch the current handler and the current state const HandlerInstance *h = stack.empty() ? nullptr : &stack.top(); @@ -117,7 +111,7 @@ void ParserStack::start(std::string name, char **attrs) } // Instantiate the handler and call its start function - stack.emplace(descr->create(ctx, name, curState, isChild, attrs)); + stack.emplace(descr->create(ctx, name, curState, isChild, args)); } void ParserStack::end() @@ -141,7 +135,7 @@ void ParserStack::end() } } -void ParserStack::data(const char *data, int len) +void ParserStack::data(const std::string &data, int field) { // Check whether there is any command the data can be sent to if (stack.empty()) { @@ -149,7 +143,7 @@ void ParserStack::data(const char *data, int len) } // Pass the data to the current Handler instance - stack.top().handler->data(data, len); + stack.top().handler->data(data, field); } } } -- cgit v1.2.3