Class CoreAsyncHost
Package: grammarkdown
A Host is a user-provided service that indicates how various Grammarkdown services can interact with a file system. The CoreAsyncHost
class provides the API surface that Grammarkdown uses to interact with a host that is able to access the file system asynchronously.
Examples
The following example shows how you can create a CoreAsyncHost from a Map
:
const files = new Map([
["a.grammar", "..."],
["b.grammar", "..."]
]);
const host = new CoreAsyncHost({
ignoreCase: false,
useBuiltinGrammers: false,
resolveFile: file => file,
readFile: file => files.get(file)
});
Constructors
constructor(options, hostFallback)
Constructs a new instance of the CoreAsyncHost
class
Declaration
constructor(options: CoreAsyncHostOptions, hostFallback?: CoreAsyncHost);
Parameters
- hostFallback
- CoreAsyncHost
An optional host to use as a fallback for operations not supported by this host.
Properties
ignoreCase
Indicates whether comparisons for this host should be case insensitive.
Declaration
get ignoreCase(): boolean;
Property Value
parser
Gets the parser instance associated with this host.
Declaration
protected get parser(): Parser;
Property Value
Methods
createParser()
Creates a Parser for this host.
Declaration
/** @virtual */
protected createParser(): Parser;
Returns
| Improve this DocforFile(content, file, hostFallback)
Creates a StringAsyncHost for the provided content.
Declaration
static forFile(content: PromiseLike<string> | string, file?: string, hostFallback?: CoreAsyncHost): StringAsyncHost;
Parameters
- content
- PromiseLike<string> | string
The content of the file.
- file
- string
The file name for the content.
- hostFallback
- CoreAsyncHost
An optional host to use as a fallback for operations not supported by this host.
Returns
Examples
The following example shows how you can create a CoreAsyncHost for a string
:
const content = "...";
const host = CoreAsyncHost.forFile(content);
from(custom, hostFallback)
Creates a CoreAsyncHost
.
Declaration
static from(custom: CoreAsyncHostOptions, hostFallback?: CoreAsyncHost): CoreAsyncHost;
Parameters
- custom
- CoreAsyncHostOptions
- hostFallback
- CoreAsyncHost
An optional host to use as a fallback for operations not supported by this host.
Returns
getSourceFile(file, cancelable)
Reads and parses a source file from the host.
Declaration
getSourceFile(file: string, cancelable?: Cancelable): Promise<SourceFile | undefined>;
Parameters
- file
- string
The path to the file.
Returns
A Promise
for either the parsed SourceFile of the file if the file could be read, or undefined
if it could not be read.
normalizeFile(file)
Normalize a file path's string representation for use as a key based on the case sensitivity of the host.
Declaration
normalizeFile(file: string): string;
Parameters
- file
- string
The file path.
Returns
normalizeFileCore(file)
When overridden in a derived class, normalizes a file path's string representation for use as a key based on the case sensitivity of the host.
Declaration
/** @virtual */
protected normalizeFileCore(file: string): string;
Parameters
- file
- string
The file path.
Returns
parseSourceFile(file, text, cancelable)
Parse a source file.
Declaration
parseSourceFile(file: string, text: string, cancelable?: Cancelable): SourceFile;
Parameters
- file
- string
The path to the source file.
- text
- string
The text of the source file.
Returns
readFile(file, cancelable)
Reads a file from the host.
Declaration
readFile(file: string, cancelable?: Cancelable): Promise<string | undefined>;
Parameters
- file
- string
The path to the file.
Returns
A Promise
for either a string
containing the content if the file could be read, or undefined
if the file could not be read.
readFileCore(file, cancelToken)
When overridden in a derived class, reads a file from the host.
Declaration
/** @virtual */
protected readFileCore(file: string, cancelToken?: CancelToken): Promise<string | undefined>;
Parameters
- file
- string
The path to the file.
Returns
A Promise
for either a string
containing the content if the file could be read, or undefined
if the file could not be read.
registerKnownGrammar(name, file)
Registers a known grammar for use with @import
directives.
Declaration
registerKnownGrammar(name: string, file: string): void;
Parameters
- name
- string
The name for the grammar.
- file
- string
The file path of the grammar.
Returns
registerKnownGrammarCore(name, file)
When overridden in a derived clas, registers a known grammar for use with @import
directives.
Declaration
/** @virtual */
protected registerKnownGrammarCore(name: string, file: string): void;
Parameters
- name
- string
The name for the grammar.
- file
- string
The file path of the grammar.
Returns
resolveFile(file, referrer)
Resolve the full path of a file relative to the provided referrer.
Declaration
resolveFile(file: string, referrer?: string): string;
Parameters
- file
- string
The path to the requested file.
- referrer
- string
An optional path indicating the file from which the path should be resolved.
Returns
resolveFileCore(file, referrer)
When overridden in a derived class, resolves the full path of a file relative to the provided referrer.
Declaration
/** @virtual */
protected resolveFileCore(file: string, referrer?: string): string;
Parameters
- file
- string
The path to the requested file.
- referrer
- string
An optional path indicating the file from which the path should be resolved.
Returns
resolveKnownGrammar(name)
Returns the path for a known or built-in grammar based on its name (i.e., "es2015"
, etc.)
Declaration
resolveKnownGrammar(name: string): string | undefined;
Parameters
- name
- string
The name of the grammar.
Returns
resolveKnownGrammarCore(name)
When overridden in a derived class, returns the path for a known or built-in grammar based on its name (i.e., "es2015"
, etc.)
Declaration
/** @virtual */
protected resolveKnownGrammarCore(name: string): string | undefined;
Parameters
- name
- string
The name of the grammar.
Returns
writeFile(file, text, cancelable)
Writes a file to the host.
Declaration
writeFile(file: string, text: string, cancelable?: Cancelable): Promise<void>;
Parameters
- file
- string
The path to the file.
- text
- string
The contents of the file.
Returns
A Promise
that is settled when the operation completes.
writeFileCore(file, content, cancelToken)
When overridden in a derived class, writes a file to the host.
Declaration
/** @virtual */
protected writeFileCore(file: string, content: string, cancelToken?: CancelToken): Promise<void>;
Parameters
- file
- string
The path to the file.
- content
- string
Returns
A Promise
that is settled when the operation completes.