File: Item.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (556 lines) | stat: -rw-r--r-- 12,371 bytes parent folder | download | duplicates (3)
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#include "stdafx.h"
#include "Item.h"
#include "Exception.h"

namespace storm {
	namespace syntax {
		namespace glr {

			/**
			 * Item.
			 */

			Item::Item(Syntax *world, Production *p) {
				id = world->lookup(p);
				pos = firstPos(p);
			}

			Item::Item(Syntax *world, Nat production) {
				id = production;
				switch (Syntax::specialProd(production)) {
				case 0:
					pos = firstPos(world->production(production));
					break;
				case Syntax::prodEpsilon:
				case Syntax::prodESkip:
					pos = endPos;
					break;
				case Syntax::prodRepeat:
					pos = firstRepPos(world->production(production));
					break;
				}
			}

			Item::Item(Nat id, Nat pos) : id(id), pos(pos) {}

			Item last(Nat production) {
				return Item(production, Item::endPos);
			}

			Item special(Nat production) {
				return Item(production, Item::specialPos);
			}

			Nat Item::endPos = -1;
			Nat Item::specialPos = -2;

			Nat Item::firstPos(Production *p) {
				if (skippable(p->repType) && p->repStart == 0)
					return specialPos;
				else if (p->tokens->empty())
					return endPos;
				else
					return 0;
			}

			Nat Item::nextPos(Production *p, Nat pos) {
				if (pos == specialPos) {
					if (p->repEnd >= p->tokens->count())
						return endPos;
					else
						return p->repEnd;
				} else if (skippable(p->repType)) {
					if (pos + 1 == p->repStart)
						return specialPos;
				} else if (p->repType != repNone) {
					if (pos + 1 == p->repEnd)
						return specialPos;
				}

				if (pos + 1 >= p->tokens->count())
					return endPos;
				else
					return pos + 1;
			}

			Bool Item::prevPos(Production *p, Nat &pos) {
				if (pos == endPos) {
					pos = p->tokens->count();
					if (pos == 0) {
						pos = endPos;
						return false;
					}
					if (pos == p->repEnd && p->repType != repNone)
						pos = specialPos;
					else
						pos--;
					return true;
				} else if (pos == specialPos) {
					pos = skippable(p->repType) ? p->repStart : p->repEnd;
					if (pos == 0) {
						pos = specialPos;
						return false;
					}
					pos--;
					return true;
				} else {
					if (pos == 0)
						return false;
					if (pos-- == p->repEnd && p->repType != repNone)
						pos = specialPos;
					return true;
				}
			}

			Nat Item::firstRepPos(Production *p) {
				if (repeatable(p->repType))
					return specialPos;
				else if (p->repStart == p->repEnd)
					return endPos;
				else
					return p->repStart;
			}

			Nat Item::nextRepPos(Production *p, Nat pos) {
				if (pos == specialPos)
					return p->repStart;

				if (pos + 1 >= p->repEnd)
					return endPos;
				else
					return pos + 1;
			}

			Bool Item::prevRepPos(Production *p, Nat &pos) {
				if (pos == endPos) {
					pos = p->repEnd;
					if (pos == p->repStart)
						pos = specialPos;
					else
						pos--;
					return true;
				} else if (pos == specialPos) {
					// This is always the first token if we ever get here.
					return false;
				} else {
					if (pos == p->repStart)
						if (repeatable(p->repType))
							pos = specialPos;
						else
							return false;
					else
						pos--;
					return true;
				}
			}

			Item Item::next(Syntax *syntax) const {
				if (pos == endPos)
					return Item(id, endPos);
				if (Syntax::specialProd(id) == Syntax::prodESkip)
					return Item(id, endPos);

				return next(syntax->production(id));
			}

			Item Item::next(Production *p) const {
				if (pos == endPos)
					return Item(id, endPos);

				switch (Syntax::specialProd(id)) {
				case 0:
					return Item(id, nextPos(p, pos));
				case Syntax::prodEpsilon:
				case Syntax::prodESkip:
					return Item(id, endPos);
				case Syntax::prodRepeat:
					return Item(id, nextRepPos(p, pos));
				}

				return Item(id, endPos);
			}

			Bool Item::prev(Syntax *syntax) {
				if (Syntax::specialProd(id) == Syntax::prodESkip)
					return false;

				return prev(syntax->production(id));
			}

			Bool Item::prev(Production *p) {
				switch (Syntax::specialProd(id)) {
				case 0:
					return prevPos(p, pos);
				case Syntax::prodEpsilon:
				case Syntax::prodESkip:
					return false;
				case Syntax::prodRepeat:
					return prevRepPos(p, pos);
				}

				return false;
			}

