-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.hpp
450 lines (350 loc) · 15.6 KB
/
parser.hpp
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
440
441
442
443
444
445
446
447
448
449
450
#ifndef PARSER_HEADER
#define PARSER_HEADER
#include <iostream>
#include <utility>
#include <vector>
#include <unordered_set>
#include "base_class.hpp"
#include "codepoints.hpp"
#include "token.hpp"
#include "monoid_sequence.hpp"
#include "annotation.hpp"
#include "buffer.hpp"
#include "deduction.hpp"
#include "annotation_fact.hpp"
#include "session.hpp"
using token_sequence = monoid_sequence<token>;
using token_iterator = typename token_sequence::iterator;
class token_available : public negative_annotation_fact {
protected:
::buffer* buffer;
const token_iterator self;
public:
token_available(typename ::session&session, ::buffer*buffer, token_iterator self);
token_available(typename ::session&session, token_iterator self);
protected:
virtual bool is_equal_to_instance_of_like_class(const base_class&other) const override;
virtual std::vector<const fact_annotatable*>get_annotatables() const override;
virtual void justification_hook() const override;
virtual void unjustification_hook() const override;
virtual std::vector<fact*>get_immediate_consequences() const override;
virtual std::ostream&print(std::ostream&out) const override;
public:
virtual bool is_observation() const override { return true; }
virtual const base_class*clone() const override;
virtual size_t hash() const override;
};
class next_token : public annotation_fact {
protected:
::buffer* buffer;
const token_iterator self;
const token_iterator next;
public:
next_token(typename ::session&session, ::buffer*buffer, token_iterator self, token_iterator next);
next_token(typename ::session&session, token_iterator self, token_iterator next);
protected:
virtual bool is_equal_to_instance_of_like_class(const base_class&other) const override;
virtual std::vector<const fact_annotatable*>get_annotatables() const override;
virtual std::vector<fact*>get_immediate_consequences() const override;
virtual std::ostream&print(std::ostream&out) const override;
public:
token_iterator get_self() const;
token_iterator get_next() const;
virtual bool is_observation() const override { return true; }
virtual const base_class*clone() const override;
virtual size_t hash() const override;
};
// These functions are saturating: they return their argument if there is no
// link to a previous/next token_iterator.
token_iterator previous(const token_iterator&iterator);
token_iterator next(const token_iterator&iterator);
class end_of_unit : public combined_annotation_fact {
protected:
const token_iterator self;
public:
end_of_unit(typename ::session&session, token_iterator self, bool in_the_positive_sense);
protected:
virtual bool is_equal_to_instance_of_like_class(const base_class&other) const override;
virtual std::vector<const fact_annotatable*>get_annotatables() const override;
public:
token_iterator get_self() const;
virtual size_t hash() const override;
};
class end_of_sentence : public end_of_unit {
protected:
::buffer* buffer;
public:
end_of_sentence(typename ::session&session, ::buffer*buffer, token_iterator self, bool in_the_positive_sense);
end_of_sentence(typename ::session&session, token_iterator self, bool in_the_positive_sense);
protected:
virtual void justification_hook() const override;
virtual void unjustification_hook() const override;
virtual std::vector<fact*>get_immediate_consequences() const override;
virtual std::ostream&print(std::ostream&out) const override;
public:
virtual const base_class*clone() const override;
};
class parseme : public base_class {
public:
// This version of accepts is to be called when trying to match terminals.
virtual bool accepts(const token_iterator&iterator) const;
// This version of accepts is to be called when trying to match nonterminals.
virtual bool accepts(const parseme&other) const;
};
template<size_t hash_value>class unique_terminal : public parseme {
protected:
virtual bool is_equal_to_instance_of_like_class(const base_class&other) const override { return true; }
public:
virtual size_t hash() const override { return hash_value; }
};
class epsilon_terminal : public unique_terminal<0> {
public:
virtual bool accepts(const token_iterator&iterator) const override;
virtual const base_class*clone() const override;
};
class digits_terminal : public unique_terminal<1> {
public:
virtual bool accepts(const token_iterator&iterator) const override;
virtual const base_class*clone() const override;
};
class word_terminal : public unique_terminal<2> {
public:
virtual bool accepts(const token_iterator&iterator) const override;
virtual const base_class*clone() const override;
};
class name_word_terminal : public unique_terminal<3> {
public:
virtual bool accepts(const token_iterator&iterator) const override;
virtual const base_class*clone() const override;
};
class token_terminal : public parseme {
protected:
const i7_string* text;
public:
token_terminal(const i7_string&text);
~token_terminal();
protected:
virtual bool is_equal_to_instance_of_like_class(const base_class&other) const override;
public:
const i7_string&get_text() const;
virtual bool accepts(const token_iterator&iterator) const override;
virtual const base_class*clone() const override;
virtual size_t hash() const override;
};
class nonterminal : public parseme {
protected:
const i7_string* kind_name;
// See http://inform7.com/mantis/view.php?id=1005#c1845.
unsigned tier;
public:
nonterminal(const i7_string&kind_name, unsigned tier);
nonterminal(const nonterminal©);
nonterminal(const nonterminal©, unsigned replacement_tier);
virtual ~nonterminal();
protected:
virtual bool is_equal_to_instance_of_like_class(const base_class&other) const override;
public:
const i7_string&get_kind_name() const;
unsigned get_tier() const;
virtual bool accepts(const parseme&other) const override;
virtual const base_class*clone() const override;
virtual size_t hash() const override;
};
class production : public base_class, public fact {
public:
struct possible_beginning {
const unsigned epsilon_count;
const ::parseme* parseme;
};
protected:
// The left-hand side of the production.
const nonterminal* result;
// The right-hand side of the production, in the form (...|...)(...|...)....
// This is more general than necessary, but simplifies the logic for dynamic
// productions because Inform 7 declarations create them in exactly the same
// form.
//
// The sequence is left empty in productions that match atomic literals of
// kinds with infinite domains—numbers and strings, for instance.
std::vector<std::vector<const parseme*> >
alternatives_sequence;
// The length of the longest prefix in alternatives_sequence that can match
// all epsilons. Should not be the length of alternatives_sequence except
// mid-construction.
unsigned epsilon_prefix_length;
// All of the non-epsilon parsemes that may begin this production.
std::vector<possible_beginning> beginnings;
// The cached hash value.
size_t hash_value;
// Whether or not this production is internally generated, and therefore an
// observation.
const bool internal;
public:
production(typename ::session&session, const nonterminal&result, bool internal = false);
production(const production©);
virtual ~production();
protected:
virtual bool is_equal_to_instance_of_like_class(const base_class&other) const override;
virtual bool can_begin_sentence() const = 0;
virtual std::vector<fact*>get_immediate_consequences() const override;
public:
// Note that the following violates the fact contract. It is only safe to do
// so because productions, and internal productions in particular, never
// annotate.
virtual bool is_observation() const { return internal; }
void add_slot();
void add_alternative_to_last_slot(const parseme&alternative);
const nonterminal*get_result() const;
unsigned get_slot_count() const;
const std::vector<possible_beginning>&get_beginnings() const;
const std::vector<const parseme*>&get_alternatives(unsigned slot_index) const;
// This version of accepts is to be called when trying to match terminals.
bool accepts(unsigned slot_index, const ::token_iterator&iterator) const;
// This version of accepts is to be called when trying to match nonterminals.
bool accepts(unsigned slot_index, const ::parseme&parseme) const;
// This version of can_begin_with is to be called when trying to match terminals.
std::vector<unsigned>can_begin_with(const ::token_iterator&iterator) const;
// This version of can_begin_with is to be called when trying to match nonterminals.
std::vector<unsigned>can_begin_with(const ::parseme&parseme) const;
// For checking extra constraints like positions relative to end-of-sentence
// markers.
bool can_reach_slot_count_at(unsigned slot_count, token_iterator position) const;
virtual bool can_reach_slot_count_at(unsigned slot_count, token_iterator position, bool assumed_end_of_sentence_state) const = 0;
virtual size_t hash() const override;
};
// A subsentence matches any token sequence that does not cross a sentence
// boundary.
class subsentence : public production {
public:
subsentence(typename ::session&session, const nonterminal&result, bool internal = false);
subsentence(const subsentence©);
protected:
virtual bool can_begin_sentence() const override;
virtual void justification_hook() const override;
virtual void unjustification_hook() const override;
virtual std::ostream&print(std::ostream&out) const override;
public:
virtual operator bool() const override;
virtual bool can_reach_slot_count_at(unsigned slot_count, token_iterator position, bool assumed_end_of_sentence_state) const override;
virtual const base_class*clone() const override;
};
/* A wording matches any token sequence that begins in plain I7 (possibly within
* an extract) or documentation, ends in the same lexical state, and does not
* cross a sentence boundary.
*/
class wording : public production {
public:
wording(typename ::session&session, const nonterminal&result, bool internal = false);
wording(const wording©);
protected:
virtual bool can_begin_sentence() const override;
virtual void justification_hook() const override;
virtual void unjustification_hook() const override;
virtual std::ostream&print(std::ostream&out) const override;
public:
virtual operator bool() const override;
virtual bool can_reach_slot_count_at(unsigned slot_count, token_iterator position, bool assumed_end_of_sentence_state) const override;
virtual const base_class*clone() const override;
};
/* A sentence matches any token sequence that begins in plain I7 (possibly
* within an extract) or documentation, ends in the same lexical state just
* before a sentence boundary, and does not cross a sentence boundary.
*
* Ordinarily a sentence must also begin just after a sentence boundary, but
* this requirement is waived when matching part of a larger sentence or
* passage, a concession made for the sake of sentence-splitting commas.
*/
class sentence : public production {
public:
sentence(typename ::session&session, const nonterminal&result, bool internal = false);
sentence(const sentence©);
protected:
virtual bool can_begin_sentence() const override;
virtual void justification_hook() const override;
virtual void unjustification_hook() const override;
virtual std::ostream&print(std::ostream&out) const override;
public:
virtual operator bool() const override;
virtual bool can_reach_slot_count_at(unsigned slot_count, token_iterator position, bool assumed_end_of_sentence_state) const override;
virtual const base_class*clone() const override;
};
/* A passage matches any token sequence that begins in plain I7 (possibly within
* an extract) or documentation, usually just after a sentence boundary, and
* ends in the same lexical state just before a sentence boundary. It is
* ordinarily used to agglomerate several sentences that are part of a fixed
* pattern, like the preamble-body form in definitions, phrases, and rules.
*
* Like sentences, passages may begin in places other than after a sentence
* boundary if they are part of a larger passage. This makes the parser a
* little simpler to write, but it's not clear that this caveat is actually
* useful for anything.
*/
class passage : public production {
public:
passage(typename ::session&session, const nonterminal&result, bool internal = false);
passage(const passage©);
protected:
virtual bool can_begin_sentence() const override;
virtual void justification_hook() const override;
virtual void unjustification_hook() const override;
virtual std::ostream&print(std::ostream&out) const override;
public:
virtual operator bool() const override;
virtual bool can_reach_slot_count_at(unsigned slot_count, token_iterator position, bool assumed_end_of_sentence_state) const override;
virtual const base_class*clone() const override;
};
class potential_match : public annotation_fact {
protected:
::buffer* buffer;
// The production that is matched or partially matched.
const ::production* production;
// The number of slots matched in that production.
const unsigned slots_filled;
// The first token matched, inclusive.
const token_iterator beginning;
// The last token matched, also inclusive, contrary to the usual convention.
// (This is because a potential match may be constructed before the link to
// the exclusive bound is asserted).
const token_iterator inclusive_end;
public:
potential_match(typename ::session&session, ::buffer*buffer, const ::production&production, unsigned slots_filled, token_iterator beginning, token_iterator inclusive_end);
potential_match(typename ::session&session, ::buffer*buffer, const ::production&production, unsigned slots_filled, token_iterator beginning);
potential_match(const potential_match©);
potential_match(const potential_match&prefix, token_iterator inclusive_end, bool assume_next_token_is_justified = false);
potential_match(const ::production&production, unsigned slots_filled, const potential_match&addendum);
potential_match(const potential_match&prefix, const potential_match&addendum, bool assume_next_token_is_justified = false);
~potential_match();
protected:
virtual bool is_equal_to_instance_of_like_class(const base_class&other) const override;
virtual std::vector<const fact_annotatable*>get_annotatables() const override;
virtual std::vector<fact*>get_immediate_consequences() const override;
virtual std::ostream&print(std::ostream&out) const override;
public:
const nonterminal&get_result() const;
const ::production&get_production() const;
unsigned get_slots_filled() const;
token_iterator get_beginning() const;
token_iterator get_inclusive_end() const;
bool is_filled() const;
bool is_complete() const;
bool can_continue_with(token_iterator end, bool assume_next_token_is_justified = false) const;
bool can_continue_with(const potential_match&addendum, bool assume_next_token_is_justified = false) const;
const std::vector<const parseme*>&get_continuing_alternatives() const;
virtual const base_class*clone() const override;
virtual size_t hash() const override;
};
class match : public potential_match {
public:
match(const potential_match©);
protected:
virtual void justification_hook() const override;
virtual void unjustification_hook() const override;
virtual std::vector<fact*>get_immediate_consequences() const override;
virtual std::ostream&print(std::ostream&out) const override;
public:
bool is_complete() const;
virtual const base_class*clone() const override;
};
extern internalizer<parseme>parseme_bank;
extern internalizer<production>production_bank;
#endif