@@ -8,7 +8,9 @@ import {
88 createIdentifierMatcher,
99 createNumberRule,
1010 ECMASCRIPT_IDENTIFIER,
11+ escapeForExpression,
1112 scanIdentifier,
13+ startsWithWord,
1214 } from "./helpers";
1315
1416 interface EcmaScriptDeclaration {
@@ -25,6 +27,7 @@ interface EcmaScriptGrammarConfig {
2527 keywords: ReadonlySet<string>;
2628 literals: ReadonlySet<string>;
2729 name: string;
30+ types?: boolean;
2831 }
2932
3033 const JSX_TAG_CHILDREN: InternalRule[] = [
@@ -35,6 +38,29 @@ const JSX_TAG_CHILDREN: InternalRule[] = [
3538 { match: jsxBraceMatcher, scope: "subst" },
3639 ];
3740
41+const 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+
62+const REGEXP_PREFIX_CHARACTERS = new Set(["/", "<", ...JSX_PREFIX_CHARACTERS]);
63+
3864 export function createEcmaScriptGrammar(
3965 config: EcmaScriptGrammarConfig,
4066 ): GrammarDefinition {
@@ -119,8 +145,9 @@ function createEcmaScriptRules(
119145 pushScope: "string",
120146 relevance: 1,
121147 },
148+ { match: regexpLiteralMatcher, scope: "regexp", relevance: 1 },
122149 {
123- match: jsxTagMatcher,
150+ match: createJsxTagMatcher(config.types === true, config.builtIns),
124151 scope: "tag",
125152 children: JSX_TAG_CHILDREN,
126153 relevance: 1,
@@ -143,11 +170,23 @@ function createEcmaScriptRules(
143170 );
144171 }
145172
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+
146184 if (includeSubstBraces) {
147185 rules.push({ match: /\{/, push: "templateSubst", relevance: 0 });
148186 rules.push({ match: /\}/, pop: true, relevance: 0 });
149187 }
150188
189+ rules.push({ match: propertyMatcher, scope: "property", relevance: 0 });
151190 rules.push(createNumberRule());
152191 rules.push({
153192 match: createIdentifierMatcher({
@@ -193,7 +232,24 @@ function jsxBraceMatcher(input: string, position: number): number | null {
193232 return end === null ? null : end - position;
194233 }
195234
196-function jsxTagMatcher(input: string, position: number): number | null {
235+function 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+
243+function 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+
197253 const fragmentLength = jsxFragmentLength(input, position);
198254
199255 if (fragmentLength !== null) {
@@ -208,7 +264,24 @@ function jsxTagMatcher(input: string, position: number): number | null {
208264
209265 const end = skipJsxTagBody(input, prefix.nameEnd);
210266
211- return end === null ? null : end - position;
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;
212285 }
213286
214287 function jsxFragmentLength(input: string, position: number): number | null {
@@ -263,6 +336,265 @@ function jsxTagPrefix(
263336 return { nameEnd };
264337 }
265338
339+function 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+
360+function 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+
376+function 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+
386+function 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+
412+function 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+
423+function 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+
433+function 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+
466+function 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+
482+function 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+
505+function 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+
526+function 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+
536+function looksLikeTypeReference(value: string): boolean {
537+ return value.length > 1 && /^[A-Z]/.test(value);
538+}
539+
540+function 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+
555+function 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+
580+function 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+
266598 function scanJsxName(input: string, position: number): number | null {
267599 let end = scanIdentifier(input, position, ECMASCRIPT_IDENTIFIER);
268600
@@ -368,6 +700,68 @@ function skipJsxTagBody(input: string, position: number): number | null {
368700 return null;
369701 }
370702
703+function 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+
725+function 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+
755+function 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+
371765 function skipQuotedString(
372766 input: string,
373767 position: number,