			Nat Item::rule(Syntax *syntax) const {
				switch (Syntax::specialProd(id)) {
				case 0:
				default:
					return syntax->lookup(syntax->production(Syntax::baseProd(id))->rule());
				case Syntax::prodEpsilon:
				case Syntax::prodRepeat:
					return Syntax::baseProd(id) | Syntax::ruleRepeat;
				case Syntax::prodESkip:
					return Syntax::baseProd(id) | Syntax::ruleESkip;
				}
			}

			Nat Item::length(Syntax *syntax) const {
				Nat special = Syntax::specialProd(id);
				if (special == Syntax::prodESkip || special == Syntax::prodEpsilon)
					return 0;

				return length(syntax->production(id));
			}

			Nat Item::length(Production *p) const {
				Nat special = Syntax::specialProd(id);
				if (special == Syntax::prodESkip || special == Syntax::prodEpsilon)
					return 0;

				Nat rep = p->repEnd - p->repStart;
				if (special == Syntax::prodRepeat) {
					if (repeatable(p->repType))
						return rep + 1;
					else
						return rep;
				} else {
					if (skippable(p->repType))
						return p->tokens->count() - rep + 1;
					else if (p->repType == repNone)
						return p->tokens->count();
					else
						return p->tokens->count() + 1;
				}
			}

			Nat Item::tokenPos(Syntax *syntax) const {
				if (pos == endPos)
					return length(syntax);

				switch (Syntax::specialProd(id)) {
				case 0:
				case Syntax::prodRepeat:
					return tokenPos(syntax->production(id));
				case Syntax::prodEpsilon:
				case Syntax::prodESkip:
				default:
					return 0;
				}
			}

			Nat Item::tokenPos(Production *p) const {
				if (pos == endPos)
					return length(p);

				switch (Syntax::specialProd(id)) {
				case 0:
					if (pos == specialPos)
						return p->repStart;
					else if (p->repType != repNone && pos >= p->repEnd)
						return p->repStart + 1 + pos - p->repEnd;
					else
						return pos;
				case Syntax::prodRepeat:
					if (pos == specialPos)
						return 0;
					else
						return pos + 1 - p->repStart;
				case Syntax::prodEpsilon:
				case Syntax::prodESkip:
				default:
					return 0;
				}
			}

			Bool Item::end() const {
				return pos == endPos;
			}

			Bool Item::isRule(Syntax *syntax) const {
				if (end())
					return false;
				if (pos == specialPos)
					return true;

				Production *p = syntax->production(id);
				return p->tokens->at(pos)->asRule() != null;
			}

			Nat Item::nextRule(Syntax *syntax) const {
				assert(!end());
				assert(isRule(syntax));

				if (pos == specialPos)
					return Syntax::baseProd(id) | Syntax::ruleRepeat;

				Production *p = syntax->production(id);
				RuleToken *r = p->tokens->at(pos)->asRule();
				return syntax->lookup(r->rule);
			}

			Regex Item::nextRegex(Syntax *syntax) const {
				assert(!end());
				assert(!isRule(syntax));

				Production *p = syntax->production(id);
				return p->tokens->at(pos)->asRegex()->regex;
			}

			Bool Item::regexBefore(Syntax *syntax) const {
				Item i = *this;
				while (i.prev(syntax)) {
					if (!i.isRule(syntax)) {
						// This can not be the end, so it has to be a regex!
						return true;
					}
				}

				return false;
			}

			Nat Item::hash() const {
				return id + pos + (id << 8);
			}

			void Item::outputToken(StrBuf *to, Production *p, Nat pos) const {
				if (pos == specialPos) {
					*to << p->rule()->identifier() << S("'");
				} else {
					p->tokens->at(pos)->toS(to, false);
				}
			}

			void Item::output(StrBuf *to, Production *p, Nat mark, FirstFn first, NextFn next) const {
				bool prevDelim = false;
				Nat firstId = (*first)(p);
				for (Nat i = firstId; i != endPos; i = (*next)(p, i)) {
					bool currentDelim = false;
					if (i < p->tokens->count())
						currentDelim = p->tokens->at(i)->asDelim() != null;

					if (i != firstId && !currentDelim && !prevDelim)
						*to << S(" - ");

					if (i == mark)
						*to << S("<>");

					if (currentDelim)
						*to << S(", ");
					else
						outputToken(to, p, i);

					prevDelim = currentDelim;
				}
			}

