-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
222 lines (186 loc) · 5.08 KB
/
test.js
File metadata and controls
222 lines (186 loc) · 5.08 KB
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
import vm from "vm"
import path from "path"
import fs from "fs"
import babel from "babel-core"
import colors from "colors"
import Lexer from "lexie"
import generate, {parserFactory, read, generateParsingTable} from "./src/index.js"
const {min, max, round, random} = Math;
const pass = colors.green('\u2714'), fail = colors.red('\u2718');
const dirname = path.dirname(new URL(import.meta.url).pathname);
const depad = (str) => {
const lined = str.split('\n');
const reducer = (acc, val) =>
!val.trim() ?
acc :
min(val.match(/^[\t ]*/)[0].length, acc) ;
const space = lined.reduce(reducer, Infinity);
return lined
.map(line => line.trim() ? line.slice(space) : '')
.join('\n');
}
const getCraneGrammar = (source, opts) => {
const grammar = depad(source);
const logs = [];
const options = {
...opts,
debug: true,
logger: {
log(...args) {
logs.push(args);
}
}
};
return {
grammar: read(grammar, options),
log() {
for (const log of logs) {
console.log(...log);
}
}
}
}
const tokenize = function* (text) {
const prototype = {
get loc() {
const begin = {
line: 1,
column: 0
};
return {
start: begin,
end: begin
};
},
get string() {
return this.source.slice(...this.range);
}
};
for (const token of lexer.tokenize(text)) {
if (token.type === 'ws')
continue;
Object.setPrototypeOf(token, prototype);
yield token;
}
}
const language = (source) => {
const grammar = depad(source);
const name = 'fuckery';
const logs = [];
const options = {
debug: true,
logger: {
log(...args) {
logs.push(args);
}
}
};
const parserSource = generate(grammar, options);
const {code} = babel.transform(parserSource, {
plugins: ["transform-es2015-modules-commonjs"]
});
const context = {
console,
exports: {}
};
context.module = context;
return {
grammar,
generated: (vm.runInNewContext(code, context), context['exports']),
parse(text, debug = false) {
// TODO: debug this
const parsing = new this.generated.Parser();
const showStack = () => {
if (debug) {
const [start, ...states] = parsing.states.slice(0);
const stack = parsing.stack.slice(0);
let both = [];
for (let i = 0; i < stack.length; i++) {
const symbol = this.generated.untranslate(stack[i]);
both.push(`${colors.blue(symbol)} ${states[i]}`);
}
console.log(`${start} ${both.join(' ')}`);
}
}
parsing.onreduceend = () => {
if (debug)
console.log('now')
showStack();
}
showStack();
for (let token of tokenize(text)) {
parsing.push(token);
showStack();
}
return parsing.finish();
},
log() {
for (const log of logs) {
console.log(...log);
}
}
}
}
const lexer = new Lexer([
{
regex: /[\+\-\*\/\^\(\)\%\,\|\&\[\]\{\}]/
},
{
regex: /[0-9]+/,
type: 'int'
},
{
regex: /[\t ]+/,
type: 'ws'
},
{
regex: /[a-z]+/,
type: 'id'
}
]);
let i = 0;
let file = null;
const context = {
console,
language,
tokenize,
parserFactory,
getCraneGrammar,
generateParsingTable,
languages: {},
test(title, fn) {
const padded = (++i + '').padEnd(4);
try {
fn({
assert(condition) {
if (!condition) {
throw new Error(`Assertion error!`);
}
},
eq(a, b) {
if (a !== b) {
throw new Error(`Values not equal: ${a}, ${b}!`);
}
},
throws(fn) {
try {
fn();
} catch (e) {
return;
}
throw new Error(`Function failed to throw error!`);
}
});
console.log(`${padded} ${pass} \t ${title.padEnd(40)} ${file}`);
} catch (e) {
console.log(e);
console.log(`${padded} ${fail} \t ${title.padEnd(40)} ${file}`);
}
}
}
for (let node of fs.readdirSync("./tests")) {
if (node.endsWith('.js')) {
file = node.slice(0, -3);
vm.runInNewContext(fs.readFileSync(`${dirname}/tests/${node}`, 'utf8'), context);
}
}