Skip to content

Commit

Permalink
Improved types
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Feb 10, 2024
1 parent 8d15ad9 commit 54ef4cd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/core/prism.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -481,8 +481,8 @@ function stringify(o: string | Token | TokenStream, language: string, hooks: Hoo
return '<' + env.tag + ' class="' + env.classes.join(' ') + '"' + attributes + '>' + env.content + '</' + env.tag + '>';
}

function toGrammarToken(pattern: GrammarToken | RegExp): GrammarToken {
if (pattern.exec) {
function toGrammarToken(pattern: GrammarToken | RegExpLike): GrammarToken {
if (!pattern.pattern) {
return { pattern };
} else {
return pattern;
Expand Down
6 changes: 3 additions & 3 deletions 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

Expand Down Expand Up @@ -119,8 +119,8 @@ function cloneGrammar(grammar: Grammar, id: string): Grammar {

const visited = new Map<Grammar, Grammar>();

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 };
Expand Down
13 changes: 4 additions & 9 deletions src/types.d.ts
Expand Up @@ -62,14 +62,16 @@ 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.
*/
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.
*
Expand Down Expand Up @@ -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<Record<TokenName, RegExp | GrammarToken | (RegExp | GrammarToken)[]>>;
export type GrammarTokens = Partial<Record<TokenName, RegExpLike | GrammarToken | (RegExpLike | GrammarToken)[]>>;
export interface GrammarSymbols {
/**
* An optional grammar object that will be appended to this grammar.
Expand Down

0 comments on commit 54ef4cd

Please sign in to comment.