diff options
Diffstat (limited to 'src/formats')
| -rw-r--r-- | src/formats/osml/OsmlParser.cpp | 96 | ||||
| -rw-r--r-- | src/formats/osml/OsmlParser.hpp | 16 | 
2 files changed, 86 insertions, 26 deletions
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_ */  | 
