Class CoreSyncHost
Package: grammarkdown
A Host is a user-provided service that indicates how various Grammarkdown services can interact with a file system. The CoreSyncHost
class provides the API surface that Grammarkdown uses to interact with a host that is able to access the file system synchronously.
Examples
The following example shows how you can create a CoreSyncHost from a Map
:
const files = new Map([
["a.grammar", "..."],
["b.grammar", "..."]
]);
const host = new CoreSyncHost({
ignoreCase: false,
useBuiltinGrammers: false,
resolveFile: file => file,
readFileSync: file => files.get(file)
});
Constructors
constructor(options, hostFallback)
Constructs a new instance of the CoreSyncHost
class
Declaration
constructor(options: CoreSyncHostOptions, hostFallback?: CoreSyncHost);
Parameters
- hostFallback
- CoreSyncHost
An optional host to use as a fallback for operations not supported by this host.
Methods
| Improve this DocforFile(content, file, hostFallback)
Creates a StringSyncHost for the provided content.
Declaration
static forFile(content: string, file?: string, hostFallback?: CoreSyncHost): StringSyncHost;
Parameters
- content
- string
The content of the file.
- file
- string
The file name for the content.
- hostFallback
- CoreSyncHost
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 CoreSyncHost for a string
:
const content = "...";
const host = CoreSyncHost.forFile(content);
from(options, hostFallback)
Creates a CoreSyncHost
.
Declaration
static from(options: CoreSyncHostOptions, hostFallback?: CoreSyncHost): CoreSyncHost;
Parameters
- hostFallback
- CoreSyncHost
An optional host to use as a fallback for operations not supported by this host.
Returns
getSourceFileSync(file, cancelable)
Reads and parses a source file from the host.
Declaration
getSourceFileSync(file: string, cancelable?: Cancelable): SourceFile | undefined;
Parameters
- file
- string
The path to the file.
Returns
The parsed SourceFile of the file if the file could be read; otherwise, undefined
.
getSourceFileSync(file, cancelable)
Warning
Deprecated
since 2.1.0 - `prex.CancellationToken` may no longer be accepted in future releases. Please use a token that implements `@esfx/cancelable.Cancelable`Reads and parses a source file from the host.
Declaration
getSourceFileSync(file: string, cancelable?: CancellationToken | Cancelable): SourceFile | undefined;
Parameters
- file
- string
The path to the file.
- cancelable
- CancellationToken | Cancelable
A cancelable object that can be used to abort the operation.
Returns
The parsed SourceFile of the file if the file could be read; otherwise, undefined
.
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
/** @override */
protected normalizeFileCore(file: string): string;
Parameters
- file
- string
The file path.
Returns
readFileSync(file, cancelable)
Reads a file from the host.
Declaration
readFileSync(file: string, cancelable?: Cancelable): string | undefined;
Parameters
- file
- string
The path to the file.
Returns
A string
containing the content if the file could be read; otherwise, undefined
.
readFileSync(file, cancelable)
Warning
Deprecated
since 2.1.0 - `prex.CancellationToken` may no longer be accepted in future releases. Please use a token that implements `@esfx/cancelable.Cancelable`Reads a file from the host.
Declaration
readFileSync(file: string, cancelable?: CancellationToken | Cancelable): string | undefined;
Parameters
- file
- string
The path to the file.
- cancelable
- CancellationToken | Cancelable
A cancelable object that can be used to abort the operation.
Returns
A string
containing the content if the file could be read; otherwise, undefined
.
readFileSyncCore(file, cancelToken)
When overridden in a derived class, reads a file from the host.
Declaration
/** @virtual */
protected readFileSyncCore(file: string, cancelToken?: CancelToken): string | undefined;
Parameters
- file
- string
The path to the file.
Returns
A string
containing the content if the file could be read; otherwise, undefined
.
registerKnownGrammarCore(name, file)
When overridden in a derived clas, registers a known grammar for use with @import
directives.
Declaration
/** @override */
protected registerKnownGrammarCore(name: string, file: string): void;
Parameters
- name
- string
The name for the grammar.
- file
- string
The file path of the grammar.
Returns
resolveFileCore(file, referer)
When overridden in a derived class, resolves the full path of a file relative to the provided referer.
Declaration
/** @override */
protected resolveFileCore(file: string, referer?: string): string;
Parameters
- file
- string
The path to the requested file.
- referer
- string
An optional path indicating the file from which the path should be resolved.
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
/** @override */
protected resolveKnownGrammarCore(name: string): string | undefined;
Parameters
- name
- string
The name of the grammar.
Returns
writeFileSync(file, text, cancelable)
Writes a file to the host.
Declaration
writeFileSync(file: string, text: string, cancelable?: Cancelable): void;
Parameters
- file
- string
The path to the file.
- text
- string
The contents of the file.
Returns
writeFileSync(file, text, cancelable)
Warning
Deprecated
since 2.1.0 - `prex.CancellationToken` may no longer be accepted in future releases. Please use a token that implements `@esfx/cancelable.Cancelable`Writes a file to the host.
Declaration
writeFileSync(file: string, text: string, cancelable?: CancellationToken | Cancelable): void;
Parameters
- file
- string
The path to the file.
- text
- string
The contents of the file.
- cancelable
- CancellationToken | Cancelable
A cancelable object that can be used to abort the operation.
Returns
writeFileSyncCore(file, content, cancelToken)
When overridden in a derived class, writes a file to the host.
Declaration
/** @virtual */
protected writeFileSyncCore(file: string, content: string, cancelToken?: CancelToken): void;
Parameters
- file
- string
The path to the file.
- content
- string
Returns