File: Parse.cpp

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; 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 (827 lines) | stat: -rw-r--r-- 21,345 bytes parent folder | download
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
#include "stdafx.h"
#include "Parse.h"
#include "Tokenizer.h"
#include "Exception.h"
#include "Utils/TextReader.h"
#include "Utils/FileStream.h"

/**
 * Namespace to add stuff to the global World.
 */
class WorldNamespace : public Namespace {
public:
	WorldNamespace(World &to, const CppName &prefix) : to(to), prefix(prefix) {}

	World &to;
	CppName prefix;

	void add(const Variable &v) {
		// Add stuff to 'world'.
		// PLN(L"Global variable " << v << L" ignored.");
		// TODO: Support globals!
	}

	void add(const Function &f) {
		Function g = f;
		g.name = prefix + f.name;
		to.functions.push_back(g);
	}
};

/**
 * Parsing environment.
 */
struct ParseEnv {
	// Current package.
	String pkg;

	// Export types in here?
	bool exportAll;

	// The world in which we want to store everything.
	World &world;

	// Last documentation comment seen here.
	Auto<Doc> doc;
};

// Get the current documentation.
static Auto<Doc> getDoc(Tokenizer &tok, ParseEnv &env) {
	Comment r = tok.comment();
	tok.clearComment();
	if (r.empty())
		return env.doc;

	env.doc = new Doc(r);
	return env.doc;
}

// Read a name.
static CppName parseName(Tokenizer &tok) {
	std::wostringstream out;
	out << tok.next().token;

	while (tok.more() && tok.skipIf(L"::")) {
		out << L"::" << tok.next().token;
	}

	return CppName(out.str());
}

// Read a type.
static Auto<TypeRef> parseTypeRef(Tokenizer &tok) {
	bool isConst = false;

	if (tok.skipIf(L"const"))
		isConst = true;
	tok.skipIf(L"volatile"); // Just ignore 'volatile' so far.
	tok.skipIf(L"mutable"); // Just ignore 'mutable' -- we do not worry about const.

	Auto<TypeRef> type;
	if (tok.skipIf(L"MAYBE")) {
		tok.expect(L"(");
		type = new MaybeClassType(parseTypeRef(tok));
		tok.expect(L")");
	} else if (tok.skipIf(L"UNKNOWN")) {
		tok.expect(L"(");
		CppName kind = parseName(tok);
		tok.expect(L")");
		type = new UnknownType(kind, parseTypeRef(tok));
	} else {
		SrcPos pos = tok.peek().pos;
		CppName n = parseName(tok);

		if (tok.skipIf(L"<")) {
			// Template parameters.
			Auto<TemplateType> t = new TemplateType(pos, n);

			if (!tok.skipIf(L">")) {
				t->params.push_back(parseTypeRef(tok));
				while (tok.skipIf(L","))
					t->params.push_back(parseTypeRef(tok));
				tok.expect(L">");
			}

			type = t;
		} else {
			type = new NamedType(pos, n);
		}
	}

	if (isConst)
		type->constType = true;

	while (tok.more()) {
		if (tok.skipIf(L"const"))
			type->constType = true;

		Token modifiers = tok.peek();
		bool wrong = false;
		Auto<TypeRef> nType = type;
		for (nat i = 0; i < modifiers.token.size(); i++) {
			if (modifiers.token[i] == '*') {
				nType = new PtrType(nType);
			} else if (modifiers.token[i] == '&') {
				nType = new RefType(nType);
			} else {
				wrong = true;
			}
		}

		if (wrong)
			break;

		tok.next();
		type = nType;
	}

	return type;
}

// Parse an unknown block.
static void parseBlock(Tokenizer &tok) {
	while (tok.more()) {
		tok.clearComment();
		Token t = tok.next();
		if (t.token == L"{")
			parseBlock(tok);
		else if (t.token == L"}")
			break;
	}
}

// Skip an empty array modifier (eg. int foo[]) if applicable.
static void skipArray(Tokenizer &tok, bool skippable) {
	if (tok.skipIf(L"[")) {
		if (skippable)
			tok.expect(L"]");
		else
			throw Error(L"Can not use the array syntax on parameters here. Use regular pointers instead.", tok.peek().pos);
	}
}

