From 6decad0b8e7e369bd8215f31a45dd3eae982d2a9 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Wed, 21 Jan 2015 01:17:49 +0100 Subject: Some further refactoring -- renamed Scope to ParserScope, got rid of parser namespace, added new functionality to RegistryClass, wrote documentation, added function for extracting file extensions to Utils --- src/plugins/xml/XmlParser.cpp | 12 ++---------- src/plugins/xml/XmlParser.hpp | 18 ++---------------- 2 files changed, 4 insertions(+), 26 deletions(-) (limited to 'src/plugins/xml') diff --git a/src/plugins/xml/XmlParser.cpp b/src/plugins/xml/XmlParser.cpp index 434a72c..ef738d8 100644 --- a/src/plugins/xml/XmlParser.cpp +++ b/src/plugins/xml/XmlParser.cpp @@ -25,13 +25,12 @@ #include #include #include +#include #include #include "XmlParser.hpp" namespace ousia { -namespace parser { -namespace xml { using namespace ousia::model; @@ -291,12 +290,7 @@ static void xmlCharacterDataHandler(void *p, const XML_Char *s, int len) /* Class XmlParser */ -std::set XmlParser::mimetypes() -{ - return std::set{{"text/vnd.ousia.oxm", "text/vnd.ousia.oxd"}}; -} - -Rooted XmlParser::parse(CharReader &reader, ParserContext &ctx) +Rooted XmlParser::doParse(CharReader &reader, ParserContext &ctx) { // Create the parser object ScopedExpatXmlParser p{"UTF-8"}; @@ -346,6 +340,4 @@ Rooted XmlParser::parse(CharReader &reader, ParserContext &ctx) return nullptr; } } -} -} diff --git a/src/plugins/xml/XmlParser.hpp b/src/plugins/xml/XmlParser.hpp index 62f0128..3c0ffb7 100644 --- a/src/plugins/xml/XmlParser.hpp +++ b/src/plugins/xml/XmlParser.hpp @@ -31,23 +31,13 @@ #include namespace ousia { -namespace parser { -namespace xml { /** * The XmlParser class implements parsing the various types of Ousía XML * documents using the expat stream XML parser. */ class XmlParser : public Parser { -public: - /** - * Returns the mimetype supported by the XmlParser which is - * "text/vnd.ousia.oxm" and "text/vnd.ousia.oxd". - * - * @return a list containing the mimetype supported by Ousía. - */ - std::set mimetypes() override; - +protected: /** * Parses the given input stream as XML file and returns the parsed * top-level node. @@ -56,13 +46,9 @@ public: * @param ctx is a reference to the ParserContext instance that should be * used. */ - Rooted parse(CharReader &reader, ParserContext &ctx) override; - - using Parser::parse; + Rooted doParse(CharReader &reader, ParserContext &ctx) override; }; -} -} } #endif /* _OUSIA_XML_PARSER_HPP_ */ -- cgit v1.2.3 From 31873a51b21ca8d549c172f9e773818356021a55 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Fri, 23 Jan 2015 15:30:07 +0100 Subject: Adapted XMLParser to new SourceLocation --- src/plugins/xml/XmlParser.cpp | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) (limited to 'src/plugins/xml') diff --git a/src/plugins/xml/XmlParser.cpp b/src/plugins/xml/XmlParser.cpp index ef738d8..78d9df8 100644 --- a/src/plugins/xml/XmlParser.cpp +++ b/src/plugins/xml/XmlParser.cpp @@ -131,11 +131,12 @@ public: !(defaultValue.isObject() && defaultValue.asObject() == nullptr); Rooted structType = scope().getLeaf().cast(); - Rooted attribute = structType->createAttribute( - name, defaultValue, optional, logger()); + Rooted attribute = + structType->createAttribute(name, defaultValue, optional, logger()); // Try to resolve the type - scope().resolve(type, logger(), + scope().resolve( + type, logger(), [attribute](Handle type, Logger &logger) mutable { attribute->setType(type, logger); }, @@ -234,16 +235,23 @@ public: /* Adapter Expat -> ParserStack */ +struct XMLParserUserData { + SourceId sourceId; +}; + static SourceLocation syncLoggerPosition(XML_Parser p) { + // Fetch the parser stack and the associated user data + ParserStack *stack = static_cast(XML_GetUserData(p)); + XMLParserUserData *ud = + static_cast(stack->getUserData()); + // Fetch the current location in the XML file - int line = XML_GetCurrentLineNumber(p); - int column = XML_GetCurrentColumnNumber(p); size_t offs = XML_GetCurrentByteIndex(p); - SourceLocation loc{line, column, offs}; - // Update the default location of the current logger instance - ParserStack *stack = static_cast(XML_GetUserData(p)); + // Build the source location and update the default location of the current + // logger instance + SourceLocation loc{ud->sourceId, offs}; stack->getContext().logger.setDefaultLocation(loc); return loc; } @@ -270,9 +278,9 @@ static void xmlStartElementHandler(void *p, const XML_Char *name, static void xmlEndElementHandler(void *p, const XML_Char *name) { XML_Parser parser = static_cast(p); - syncLoggerPosition(parser); - ParserStack *stack = static_cast(XML_GetUserData(parser)); + + syncLoggerPosition(parser); stack->end(); } @@ -297,7 +305,10 @@ Rooted XmlParser::doParse(CharReader &reader, ParserContext &ctx) // Create the parser stack instance and pass the reference to the state // machine descriptor - ParserStack stack{ctx, XML_HANDLERS}; + XMLParserUserData data; + data.sourceId = reader.getSourceId(); + + ParserStack stack{ctx, XML_HANDLERS, &data}; XML_SetUserData(&p, &stack); XML_UseParserAsHandlerArg(&p); @@ -321,15 +332,14 @@ Rooted XmlParser::doParse(CharReader &reader, ParserContext &ctx) // Parse the data and handle any XML error if (!XML_ParseBuffer(&p, bytesRead, bytesRead == 0)) { - // Fetch the current line number and column - int line = XML_GetCurrentLineNumber(&p); - int column = XML_GetCurrentColumnNumber(&p); + // Fetch the xml parser byte offset size_t offs = XML_GetCurrentByteIndex(&p); // Throw a corresponding exception XML_Error code = XML_GetErrorCode(&p); std::string msg = std::string{XML_ErrorString(code)}; - throw LoggableException{"XML: " + msg, line, column, offs}; + throw LoggableException{"XML: " + msg, + SourceLocation{reader.getSourceId(), offs}}; } // Abort once there are no more bytes in the stream -- cgit v1.2.3