diff --git a/src/core/prism.ts b/src/core/prism.ts index d370c3e7f..940f3b89c 100644 --- a/src/core/prism.ts +++ b/src/core/prism.ts @@ -7,7 +7,7 @@ import { LinkedList } from './linked-list'; import { Registry } from './registry'; import { Token } from './token'; import type { KnownPlugins } from '../known-plugins'; -import type { Grammar, GrammarToken, GrammarTokens } from '../types'; +import type { Grammar, GrammarToken, GrammarTokens, RegExpLike } from '../types'; import type { HookEnvMap } from './hooks'; import type { LinkedListHeadNode, LinkedListMiddleNode, LinkedListTailNode } from './linked-list'; import type { TokenStream } from './token'; @@ -481,8 +481,8 @@ function stringify(o: string | Token | TokenStream, language: string, hooks: Hoo return '<' + env.tag + ' class="' + env.classes.join(' ') + '"' + attributes + '>' + env.content + ''; } -function toGrammarToken(pattern: GrammarToken | RegExp): GrammarToken { - if (pattern.exec) { +function toGrammarToken(pattern: GrammarToken | RegExpLike): GrammarToken { + if (!pattern.pattern) { return { pattern }; } else { return pattern; diff --git a/src/shared/language-util.ts b/src/shared/language-util.ts index 6078ab570..be3ccc43c 100644 --- a/src/shared/language-util.ts +++ b/src/shared/language-util.ts @@ -1,5 +1,5 @@ import { rest, tokenize } from './symbols'; -import type { Grammar, GrammarToken, GrammarTokens } from '../types'; +import type { Grammar, GrammarToken, GrammarTokens, RegExpLike } from '../types'; // TODO: Update documentation @@ -119,8 +119,8 @@ function cloneGrammar(grammar: Grammar, id: string): Grammar { const visited = new Map(); - function cloneToken(value: GrammarToken | RegExp) { - if (value.exec) { + function cloneToken(value: GrammarToken | RegExpLike) { + if (!value.pattern) { return value; } else { const copy: GrammarToken = { pattern: value.pattern }; diff --git a/src/types.d.ts b/src/types.d.ts index 09fb6f89b..ded966f3b 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -62,6 +62,8 @@ export type StandardTokenName = export type TokenName = string & {} | StandardTokenName; +export type RegExpLike = RegExp & { readonly pattern?: never; }; + /** * The expansion of a simple `RegExp` literal to support additional properties. */ @@ -69,7 +71,7 @@ export interface GrammarToken { /** * The regular expression of the token. */ - pattern: RegExp + pattern: RegExpLike /** * If `true`, then the first capturing group of `pattern` will (effectively) behave as a lookbehind group meaning that the captured text will not be part of the matched text of the new token. * @@ -97,16 +99,9 @@ export interface GrammarToken { * each another. */ inside?: string | Grammar | null - /** - * A property to make the types {@link GrammarToken} and {@link RegExp} non-overlapping. - * - * Since {@link GrammarToken} requires `exec` to be `undefined` and {@link RegExp} requires it to be a function, - * there can be no object that is both a {@link GrammarToken} and a {@link RegExp}. - */ - readonly exec?: never; } -export type GrammarTokens = Partial>; +export type GrammarTokens = Partial>; export interface GrammarSymbols { /** * An optional grammar object that will be appended to this grammar.