// Skip an ALIGN_AS definition if present.
static void skipAlignAs(Tokenizer &tok) {
	if (tok.skipIf(L"ALIGN_AS")) {
		tok.expect(L"(");
		nat level = 1;
		while (level > 0) {
			Token t = tok.next();
			if (t.token == L"(")
				level++;
			else if (t.token == L")")
				level--;
		}
	}
}

// Skip an initializer list for a constructor. Assumes we have just consumed the ":".
static void skipInitList(Tokenizer &tok) {
	do {
		tok.skip(); // variable name to initialize.

		nat parens = 0;
		nat braces = 0;
		Token t = tok.next();
		if (t.token == L"(") {
			parens++;
		} else if (t.token == L"{") {
			braces++;
		} else {
			throw Error(L"Expected ( or {, but got " + t.token + L".", t.pos);
		}

		// This is the tricky part. We don't really want to attempt to parse arbitrary C++
		// expressions. Thus, we fall back to counting the number of () and {} and end when we find a
		// corresponding closing tag for the start token. We don't bother to make sure they are matched:
		// the C++ compiler will do that anyway. As long as we manage to skip correct initializer lists
		// that are not too complicated, we're happy.
		while (parens > 0 && parens > 0) {
			Token t = tok.next();
			if (t.token == L"(") {
				parens++;
			} else if (t.token == L"{") {
				braces++;
			} else if (t.token == L"}") {
				if (braces > 0)
					braces--;
				else
					throw Error(L"Unexpected }.", t.pos);
			} else if (t.token == L")") {
				if (parens > 0)
					parens--;
				else
					throw Error(L"Unexpected ).", t.pos);
			}
		}

		// When we get here, we will either get a comma, meaning another initializer in the list, or a
		// {, meaning we're done. We need to make sure not to consume the {, so we don't really check
		// for it and leave that to the caller.
	} while (tok.skipIf(L","));
}

