File: InfoNode.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • 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 (409 lines) | stat: -rw-r--r-- 10,025 bytes parent folder | download | duplicates (2)
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
#include "stdafx.h"
#include "InfoNode.h"
#include "Core/Array.h"
#include "Core/Str.h"
#include "Core/StrBuf.h"
#include "Rule.h"
#include "Production.h"
#include "Node.h"
#include "Lib/Array.h"

namespace storm {
	namespace syntax {

		const Nat InfoNode::errorMask  = 0x80000000;
		const Nat InfoNode::delimMask  = 0x40000000;
		const Nat InfoNode::lengthMask = 0x3FFFFFFF;

		InfoNode::InfoNode() {
			parentNode = null;
			color = tNone;
			data = 0;
			invalidate();
		}

		Nat InfoNode::length() {
			if ((data & lengthMask) == lengthMask) {
				data &= ~lengthMask;
				data |= computeLength() & lengthMask;
			}

			return data & lengthMask;
		}

		Bool InfoNode::error() const {
			return (data & errorMask) != 0;
		}

		void InfoNode::error(Bool v) {
			if (v)
				data |= errorMask;
			else
				data &= ~errorMask;
		}

		Bool InfoNode::delimiter() const {
			return (data & delimMask) != 0;
		}

		void InfoNode::delimiter(Bool v) {
			if (v)
				data |= delimMask;
			else
				data &= ~delimMask;
		}

		InfoLeaf *InfoNode::leafAt(Nat pos) {
			return null;
		}

		TextIndent InfoNode::indentAt(Nat pos) {
			return TextIndent();
		}

		Nat InfoNode::computeLength() {
			return 0;
		}

		void InfoNode::invalidate() {
			data |= lengthMask;
			if (parentNode)
				parentNode->invalidate();
		}

		Str *InfoNode::format() const {
			StrBuf *to = new (this) StrBuf();
			format(to);
			return to->toS();
		}

		void InfoNode::format(StrBuf *to) const {
			if (delimiter())
				*to << L" (delimiter)";
			if (error())
				*to << L" (contains errors)";
			if (color != tNone)
				*to << L" #" << name(engine(), color);
		}

		Nat InfoNode::dbg_size() {
			return sizeof(InfoNode);
		}

		/**
		 * Internal node.
		 */

		InfoInternal::InfoInternal(Production *p, Nat children) {
			this->prod = p;
			this->children = runtime::allocArray<InfoNode *>(engine(), &pointerArrayType, children);
		}

		InfoInternal::InfoInternal(InfoInternal *src, Nat children) {
			this->prod = src->prod;
			this->children = runtime::allocArray<InfoNode *>(engine(), &pointerArrayType, children);
			Nat copy = min(children, src->count());
			for (Nat i = 0; i < copy; i++)
				this->children->v[i] = src->children->v[i];
		}

		InfoLeaf *InfoInternal::leafAt(Nat pos) {
			for (nat i = 0; i < count(); i++) {
				Nat len = at(i)->length();
				if (pos < len)
					return at(i)->leafAt(pos);
				pos -= len;
			}
			return null;
		}

		TextIndent InfoInternal::indentAt(Nat pos) {
			Nat offset = 0;
			Nat indentStartOffsetBeg = 0;
			Nat indentStartOffsetEnd = 0;

			for (Nat i = 0; i < count(); i++) {
				InfoNode *child = at(i);
				Nat len = child->length();

				if (indent && i + 1 == indent->start)
					indentStartOffsetBeg = offset;
				if (indent && i == indent->start)
					indentStartOffsetEnd = offset;

				if (pos >= offset && pos < offset + len && len > 0) {
					TextIndent r = child->indentAt(pos - offset);
					r.offset(offset);

					if (indent)
						r.applyParent(indent, i, indentStartOffsetBeg, indentStartOffsetEnd);
					return r;
				}

				offset += len;
			}

			return TextIndent();
		}

		Nat InfoInternal::computeLength() {
			Nat len = 0;
			for (Nat i = 0; i < count(); i++)
				len += at(i)->length();
			return len;
		}

		void InfoInternal::outOfBounds(Nat v) {
			throw new (this) ArrayError(v, count());
		}

		void InfoInternal::set(Nat id, InfoNode *node) {
			if (id < count()) {
				children->v[id] = node;
				node->parent(this);
				invalidate();
			} else {
				outOfBounds(id);
			}
		}

		static Node *allocNode(Production *from, SrcPos pos) {
			ProductionType *type = from->type();

			// A bit ugly, but this is enough for the object to be considered a proper object
			// when it is populated.
			void *mem = runtime::allocObject(type->size().current(), type);
			Node *r = new (Place(mem)) Node(pos);
			type->vtable()->insert(r);

			// Create any arrays needed.
			for (nat i = 0; i < type->arrayMembers->count(); i++) {
				MemberVar *v = type->arrayMembers->at(i);
				int offset = v->rawOffset().current();

				// This will actually create the correct subtype as long as we're creating
				// something inherited from Object or TObject (which we are).
				Array<Object *> *arr = new (v->type.type) Array<Object *>();
				OFFSET_IN(r, offset, Object *) = arr;
			}

			return r;
		}

		template <class T>
		static void setValue(Node *node, MemberVar *target, T *elem) {
			int offset = target->rawOffset().current();
			if (isArray(target->type)) {
				// Arrays are initialized earlier.
				OFFSET_IN(node, offset, Array<T *> *)->push(elem);
			} else {
				OFFSET_IN(node, offset, T *) = elem;
			}
		}

		Node *InfoInternal::tree(Url *url) {
			return tree(url, 0);
		}

