-
Notifications
You must be signed in to change notification settings - Fork 10
/
kni.js
executable file
·325 lines (286 loc) · 8.62 KB
/
kni.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
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
#!/usr/bin/env node
'use strict';
var tee = require('tee');
var Console = require('./console');
var Readline = require('./readline');
var Engine = require('./engine');
var Scanner = require('./scanner');
var OutlineLexer = require('./outline-lexer');
var InlineLexer = require('./inline-lexer');
var Parser = require('./parser');
var Story = require('./story');
var Path = require('./path');
var grammar = require('./grammar');
var link = require('./link');
var verify = require('./verify');
var exec = require('shon/exec');
var usage = require('./kni.json');
var xorshift = require('xorshift');
var table = require('table').default;
var getBorderCharacters = require('table').getBorderCharacters;
var describe = require('./describe');
var makeHtml = require('./html');
function run(args, out, done) {
var config = exec(usage, args);
if (!config) {
done(null);
return;
}
serial(config.scripts, readAndKeep, onKniscripts);
function onKniscripts(err, kniscripts) {
if (err) {
done(err);
return;
}
var interactive = true;
if (config.transcript === out) {
config.transcript = null;
}
if (config.transcript) {
out = tee(config.transcript, out);
}
var states;
if (config.fromJson) {
states = JSON.parse(kniscripts[0].content); // TODO test needed
} else {
var story = new Story();
for (var i = 0; i < kniscripts.length; i++) {
var kniscript = kniscripts[i].content;
if (config.debugInput) {
console.log(kniscript);
}
var path = Path.start();
var base = [];
if (kniscripts.length > 1) {
path = kniscripts[i].stream.path;
path = [path.split('/').pop().split('.').shift()];
base = path;
}
var p = new Parser(grammar.start(story, path, base));
var il = new InlineLexer(p);
var ol = new OutlineLexer(il);
var s = new Scanner(ol, kniscripts[i].stream.path);
// Kick off each file with a fresh paragraph.
p.next('token', '', '//', s);
if (config.debugParser) {
p.debug = true;
interactive = false;
}
if (config.debugInlineLexer) {
il.debug = true;
interactive = false;
}
if (config.debugOutlineLexer) {
ol.debug = true;
interactive = false;
}
if (config.debugScanner) {
s.debug = true;
interactive = false;
}
s.next(kniscript);
s.return();
}
link(story);
states = story.states;
if (story.errors.length) {
if (config.transcript != null) {
dump(story.errors, config.transcript);
}
var storyError = new Error('internal story error');
storyError.story = story;
done(storyError);
return;
}
}
if (config.describe) {
describeStory(states, out, done);
return;
}
if (config.toJson) {
console.log(JSON.stringify(states, null, 4), done);
interactive = false;
} else if (config.toHtml) {
makeHtml(states, config.toHtml, {
title: config.htmlTitle,
color: config.htmlColor,
backgroundColor: config.htmlBackgroundColor,
});
interactive = false;
}
var randomer = xorshift;
if (config.transcript || config.seed) {
// I rolled 4d64k this morning.
randomer = new xorshift.constructor([
37615 ^ config.seed,
54552 ^ config.seed,
59156 ^ config.seed,
24695 ^ config.seed
]);
}
if (config.expected) {
read(config.expected, function onTypescript(err, typescript) {
if (err) {
done(err);
return;
}
var result = verify(kniscript, typescript);
if (!result.pass) {
console.log(result.actual);
done(new Error('verification failed'));
return;
}
});
done(null);
return;
}
if (interactive) {
var readline = new Readline(config.transcript);
var render = new Console(out);
var engine = new Engine({
story: states,
start: config.start,
render: render,
dialog: readline,
randomer: randomer
});
if (config.debugRuntime) {
engine.debug = true;
}
if (config.waypoint) {
read(config.waypoint, function onWaypoint(err, waypoint) {
if (err) {
done(err);
return;
}
waypoint = JSON.parse(waypoint);
engine.continue(waypoint);
});
} else {
engine.continue();
}
}
}
done(null);
}
function describeStory(states, out, done) {
var keys = Object.keys(states);
var cells = [['L:C', 'AT', 'DO', 'S', 'USING', 'S', 'GO']];
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var node = states[key];
var next;
if (i === keys.length - 1) {
next = null;
} else {
next = keys[i + 1];
}
cells.push([
stripe(i, node.position),
stripe(i, key),
stripe(i, node.mode || node.type),
stripe(i, node.lift ? '-' : ' '),
stripe(i, describe(node)),
stripe(i, node.drop ? '-' : ' '),
stripe(i, describeNext(node.next, next))
]);
}
out.write(table(cells, {
border: getBorderCharacters('void'),
columnDefault: {
paddingLeft: 0,
paddingRight: 2
},
columns: {
4: {
width: 40,
wrapWord: true
}
},
drawHorizontalLine: no
}), done);
}
function stripe(index, text) {
if (index % 2 === 1) {
return text;
} else {
return '\x1b[90m' + text + '\x1b[0m';
}
}
function describeNext(jump, next) {
if (jump === undefined) {
return '';
} else if (jump === next) {
return '';
} else if (jump == 'RET') {
return '<-';
} else if (jump == 'ESC') {
return '<<';
} else {
return '-> ' + jump;
}
}
function no() {
return false;
}
function readAndKeep(stream, callback) {
read(stream, function onRead(err, content) {
if (err != null) {
return callback(err);
}
callback(null, {stream: stream, content: content});
});
}
function read(stream, callback) {
stream.setEncoding('utf8');
var string = '';
stream.on('data', onData);
stream.on('end', onEnd);
stream.on('error', onEnd);
function onData(chunk) {
string += chunk;
}
function onEnd(err) {
if (err) {
callback(err);
return;
}
callback(null, string);
}
}
function serial(array, eachback, callback) {
var values = [];
next(0);
function next(i) {
if (i >= array.length) {
return callback(null, values);
}
eachback(array[i], onEach);
function onEach(err, value) {
if (err != null) {
return callback(err, null);
}
values.push(value);
next(i+1);
}
}
}
function dump(errors, writer) {
for (var i = 0; i < errors.length; i++) {
writer.write(errors[i] + '\n');
}
}
if (require.main === module) {
run(null, process.stdout, function runDone(err) {
if (err) {
console.error(typeof err === 'object' && err.message ? err.message : err);
if (typeof err.story === 'object' && err.story) {
var story = err.story;
dump(story.errors, process.stderr);
}
process.exit(-1);
}
});
} else {
module.exports = run;
}