// Parse a variable or function.
static void parseMember(Tokenizer &tok, ParseEnv &env, Namespace &addTo, Access access, bool isStatic) {
	if (!tok.more() || tok.skipIf(L";"))
		return;

	// Primitive parsing of annotations (useful with the [[deprecated]] annotation).
	if (tok.skipIf(L"[")) {
		tok.expect(L"[");
		tok.skip();
		tok.expect(L"]");
		tok.expect(L"]");
	}

	Token name(L"", SrcPos());
	bool castFn = tok.skipIf(L"STORM_CAST_CTOR");
	bool exportFn = castFn || tok.skipIf(L"STORM_CTOR");

	bool isVirtual = tok.skipIf(L"virtual");
	tok.skipIf(L"inline"); // The combination 'virtual inline' is insane, but we'll roll with it...

	Auto<Doc> doc = getDoc(tok, env);

	// First, we should have a type.
	Auto<TypeRef> type;
	if (tok.skipIf(L"~")) {
		type = new NamedType(tok.peek().pos, L"void");
		tok.skip(); // Ignore the name of the destructor.
		name.token = Function::dtor;
	} else {
		type = parseTypeRef(tok);
	}

	// Something else, skip.
	if (!tok.more() || tok.skipIf(L";"))
		return;

	// Is this function supposed to be exported to Storm?
	exportFn |= tok.skipIf(L"STORM_FN");
	bool assignFn = tok.skipIf(L"STORM_ASSIGN");
	exportFn |= assignFn;

	// Other markers...
	tok.skipIf(L"CODECALL");

	bool isCtor = false;

	name.pos = type->pos;
	String stormName;
	if (tok.peek().token != L"(") {
		// Then, we should have the member's name.
		name = tok.next();
		if (name.token == L"operator") {
			// < and > are tokenized one at a time.
			if (tok.skipIf(L"<")) {
				if (tok.skipIf(L"<"))
					name.token += L" <<";
				else
					name.token += L" <";
			} else if (tok.skipIf(L">")) {
				if (tok.skipIf(L">"))
					name.token += L" >>";
				else
					name.token += L" >";
			} else if (tok.skipIf(L"[")) {
				if (tok.skipIf(L"]"))
					name.token += L" []";
				else
					name.token += L" [";
			} else {
				name.token += L" " + tok.next().token;
			}
			// Is this a ?= operator?
			if (tok.skipIf(L"="))
				name.token += L"=";
		} else if (name.token == L"STORM_NAME") {
			tok.expect(L"(");
			name = tok.next();
			tok.expect(L",");
			stormName = tok.next().token;
			tok.expect(L")");
		}
	} else if (name.token != Function::dtor) {
		// Constructor
		isCtor = true;
		type = new NamedType(name.pos, L"void");
		name.token = Function::ctor;
	}

	if (tok.skipIf(L"(")) {
		// Function.
		Function f(CppName(name.token), env.pkg, access, name.pos, doc, type);
		if (!stormName.empty())
			f.stormName = stormName;
		f.set(Function::isVirtual, isVirtual);
		f.set(Function::isAssign, assignFn);
		f.set(Function::castMember, castFn);

		if (!tok.skipIf(L")")) {
			f.params.push_back(parseTypeRef(tok));
			f.paramNames.push_back(tok.next().token);
			skipArray(tok, !exportFn);

			while (tok.more() && tok.skipIf(L",")) {
				if (tok.skipIf(L"...")) {
					if (exportFn)
						throw Error(L"Can not export variadic functions.", name.pos);

					// No more parameters are allowed after a ...
					break;
				}
				f.params.push_back(parseTypeRef(tok));
				f.paramNames.push_back(tok.next().token);
				skipArray(tok, !exportFn);
			}

			tok.expect(L")");
		}

		if (tok.skipIf(L"const"))
			f.set(Function::isConst);

		while (true) {
			Token next = tok.peek();
			if (next.token == L";" || next.token == L"{")
				break;

			if (tok.skipIf(L"ON")) {
				tok.expect(L"(");
				f.thread = parseName(tok);
				tok.expect(L")");
			} else if (tok.skipIf(L"ABSTRACT")) {
				f.set(Function::isAbstract);
			} else if (tok.skipIf(L"override")) {
				// We don't really handle this, but why not allow it?
				// TODO: Perhaps we want to set 'isVirtual'?
			} else if (tok.skipIf(L"final")) {
				// This means 'final'.
				f.clear(Function::isVirtual);
			} else if (isCtor && tok.skipIf(L":")) {
				skipInitList(tok);
			} else {
				throw Error(L"Unsupported function modifier: " + tok.peek().token, tok.peek().pos);
			}
		}

		f.set(Function::isStatic, isStatic);
		f.set(Function::exported, env.exportAll);

		// Save if 'exportFn' is there.
		if (exportFn || f.name == Function::dtor)
			// We need to export abstract functions so that we can generate stubs for them!
			if (env.exportAll || f.has(Function::isAbstract))
				addTo.add(f);

	} else if (tok.skipIf(L"[")) {
		// We're only interested when we're not dealing with a non-static array.
		if (!isStatic)
			throw Error(L"C-style arrays not supported in classes exposed to Storm.", name.pos);
	} else if (tok.skipIf(L"=")) {
		// Initialized variable. Skip the initialization part.
		while (!tok.skipIf(L";"))
			tok.skip();

		// We don't do static variables.
		if (!isStatic) {
			Variable v(CppName(name.token), type, access, name.pos, doc);
			if (!stormName.empty())
				v.stormName = stormName;
			addTo.add(v);
		}
	} else if (tok.skipIf(L";")) {
		// We don't do static variables.
		if (!isStatic) {
			// Variable.
			Variable v(CppName(name.token), type, access, name.pos, doc);
			if (!stormName.empty())
				v.stormName = stormName;
			addTo.add(v);
		}
	} else {
		// Unknown construct; ignore it.
	}
}

