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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
/*
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 ParserStack.hpp
*
* Helper classes for document or description parsers. Contains the ParserStack
* class, which is an pushdown automaton responsible for accepting commands in
* the correct order and calling specified handlers.
*
* @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de)
*/
#ifndef _OUSIA_PARSER_STACK_HPP_
#define _OUSIA_PARSER_STACK_HPP_
#include <cstdint>
#include <map>
#include <memory>
#include <set>
#include <stack>
#include <vector>
#include <core/common/Variant.hpp>
#include <core/common/Logger.hpp>
#include <core/common/Argument.hpp>
#include "Parser.hpp"
#include "ParserContext.hpp"
namespace ousia {
/**
* The State type alias is used to
*/
using State = int16_t;
static const State STATE_ALL = -2;
static const State STATE_NONE = -1;
/**
* Struct collecting all the data that is being passed to a Handler instance.
*/
struct HandlerData {
/**
* Reference to the ParserContext instance that should be used to resolve
* references to nodes in the Graph.
*/
const ParserContext &ctx;
/**
* Contains the name of the tag that is being handled.
*/
const std::string name;
/**
* Contains the current state of the state machine.
*/
const State state;
/**
* Contains the state of the state machine when the parent node was handled.
*/
const State parentState;
/**
* Set to true if the tag that is being handled is not the tag that was
* specified in the state machine but a child tag of that tag.
*/
const bool isChild;
/**
* Current source code location.
*/
const SourceLocation location;
/**
* Constructor of the HandlerData class.
*
* @param ctx is the parser context the handler should be executed in.
* @param name is the name of the string.
* @param state is the state this handler was called for.
* @param parentState is the state of the parent command.
* @param isChild specifies whether this handler was called not for the
* command that was specified in the state machine but a child command.
* @param location is the location at which the handler is created.
*/
HandlerData(const ParserContext &ctx, std::string name, State state,
State parentState, bool isChild, const SourceLocation location)
: ctx(ctx),
name(std::move(name)),
state(state),
parentState(parentState),
isChild(isChild),
location(location){};
};
/**
* The handler class provides a context for handling an XML tag. It has to be
* overridden and registered in the StateStack class to form handlers for
* concrete XML tags.
*/
class Handler {
private:
/**
* Structure containing the internal handler data.
*/
const HandlerData handlerData;
public:
/**
* Constructor of the Handler class.
*
* @param data is a structure containing all data being passed to the
* handler.
*/
Handler(const HandlerData &handlerData) : handlerData(handlerData){};
/**
* Virtual destructor.
*/
virtual ~Handler(){};
const std::string &name() { return handlerData.name; }
ParserScope &scope() { return handlerData.ctx.scope; }
Registry ®istry() { return handlerData.ctx.registry; }
Manager &manager() { return handlerData.ctx.manager; }
Logger &logger() { return handlerData.ctx.logger; }
Rooted<model::Project> project() { return handlerData.ctx.project; }
State state() { return handlerData.state; }
State parentState() { return handlerData.parentState; }
SourceLocation location() { return handlerData.location; }
bool isChild() { return handlerData.isChild; }
/**
* 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).
*/
virtual void start(Variant::mapType &args) = 0;
/**
* Called whenever the command for which this handler is defined ends.
*/
virtual void end() = 0;
/**
* Called whenever raw data (int the form of a string) is available for the
* Handler instance. In the default handler an exception is raised if the
* received data contains non-whitespace characters.
*
* @param data is a pointer at the character data that is available for the
* Handler instance.
* @param field is the field number (the interpretation of this value
* depends on the format that is being parsed).
*/
virtual void data(const std::string &data, int field);
/**
* Called whenever a direct child element was created and has ended.
*
* @param handler is a reference at the child Handler instance.
*/
virtual void child(std::shared_ptr<Handler> handler);
};
/**
* 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);
struct HandlerDescriptor;
/**
* Used internlly by StateStack to store Handler instances and parameters
* from HandlerDescriptor that are not stored in the Handler instance
* itself. Instances of the HandlerInstance class can be created using the
* HandlerDescriptor "create" method.
*/
struct HandlerInstance {
/**
* Pointer at the actual handler instance.
*/
std::shared_ptr<Handler> handler;
/**
* Pointer pointing at the descriptor from which the handler instance was
* derived.
*/
const HandlerDescriptor *descr;
HandlerInstance(Handler *handler, const HandlerDescriptor *descr)
: handler(handler), descr(descr)
{
}
};
/**
* Used internally by StateStack to store the pushdown automaton
* description.
*/
struct HandlerDescriptor {
/**
* The valid parent states.
*/
const std::set<State> parentStates;
/**
* Pointer at a function which creates a new concrete Handler instance.
*/
const HandlerConstructor ctor;
/**
* The target state for the registered handler.
*/
const State targetState;
/**
* Set to true if this handler instance allows arbitrary children as
* tags.
*/
const bool arbitraryChildren;
/**
* Reference at an argument descriptor that should be used for validating
* the incomming arguments.
*/
const Arguments arguments;
/**
* Constructor of the HandlerDescriptor class.
*
* @param parentStates is a set of states in which a new handler of this
* type may be instantiated.
* @param ctor is a function pointer pointing at a function that
* instantiates the acutal Handler instance.
* @param targetState is the state the ParserStack switches to after
* instantiating an in instance of the described Handler instances.
* @param arbitraryChildren allows the Handler instance to handle any child
* node.
* @param arguments is an optional argument descriptor used for validating
* the arguments that are passed to the instantiation of a handler function.
*/
HandlerDescriptor(std::set<State> parentStates, HandlerConstructor ctor,
State targetState, bool arbitraryChildren = false,
Arguments arguments = Arguments::None)
: parentStates(std::move(parentStates)),
ctor(ctor),
targetState(targetState),
arbitraryChildren(arbitraryChildren),
arguments(std::move(arguments))
{
}
/**
* Creates an instance of the concrete Handler class represented by the
* HandlerDescriptor and calls its start function.
*/
HandlerInstance create(const ParserContext &ctx, std::string name,
State parentState, bool isChild,
Variant::mapType &args,
const SourceLocation &location) const;
};
/**
* The ParserStack class is a pushdown automaton responsible for turning a
* command stream into a tree of Node instances.
*/
class ParserStack {
private:
/**
* Reference at the parser context.
*/
ParserContext &ctx;
/**
* Current location in the source code.
*/
SourceLocation location;
/**
* Map containing all registered command names and the corresponding
* handler
* descriptor.
*/
const std::multimap<std::string, HandlerDescriptor> &handlers;
/**
* Internal stack used for managing the currently active Handler instances.
*/
std::stack<HandlerInstance> stack;
/**
* Reference at some user defined data.
*/
void *userData;
/**
* Used internally to get all expected command names for the given state
* (does not work if the current Handler instance allows arbitrary
* children). This function is used to build error messages.
*
* @param state is the state for which all expected command names should be
* returned.
*/
std::set<std::string> expectedCommands(State state);
public:
/**
* Creates a new instance of the ParserStack class.
*
* @param ctx is the parser context the parser stack is working on.
* @param handlers is a map containing the command names and the
* corresponding HandlerDescriptor instances.
*/
ParserStack(ParserContext &ctx,
const std::multimap<std::string, HandlerDescriptor> &handlers,
void *userData = nullptr)
: ctx(ctx), handlers(handlers), userData(userData){};
/**
* Returns the state the ParserStack instance currently is in.
*
* @return the state of the currently active Handler instance or STATE_NONE
* if no handler is on the stack.
*/
State currentState()
{
return stack.empty() ? STATE_NONE : stack.top().handler->state();
}
/**
* Returns the command name that is currently being handled.
*
* @return the name of the command currently being handled by the active
* Handler instance or an empty string if no handler is currently active.
*/
std::string currentName()
{
return stack.empty() ? std::string{} : stack.top().handler->name();
}
/**
* Returns whether the current command handler allows arbitrary children.
*
* @return true if the handler allows arbitrary children, false otherwise.
*/
bool currentArbitraryChildren()
{
return stack.empty() ? false : stack.top().descr->arbitraryChildren;
}
/**
* Function that should be called whenever a new command starts.
*
* @param name is the name of the command.
* @param args is a map from strings to variants (argument name and value).
* @param location is the location in the source file at which the command
* starts.
*/
void start(std::string name, Variant::mapType &args,
const SourceLocation &location = SourceLocation{});
/**
* Function that should be called whenever a new command starts.
*
* @param name is the name of the command.
* @param args is a map from strings to variants (argument name and value).
* @param location is the location in the source file at which the command
* starts.
*/
void start(std::string name, const Variant::mapType &args,
const SourceLocation &location = SourceLocation{});
/**
* Function called whenever a command ends.
*/
void end();
/**
* Function that should be called whenever data is available for the
* command.
*
* @param data is the data that should be passed to the handler.
* @param field is the field number (the interpretation of this value
* depends on the format that is being parsed).
*/
void data(const std::string &data, int field = 0);
/**
* Returns a reference to the parser context the parser stack is currently
* working on.
*
* @return a reference to the parser context.
*/
ParserContext &getContext() { return ctx; }
/**
* Returns the user defined data.
*
* @return the userData pointer that was given in the constructor.
*/
void *getUserData() { return userData; }
};
}
#endif /* _OUSIA_PARSER_STACK_HPP_ */
|