mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-13 12:29:42 +02:00
refactor: Make parser.parse async
This commit is contained in:
@@ -121,7 +121,7 @@ export type DrawDefinition = (
|
|||||||
) => void | Promise<void>;
|
) => void | Promise<void>;
|
||||||
|
|
||||||
export interface ParserDefinition {
|
export interface ParserDefinition {
|
||||||
parse: (text: string) => void;
|
parse: (text: string) => void | Promise<void>;
|
||||||
parser?: { yy: DiagramDB };
|
parser?: { yy: DiagramDB };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,35 +1,34 @@
|
|||||||
import type { LangiumParser, ParseResult } from 'langium';
|
import type { LangiumParser, ParseResult } from 'langium';
|
||||||
import type { Info, Packet } from './index.js';
|
import type { Info, Packet } from './index.js';
|
||||||
import { createInfoServices, createPacketServices } from './language/index.js';
|
|
||||||
|
|
||||||
export type DiagramAST = Info | Packet;
|
export type DiagramAST = Info | Packet;
|
||||||
|
|
||||||
const parsers: Record<string, LangiumParser> = {};
|
const parsers: Record<string, LangiumParser> = {};
|
||||||
|
|
||||||
const initializers = {
|
const initializers = {
|
||||||
info: () => {
|
info: async () => {
|
||||||
// Will have to make parse async to use this. Can try later...
|
const { createInfoServices } = await import('./language/info/index.js');
|
||||||
// const { createInfoServices } = await import('./language/info/index.js');
|
|
||||||
const parser = createInfoServices().Info.parser.LangiumParser;
|
const parser = createInfoServices().Info.parser.LangiumParser;
|
||||||
parsers['info'] = parser;
|
parsers['info'] = parser;
|
||||||
},
|
},
|
||||||
packet: () => {
|
packet: async () => {
|
||||||
|
const { createPacketServices } = await import('./language/packet/index.js');
|
||||||
const parser = createPacketServices().Packet.parser.LangiumParser;
|
const parser = createPacketServices().Packet.parser.LangiumParser;
|
||||||
parsers['packet'] = parser;
|
parsers['packet'] = parser;
|
||||||
},
|
},
|
||||||
} as const;
|
} as const;
|
||||||
export function parse(diagramType: 'info', text: string): Info;
|
export async function parse(diagramType: 'info', text: string): Promise<Info>;
|
||||||
export function parse(diagramType: 'packet', text: string): Packet;
|
export async function parse(diagramType: 'packet', text: string): Promise<Packet>;
|
||||||
export function parse<T extends DiagramAST>(
|
export async function parse<T extends DiagramAST>(
|
||||||
diagramType: keyof typeof initializers,
|
diagramType: keyof typeof initializers,
|
||||||
text: string
|
text: string
|
||||||
): T {
|
): Promise<T> {
|
||||||
const initializer = initializers[diagramType];
|
const initializer = initializers[diagramType];
|
||||||
if (!initializer) {
|
if (!initializer) {
|
||||||
throw new Error(`Unknown diagram type: ${diagramType}`);
|
throw new Error(`Unknown diagram type: ${diagramType}`);
|
||||||
}
|
}
|
||||||
if (!parsers[diagramType]) {
|
if (!parsers[diagramType]) {
|
||||||
initializer();
|
await initializer();
|
||||||
}
|
}
|
||||||
const parser: LangiumParser = parsers[diagramType];
|
const parser: LangiumParser = parsers[diagramType];
|
||||||
const result: ParseResult<T> = parser.parse<T>(text);
|
const result: ParseResult<T> = parser.parse<T>(text);
|
||||||
|
Reference in New Issue
Block a user