// Parse an enum declaration.
static void parseEnum(Tokenizer &tok, ParseEnv &env, const CppName &inside) {
	Token name = tok.next();
	if (name.token == L"STORM_HIDDEN" && tok.skipIf(L"(")) {
		// This enum is supposed to be hidden from Storm.
		tok.skip();
		tok.expect(L")");
		name.token = L"";
	}

	// Forward-declaration?
	if (tok.skipIf(L";"))
		return;

	Auto<Doc> doc = getDoc(tok, env);

	if (name.token == L"{") {
		name.token = L"";
	} else {
		tok.expect(L"{");
	}

	// Add the type.
	CppName fullName = inside + name.token;
	Auto<Enum> type = new Enum(fullName, env.pkg, name.pos, doc);

	ParseEnv childEnv = env;
	childEnv.doc = Auto<Doc>();
	do {
		Token member = tok.next();
		if (member.token == L"}")
			break;

		Token stormName = member;
		if (member.token == L"STORM_NAME") {
			tok.expect(L"(");
			member = tok.next();
			tok.expect(L",");
			stormName = tok.next();
			tok.expect(L")");
		}

		type->members.push_back(member.token);
		type->stormMembers.push_back(stormName.token);
		type->memberDoc.push_back(getDoc(tok, childEnv));

		if (tok.skipIf(L"=")) {
			// Some expression for initialization. Skip it.
			while (tok.peek().token != L"," && tok.peek().token != L"}")
				tok.next();
		}

	} while (tok.skipIf(L","));
	tok.clearComment();
	tok.skipIf(L"}");
	tok.expect(L";");

	type->external = !env.exportAll;
	if (!name.token.empty())
		env.world.add(type);
}

// Parse the parent class of a type.
static Auto<Class> parseParent(Tokenizer &tok, ParseEnv &env, const CppName &fullName, const String &pkg, const SrcPos &pos) {
	Auto<Doc> doc = getDoc(tok, env);
	Auto<Class> result = new Class(fullName, pkg, pos, doc);
	if (!tok.skipIf(L":"))
		return result;

	if (tok.skipIf(L"public")) {
		if (tok.skipIf(L"STORM_HIDDEN")) {
			tok.expect(L"(");
			result->parent = parseName(tok);
			result->set(Class::hiddenParent);
			tok.expect(L")");
		} else if (tok.skipIf(L"ObjectOn")) {
			tok.expect(L"<");
			result->parent = CppName(L"storm::TObject");
			result->clear(Class::hiddenParent);
			result->thread = parseName(tok);
			tok.expect(L">");
		} else {
			result->parent = parseName(tok);
			result->clear(Class::hiddenParent);
		}
	}

	// Skip any multi-inheritance or private inheritance.
	while (tok.peek().token != L"{")
		tok.skip();

	return result;
}

// Parse a type.
static void parseType(Tokenizer &tok, ParseEnv &env, const CppName &inside, bool isPrivate) {
	// Exported exception?
	bool exported = tok.skipIf(L"EXCEPTION_EXPORT");

	Token name = tok.next();

	// Forward-declaration?
	if (tok.skipIf(L";"))
		return;

	CppName fullName = inside + name.token;
	Auto<Class> type = parseParent(tok, env, fullName, env.pkg, name.pos);

	tok.expect(L"{");

	// Are we interested in this class at all?
	if (tok.skipIf(L"STORM_CLASS")) {
		type->clear(Class::value);
	} else if (tok.skipIf(L"STORM_ABSTRACT_CLASS")) {
		type->clear(Class::value);
		type->set(Class::abstract);
	} else if (tok.skipIf(L"STORM_VALUE")) {
		type->set(Class::value);
	} else if (tok.skipIf(L"STORM_EXCEPTION")) {
		type->clear(Class::value);
		type->set(Class::exception);
	} else if (tok.skipIf(L"STORM_EXCEPTION_BASE")) {
		type->clear(Class::value);
		type->set(Class::rootException);
	} else if (exported) {
		// Warn about STORM_EXCEPTION not being present.
		throw Error(L"Exceptions from Storm must be exported with STORM_EXCEPTION as well as EXCEPTION_EXPORT.", tok.peek().pos);
	} else {
		// No. Ignore the rest of the block!
		parseBlock(tok);
		return;
	}

	tok.expect(L";");

	if (type->has(Class::exception) && !exported)
		throw Error(L"Exceptions must be exported with EXCEPTION_EXPORT after 'class'.", name.pos);

	Access access = aPrivate;
	bool isStatic = false;
	while (tok.more()) {
		Token t = tok.peek();
		bool wasStatic = isStatic;
		isStatic = false;

		if (t.token == L"{") {
			tok.skip();
			parseBlock(tok);
		} else if (t.token == L"}") {
			tok.skip();
			break;
		} else if (t.token == L"class" || t.token == L"struct") {
			tok.skip();
			skipAlignAs(tok);
			ParseEnv sub = env;
			if (sub.pkg.empty())
				sub.pkg = name.token;
			else
				sub.pkg += L"." + name.token;
			parseType(tok, sub, fullName, access == aPrivate);
		} else if (t.token == L"extern") {
			tok.skip();
		} else if (t.token == L"enum") {
			tok.skip();
			ParseEnv sub = env;
			if (sub.pkg.empty())
				sub.pkg = name.token;
			else
				sub.pkg += L"." + name.token;
			parseEnum(tok, sub, fullName);
		} else if (t.token == L"template") {
			// Two possibilities:
			while (true) {
				// Either, we have something that ends with a semicolon. Then we're done.
				if (tok.skipIf(L";")) {
					break;
				}
				// Or, we have something declared inline. In that case, we skip the body as well.
				if (tok.skipIf(L"{")) {
					parseBlock(tok);
					break;
				}

				tok.skip();
			}
		} else if (t.token == L"public") {
			tok.skip();
			tok.expect(L":");
			access = aPublic;
		} else if (t.token == L"private") {
			tok.skip();
			tok.expect(L":");
			access = aPrivate;
		} else if (t.token == L"protected") {
			tok.skip();
			tok.expect(L":");
			access = aProtected;
		} else if (t.token == L"static") {
			tok.skip();
			isStatic = true;
		} else if (t.token == L"friend") {
			while (!tok.skipIf(L";"))
				tok.skip();
		} else if (t.token == L"using" || t.token == L"typedef") {
			while (!tok.skipIf(L";"))
				tok.skip();
		} else if (t.token == L"inline") {
			tok.skip();
		} else {
			ClassNamespace ns(env.world, *type);
			parseMember(tok, env, ns, access, wasStatic);
		}
	}

	type->external = !env.exportAll;
	type->isPrivate = isPrivate;
	env.world.add(type);

	tok.clearComment();
	tok.expect(L";");
}

