-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.ts
455 lines (388 loc) Β· 10 KB
/
editor.ts
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
import { Injectable, Inject } from '@nestjs/common';
import {
ParseWarning,
mergeParseErrors,
removeFromParseError,
} from './exceptions';
import type { Host } from './host';
import { Token } from './tokens';
import { Line, Program } from './ast';
import { Source } from './ast/source';
import {
ExprVisitor,
Unary,
Binary,
Logical,
Group,
Variable,
IntLiteral,
RealLiteral,
BoolLiteral,
StringLiteral,
PromptLiteral,
NilLiteral,
} from './ast/expr';
import {
Instr,
InstrVisitor,
Rem,
Let,
Assign,
Expression,
Print,
Exit,
End,
New,
Load,
List,
Renum,
Run,
Save,
ShortIf,
If,
Else,
ElseIf,
EndIf,
} from './ast/instr';
type LineNo = number;
interface Index {
i: number;
lineNo: LineNo | null;
match: boolean;
}
// An index corresponding to inserting the line at the very front
const HEAD: number = -1;
type Numbering = {
to: number;
toStr: string;
shift: number;
source: Source;
};
type Renumbering = Record<LineNo, Numbering>;
export enum Justify {
Left,
Right,
}
class InstrShifter implements InstrVisitor<void>, ExprVisitor<void> {
constructor(public shift: number) {}
private shiftInstr(instr: Instr): void {
instr.offsetStart += this.shift;
instr.offsetEnd += this.shift;
}
private shiftToken(tok: Token): void {
tok.offsetStart += this.shift;
tok.offsetEnd += this.shift;
}
visitRemInstr(rem: Rem): void {
this.shiftInstr(rem);
}
visitLetInstr(let_: Let): void {
this.shiftInstr(let_);
let_.variable.accept(this);
}
visitAssignInstr(assign: Assign): void {
this.shiftInstr(assign);
assign.variable.accept(this);
}
visitExpressionInstr(expr: Expression): void {
this.shiftInstr(expr);
expr.expression.accept(this);
}
visitPrintInstr(print: Print): void {
this.shiftInstr(print);
print.expression.accept(this);
}
visitExitInstr(exit: Exit): void {
this.shiftInstr(exit);
if (exit.expression) {
exit.expression.accept(this);
}
}
visitEndInstr(end: End): void {
this.shiftInstr(end);
}
visitNewInstr(new_: New): void {
this.shiftInstr(new_);
if (new_.filename) {
new_.filename.accept(this);
}
}
visitLoadInstr(load: Load): void {
this.shiftInstr(load);
if (load.filename) {
load.filename.accept(this);
}
}
visitListInstr(list: List): void {
this.shiftInstr(list);
}
visitRenumInstr(renum: Renum): void {
this.shiftInstr(renum);
}
visitRunInstr(run: Run): void {
this.shiftInstr(run);
}
visitSaveInstr(save: Save): void {
this.shiftInstr(save);
if (save.filename) {
save.filename.accept(this);
}
}
visitShortIfInstr(shortIf: ShortIf): void {
this.shiftInstr(shortIf);
shortIf.condition.accept(this);
for (const instr of shortIf.then) {
instr.accept(this);
}
for (const instr of shortIf.else_) {
instr.accept(this);
}
}
visitIfInstr(if_: If): void {
this.shiftInstr(if_);
if_.condition.accept(this);
}
visitElseInstr(else_: Else): void {
this.shiftInstr(else_);
}
visitElseIfInstr(elseIf: ElseIf): void {
this.shiftInstr(elseIf);
elseIf.condition.accept(this);
}
visitEndIfInstr(endIf: EndIf): void {
this.shiftInstr(endIf);
}
visitUnaryExpr(unary: Unary): void {
unary.expr.accept(this);
}
visitBinaryExpr(binary: Binary): void {
binary.left.accept(this);
binary.right.accept(this);
}
visitLogicalExpr(logical: Logical): void {
logical.left.accept(this);
logical.right.accept(this);
}
visitGroupExpr(group: Group): void {
group.expr.accept(this);
}
visitVariableExpr(variable: Variable): void {
this.shiftToken(variable.ident);
}
visitIntLiteralExpr(_int: IntLiteral): void {}
visitRealLiteralExpr(_real: RealLiteral): void {}
visitBoolLiteralExpr(_bool: BoolLiteral): void {}
visitStringLiteralExpr(_str: StringLiteral): void {}
visitPromptLiteralExpr(_prompt: PromptLiteral): void {}
visitNilLiteralExpr(_nil: NilLiteral): void {}
}
@Injectable()
export class Editor {
public program: Program;
public warning: ParseWarning | null;
public justify: Justify = Justify.Left;
constructor(@Inject('Host') public host: Host) {
this.program = new Program('untitled.bas', []);
this.warning = null;
}
get filename(): string {
return this.host.relativePath('.', this.program.filename);
}
set filename(filename: string) {
this.program.filename = this.host.resolvePath(filename);
}
/**
* Initialize editor state.
*
* @param program A program.
* @param warning An associated warning, if any.
*/
init(program: Program, warning: ParseWarning | null): void {
this.program = program;
this.warning = warning;
}
/**
* Reset the editor.
*/
reset(): void {
this.program = new Program('untitled.bas', []);
this.warning = null;
}
/**
* List the current program.
*
* @returns The source code for the program.
*/
list(): string {
return this.program.lines
.map((l) => {
if (
l.instructions.length === 1 &&
l.instructions[0] instanceof Rem &&
l.instructions[0].remark === ''
) {
return `${l.lineNo}`;
}
return l.source;
})
.join('\n');
}
/**
* Renumber the current program.
*/
renum(): void {
const renumbering: Renumbering = {};
// Used to calculate padding
let maxToLength: number = 0;
for (let i = 0; i < this.program.lines.length; i++) {
const line = this.program.lines[i];
// Calculate new line number
const from = line.lineNo;
const to = (i + 1) * 10;
const toStr = String(to);
renumbering[from] = {
to,
toStr,
shift: 0,
source: line.source,
};
// Track max length
maxToLength = Math.max(maxToLength, toStr.length);
}
for (const line of this.program.lines) {
const from = line.lineNo;
const { to, toStr } = renumbering[from];
// Justification
const leftPadding =
this.justify === Justify.Left
? ''
: ' '.repeat(maxToLength - toStr.length);
const rightPadding =
this.justify === Justify.Left
? ' '.repeat(maxToLength - toStr.length + 1)
: ' ';
const fromWidth = line.source.prefix.length;
const toWidth = leftPadding.length + toStr.length + rightPadding.length;
const shift = toWidth - fromWidth;
// Renumber the line
line.lineNo = to;
line.source.leadingWs = leftPadding;
line.source.lineNo = toStr;
line.source.separatingWs = rightPadding;
// Shift the instructions
for (const instr of line.instructions) {
instr.accept(new InstrShifter(shift));
}
// Store for shifting the warnings
renumbering[from].shift = shift;
}
// Renumber/format warnings
if (this.warning) {
for (let i = 0; i < this.warning.warnings.length; i++) {
const warning = this.warning.warnings[i];
const { to, shift, source } = renumbering[warning.lineNo];
if (to) {
warning.lineNo = to;
// TODO: If the warning's source is known to be the same object
// as line.source, this assignment is unnecessary. But I'd need to
// look and confirm.
warning.source = source;
warning.offsetStart += shift;
warning.offsetEnd += shift;
}
}
}
}
// A binary search to find the index containing a lineNo. If there's no
// exact match, return the index just prior.
private findLineIndex(lineNo: number): Index {
let lower = 0;
let upper = this.program.lines.length - 1;
if (!this.program.lines.length) {
return { i: HEAD, lineNo: null, match: false };
}
let lowerLine = this.program.lines[lower];
let upperLine = this.program.lines[upper];
if (lowerLine.lineNo > lineNo) {
return {
i: HEAD,
lineNo: null,
match: false,
};
}
if (lowerLine.lineNo === lineNo) {
return {
i: lower,
lineNo: lowerLine.lineNo,
match: true,
};
}
if (upperLine.lineNo <= lineNo) {
return {
i: upper,
lineNo: upperLine.lineNo,
match: upperLine.lineNo === lineNo,
};
}
// At this point, we know that the line, if defined, *must* exist
// *between* lower and upper.
while (true) {
// We've exhausted our options and don't have a match. Since lower and
// upper are guaranteed to be below and above our line respectively,
// we can just return lower as the prior line.
if (upper - lower <= 1) {
return {
i: lower,
lineNo: lowerLine.lineNo,
match: false,
};
}
const middle = Math.floor((lower + upper) / 2);
const middleLine = this.program.lines[middle];
// Exact match!
if (middleLine.lineNo === lineNo) {
return {
i: middle,
lineNo: middleLine.lineNo,
match: true,
};
}
// middle is a new bound, either lower or upper
if (middleLine.lineNo > lineNo) {
upper = middle;
upperLine = middleLine;
} else if (middleLine.lineNo < lineNo) {
lower = middle;
lowerLine = middleLine;
}
}
}
/**
* Set a line and its corresponding warnings.
*
* If a line has no instructions, delete the corresponding line. Otherwise,
* insert or update the line based on lineNo.
*/
setLine(line: Line, warning: ParseWarning | null): void {
const { i, match } = this.findLineIndex(line.lineNo);
if (!line.instructions.length) {
if (match) {
this.program.lines.splice(i, 1);
this.warning = removeFromParseError(
this.warning,
'lineNo',
line.lineNo,
);
}
return;
}
if (match) {
this.program.lines[i] = line;
if (warning) {
this.warning = mergeParseErrors([this.warning, warning]);
}
return;
}
this.program.lines.splice(i + 1, 0, line);
}
}