summaryrefslogtreecommitdiff
path: root/src/core/parser/stack/Handler.hpp
blob: 0701343386e3744565ad8d8f3e0f7945ca4b0210 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
    Ousía
    Copyright (C) 2014  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/>.
*/

#ifndef _OUSIA_PARSER_STATE_HANDLER_HPP_
#define _OUSIA_PARSER_STATE_HANDLER_HPP_

#include <memory>
#include <string>

#include <core/common/Location.hpp>
#include <core/common/Variant.hpp>

namespace ousia {

// Forward declarations
class ParserContext;
class Callbacks;
class Logger;
class Project;

namespace parser_stack {

// More forward declarations
class State;

/**
 * Class collecting all the data that is being passed to a Handler
 * instance.
 */
class HandlerData {
public:
	/**
	 * Reference to the ParserContext instance that should be used to resolve
	 * references to nodes in the Graph.
	 */
	ParserContext &ctx;

	/**
	 * Reference at an instance of the Callbacks class, used for
	 * modifying the behaviour of the parser (like registering tokens, setting
	 * the data type or changing the whitespace handling mode).
	 */
	Callbacks &callbacks;

	/**
	 * Contains the name of the command that is being handled.
	 */
	std::string name;

	/**
	 * Contains the current state of the state machine.
	 */
	const State &state;

	/**
	 * Current source code location.
	 */
	SourceLocation location;

	/**
	 * Constructor of the HandlerData class.
	 *
	 * @param ctx is the parser context the handler should be executed in.
	 * @param callbacks is an instance of Callbacks used to notify
	 * the parser about certain state changes.
	 * @param name is the name of the string.
	 * @param state is the state this handler was called for.
	 * @param location is the location at which the handler is created.
	 */
	HandlerData(ParserContext &ctx, Callbacks &callbacks, std::string name,
	            const State &state, const SourceLocation &location);
};

/**
 * The Handler class provides a context for handling a generic stack element.
 * It has to beoverridden and registered in the StateStack class to form
 * handlers for concrete XML tags.
 */
class Handler {
private:
	/**
	 * Structure containing the internal handler data.
	 */
	const HandlerData internalData;

protected:
	/**
	 * Constructor of the Handler class.
	 *
	 * @param data is a structure containing all data being passed to the
	 * handler.
	 */
	Handler(const HandlerData &internalData);

	/**
	 * Returns a reference at the ParserContext.
	 *
	 * @return a reference at the ParserContext.
	 */
	ParserContext &context();

	/**
	 * Returns the command name for which the handler was created.
	 *
	 * @return a const reference at the command name.
	 */
	const std::string &name();

	/**
	 * Returns a reference at the ParserScope instance.
	 *
	 * @return a reference at the ParserScope instance.
	 */
	ParserScope &scope();

	/**
	 * Returns a reference at the Manager instance which manages all nodes.
	 *
	 * @return a referance at the Manager instance.
	 */
	Manager &manager();

	/**
	 * Returns a reference at the Logger instance used for logging error
	 * messages.
	 *
	 * @return a reference at the Logger instance.
	 */
	Logger &logger();

	/**
	 * Reference at the State descriptor for which this Handler was created.
	 *
	 * @return a const reference at the constructing State descriptor.
	 */
	const State &state();

	/**
	 * Returns the current location in the source file.
	 *
	 * @return the current location in the source file.
	 */
	SourceLocation location();

public:
	/**
	 * Virtual destructor.
	 */
	virtual ~Handler();

	/**
	 * Calls the corresponding function in the Callbacks instance. Sets the
	 * whitespace mode that specifies how string data should be processed. The
	 * calls to this function are placed on a stack by the underlying Stack
	 * class.
	 *
	 * @param whitespaceMode specifies one of the three WhitespaceMode constants
	 * PRESERVE, TRIM or COLLAPSE.
	 */
	void setWhitespaceMode(WhitespaceMode whitespaceMode);

