This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
token_analyzer.js
207 lines (158 loc) · 4.96 KB
/
token_analyzer.js
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
var path = require('path');
var language_parser = require('./language_parser');
var lookupSymbol = require('./symbol_locator').lookupSymbol;
exports.Node = function() {
this.childNodes = [];
};
exports.ClassNode = function() {
exports.Node.call(this);
};
exports.PropNode = function() {
exports.Node.call(this);
this.default = [];
};
exports.EnumNode = function() {
exports.Node.call(this);
};
exports.analyze = function(tokens) {
var root = new exports.Node();
while (tokens.length > 0) {
var cur = tokens.shift();
switch (cur.name) {
case 'EOF':
break;
case 'preprocess':
var text = expect(tokens, 'string');
if (cur.value == 'import') {
var parentTokens = language_parser.tokenizeString(require('fs').readFileSync(path.join(__dirname, '../../steam_language', text.value), { encoding: 'ascii' }));
var newRoot = exports.analyze(parentTokens);
newRoot.childNodes.forEach(function(child) {
root.childNodes.push(child);
});
}
break;
case 'identifier':
var name, op1, op2;
switch (cur.value) {
case 'class':
{
name = expect(tokens, 'identifier');
var ident = null, parent = null;
op1 = optional(tokens, 'operator', '<');
if (op1) {
ident = expect(tokens, 'identifier');
op2 = expect(tokens, 'operator', '>');
}
var expects = optional(tokens, 'identifier', 'expects');
if (expects) {
parent = expect(tokens, 'identifier');
}
var cnode = new exports.ClassNode();
cnode.name = name.value;
if (ident) {
cnode.ident = lookupSymbol(root, ident.value, false);
}
if (parent) {
//cnode.parent = lookupSymbol(root, parent.value, true);
}
root.childNodes.push(cnode);
parseInnerScope(tokens, cnode, root);
}
break;
case 'enum':
{
name = expect(tokens, 'identifier');
var datatype = null;
op1 = optional(tokens, 'operator', '<');
if (op1) {
datatype = expect(tokens, 'identifier');
op2 = expect(tokens, 'operator', '>');
}
var flag = optional(tokens, 'identifier', 'flags');
var enode = new exports.EnumNode();
enode.name = name.value;
if (flag) {
enode.flags = flag.value;
}
if (datatype) {
enode.type = lookupSymbol(root, datatype.value, false);
}
root.childNodes.push(enode);
parseInnerScope(tokens, enode, root);
}
break;
}
break;
}
}
return root;
};
function parseInnerScope(tokens, parent, root) {
var scope1 = expect(tokens, 'operator', '{');
var scope2 = optional(tokens, 'operator', '}');
while (!scope2) {
var pnode = new exports.PropNode();
var t1 = tokens.shift();
var t1op1 = optional(tokens, 'operator', '<');
var flagop = null;
if (t1op1) {
flagop = expect(tokens, 'identifier');
var t1op2 = expect(tokens, 'operator', '>');
pnode.flagsOpt = flagop.value;
}
var t2 = optional(tokens, 'identifier');
var t3 = optional(tokens, 'identifier');
if (t3) {
pnode.name = t3.value;
pnode.type = lookupSymbol(root, t2.value, false);
pnode.flags = t1.value;
} else if (t2) {
pnode.name = t2.value;
pnode.type = lookupSymbol(root, t1.value, false);
} else {
pnode.name = t1.value;
}
var defop = optional(tokens, 'operator', '=');
if (defop) {
while (true) {
var value = tokens.shift();
pnode.default.push(lookupSymbol(root, value.value, false));
if (optional(tokens, 'operator', '|'))
continue;
expect(tokens, 'terminator', ';');
break;
}
} else {
expect(tokens, 'terminator', ';');
}
var obsolete = optional(tokens, 'identifier', 'obsolete');
if (obsolete) {
pnode.obsolete = '';
var obsoleteReason = optional(tokens, 'string');
if (obsoleteReason)
pnode.obsolete = obsoleteReason.value;
}
parent.childNodes.push(pnode);
scope2 = optional(tokens, 'operator', '}');
}
}
function expect(tokens, name, value) {
var peek = tokens[0];
if (!peek) {
return language_parser.Token('EOF', '');
}
if (peek.name != name || value && peek.value != value) {
throw new Error("Expecting " + name);
}
return tokens.shift();
}
function optional(tokens, name, value) {
var peek = tokens[0];
if (!peek) {
return new language_parser.Token('EOF', '');
}
if (peek.name != name || value && peek.value != value) {
return null;
}
return tokens.shift();
}