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
|
/*
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 Stack.hpp
*
* Helper classes for document or description parsers. Contains the
* Stack 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_STACK_HPP_
#define _OUSIA_PARSER_STACK_STACK_HPP_
#include <cstdint>
#include <map>
#include <memory>
#include <set>
#include <vector>
#include <core/common/Variant.hpp>
#include <core/parser/Parser.hpp>
namespace ousia {
// Forward declarations
class ParserContext;
class Logger;
namespace parser_stack {
// Forward declarations
class Handler;
class State;
/**
* The HandlerInfo class is used internally by the stack to associate additional
* (mutable) data with a handler instance.
*/
class HandlerInfo {
public:
/**
* Pointer pointing at the actual handler instance.
*/
std::shared_ptr<Handler> handler;
/**
* Next field index to be passed to the "fieldStart" function of the Handler
* class.
*/
size_t fieldIdx;
/**
* Set to true if the handler is valid (which is the case if the "start"
* method has returned true). If the handler is invalid, no more calls are
* directed at it until it can be removed from the stack.
*/
bool valid : 1;
/**
* Set to true if this is an implicit handler, that was created when the
* current stack state was deduced.
*/
bool implicit : 1;
/**
* Set to true if the handler currently is in a field.
*/
bool inField : 1;
/**
* Set to true if the handler currently is in the default field.
*/
bool inDefaultField : 1;
/**
* Set to true if the handler currently is in an implicitly started default
* field.
*/
bool inImplicitDefaultField : 1;
/**
* Set to false if this field is only opened pro-forma and does not accept
* any data. Otherwise set to true.
*/
bool inValidField : 1;
/**
* Set to true, if the default field was already started.
*/
bool hadDefaultField : 1;
/**
* Default constructor of the HandlerInfo class.
*/
HandlerInfo();
/**
* Constructor of the HandlerInfo class, allows to set all flags manually.
*/
HandlerInfo(bool valid, bool implicit, bool inField, bool inDefaultField,
bool inImplicitDefaultField, bool inValidField);
/**
* Constructor of the HandlerInfo class, taking a shared_ptr to the handler
* to which additional information should be attached.
*/
HandlerInfo(std::shared_ptr<Handler> handler);
/**
* Destructor of the HandlerInfo class (to allow Handler to be forward
* declared).
*/
~HandlerInfo();
/**
* Updates the "field" flags according to a "fieldStart" event.
*/
void fieldStart(bool isDefault, bool isImplicit, bool isValid);
/**
* Updates the "fields" flags according to a "fieldEnd" event.
*/
void fieldEnd();
};
/**
* The Stack class is a pushdown automaton responsible for turning a command
* stream into a tree of Node instances. It does so by following a state
* transition graph and creating a set of Handler instances, which are placed
* on the stack.
*/
class Stack {
private:
/**
* Reference at the parser context.
*/
ParserContext &ctx;
/**
* Map containing all registered command names and the corresponding
* state descriptors.
*/
const std::multimap<std::string, const State *> &states;
/**
* Internal stack used for managing the currently active Handler instances.
*/
std::vector<HandlerInfo> stack;
/**
* Return the reference in the Logger instance stored within the context.
*/
Logger &logger();
/**
* Used internally to get all expected command names for the current state.
* This function is used to build error messages.
*
* @return a set of strings containing the names of the expected commands.
*/
std::set<std::string> expectedCommands();
/**
* Returns the targetState for a command with the given name that can be
* reached from the current state.
*
* @param name is the name of the requested command.
* @return nullptr if no target state was found, a pointer at the target
* state otherwise.
*/
const State *findTargetState(const std::string &name);
/**
* Returns the targetState for a command with the given name that can be
* reached from the current state, also including the wildcard "*" state.
* Throws an exception if the given target state is not a valid identifier.
*
* @param name is the name of the requested command.
* @return nullptr if no target state was found, a pointer at the target
* state otherwise.
*/
const State *findTargetStateOrWildcard(const std::string &name);
/**
* Tries to reconstruct the parser state from the Scope instance of the
* ParserContext given in the constructor. This functionality is needed for
* including files,as the Parser of the included file needs to be brought to
* an equivalent state as the one in the including file.
*/
void deduceState();
/**
* Returns a reference at the current HandlerInfo instance (or a stub
* HandlerInfo instance if the stack is empty).
*/
HandlerInfo ¤tInfo();
/**
* Returns a reference at the last HandlerInfo instance (or a stub
* HandlerInfo instance if the stack has only one element).
*/
HandlerInfo &lastInfo();
/**
* Ends all handlers that currently are not inside a field and already had
* a default field. This method is called whenever the data() and command()
* events are reached.
*/
void endOverdueHandlers();
/**
* Ends the current handler and removes the corresponding element from the
* stack.
*/
void endCurrentHandler();
/**
* Tries to start a default field for the current handler, if currently the
* handler is not inside a field and did not have a default field yet.
*
* @return true if the handler is inside a field, false if no field could
* be started.
*/
bool ensureHandlerIsInField();
/**
* Returns true if all handlers on the stack are currently valid, or false
* if at least one handler is invalid.
*
* @return true if all handlers on the stack are valid.
*/
bool handlersValid();
public:
/**
* Creates a new instance of the Stack class.
*
* @param ctx is the parser context the parser stack is working on.
* @param states is a map containing the command names and pointers at the
* corresponding State instances.
*/
Stack(ParserContext &ctx,
const std::multimap<std::string, const State *> &states);
/**
* Destructor of the Stack class.
*/
~Stack();
/**
* Returns the state the Stack instance currently is in.
*
* @return the state of the currently active Handler instance or STATE_NONE
* if no handler is on the stack.
*/
const State ¤tState();
/**
* 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 currentCommandName();
/**
* Function that should be called whenever a new command is reached.
*
* @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 containing the arguments that were passed to the
* command.
*/
void command(const Variant &name, const Variant::mapType &args);
/**
* Function that shuold be called whenever character data is found in the
* input stream. May only be called if the currently is a command on the
* stack.
*
* @param data is a string variant containing the data that has been found.
*/
void data(const Variant &data);
/**
* Function that should be called whenever a new field starts. Fields of the
* same command may not be separated by calls to data or annotations. Doing
* so will result in a LoggableException.
*
* @param isDefault should be set to true if the started field explicitly
* is the default field.
*/
void fieldStart(bool isDefault);
/**
* Function that should be called whenever a field ends. Calling this
* function if there is no field to end will result in a LoggableException.
*/
void fieldEnd();
/**
* Function that should be called whenever an annotation starts.
*
* @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(const Variant &className, const Variant &args);
/**
* Function that should be called whenever an annotation ends.
*
* @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(const Variant &className, const Variant &elementName);
/**
* Function that should be called whenever a previously registered token
* is found in the input stream.
*
* @param token is string variant containing the token that was encountered.
*/
void token(Variant token);
};
}
}
#endif /* _OUSIA_STACK_HPP_ */
|