// Parse in the context of a namespace.
static void parseNamespace(Tokenizer &tok, ParseEnv &env, const CppName &name) {
	while (tok.more()) {
		Token t = tok.peek();

		if (t.token == L"class" || t.token == L"struct") {
			tok.skip();
			skipAlignAs(tok);
			parseType(tok, env, name, false);
		} else if (t.token == L"enum") {
			tok.skip();
			parseEnum(tok, env, name);
		} else if (t.token == L"template") {
			// Skip until we find a { or a ;
			while (true) {
				if (tok.skipIf(L"{")) {
					// Skip the body as well.
					parseBlock(tok);
					break;
				} else if (tok.skipIf(L";")) {
					// Fwd decl, we are done.
					break;
				}
				tok.skip();
			}
		} else if (t.token == L"extern") {
			tok.skip();
			tok.skipIf(L"\"C\""); // for 'extern "C" {}'
		} else if (t.token == L"static") {
			tok.skip();
		} else if (t.token == L"using") {
			tok.next();
			if (tok.skipIf(L"namespace")) {
				// Skip it.
				while (!tok.skipIf(L";"))
					tok.skip();
			} else {
				// Add an alias.
				CppName a = parseName(tok);
				env.world.aliases.insert(make_pair(name + a.last(), a));
				tok.expect(L";");
			}
		} else if (t.token == L"typedef") {
			while (!tok.skipIf(L";"))
				tok.skip();
		} else if (t.token == L"STORM_PKG") {
			tok.next();
			tok.expect(L"(");
			// TODO: Take care of this name properly!
			String pkg = tok.next().token;
			while (tok.skipIf(L"."))
				pkg = pkg + L"." + tok.next().token;
			tok.expect(L")");
			tok.expect(L";");
			env.pkg = pkg;
		} else if (t.token == L"namespace") {
			tok.skip();
			Token n = tok.next();
			if (n == L"{") {
				// Anonymous namespace.
				parseNamespace(tok, env, name);
			} else {
				tok.expect(L"{");
				parseNamespace(tok, env, name + n.token);
			}
		} else if (t.token == L"BITMASK_OPERATORS") {
			tok.skip();
			tok.expect(L"(");
			SrcPos pos = tok.peek().pos;
			CppName n = parseName(tok);
			Type *t = env.world.types.find(n, name, pos);
			if (Enum *e = as<Enum>(t)) {
				e->bitmask = true;
			} else {
				throw Error(L"The type " + toS(n) + L" is not an enum.", pos);
			}
			tok.expect(L")");
			tok.expect(L";");
		} else if (t.token == L"STORM_TEMPLATE") {
			tok.skip();
			tok.expect(L"(");
			Token tName = tok.next();
			tok.expect(L",");
			CppName gen = parseName(tok);
			Auto<Doc> doc = getDoc(tok, env);
			Auto<Template> t = new Template(name + tName.token, env.pkg, gen, tName.pos, doc);
			t->external = !env.exportAll;
			env.world.templates.insert(t);
			tok.expect(L")");
			tok.expect(L";");
		} else if (t.token == L"STORM_PRIMITIVE") {
			tok.skip();
			tok.expect(L"(");
			Token tName = tok.next();
			tok.expect(L",");
			Auto<Doc> doc = getDoc(tok, env);
			CppName gen = parseName(tok);
			Auto<Primitive> p = new Primitive(name + tName.token, env.pkg, gen, tName.pos, doc);
			p->external = !env.exportAll;
			env.world.types.insert(p);
			tok.expect(L")");
			tok.expect(L";");
		} else if (t.token == L"STORM_UNKNOWN_PRIMITIVE") {
			tok.skip();
			tok.expect(L"(");
			Token tName = tok.next();
			tok.expect(L",");
			CppName gen = parseName(tok);
			Auto<UnknownPrimitive> p = new UnknownPrimitive(name + tName.token, env.pkg, gen, tName.pos);
			p->external = !env.exportAll;
			env.world.types.insert(p);
			tok.expect(L")");
			tok.expect(L";");
		} else if (t.token == L"STORM_THREAD") {
			tok.skip();
			tok.expect(L"(");
			Token tName = tok.next();
			Auto<Doc> doc = getDoc(tok, env);
			Auto<Thread> t = new Thread(name + tName.token, env.pkg, tName.pos, doc, !env.exportAll);
			env.world.threads.insert(t);
			tok.expect(L")");
			tok.expect(L";");
		} else if (t.token.startsWith(L"PROXY")) {
			tok.skip();
			tok.expect(L"(");
			while (!tok.skipIf(L")"))
				tok.skip();
			tok.expect(L";");
		} else if (t.token == L"{") {
			tok.skip();
			parseBlock(tok);
		} else if (t.token == L"}") {
			tok.clearComment();
			tok.skip();
			break;
		} else if (t.token == L";") {
			// Stray semicolon, skip it.
			tok.skip();
		} else {
			// Everything is considered 'public' and non-static outside of a class.
			WorldNamespace tmp(env.world, name);
			parseMember(tok, env, tmp, aPublic, false);
		}
	}
}

