Skip to content

Commit 3eec379

Browse files
committed
feat: tap parser
add node parallel tests for lexer
1 parent 791c410 commit 3eec379

File tree

4 files changed

+494
-452
lines changed

4 files changed

+494
-452
lines changed

lib/internal/test_runner/tap_lexer.js

+41-40
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ const TokenKind = {
2424
};
2525

2626
class Token {
27-
constructor({ type, value, stream }) {
28-
this.type = type;
27+
constructor({ kind, value, stream }) {
28+
this.kind = kind;
2929
this.value = value;
3030
this.location = {
3131
line: stream.line,
@@ -150,7 +150,7 @@ class TapLexer {
150150
this.escapeStack = [];
151151

152152
this.lastScannedToken = new Token({
153-
type: TokenKind.EOL,
153+
kind: TokenKind.EOL,
154154
value: TokenKind.EOL,
155155
stream: this.source,
156156
});
@@ -161,7 +161,7 @@ class TapLexer {
161161
let token = this.scanToken();
162162

163163
// remember the last scanned token (except for whitespace)
164-
if (token.type !== TokenKind.WHITESPACE) {
164+
if (token.kind !== TokenKind.WHITESPACE) {
165165
this.lastScannedToken = token;
166166
}
167167
yield token;
@@ -223,27 +223,28 @@ class TapLexer {
223223
// escape chars from the stack and start fresh for the next line
224224
this.escapeStack = [];
225225
return new Token({
226-
type: TokenKind.EOL,
226+
kind: TokenKind.EOL,
227227
value: char,
228228
stream: this.source,
229229
});
230230
}
231231

232232
scanEOF() {
233233
return new Token({
234-
type: TokenKind.EOF,
234+
kind: TokenKind.EOF,
235235
value: TokenKind.EOF,
236236
stream: this.source,
237237
});
238238
}
239239

240240
scanEscapeSymbol(char) {
241-
// if the escape symbol has been escaped, then it is not an escape symbol
242-
// consume it as a literal.
243-
if (this.hasTheCurrentCharacterBeenEscaped(char)) {
241+
// if the escape symbol has been escaped (by previous symbol),
242+
// or if the next symbol is a whitespace symbol,
243+
// then consume it as a literal.
244+
if (this.hasTheCurrentCharacterBeenEscaped() || this.source.peek(1) === TokenKind.WHITESPACE) {
244245
this.escapeStack.pop();
245246
return new Token({
246-
type: TokenKind.LITERAL,
247+
kind: TokenKind.LITERAL,
247248
value: char,
248249
stream: this.source,
249250
});
@@ -254,15 +255,15 @@ class TapLexer {
254255
// and consume the next character as a literal (done in the next turn)
255256
this.escapeStack.push(char);
256257
return new Token({
257-
type: TokenKind.ESCAPE,
258+
kind: TokenKind.ESCAPE,
258259
value: char,
259260
stream: this.source,
260261
});
261262
}
262263

263264
scanWhitespace(char) {
264265
return new Token({
265-
type: TokenKind.WHITESPACE,
266+
kind: TokenKind.WHITESPACE,
266267
value: char,
267268
stream: this.source,
268269
});
@@ -279,15 +280,15 @@ class TapLexer {
279280
}
280281

281282
return new Token({
282-
type: TokenKind.DASH,
283+
kind: TokenKind.DASH,
283284
value: char,
284285
stream: this.source,
285286
});
286287
}
287288

288289
scanPlus(char) {
289290
return new Token({
290-
type: TokenKind.PLUS,
291+
kind: TokenKind.PLUS,
291292
value: char,
292293
stream: this.source,
293294
});
@@ -296,25 +297,25 @@ class TapLexer {
296297
scanHash(char) {
297298
// if last token is whitespace or EOL, we consume it as a comment
298299
if (
299-
this.lastScannedToken.type === TokenKind.WHITESPACE ||
300-
this.lastScannedToken.type === TokenKind.EOL
300+
this.lastScannedToken.kind === TokenKind.WHITESPACE ||
301+
this.lastScannedToken.kind === TokenKind.EOL
301302
) {
302303
this.isComment = true;
303304
return new Token({
304-
type: TokenKind.COMMENT,
305+
kind: TokenKind.COMMENT,
305306
value: char,
306307
stream: this.source,
307308
});
308309
}
309310

310-
const charHasBeenEscaped = this.hasTheCurrentCharacterBeenEscaped(char);
311+
const charHasBeenEscaped = this.hasTheCurrentCharacterBeenEscaped();
311312
if (this.isComment || charHasBeenEscaped) {
312313
if (charHasBeenEscaped) {
313314
this.escapeStack.pop();
314315
}
315316

316317
return new Token({
317-
type: TokenKind.LITERAL,
318+
kind: TokenKind.LITERAL,
318319
value: char,
319320
stream: this.source,
320321
});
@@ -323,7 +324,7 @@ class TapLexer {
323324
// when a hash is found, we assume the rest of the line is a comment
324325
this.isComment = true;
325326
return new Token({
326-
type: TokenKind.HASH,
327+
kind: TokenKind.HASH,
327328
value: char,
328329
stream: this.source,
329330
});
@@ -349,7 +350,7 @@ class TapLexer {
349350
this.error(
350351
`Exepcted YAML end block: ...`,
351352
new Token({
352-
type: TokenKind.EOF,
353+
kind: TokenKind.EOF,
353354
value: TokenKind.EOF,
354355
stream: this.source,
355356
})
@@ -358,7 +359,7 @@ class TapLexer {
358359
}
359360

360361
return new Token({
361-
type: TokenKind.TAP_YAML,
362+
kind: TokenKind.TAP_YAML,
362363
value: yaml, // don't trim on purpose!
363364
stream: this.source,
364365
});
@@ -377,7 +378,7 @@ class TapLexer {
377378
comment = comment.replace(/^# /, '');
378379

379380
return new Token({
380-
type: TokenKind.COMMENT,
381+
kind: TokenKind.COMMENT,
381382
value: comment,
382383
stream: this.source,
383384
});
@@ -397,7 +398,7 @@ class TapLexer {
397398
description = description.replace(/^- /, '');
398399

399400
return new Token({
400-
type: TokenKind.COMMENT,
401+
kind: TokenKind.COMMENT,
401402
value: description.trim(),
402403
stream: this.source,
403404
});
@@ -444,60 +445,60 @@ class TapLexer {
444445
}
445446

446447
return new Token({
447-
type: TokenKind.LITERAL,
448+
kind: TokenKind.LITERAL,
448449
value: word,
449450
stream: this.source,
450451
});
451452
}
452453

453454
scanTAPkeyword(word) {
454-
if (word === 'TAP' && this.lastScannedToken.type === TokenKind.EOL) {
455+
if (word === 'TAP' && this.lastScannedToken.kind === TokenKind.EOL) {
455456
return new Token({
456-
type: TokenKind.TAP,
457+
kind: TokenKind.TAP,
457458
value: word,
458459
stream: this.source,
459460
});
460461
}
461462

462-
if (word === 'version' && this.lastScannedToken.type === TokenKind.TAP) {
463+
if (word === 'version' && this.lastScannedToken.kind === TokenKind.TAP) {
463464
return new Token({
464-
type: TokenKind.TAP_VERSION,
465+
kind: TokenKind.TAP_VERSION,
465466
value: word,
466467
stream: this.source,
467468
});
468469
}
469470

470-
if (word === '..' && this.lastScannedToken.type === TokenKind.NUMERIC) {
471+
if (word === '..' && this.lastScannedToken.kind === TokenKind.NUMERIC) {
471472
return new Token({
472-
type: TokenKind.TAP_PLAN,
473+
kind: TokenKind.TAP_PLAN,
473474
value: word,
474475
stream: this.source,
475476
});
476477
}
477478

478-
if (word === 'not' && this.lastScannedToken.type === TokenKind.EOL) {
479+
if (word === 'not' && this.lastScannedToken.kind === TokenKind.EOL) {
479480
return new Token({
480-
type: TokenKind.TAP_TEST_NOTOK,
481+
kind: TokenKind.TAP_TEST_NOTOK,
481482
value: word,
482483
stream: this.source,
483484
});
484485
}
485486

486487
if (
487488
word === 'ok' &&
488-
(this.lastScannedToken.type === TokenKind.TAP_TEST_NOTOK ||
489-
this.lastScannedToken.type === TokenKind.EOL)
489+
(this.lastScannedToken.kind === TokenKind.TAP_TEST_NOTOK ||
490+
this.lastScannedToken.kind === TokenKind.EOL)
490491
) {
491492
return new Token({
492-
type: TokenKind.TAP_TEST_OK,
493+
kind: TokenKind.TAP_TEST_OK,
493494
value: word,
494495
stream: this.source,
495496
});
496497
}
497498

498-
if (word === 'pragma' && this.lastScannedToken.type === TokenKind.EOL) {
499+
if (word === 'pragma' && this.lastScannedToken.kind === TokenKind.EOL) {
499500
return new Token({
500-
type: TokenKind.TAP_PRAGMA,
501+
kind: TokenKind.TAP_PRAGMA,
501502
value: word,
502503
stream: this.source,
503504
});
@@ -518,13 +519,13 @@ class TapLexer {
518519
}
519520
}
520521
return new Token({
521-
type: TokenKind.NUMERIC,
522+
kind: TokenKind.NUMERIC,
522523
value: number,
523524
stream: this.source,
524525
});
525526
}
526527

527-
hasTheCurrentCharacterBeenEscaped(char) {
528+
hasTheCurrentCharacterBeenEscaped() {
528529
// use the escapeStack to keep track of the escape characters
529530
return this.escapeStack.length > 0;
530531
}

0 commit comments

Comments
 (0)