719
Le
lextide / files
lextide / src / languages / ecmascript.ts
16.1 KB
1import type { InternalRule } from "../core/grammar"; 2import { defineGrammar } from "../core/grammar"; 3import type { GrammarDefinition } from "../types"; 4 5import { 6 consumeUntil, 7 createDeclarationRule, 8 createIdentifierMatcher, 9 createNumberRule, 10 ECMASCRIPT_IDENTIFIER, 11 escapeForExpression, 12 scanIdentifier, 13 startsWithWord, 14} from "./helpers"; 15 16interface EcmaScriptDeclaration { 17 keyword: string; 18 relevance: number; 19 scope: string; 20} 21 22interface EcmaScriptGrammarConfig { 23 aliases: string[]; 24 builtIns?: ReadonlySet<string>; 25 declarations: readonly EcmaScriptDeclaration[]; 26 decorators?: boolean; 27 keywords: ReadonlySet<string>; 28 literals: ReadonlySet<string>; 29 name: string; 30 types?: boolean; 31} 32 33const JSX_TAG_CHILDREN: InternalRule[] = [ 34 { match: jsxTagNameMatcher, scope: "name" }, 35 { match: jsxAttributeMatcher, scope: "attr" }, 36 { match: /"[^"\\]*(?:\\.[^"\\]*)*"/, scope: "string" }, 37 { match: /'[^'\\]*(?:\\.[^'\\]*)*'/, scope: "string" }, 38 { match: jsxBraceMatcher, scope: "subst" }, 39]; 40 41const JSX_PREFIX_CHARACTERS = new Set([ 42 "!", 43 "%", 44 "&", 45 "(", 46 "*", 47 "+", 48 ",", 49 ":", 50 ";", 51 "=", 52 ">", 53 "?", 54 "[", 55 "^", 56 "{", 57 "|", 58 "~", 59 "-", 60]); 61 62const REGEXP_PREFIX_CHARACTERS = new Set(["/", "<", ...JSX_PREFIX_CHARACTERS]); 63 64export function createEcmaScriptGrammar( 65 config: EcmaScriptGrammarConfig, 66): GrammarDefinition { 67 return defineGrammar({ 68 aliases: config.aliases, 69 name: config.name, 70 relevance: 0, 71 startState: "root", 72 states: { 73 blockComment: { 74 defaultScope: "comment", 75 rules: [{ match: /\*\//, pop: true }, { match: consumeUntil(["*"]) }], 76 }, 77 doubleString: { 78 defaultScope: "string", 79 rules: [ 80 { match: /\\./, scope: "escape" }, 81 { match: /"/, pop: true }, 82 { match: consumeUntil(["\\", '"']) }, 83 ], 84 }, 85 root: { 86 rules: createEcmaScriptRules(config, false), 87 }, 88 singleString: { 89 defaultScope: "string", 90 rules: [ 91 { match: /\\./, scope: "escape" }, 92 { match: /'/, pop: true }, 93 { match: consumeUntil(["\\", "'"]) }, 94 ], 95 }, 96 templateString: { 97 defaultScope: "string", 98 rules: [ 99 { match: /\\./, scope: "escape" }, 100 { 101 match: /\$\{/, 102 push: "templateSubst", 103 pushScope: "subst", 104 relevance: 1, 105 }, 106 { match: /`/, pop: true }, 107 { match: consumeUntil(["\\", "`", "$"]) }, 108 ], 109 }, 110 templateSubst: { 111 defaultScope: "subst", 112 rules: createEcmaScriptRules(config, true), 113 }, 114 }, 115 }); 116} 117 118function createEcmaScriptRules( 119 config: EcmaScriptGrammarConfig, 120 includeSubstBraces: boolean, 121): InternalRule[] { 122 const rules: InternalRule[] = [ 123 { 124 match: /\/\*/, 125 push: "blockComment", 126 pushScope: "comment", 127 relevance: 1, 128 }, 129 { match: /\/\/[^\n]*/, scope: "comment", relevance: 1 }, 130 { 131 match: /"/, 132 push: "doubleString", 133 pushScope: "string", 134 relevance: 1, 135 }, 136 { 137 match: /'/, 138 push: "singleString", 139 pushScope: "string", 140 relevance: 1, 141 }, 142 { 143 match: /`/, 144 push: "templateString", 145 pushScope: "string", 146 relevance: 1, 147 }, 148 { match: regexpLiteralMatcher, scope: "regexp", relevance: 1 }, 149 { 150 match: createJsxTagMatcher(config.types === true, config.builtIns), 151 scope: "tag", 152 children: JSX_TAG_CHILDREN, 153 relevance: 1, 154 }, 155 ]; 156 157 if (config.decorators) { 158 rules.push({ 159 match: /@(?:[$_]|[\p{ID_Start}])(?:[$_\u200C\u200D]|[\p{ID_Continue}])*/u, 160 scope: "meta", 161 }); 162 } 163 164 for (const declaration of config.declarations) { 165 rules.push( 166 createDeclarationRule(declaration.keyword, declaration.scope, { 167 identifier: ECMASCRIPT_IDENTIFIER, 168 relevance: declaration.relevance, 169 }), 170 ); 171 } 172 173 rules.push({ match: inheritanceClauseMatcher, relevance: 1 }); 174 175 if (config.types) { 176 rules.push({ match: typeAttributeMatcher, scope: "attr", relevance: 0 }); 177 rules.push({ 178 match: typeReferenceMatcher, 179 scope: "title.class", 180 relevance: 0, 181 }); 182 } 183 184 if (includeSubstBraces) { 185 rules.push({ match: /\{/, push: "templateSubst", relevance: 0 }); 186 rules.push({ match: /\}/, pop: true, relevance: 0 }); 187 } 188 189 rules.push({ match: propertyMatcher, scope: "property", relevance: 0 }); 190 rules.push(createNumberRule()); 191 rules.push({ 192 match: createIdentifierMatcher({ 193 builtIns: config.builtIns, 194 identifier: ECMASCRIPT_IDENTIFIER, 195 keywordRelevance: 2, 196 keywords: config.keywords, 197 literalRelevance: 1, 198 literals: config.literals, 199 }), 200 }); 201 202 return rules; 203} 204 205function jsxAttributeMatcher(input: string, position: number): number | null { 206 if (position <= 0 || !/\s/.test(input[position - 1])) { 207 return null; 208 } 209 210 const end = scanJsxName(input, position); 211 212 if (end === null) { 213 return null; 214 } 215 216 const next = input[skipWhitespace(input, end)] ?? ""; 217 218 if (next === "=" || next === ">" || next === "/" || /\s/.test(next)) { 219 return end - position; 220 } 221 222 return null; 223} 224 225function jsxBraceMatcher(input: string, position: number): number | null { 226 if (input[position] !== "{") { 227 return null; 228 } 229 230 const end = skipBalancedBraces(input, position); 231 232 return end === null ? null : end - position; 233} 234 235function createJsxTagMatcher( 236 types: boolean, 237 typeBuiltIns?: ReadonlySet<string>, 238): (input: string, position: number) => number | null { 239 return (input, position) => 240 jsxTagMatcher(input, position, types, typeBuiltIns); 241} 242 243function jsxTagMatcher( 244 input: string, 245 position: number, 246 types = false, 247 typeBuiltIns?: ReadonlySet<string>, 248): number | null { 249 if (!isLikelyJsxStart(input, position)) { 250 return null; 251 } 252 253 const fragmentLength = jsxFragmentLength(input, position); 254 255 if (fragmentLength !== null) { 256 return fragmentLength; 257 } 258 259 const prefix = jsxTagPrefix(input, position); 260 261 if (prefix === null) { 262 return null; 263 } 264 265 const end = skipJsxTagBody(input, prefix.nameEnd); 266 267 if (end === null) { 268 return null; 269 } 270 271 if ( 272 types && 273 looksLikeTypeAssertionTag( 274 input, 275 position, 276 prefix.nameEnd, 277 end, 278 typeBuiltIns, 279 ) 280 ) { 281 return null; 282 } 283 284 return end - position; 285} 286 287function jsxFragmentLength(input: string, position: number): number | null { 288 if (input[position] !== "<") { 289 return null; 290 } 291 292 if (input.startsWith("</>", position)) { 293 return 3; 294 } 295 296 if (input.startsWith("<>", position)) { 297 return 2; 298 } 299 300 return null; 301} 302 303function jsxTagNameMatcher(input: string, position: number): number | null { 304 if ( 305 !(position === 1 && input[0] === "<") && 306 !(position === 2 && input[0] === "<" && input[1] === "/") 307 ) { 308 return null; 309 } 310 311 const end = scanJsxName(input, position); 312 313 return end === null ? null : end - position; 314} 315 316function jsxTagPrefix( 317 input: string, 318 position: number, 319): { nameEnd: number } | null { 320 if (input[position] !== "<") { 321 return null; 322 } 323 324 let cursor = position + 1; 325 326 if (input[cursor] === "/") { 327 cursor += 1; 328 } 329 330 const nameEnd = scanJsxName(input, cursor); 331 332 if (nameEnd === null || !isValidJsxNameSuffix(input, nameEnd)) { 333 return null; 334 } 335 336 return { nameEnd }; 337} 338 339function isLikelyJsxStart(input: string, position: number): boolean { 340 if (input[position] !== "<") { 341 return false; 342 } 343 344 const previous = previousSignificantIndex(input, position); 345 346 if (previous < 0) { 347 return true; 348 } 349 350 if (/^[\])}`'"._$\p{ID_Continue}]$/u.test(input[previous])) { 351 const keyword = previousAsciiWord(input, position); 352 return ( 353 keyword !== null && /^(?:case|else|return|throw|yield)$/.test(keyword) 354 ); 355 } 356 357 return JSX_PREFIX_CHARACTERS.has(input[previous]); 358} 359 360function previousAsciiWord(input: string, position: number): string | null { 361 let end = position; 362 363 while (end > 0 && /\s/.test(input[end - 1])) { 364 end -= 1; 365 } 366 367 let start = end; 368 369 while (start > 0 && /[A-Za-z]/.test(input[start - 1])) { 370 start -= 1; 371 } 372 373 return start === end ? null : input.slice(start, end); 374} 375 376function previousSignificantIndex(input: string, position: number): number { 377 let cursor = position - 1; 378 379 while (cursor >= 0 && /\s/.test(input[cursor])) { 380 cursor -= 1; 381 } 382 383 return cursor; 384} 385 386function looksLikeTypeAssertionTag( 387 input: string, 388 position: number, 389 nameEnd: number, 390 end: number, 391 typeBuiltIns?: ReadonlySet<string>, 392): boolean { 393 if (input[position + 1] === "/") { 394 return false; 395 } 396 397 const suffix = input.slice(nameEnd, end - 1).trim(); 398 399 if (suffix.length > 0) { 400 return false; 401 } 402 403 const name = input.slice(position + 1, nameEnd); 404 405 if (!looksLikeTypeAssertionName(name, typeBuiltIns)) { 406 return false; 407 } 408 409 return !hasMatchingJsxClosingTagAhead(input, end, name); 410} 411 412function looksLikeTypeAssertionName( 413 value: string, 414 typeBuiltIns?: ReadonlySet<string>, 415): boolean { 416 return ( 417 /^[A-Z]$/.test(value) || 418 looksLikeTypeReference(value) || 419 typeBuiltIns?.has(value) === true 420 ); 421} 422 423function hasMatchingJsxClosingTagAhead( 424 input: string, 425 position: number, 426 name: string, 427): boolean { 428 const expression = new RegExp(`</${escapeForExpression(name)}\\s*>`); 429 430 return expression.test(input.slice(position)); 431} 432 433function inheritanceClauseMatcher( 434 input: string, 435 position: number, 436): { children: InternalRule[]; length: number } | null { 437 const keyword = startsWithWord(input, position, "extends") 438 ? "extends" 439 : startsWithWord(input, position, "implements") 440 ? "implements" 441 : null; 442 443 if (keyword === null) { 444 return null; 445 } 446 447 const titleStart = skipWhitespace(input, position + keyword.length); 448 const titleEnd = scanIdentifier(input, titleStart, ECMASCRIPT_IDENTIFIER); 449 450 if (titleEnd === null) { 451 return null; 452 } 453 454 return { 455 children: [ 456 { match: new RegExp(keyword), scope: "keyword" }, 457 { 458 match: ECMASCRIPT_IDENTIFIER.pattern, 459 scope: "title.class.inherited", 460 }, 461 ], 462 length: titleEnd - position, 463 }; 464} 465 466function propertyMatcher(input: string, position: number): number | null { 467 const end = scanIdentifier(input, position, ECMASCRIPT_IDENTIFIER); 468 469 if (end === null) { 470 return null; 471 } 472 473 const previous = previousSignificantIndex(input, position); 474 475 if (previous < 0 || input[previous] !== ".") { 476 return null; 477 } 478 479 return end - position; 480} 481 482function typeAttributeMatcher(input: string, position: number): number | null { 483 const end = scanIdentifier(input, position, ECMASCRIPT_IDENTIFIER); 484 485 if (end === null || !hasTypeAnnotationSuffix(input, end)) { 486 return null; 487 } 488 489 const previous = previousSignificantIndex(input, position); 490 const keyword = previousAsciiWord(input, position); 491 492 if ( 493 (previous >= 0 && ["{", ",", ";"].includes(input[previous])) || 494 (keyword !== null && 495 /^(?:const|declare|let|private|protected|public|readonly|var)$/.test( 496 keyword, 497 )) 498 ) { 499 return end - position; 500 } 501 502 return null; 503} 504 505function typeReferenceMatcher(input: string, position: number): number | null { 506 const end = scanIdentifier(input, position, ECMASCRIPT_IDENTIFIER); 507 508 if (end === null || !looksLikeTypeReference(input.slice(position, end))) { 509 return null; 510 } 511 512 const previous = previousSignificantIndex(input, position); 513 const keyword = previousAsciiWord(input, position); 514 515 if ( 516 (previous >= 0 && 517 ["&", "(", ",", ":", "<", "=", "[", "|"].includes(input[previous])) || 518 (keyword !== null && /^(?:as|extends|implements|infer|is)$/.test(keyword)) 519 ) { 520 return end - position; 521 } 522 523 return null; 524} 525 526function hasTypeAnnotationSuffix(input: string, position: number): boolean { 527 const probe = skipWhitespace(input, position); 528 529 if (input[probe] === ":") { 530 return true; 531 } 532 533 return input[probe] === "?" && input[probe + 1] === ":"; 534} 535 536function looksLikeTypeReference(value: string): boolean { 537 return value.length > 1 && /^[A-Z]/.test(value); 538} 539 540function regexpLiteralMatcher(input: string, position: number): number | null { 541 if ( 542 input[position] !== "/" || 543 input.startsWith("//", position) || 544 input.startsWith("/*", position) || 545 !regexpCanStart(input, position) 546 ) { 547 return null; 548 } 549 550 const end = skipRegExpLiteral(input, position); 551 552 return end === null ? null : end - position; 553} 554 555function regexpCanStart(input: string, position: number): boolean { 556 const previous = previousSignificantIndex(input, position); 557 558 if (previous < 0) { 559 return true; 560 } 561 562 if (isPostfixUpdateOperator(input, previous)) { 563 return false; 564 } 565 566 if (REGEXP_PREFIX_CHARACTERS.has(input[previous])) { 567 return true; 568 } 569 570 const keyword = previousAsciiWord(input, position); 571 572 return ( 573 keyword !== null && 574 /^(?:await|case|delete|in|instanceof|new|return|throw|typeof|void|yield)$/.test( 575 keyword, 576 ) 577 ); 578} 579 580function isPostfixUpdateOperator(input: string, position: number): boolean { 581 const character = input[position]; 582 583 if (character !== "+" && character !== "-") { 584 return false; 585 } 586 587 let count = 0; 588 let cursor = position; 589 590 while (cursor >= 0 && input[cursor] === character) { 591 count += 1; 592 cursor -= 1; 593 } 594 595 return count === 2; 596} 597 598function scanJsxName(input: string, position: number): number | null { 599 let end = scanIdentifier(input, position, ECMASCRIPT_IDENTIFIER); 600 601 if (end === null) { 602 return null; 603 } 604 605 while (end < input.length && /[.:-]/.test(input[end])) { 606 const nextEnd = scanIdentifier(input, end + 1, ECMASCRIPT_IDENTIFIER); 607 608 if (nextEnd === null) { 609 break; 610 } 611 612 end = nextEnd; 613 } 614 615 return end; 616} 617 618function isValidJsxNameSuffix(input: string, nameEnd: number): boolean { 619 const probe = skipWhitespace(input, nameEnd); 620 const next = input[probe]; 621 622 if (!next || next === "<" || next === "," || next === "=" || next === "[") { 623 return false; 624 } 625 626 return !(probe > nameEnd && input.startsWith("extends", probe)); 627} 628 629function skipBalancedBraces(input: string, position: number): number | null { 630 let depth = 0; 631 let cursor = position; 632 633 while (cursor < input.length) { 634 const advanced = skipJsxEmbedded(input, cursor); 635 636 if (advanced !== null) { 637 cursor = advanced; 638 continue; 639 } 640 641 if (input[cursor] === "{") { 642 depth += 1; 643 } else if (input[cursor] === "}") { 644 depth -= 1; 645 646 if (depth === 0) { 647 return cursor + 1; 648 } 649 } 650 651 cursor += 1; 652 } 653 654 return null; 655} 656 657function skipJsxEmbedded(input: string, position: number): number | null { 658 const character = input[position]; 659 660 if (character === '"' || character === "'") { 661 return skipQuotedString(input, position, character); 662 } 663 664 return null; 665} 666 667function skipJsxTagBody(input: string, position: number): number | null { 668 let cursor = position; 669 670 while (cursor < input.length) { 671 const embeddedEnd = skipJsxEmbedded(input, cursor); 672 673 if (embeddedEnd !== null) { 674 cursor = embeddedEnd; 675 continue; 676 } 677 678 if (input[cursor] === "{") { 679 const braceEnd = skipBalancedBraces(input, cursor); 680 681 if (braceEnd === null) { 682 return null; 683 } 684 685 cursor = braceEnd; 686 continue; 687 } 688 689 if (input[cursor] === ">") { 690 return cursor + 1; 691 } 692 693 if (input[cursor] === "<") { 694 return null; 695 } 696 697 cursor += 1; 698 } 699 700 return null; 701} 702 703function skipRegExpLiteral(input: string, position: number): number | null { 704 let cursor = position + 1; 705 let inCharacterClass = false; 706 707 while (cursor < input.length) { 708 const advanced = advanceRegExpCursor(input, cursor, inCharacterClass); 709 710 if (advanced === null) { 711 return null; 712 } 713 714 cursor = advanced.cursor; 715 inCharacterClass = advanced.inCharacterClass; 716 717 if (advanced.closed) { 718 return skipRegExpFlags(input, cursor); 719 } 720 } 721 722 return null; 723} 724 725function advanceRegExpCursor( 726 input: string, 727 cursor: number, 728 inCharacterClass: boolean, 729): { closed?: boolean; cursor: number; inCharacterClass: boolean } | null { 730 const character = input[cursor]; 731 732 if (character === "\\") { 733 return { cursor: cursor + 2, inCharacterClass }; 734 } 735 736 if (character === "\n") { 737 return null; 738 } 739 740 if (character === "[" && !inCharacterClass) { 741 return { cursor: cursor + 1, inCharacterClass: true }; 742 } 743 744 if (character === "]" && inCharacterClass) { 745 return { cursor: cursor + 1, inCharacterClass: false }; 746 } 747 748 if (character === "/" && !inCharacterClass) { 749 return { closed: true, cursor: cursor + 1, inCharacterClass }; 750 } 751 752 return { cursor: cursor + 1, inCharacterClass }; 753} 754 755function skipRegExpFlags(input: string, position: number): number { 756 let cursor = position; 757 758 while (/[A-Za-z]/.test(input[cursor] ?? "")) { 759 cursor += 1; 760 } 761 762 return cursor; 763} 764 765function skipQuotedString( 766 input: string, 767 position: number, 768 quote: string, 769): number | null { 770 let cursor = position + 1; 771 772 while (cursor < input.length) { 773 const character = input[cursor]; 774 775 if (character === "\\") { 776 cursor += 2; 777 continue; 778 } 779 780 if (character === quote) { 781 return cursor + 1; 782 } 783 784 cursor += 1; 785 } 786 787 return null; 788} 789 790function skipWhitespace(input: string, position: number): number { 791 let cursor = position; 792 793 while (cursor < input.length && /\s/.test(input[cursor])) { 794 cursor += 1; 795 } 796 797 return cursor; 798}