// Parse an entire header file.
static void parseFile(nat id, World &world) {
	Tokenizer tok(id);
	ParseEnv env = { L"", id >= SrcPos::firstExport, world };
	parseNamespace(tok, env, CppName());
}

// Parse a license file.
static void parseLicense(const Path &path, World &world) {
	TextReader *src = TextReader::create(new FileStream(path, Stream::mRead));

	String cond = src->getLine();
	String pkg;
	if (cond.empty() || cond[0] != '#') {
		pkg = cond;
		cond = L"";
	} else {
		cond = cond.substr(1);
		pkg = src->getLine();
	}
	String title = src->getLine();
	String author = src->getLine();
	String body = src->getAll();

	delete src;

	world.licenses.push_back(License(path.titleNoExt(), pkg, cond, title, author, body));
}

// Parse a version file.
static void parseVersion(const Path &path, World &world) {
	TextReader *src = TextReader::create(new FileStream(path, Stream::mRead));

	String pkg = src->getLine();
	String ver = src->getLine();

	delete src;

	world.versions.push_back(Version(path.titleNoExt(), pkg, ver));
}

void parseWorld(World &world, const vector<Path> &licenses, const vector<Path> &versions) {
	for (nat i = 0; i < SrcPos::files.size(); i++) {
		parseFile(i, world);
	}
	for (nat i = 0; i < licenses.size(); i++) {
		parseLicense(licenses[i], world);
	}
	for (nat i = 0; i < versions.size(); i++) {
		parseVersion(versions[i], world);
	}
}