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:

    • JavaScript
    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
    options
    CoreAsyncHostOptions

    The options used to configure the host.

    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
    boolean

    parser

    Gets the parser instance associated with this host.

    Declaration
    protected get parser(): Parser;
    Property Value
    Parser

    Methods

    createParser()

    Creates a Parser for this host.

    Declaration
    /** @virtual */
    protected createParser(): Parser;
    Returns
    Parser

    | Improve this Doc

    forFile(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
    StringAsyncHost

    Examples

    The following example shows how you can create a CoreAsyncHost for a string:

    • JavaScript
    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
    CoreAsyncHost

    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.

    cancelable
    Cancelable

    A cancelable object that can be used to abort the operation.

    Returns
    Promise<SourceFile | undefined>

    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
    string

    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
    string

    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.

    cancelable
    Cancelable

    An optional cancelable object that can be used to abort a long-running parse.

    Returns
    SourceFile

    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.

    cancelable
    Cancelable

    A cancelable object that can be used to abort the operation.

    Returns
    Promise<string | undefined>

    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.

    cancelToken
    CancelToken

    A cancellation token that can be used by the caller to abort the operation.

    Returns
    Promise<string | undefined>

    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
    void

    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
    void

    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
    string

    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
    string

    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
    string | undefined

    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
    string | undefined

    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.

    cancelable
    Cancelable

    A cancelable object that can be used to abort the operation.

    Returns
    Promise<void>

    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

    cancelToken
    CancelToken

    A cancellation token that can be used by the caller to abort the operation.

    Returns
    Promise<void>

    A Promise that is settled when the operation completes.

    • Improve this Doc
    Generated by DocFX