		Node *InfoInternal::tree(Url *url, Nat start) {
			if (error() || !prod)
				throw new (this) UsageError(S("Trying to create a syntax tree from a node that contains an error."));


			// Check number of children:
			Nat tokCount = prod->tokens->count();
			Nat repLength = 0;
			Bool countOk = tokCount == count();
			Nat repetitions = 0;
			if (prod->repType != repNone) {
				// TODO: Check so that this is correct!
				repLength = prod->repEnd - prod->repStart;
				if (skippable(prod->repType)) {
					if (tokCount == count() + repLength) {
						repetitions = 0;
						countOk = true;
					}
				}
				if (repeatable(prod->repType) && count() > tokCount) {
					Nat diff = count() - tokCount;
					if ((diff % repLength) == 0) {
						repetitions = 1 + (diff / repLength);
						countOk = true;
					}
				}
			}
			if (!countOk)
				throw new (this) UsageError(S("Invalid number of children for node."));

			// Output node.
			Node *out = allocNode(prod, SrcPos(url, start, start + length()));

			// Keep track of current position inside the node.
			Nat pos = start;

			// Keep track of the captured range if we need to.
			Nat captureStart = 0;
			Nat captureEnd = start + length();
			StrBuf *captureText = null;
			if (prod->repCapture)
				captureText = new (this) StrBuf();

			// Go through all children.
			for (Nat i = 0; i < count(); i++) {
				InfoNode *child = at(i);

				// Figure out the original token ID (= undo repetitions)
				Nat tokenId = i;
				if (prod->repType != repNone) {
					PVAR(prod->repStart + repLength * repetitions);
					if (i >= prod->repStart + repLength * repetitions)
						tokenId -= repLength * repetitions;
					else if (i >= prod->repStart)
						tokenId = prod->repStart + (i - prod->repStart) % repLength;
				}

				// Update capture
				if (i == prod->repStart)
					captureStart = pos;
				else if (tokenId == prod->repEnd)
					captureEnd = pos;
				if (captureText && i >= prod->repStart && tokenId < prod->repEnd)
					child->toS(captureText);

				// Validate and store the token:
				Token *token = prod->tokens->at(tokenId);

				if (token->asRegex()) {
					InfoLeaf *leaf = as<InfoLeaf>(child);
					if (!leaf)
						throw new (this) UsageError(S("Expected a leaf node but got an internal node."));

					if (token->target) {
						SrcPos p(url, pos, pos + leaf->length());
						setValue(out, token->target, new (this) SStr(leaf->toS(), p));
					}
				} else if (RuleToken *rt = token->asRule()) {
					InfoInternal *internal = as<InfoInternal>(child);
					if (!internal)
						throw new (this) UsageError(S("Expected an internal node but got a leaf node."));

					if (Production *p = internal->production()) {
						if (p->rule() != rt->rule)
							throw new (this) UsageError(S("Found an internal node that does not represent the proper rule."));
					} else {
						throw new (this) UsageError(S("Found an internal node that does not match a production."));
					}

					if (token->target)
						setValue(out, token->target, internal->tree(url, pos));
				} else {
					assert(false, L"Unknown token type used!");
				}

				pos += child->length();
			}

			if (prod->repCapture && prod->repCapture->target) {
				SrcPos p(url, captureStart, captureEnd);
				setValue(out, prod->repCapture->target, new (this) SStr(captureText->toS(), p));
			}

			return out;
		}

		void InfoInternal::toS(StrBuf *to) const {
			for (Nat i = 0; i < children->count; i++)
				children->v[i]->toS(to);
		}

		void InfoInternal::format(StrBuf *to) const {
			*to << L"{";
			{
				Indent z(to);
				if (ProductionType *type = prod->type()) {
					*to << L"\nproduction: " << type->name;
					if (Rule *owner = prod->rule())
						*to << L" of " << owner->name;
				}

				if (indent)
					*to << L"\nindent: " << indent;

				for (Nat i = 0; i < children->count; i++) {
					InfoNode *node = children->v[i];
					*to << L"\n" << node->length() << L" -> ";
					children->v[i]->format(to);
				}
			}
			*to << L"\n}";
			InfoNode::format(to);
		}

		Nat InfoInternal::dbg_size() {
			Nat total = sizeof(InfoInternal);
			total += sizeof(GcArray<InfoNode *>) + (count()*sizeof(InfoNode *)) - sizeof(InfoNode *);

			if (indent)
				total += sizeof(InfoIndent);

			for (Nat i = 0; i < count(); i++) {
				if (children->v[i])
					total += children->v[i]->dbg_size();
			}

			return total;
		}

		/**
		 * Leaf node.
		 */

		InfoLeaf::InfoLeaf(RegexToken *regex, Str *match) : v(match), regex(regex) {}

		InfoLeaf *InfoLeaf::leafAt(Nat pos) {
			return this;
		}

		Nat InfoLeaf::computeLength() {
			// This is in codepoints!
			return v->count();
		}

		void InfoLeaf::set(Str *v) {
			this->v = v;
			invalidate();
		}

		Bool InfoLeaf::matchesRegex() const {
			return matchesRegex(v);
		}

		Bool InfoLeaf::matchesRegex(Str *s) const {
			if (!regex)
				return false;

			return regex->regex.matchAll(s);
		}

		Str *InfoLeaf::toS() const {
			return v;
		}

		void InfoLeaf::toS(StrBuf *to) const {
			*to << v;
		}

		void InfoLeaf::format(StrBuf *to) const {
			*to << L"'" << v->escape('\'') << L"'";
			if (regex)
				*to << L" (matches \"" << regex->regex << L"\")";
			InfoNode::format(to);
		}

		Nat InfoLeaf::dbg_size() {
			// Approximation...
			return sizeof(InfoLeaf) + sizeof(Str) + sizeof(wchar)*length();
		}

	}
}