			Str *Item::toS(Syntax *syntax) const {
				StrBuf *to = new (syntax) StrBuf();

				switch (Syntax::specialProd(id)) {
				case 0:
					*to << id;
					break;
				case Syntax::prodRepeat:
					*to << Syntax::baseProd(id) << S("'");
					break;
				case Syntax::prodEpsilon:
					*to << Syntax::baseProd(id) << S("''");
					break;
				case Syntax::prodESkip:
					*to << S("red") << Syntax::baseProd(id) << S(" ->");
					return to->toS();
				}
				*to << S(": ");

				Production *p = syntax->production(id);
				*to << p->rule()->identifier();
				if (Syntax::specialProd(id))
					*to << S("'");
				else if (p->priority != 0)
					*to << S("[") << p->priority << S("]");
				*to << S(" -> ");

				switch (Syntax::specialProd(id)) {
				case 0:
					output(to, p, pos, &Item::firstPos, &Item::nextPos);
					break;
				case Syntax::prodRepeat:
					output(to, p, pos, &Item::firstRepPos, &Item::nextRepPos);
					break;
				}

				if (pos == endPos)
					*to << S("<>");

				return to->toS();
			}

			void Item::toS(StrBuf *to) const {
				*to << S("(") << id << S(", ") << pos << S(")");
			}


			/**
			 * Item set.
			 */

			ItemSet::ItemSet() : data(null) {}

			Item ItemSet::operator [](Nat id) const {
				assert(id < count(), L"Index out of bounds!");
				return at(id);
			}

			Bool ItemSet::operator ==(ItemSet o) const {
				Nat c = count();
				if (c != o.count())
					return false;

				for (Nat i = 0; i < c; i++)
					if (data->v[i] != o.data->v[i])
						return false;

				return true;
			}

			Bool ItemSet::operator !=(ItemSet o) const {
				return !(*this == o);
			}

			Bool ItemSet::has(Item i) const {
				Nat p = itemPos(i);
				if (p >= count())
					return false;
				return data->v[p] == i;
			}

			Nat ItemSet::hash() const {
				Nat r = 5381;
				Nat c = count();
				for (Nat i = 0; i < c; i++) {
					r = ((r << 5) + r) + data->v[i].hash();
				}
				return r;
			}

			static const GcType itemType = {
				GcType::tArray,
				null,
				null,
				sizeof(Item),
				0,
				{},
			};

			Nat ItemSet::itemPos(Item find) const {
				Nat from = 0;
				Nat to = count();
				while (from < to) {
					nat mid = (to - from) / 2 + from;

					if (data->v[mid] < find) {
						from = mid + 1;
					} else {
						to = mid;
					}
				}

				return from;
			}

			Bool ItemSet::push(Engine &e, Item item) {
				// Find insertion position.
				Nat c = count();
				Nat insert = itemPos(item);

				// Already present?
				if (insert < c && item == data->v[insert])
					return false;

				// Insert and move the others.
				if (!data || data->filled == data->count) {
					// New allocation is needed.
					GcArray<Item> *alloc = runtime::allocArray<Item>(e, &itemType, c + grow);

					alloc->v[insert] = item;
					if (data) {
						alloc->filled = data->filled;
						memcpy(alloc->v, data->v, sizeof(Item)*insert);
						memcpy(alloc->v + insert + 1, data->v + insert, sizeof(Item)*(c - insert));
					}
					data = alloc;
				} else {
					memmove(data->v + insert + 1, data->v + insert, sizeof(Item)*(c - insert));
					data->v[insert] = item;
				}

				data->filled++;
				return true;
			}

			static void expand(Engine &e, ItemSet &to, const Item &i, Syntax *syntax) {
				if (!to.push(e, i))
					return;

				if (!i.isRule(syntax))
					return;

				Nat rule = i.nextRule(syntax);
				RuleInfo *info = syntax->ruleInfo(rule);
				for (Nat i = 0; i < info->count(); i++) {
					expand(e, to, Item(syntax, info->at(i)), syntax);
				}
			}

			ItemSet ItemSet::expand(Syntax *syntax) const {
				ItemSet result;
				Engine &e = syntax->engine();

				for (Nat i = 0; i < count(); i++) {
					glr::expand(e, result, at(i), syntax);
				}

				return result;
			}

			Str *ItemSet::toS(Syntax *syntax) const {
				StrBuf *to = new (syntax) StrBuf();
				*to << L"{\n";
				{
					Indent z(to);
					for (Nat i = 0; i < count(); i++) {
						*to << at(i) << L"\n";
					}
				}
				*to << L"}";
				return to->toS();
			}

			Bool push(EnginePtr e, ItemSet &to, Item item) {
				return to.push(e.v, item);
			}

			void ItemSet::toS(StrBuf *to) const {
				*to << S("{\n");
				{
					Indent z(to);
					for (Nat i = 0; i < count(); i++) {
						*to << at(i) << S("\n");
					}
				}
				*to << S("}");
			}

		}
	}
}