-
-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathtoken-cursor.ts
539 lines (500 loc) · 16.5 KB
/
token-cursor.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import { LineInputModel } from "./model";
import { Token } from "./clojure-lexer";
/**
* A mutable cursor into the token stream.
*/
export class TokenCursor {
constructor(public doc: LineInputModel, public line: number, public token: number) {
}
/** Create a copy of this cursor. */
clone() {
return new TokenCursor(this.doc, this.line, this.token);
}
/**
* Sets this TokenCursor state to the same as another.
* @param cursor the cursor to copy state from.
*/
set(cursor: TokenCursor) {
this.doc = cursor.doc;
this.line = cursor.line;
this.token = cursor.token;
}
/** Return the position */
get rowCol() {
return [this.line, this.getToken().offset];
}
/** Return the offset at the start of the token */
get offsetStart() {
return this.doc.getOffsetForLine(this.line) + this.getToken().offset;
}
/** Return the offset at the end of the token */
get offsetEnd() {
return Math.min(this.doc.maxOffset, this.doc.getOffsetForLine(this.line) + this.getToken().offset + this.getToken().raw.length);
}
/** True if we are at the start of the document */
atStart() {
return this.token == 0 && this.line == 0;
}
/** True if we are at the end of the document */
atEnd() {
return this.line == this.doc.lines.length - 1 && this.token == this.doc.lines[this.line].tokens.length - 1;
}
/** Move this cursor backwards one token */
previous() {
if (this.token > 0) {
this.token--;
} else {
if (this.line == 0) return;
this.line--;
this.token = this.doc.lines[this.line].tokens.length - 1;
}
return this;
}
/** Move this cursor forwards one token */
next() {
if (this.token < this.doc.lines[this.line].tokens.length - 1) {
this.token++;
} else {
if (this.line == this.doc.lines.length - 1) return;
this.line++;
this.token = 0;
}
return this;
}
/**
* Return the token immediately preceding this cursor. At the start of the file, a token of type "eol" is returned.
*/
getPrevToken(): Token {
if (this.line == 0 && this.token == 0)
return { type: "eol", raw: "\n", offset: 0, state: null };
let cursor = this.clone();
cursor.previous();
return cursor.getToken();
}
/**
* Returns the token at this cursor position.
*/
getToken() {
return this.doc.lines[this.line].tokens[this.token];
}
equals(cursor: TokenCursor) {
return this.line == cursor.line && this.token == cursor.token && this.doc == cursor.doc;
}
}
export class LispTokenCursor extends TokenCursor {
constructor(public doc: LineInputModel, public line: number, public token: number) {
super(doc, line, token);
}
/** Create a copy of this cursor. */
clone() {
return new LispTokenCursor(this.doc, this.line, this.token);
}
/**
* Indicates if the current token is inside a string (e.g. a documentation string)
*/
isInString() {
const strTypes = ['str', 'str-start', 'str-inside', 'str-end'],
token = this.getToken();
if (token.type == 'eol') {
let next = this.clone().next()
let previous = this.clone().previous();
if (next && strTypes.includes(next.getToken().type) &&
previous && strTypes.includes(previous.getToken().type)) {
return (true);
}
} else if (strTypes.includes(token.type)) {
return (true);
}
return false;
}
/**
* Moves this token past the inside of a multiline string
*/
forwardString() {
while (!this.atEnd()) {
switch (this.getToken().type) {
case "eol":
case "str-inside":
case "str-start":
this.next();
continue;
default:
return;
}
}
}
/**
* Moves this token past any whitespace or comment.
*/
forwardWhitespace(includeComments = true) {
while (!this.atEnd()) {
switch (this.getToken().type) {
case "comment":
if (!includeComments)
return;
case "eol":
case "ws":
this.next();
continue;
default:
return;
}
}
}
/**
* Moves this token back past any whitespace or comment.
*/
backwardWhitespace(includeComments = true) {
while (!this.atStart()) {
switch (this.getPrevToken().type) {
case "comment":
if (!includeComments)
return;
case "eol":
this.previous();
if (this.getPrevToken().type == "comment") {
this.next();
return;
}
continue;
case "ws":
this.previous();
continue;
default:
return;
}
}
}
// Lisp navigation commands begin here.
/**
* Moves this token forward one s-expression at this level.
* If the next non whitespace token is an open paren, skips past it's matching
* close paren.
*
* If the next token is a form of closing paren, does not move.
*
* @returns true if the cursor was moved, false otherwise.
*/
forwardSexp(skipComments = false): boolean {
let delta = 0;
this.forwardWhitespace(!skipComments);
if (this.getToken().type == "close") {
return false;
}
while (!this.atEnd()) {
this.forwardWhitespace(!skipComments);
let tk = this.getToken();
switch (tk.type) {
case 'comment':
this.next(); // skip past comment
this.next(); // skip past EOL.
return true;
case 'id':
case 'lit':
case 'kw':
case 'punc':
case 'junk':
case 'str':
case 'str-end':
this.next();
if (delta <= 0)
return true;
break;
case 'str-inside':
case 'str-start':
do {
this.next();
tk = this.getToken();
} while (!this.atEnd() && (tk.type == "str-inside" || tk.type == "eol"))
continue;
case 'close':
delta--;
this.next();
if (delta <= 0)
return true;
break;
case 'open':
delta++;
this.next();
break;
default:
this.next();
break;
}
}
}
/**
* Moves this token backward one s-expression at this level.
* If the previous non whitespace token is an close paren, skips past it's matching
* open paren.
*
* If the previous token is a form of open paren, does not move.
*
* @returns true if the cursor was moved, false otherwise.
*/
backwardSexp(skipComments = true) {
let delta = 0;
this.backwardWhitespace(!skipComments);
switch (this.getPrevToken().type) {
case "open":
return false;
}
while (!this.atStart()) {
this.backwardWhitespace(!skipComments);
let tk = this.getPrevToken();
switch (tk.type) {
case 'id':
case 'lit':
case 'punc':
case 'junk':
case 'kw':
case 'comment':
case 'str':
case 'str-start':
this.previous();
if (delta <= 0)
return true;
break;
case 'str-inside':
case 'str-end':
do {
this.previous();
tk = this.getPrevToken();
} while (!this.atStart() && tk.type == "str-inside")
continue;
case 'close':
delta++;
this.previous();
break;
case 'open':
delta--;
this.previous();
if (delta <= 0)
return true;
break;
default:
this.previous();
}
}
}
/**
* Moves this cursor to the close paren of the containing sexpr, or until the end of the document.
*/
forwardList(): boolean {
let cursor = this.clone();
while (cursor.forwardSexp()) { }
if (cursor.getToken().type == "close") {
this.set(cursor);
return true;
}
return false;
}
/**
* Moves this cursor backwards to the open paren of the containing sexpr, or until the start of the document.
*/
backwardList(): boolean {
let cursor = this.clone();
while (cursor.backwardSexp()) { }
if (cursor.getPrevToken().type == "open") {
this.set(cursor);
return true;
}
return false;
}
/**
* Moves this cursor backwards to the opening `openingBracket` of the containing sexpr, or until the start of the document.
*/
backwardListOfType(openingBracket: string): boolean {
let cursor = this.clone();
while (cursor.backwardList()) {
if (cursor.getPrevToken().raw === openingBracket) {
this.set(cursor);
return true;
}
if (!cursor.backwardUpList()) {
return false;
}
}
}
/**
* If possible, moves this cursor forwards past any whitespace, and then past the immediately following open-paren and returns true.
* If the source does not match this, returns false and does not move the cursor.
*/
downList(): boolean {
let cursor = this.clone();
cursor.forwardWhitespace();
if (cursor.getToken().type == "open") {
cursor.next();
this.set(cursor);
return true;
}
return false;
}
/**
* If possible, moves this cursor forwards past any whitespace, and then past the immediately following close-paren and returns true.
* If the source does not match this, returns false and does not move the cursor.
*/
upList(): boolean {
let cursor = this.clone();
cursor.forwardWhitespace();
if (cursor.getToken().type == "close") {
cursor.next();
this.set(cursor);
return true;
}
return false;
}
/**
* If possible, moves this cursor backwards past any whitespace, and then backwards past the immediately following open-paren and returns true.
* If the source does not match this, returns false and does not move the cursor.
*/
backwardUpList(): boolean {
let cursor = this.clone();
cursor.backwardWhitespace();
if (cursor.getPrevToken().type == "open") {
cursor.previous();
this.set(cursor);
return true;
}
return false;
}
/**
* If possible, moves this cursor backwards past any whitespace, and then backwards past the immediately following close-paren and returns true.
* If the source does not match this, returns false and does not move the cursor.
*/
backwardDownList(): boolean {
let cursor = this.clone();
cursor.backwardWhitespace();
if (cursor.getPrevToken().type == "close") {
cursor.previous();
this.set(cursor);
return true;
}
return false;
}
withinWhitespace() {
let tk = this.getToken().type;
if (tk == "eol" || tk == "ws") {
return true;
}
}
withinString() {
let tk = this.getToken().type;
if (tk == "str" || tk == "str-start" || tk == "str-end" || tk == "str-inside") {
return true;
}
if (tk == "eol") {
tk = this.getPrevToken().type;
if (tk == "str-inside" || tk == "str-start")
return true;
}
return false;
}
/**
* Tells if the cursor is inside a properly closed list.
*/
withinValidList(): boolean {
let cursor = this.clone();
while (cursor.forwardSexp()) { }
return cursor.getToken().type == "close";
}
/**
* Returns the ranges for all forms in the current list.
* Returns undefined if the current cursor is not within a list.
* If you are particular about which list type that should be considered, supply an `openingBracket`.
*/
rangesForSexpsInList(openingBracket?: string): [[number, number], [number, number]][] {
let cursor = this.clone();
if (openingBracket !== undefined) {
if (!cursor.backwardListOfType(openingBracket)) {
return undefined;
}
} else {
if (!cursor.backwardList()) {
return undefined;
}
}
let ranges = [];
// TODO: Figure out how to do this ignore skipping more generally in forward/backward this or that.
let ignoreCounter = 0;
while (true) {
cursor.forwardWhitespace();
const start = cursor.rowCol;
if (cursor.getToken().raw === '#_') {
ignoreCounter++;
cursor.forwardSexp();
continue;
}
if (cursor.forwardSexp()) {
if (ignoreCounter === 0) {
const end = cursor.rowCol;
ranges.push([start, end]);
} else {
ignoreCounter--;
}
} else {
break;
}
}
return ranges;
}
/**
* Tries to move this cursor backwards to the open paren of the function, `level` functions up.
* If there aren't that many functions bahind the cursor, the cursor is not moved at all.
* @param levels how many functions up to go before placing the cursor at the start of it.
* @returns `true` if the cursor was moved, otherwise `false`
*/
backwardFunction(levels: number = 0): boolean {
const cursor = this.clone();
if (!cursor.backwardListOfType('(')) {
return false;
}
for (let i = 0; i < levels; i++) {
if (!cursor.backwardUpList()) {
return false;
}
if (!cursor.backwardListOfType('(')) {
return false;
}
}
this.set(cursor);
return true;
}
/**
* Get the name of the current function, optionally digging `levels` functions up.
* @param levels how many levels of functions to dig up.
* @returns the function name, or undefined if there is no function there.
*/
getFunction(levels: number = 0): string {
const cursor = this.clone();
if (cursor.backwardFunction(levels)) {
cursor.forwardWhitespace();
const symbol = cursor.getToken();
if (symbol.type === 'id') {
return symbol.raw;
}
}
}
/**
* Gets the enclosing function from the current cursor position.
* If it can't find a function, returns `undefined`.
*/
// getFunction(): string {
// const cursor = this.clone();
// if (cursor.backwardListOfType('(')) {
// cursor.forwardWhitespace();
// const symbol = cursor.getToken();
// if (symbol.type === 'id') {
// return symbol.raw;
// }
// }
// }
}
/**
* Creates a `LispTokenCursor` for walking and manipulating the string `s`.
*/
export function createStringCursor(s: string): LispTokenCursor {
const model = new LineInputModel();
model.insertString(0, s);
return model.getTokenCursor(0);
}