summaryrefslogtreecommitdiff
path: root/src/formats/osml/OsmlParser.cpp
blob: 87f4c002f2f18ec5261ea5f58ab0eb0793886d8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
    Ousía
    Copyright (C) 2014, 2015  Benjamin Paaßen, Andreas Stöckel

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <core/parser/stack/GenericParserStates.hpp>
#include <core/parser/stack/Stack.hpp>
#include <core/parser/ParserContext.hpp>

#include "OsmlParser.hpp"
#include "OsmlStreamParser.hpp"

namespace ousia {

using namespace parser_stack;

/**
 * The OsmlParserImplementation class contains the actual implementation of the
 * parsing process and is created in the "doParse" function of the OsmlParser.
 */
class OsmlParserImplementation {
private:
	/**
	 * OsmlStreamParser instance responsible for converting the input stream
	 * into a series of osml events that are relayed to the Stack class.
	 */
	OsmlStreamParser parser;

	/**
	 * Pushdown automaton responsible for converting the osml events into an
	 * actual Node tree.
	 */
	Stack stack;

public:
	/**
	 * 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 OsmlParser::doParse(CharReader &reader, ParserContext &ctx)
{
	OsmlParserImplementation parser(reader, ctx);
	parser.parse();
}
}