File: Reader.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 (145 lines) | stat: -rw-r--r-- 3,756 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
#include "stdafx.h"
#include "Reader.h"
#include "Class.h"
#include "Core/Io/Text.h"
#include "Compiler/Engine.h"
#include "Compiler/Exception.h"
#include "Compiler/Syntax/Parser.h"

namespace storm {
	namespace bs {

		static storm::FileReader *CODECALL createFile(FileInfo *info) {
			return new (info) UseReader(info);
		}

		PkgReader *reader(Array<Url *> *files, Package *pkg) {
			Engine &e = pkg->engine();
			return new (e) FilePkgReader(files, pkg, fnPtr(e, &createFile, Compiler::thread(e)));
		}

		// Find the syntax package for BS given a type in 'lang.bs'.
		static Package *syntaxPkg(RootObject *o) {
			Type *t = runtime::typeOf(o);
			Package *p = as<Package>(t->parent());
			assert(p);
			return p;
		}

		/**
		 * UseReader.
		 */

		UseReader::UseReader(FileInfo *info) : FileReader(info) {}

		syntax::InfoParser *UseReader::createParser() {
			syntax::Rule *r = as<syntax::Rule>(syntaxPkg(this)->find(S("SIncludes"), Scope()));
			if (!r)
				throw new (this) LangDefError(S("Can not find the 'SIncludes' rule."));
			return new (this) syntax::InfoParser(r);
		}

		FileReader *UseReader::createNext(ReaderQuery q) {
			// We do not provide syntax.
			if (q & qSyntax)
				return null;

			syntax::Parser *p = syntax::Parser::create(syntaxPkg(this), S("SIncludes"));

			if (!p->parse(info->contents, info->url, info->start))
				p->throwError();

			Array<SrcName *> *includes = p->transform<Array<SrcName *>>();
			return new (this) CodeReader(info->next(p->matchEnd()), includes, q);
		}


		/**
		 * CodeReader.
		 */

		CodeReader::CodeReader(FileInfo *info, Array<SrcName *> *includes, ReaderQuery query) : FileReader(info) {
			Array<Package *> *inc = new (this) Array<Package *>();
			for (Nat i = 0; i < includes->count(); i++) {
				SrcName *v = includes->at(i);
				Package *p = as<Package>(engine().scope().find(v));
				if (p)
					inc->push(p);
				else if ((query & qParser) == 0)
					// Only complain if we're not parsing interactively.
					throw new (this) SyntaxError(v->pos, TO_S(engine(), S("Unknown package ") << v));
			}

			BSLookup *lookup = new (this) BSLookup(inc);
			scope = Scope(info->pkg, lookup);
		}

		syntax::InfoParser *CodeReader::createParser() {
			syntax::Rule *r = as<syntax::Rule>(syntaxPkg(this)->find(S("SFile"), Scope()));
			if (!r)
				throw new (this) LangDefError(S("Can not find the 'SFile' rule."));
			syntax::InfoParser *p = new (this) syntax::InfoParser(r);
			addSyntax(scope, p);
			return p;
		}

		void CodeReader::readTypes() {
			readContent();
			Package *pkg = info->pkg;

			for (Nat i = 0; i < content->types->count(); i++) {
				pkg->add(content->types->at(i));
			}

			for (Nat i = 0; i < content->threads->count(); i++) {
				pkg->add(content->threads->at(i));
			}
		}

		void CodeReader::resolveTypes() {
			readContent();

			for (Nat i = 0; i < content->types->count(); i++) {
				if (Class *c = as<Class>(content->types->at(i))) {
					c->lookupTypes();
				}
			}
		}

		void CodeReader::readFunctions() {
			readContent();
			Package *pkg = info->pkg;

			for (Nat i = 0; i < content->decls->count(); i++) {
				pkg->add(content->decls->at(i)->create());
			}

			for (Nat i = 0; i < content->templates->count(); i++) {
				pkg->add(content->templates->at(i));
			}
		}

		void CodeReader::resolveFunctions() {
			readContent();

			for (Nat i = 0; i < content->decls->count(); i++) {
				content->decls->at(i)->resolve();
			}
		}

		void CodeReader::readContent() {
			if (content)
				return;

			syntax::Parser *p = syntax::Parser::create(syntaxPkg(this), S("SFile"));
			addSyntax(scope, p);

			p->parse(info->contents, info->url, info->start);
			if (p->hasError())
				p->throwError();

			content = p->transform<Content, Scope>(scope);
		}

	}
}