diff options
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/cli/Main.cpp | 12 | ||||
-rw-r--r-- | src/formats/osml/OsmlParser.cpp | 96 | ||||
-rw-r--r-- | src/formats/osml/OsmlParser.hpp | 16 |
4 files changed, 96 insertions, 29 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 6838900..b2a4b82 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -193,6 +193,7 @@ ADD_LIBRARY(ousia_core #) ADD_LIBRARY(ousia_osml + src/formats/osml/OsmlParser src/formats/osml/OsmlStreamParser ) diff --git a/src/cli/Main.cpp b/src/cli/Main.cpp index 8d7893a..2658851 100644 --- a/src/cli/Main.cpp +++ b/src/cli/Main.cpp @@ -50,6 +50,7 @@ #include <plugins/filesystem/FileLocator.hpp> #include <plugins/html/DemoOutput.hpp> #include <formats/osxml/OsxmlParser.hpp> +#include <formats/osml/OsmlParser.hpp> #include <plugins/xml/XmlOutput.hpp> const size_t ERROR_IN_COMMAND_LINE = 1; @@ -209,11 +210,16 @@ int main(int argc, char **argv) // fill registry registry.registerDefaultExtensions(); - OsxmlParser xmlParser; + OsmlParser osmlParser; + OsxmlParser osxmlParser; registry.registerParser( - {"text/vnd.ousia.oxm", "text/vnd.ousia.oxd"}, + {"text/vnd.ousia.osml"}, {&RttiTypes::Document, &RttiTypes::Domain, &RttiTypes::Typesystem}, - &xmlParser); + &osmlParser); + registry.registerParser( + {"text/vnd.ousia.osml+xml"}, + {&RttiTypes::Document, &RttiTypes::Domain, &RttiTypes::Typesystem}, + &osxmlParser); registry.registerResourceLocator(&fileLocator); // register search paths diff --git a/src/formats/osml/OsmlParser.cpp b/src/formats/osml/OsmlParser.cpp index 4973639..87f4c00 100644 --- a/src/formats/osml/OsmlParser.cpp +++ b/src/formats/osml/OsmlParser.cpp @@ -16,42 +16,102 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include <core/parser/generic/ParserStateCallbacks.hpp> -#include <core/parser/generic/ParserStateStack.hpp> +#include <core/parser/stack/GenericParserStates.hpp> +#include <core/parser/stack/Stack.hpp> +#include <core/parser/ParserContext.hpp> -#include "OsdmParser.hpp" -#include "OsdmStreamParser.hpp" +#include "OsmlParser.hpp" +#include "OsmlStreamParser.hpp" namespace ousia { -namespace { +using namespace parser_stack; /** - * The OsdmParserImplementation class contains the actual implementation of the - * parsing process and is created in the "doParse" function of the OsdmParser. - + * The OsmlParserImplementation class contains the actual implementation of the + * parsing process and is created in the "doParse" function of the OsmlParser. */ -class OsdmParserImplementation : public ParserStateCallbacks { +class OsmlParserImplementation { private: /** - * OsdmStreamParser instance. + * OsmlStreamParser instance responsible for converting the input stream + * into a series of osml events that are relayed to the Stack class. */ - OsdmStreamParser parser; + OsmlStreamParser parser; /** - * Instance of the ParserStateStack. + * Pushdown automaton responsible for converting the osml events into an + * actual Node tree. */ - ParserStateStack stack; + Stack stack; public: - OsdmParserImplementation parser(reader, ctx) : parser(reader), stack(ctx, std::multimap) + /** + * Constructor of the OsmlParserImplementation class. + * + * @param reader is a reference to the CharReader instance from which the + * osml should be read. + * @param ctx is a reference to the ParserContext instance that should be + * used. + */ + OsmlParserImplementation(CharReader &reader, ParserContext &ctx) + : parser(reader, ctx.getLogger()), stack(ctx, GenericParserStates) + { + } + + /** + * Starts the actual parsing process. + */ + void parse() + { + while (true) { + OsmlStreamParser::State state = parser.parse(); + switch (state) { + case OsmlStreamParser::State::COMMAND: + stack.command(parser.getCommandName(), + parser.getCommandArguments().asMap()); + break; + case OsmlStreamParser::State::DATA: + stack.data(parser.getData()); + break; + case OsmlStreamParser::State::ENTITY: + // TODO + break; + case OsmlStreamParser::State::ANNOTATION_START: + stack.annotationStart(parser.getCommandName(), + parser.getCommandArguments().asMap()); + break; + case OsmlStreamParser::State::ANNOTATION_END: { + Variant elementName = Variant::fromString(std::string{}); + const auto &args = parser.getCommandArguments().asMap(); + auto it = args.find("name"); + if (it != args.end()) { + elementName = it->second; + } + stack.annotationEnd(parser.getCommandName(), elementName); + break; + } + case OsmlStreamParser::State::FIELD_START: + stack.fieldStart(parser.inDefaultField()); + break; + case OsmlStreamParser::State::FIELD_END: + stack.fieldEnd(); + break; + case OsmlStreamParser::State::NONE: + case OsmlStreamParser::State::ERROR: + // Internally used in OsmlStreamParser, these states should + // never occur. Just contiunue. + continue; + case OsmlStreamParser::State::END: + return; + } + } + } }; -} -void OsdmParser::doParse(CharReader &reader, ParserContext &ctx) +void OsmlParser::doParse(CharReader &reader, ParserContext &ctx) { - OsdmParserImplementation parser(reader, ctx); + OsmlParserImplementation parser(reader, ctx); parser.parse(); } - } diff --git a/src/formats/osml/OsmlParser.hpp b/src/formats/osml/OsmlParser.hpp index 37505b4..f8f5b6c 100644 --- a/src/formats/osml/OsmlParser.hpp +++ b/src/formats/osml/OsmlParser.hpp @@ -17,32 +17,32 @@ */ /** - * @file OsdmParser.hpp + * @file OsmlParser.hpp * - * Contains the parser of the osdm format, the standard plain-text format used + * Contains the parser of the Osml format, the standard plain-text format used * by Ousía for documents. * * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) */ -#ifndef _OUSIA_OSDM_PARSER_HPP_ -#define _OUSIA_OSDM_PARSER_HPP_ +#ifndef _OUSIA_OSML_PARSER_HPP_ +#define _OUSIA_OSML_PARSER_HPP_ #include <core/parser/Parser.hpp> namespace ousia { /** - * OsdmParser is a small wrapper implementing the Parser interface. The actual - * parsing is performed with the OsdmStreamParser in conjunction with the + * OsmlParser is a small wrapper implementing the Parser interface. The actual + * parsing is performed with the OsmlStreamParser in conjunction with the * ParserStateStack. */ -class OsdmParser : public Parser { +class OsmlParser : public Parser { protected: void doParse(CharReader &reader, ParserContext &ctx) override; }; } -#endif /* _OUSIA_OSDM_PARSER_HPP_ */ +#endif /* _OUSIA_OSML_PARSER_HPP_ */ |