/
async-parser.ts
39 lines (33 loc) · 1.66 KB
/
async-parser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/******************************************************************************
* Copyright 2023 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/
import type { CancellationToken } from '../utils/cancellation.js';
import type { LangiumCoreServices } from '../services.js';
import type { AstNode } from '../syntax-tree.js';
import type { LangiumParser, ParseResult } from './langium-parser.js';
/**
* Async parser that allows to cancel the current parsing process.
* The sync parser implementation is blocking the event loop, which can become quite problematic for large files.
*
* Note that the default implementation is not actually async. It just wraps the sync parser in a promise.
* A real implementation would create worker threads or web workers to offload the parsing work.
*/
export interface AsyncParser {
parse<T extends AstNode>(text: string, cancelToken: CancellationToken): Promise<ParseResult<T>>;
}
/**
* Default implementation of the async parser. This implementation only wraps the sync parser in a promise.
*
* A real implementation would create worker threads or web workers to offload the parsing work.
*/
export class DefaultAsyncParser implements AsyncParser {
protected readonly syncParser: LangiumParser;
constructor(services: LangiumCoreServices) {
this.syncParser = services.parser.LangiumParser;
}
parse<T extends AstNode>(text: string): Promise<ParseResult<T>> {
return Promise.resolve(this.syncParser.parse<T>(text));
}
}