summaryrefslogtreecommitdiff
path: root/src/core/parser/generic/GenericParser.hpp
blob: 53cb982164401b1246dbcd95766068b643bdc6a2 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
    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/>.
*/

/**
 * @file GenericParser.hpp
 *
 * The GenericParser class builds an abstraction layer that separates the
 * underlying document format (e.g. osdm or osdmx) from the actual process of
 * building the document model. It provides a set of genric functions that
 * should be called by the inheriting concrete parser class, e.g. indicating a
 * command with parameters, the start/end of a field or the start/end of an
 * annotation. The GenericParser maintains an internal stack of
 * ParserStateHandlers and relays the commands to the elements of this stack.
 *
 * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de)
 */

#ifndef _OUSIA_GENERIC_PARSER_HPP_
#define _OUSIA_GENERIC_PARSER_HPP_

#include <core/parser/Parser.hpp>

#include "ParserStateStack.hpp"
#include "ParserStateHandler.hpp"
#include "ParserState.hpp"

namespace ousia {

/**
 * The abstract GenericParser class is merely a convenience class for Parsers
 * which use the ParserStateStack class. It maintains a ParserStateStack
 * instance and provides functions which directly forward the given data to the
 * ParserStateStack. It also implements the ParserStateCallbacks inteface which
 * is used by ParserStateHandlers to influence the parsing process (such as
 * setting the whitespace mode or registering new entities).
 */
class GenericParser : public Parser, public ParserStateCallbacks {

private:
	/**
	 * Internal ParserStateStack instance.
	 */
	ParserStateStack stack;

protected:
	/**
	 * Forwards the "command" event to the ParserStateStack instance.
	 *
	 * @param name is the name of the command (including the namespace
	 * separator ':') and its corresponding location. Must be a string variant.
	 * @param args is a map variant containing the arguments that were passed to
	 * the command.
	 */
	void command(Variant name, Variant args)
	{
		stack.command(std::move(name), std::move(args));
	}

	/**
	 * Forwards the "fieldStart" event to the ParserStateStack instance.
	 */
	void fieldStart()
	{
		stack.fieldStart();
	}

	/**
	 * Forwards the "fieldEnd" event to the ParserStateStack instance.
	 */
	void fieldEnd()
	{
		stack.fieldEnd();
	}

	/**
	 * Forwards the "data" event to the ParserStateStack instance.
	 *
	 * @param data is a variant of any type containing the data that was parsed
	 * as data.
	 */
	void data(Variant data)
	{
		stack.data(std::move(data));
	}

	/**
	 * Forwards the "annotationStart" event to the ParserStateStack instance.
	 *
	 * @param name is the name of the annotation class.
	 * @param args is a map variant containing the arguments that were passed
	 * to the annotation.
	 */
	void annotationStart(Variant name, Variant args)
	{
		stack.annotationStart(std::move(name), std::move(args));
	}

	/**
	 * Forwards the "annotationEnd" event to the ParserStateStack instance.
	 *
	 * @param name is the name of the annotation class that was ended.
	 * @param annotationName is the name of the annotation that was ended.
	 */
	void annotationEnd(Variant name, Variant annotationName)
	{
		stack.annotationEnd(std::move(name), std::move(annotationName));
	}

	/**
	 * Forwards the "token" call to the ParserStateStack instance.
	 *
	 * @param token is string variant containing the token that was encountered.
	 */
	void token(Variant token)
	{
		stack.token(std::move(token));
	}
};

}

#endif _OUSIA_GENERIC_PARSER_HPP_