Compare commits
2 Commits
8d15ad986d
...
ffab2edc5b
Author | SHA1 | Date |
---|---|---|
RunDevelopment | ffab2edc5b | |
RunDevelopment | 54ef4cd22f |
|
@ -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';
|
||||
|
@ -109,7 +109,7 @@ export class Prism {
|
|||
|
||||
this.hooks.run('after-highlight', env);
|
||||
this.hooks.run('complete', env);
|
||||
callback && callback(env.element);
|
||||
callback?.(env.element);
|
||||
};
|
||||
|
||||
this.hooks.run('before-sanity-check', env);
|
||||
|
@ -122,7 +122,7 @@ export class Prism {
|
|||
|
||||
if (!env.code) {
|
||||
this.hooks.run('complete', env);
|
||||
callback && callback(env.element);
|
||||
callback?.(env.element);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
|
|
@ -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<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 };
|
||||
|
|
|
@ -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<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.
|
||||
|
|
Loading…
Reference in New Issue