	/**
	 * Calls the corresponding function in the Callbacks instance.
	 * Registers the given token as token that should be reported to the handler
	 * using the "token" function.
	 *
	 * @param token is the token string that should be reported.
	 */
	void registerToken(const std::string &token);

	/**
	 * Calls the corresponding function in the Callbacks instance.
	 * Unregisters the given token, it will no longer be reported to the handler
	 * using the "token" function.
	 *
	 * @param token is the token string that should be unregistered.
	 */
	void unregisterToken(const std::string &token);

	/**
	 * Called when the command that was specified in the constructor is
	 * instanciated.
	 *
	 * @param args is a map from strings to variants (argument name and value).
	 * @return true if the handler was successful in starting the element it
	 * represents, false otherwise.
	 */
	virtual bool start(Variant::mapType &args) = 0;

	/**
	 * Called before the command for which this handler is defined ends (is
	 * forever removed from the stack).
	 */
	virtual void end() = 0;

	/**
	 * Called when a new field starts, while the handler is active. This
	 * function should return true if the field is supported, false otherwise.
	 * No error should be logged if the field cannot be started, the caller will
	 * take care of that (since it is always valid to start a default field,
	 * even though the corresponding structure does not have a field, as long as
	 * no data is fed into the field).
	 *
	 * @param isDefaultField is set to true if the field that is being started
	 * is the default/tree field. The handler should set the value of this
	 * variable to true if the referenced field is indeed the default field.
	 * @param isImplicit is set to true if the field is implicitly being started
	 * by the stack (this field always implies isDefaultField being set to
	 * true).
	 * @param fieldIndex is the numerical index of the field.
	 */
	virtual bool fieldStart(bool &isDefaultField, bool isImplicit,
	                        size_t fieldIndex) = 0;

	/**
	 * Called when a previously opened field ends, while the handler is active.
	 * Note that a "fieldStart" and "fieldEnd" are always called alternately.
	 */
	virtual void fieldEnd() = 0;

	/**
	 * Called whenever an annotation starts while this handler is active. The
	 * function should return true if starting the annotation was successful,
	 * false otherwise.
	 *
	 * @param className is a string variant containing the name of the
	 * annotation class and the location of the name in the source code.
	 * @param args is a map from strings to variants (argument name and value).
	 * @return true if the mentioned annotation could be started here, false
	 * if an error occurred.
	 */
	virtual bool annotationStart(Variant className, Variant::mapType &args) = 0;

	/**
	 * Called whenever an annotation ends while this handler is active. The 
	 * function should return true if ending the annotation was successful,
	 * false otherwise.
	 *
	 * @param className is a string variant containing the name of the
	 * annotation class and the location of the class name in the source code.
	 * @param elementName is a string variant containing the name of the
	 * annotation class and the location of the element name in the source code.
	 * @return true if the mentioned annotation could be started here, false if
	 * an error occurred.
	 */
	virtual bool annotationEnd(Variant className, Variant elementName) = 0;

	/**
	 * Called whenever raw data (int the form of a string) is available for the
	 * Handler instance.
	 *
	 * @param data is a string variant containing the character data and its
	 * location.
	 */
	virtual void data(Variant data) = 0;
};

/**
 * HandlerConstructor is a function pointer type used to create concrete
 * instances of the Handler class.
 *
 * @param handlerData is the data that should be passed to the new handler
 * instance.
 * @return a newly created handler instance.
 */
using HandlerConstructor = Handler *(*)(const HandlerData &handlerData);

/**
 * The DefaultHandler class is used in case no element handler is specified in
 * the State descriptor.
 */
/*class EmptyHandler : public Handler {
public:
	using Handler::Handler;

	void start(Variant::mapType &args) override;

	void end() override;

	static Handler *create(const HandlerData &handlerData);
};*/

}
}

#endif /* _OUSIA_PARSER_STATE_HANDLER_HPP_ */