/** * Copyright 2017 Matt Acosta * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { ErrorCode } from '../diagnostics/ErrorCode.Generated'; import { ISourceText } from '../text/ISourceText'; import { SyntaxDiagnostic } from '../diagnostics/SyntaxDiagnostic'; /** * Defines an interface for objects that can generate tokens by scanning source * text. * * @template T, TState */ export interface ILexer { /** * Scans the source text until a token is found. * * @param {TState} state * The scanning mode used by the lexer. * * @return {T} * An object containing token information. */ lex(state: TState): T; /** * Sets the current lexer position. * * @param {number} offset * The offset into the text. */ setPosition(offset: number): void; /** * Sets the source text to tokenize. */ setText(text: ISourceText): void; } /** * Provides a base class for language tokenizers. */ export declare abstract class LexerBase implements ILexer { /** * A list of diagnostics found while attempting to scan for a token. */ protected diagnostics: SyntaxDiagnostic[]; /** * The location within the text to stop scanning. */ protected end: number; /** * The current location of the scanner. */ protected offset: number; /** * The location within the text to start scanning. */ protected start: number; /** * The current lexing state of the scanner. */ protected state: TState; /** * The text to scan. */ protected text: ISourceText; /** * Constructs a `LexerBase` object. * * @param {ISourceText=} text * The source text to tokenize. * @param {TState} defaultState * The default scanning mode when starting to scan text. */ constructor(text: ISourceText, defaultState: TState); /** * @inheritDoc */ abstract lex(state: TState): T; /** * @inheritDoc */ setPosition(offset: number): void; /** * @inheritDoc */ abstract setText(text: ISourceText): void; /** * Adds a new diagnostic to the list of diagnostics found during a scan. */ protected addError(relativeOffset: number, width: number, code: ErrorCode, ...args: any[]): void; /** * Creates a diagnostic for a token. */ protected createDiagnostic(relativeOffset: number, width: number, code: ErrorCode, ...args: any[]): SyntaxDiagnostic; /** * Reads the character code at the given location, but does not consume it. * * @param {number} offset * An offset into the scannable text. * * @return {number} * The character code at the specified offset, or -1 if the offset was * outside the allowed scan range. */ protected peek(offset: number): number; /** * Sets the starting and ending locations of the text to tokenize. * * @param {number} start * The offset within the text to start tokenizing. * @param {number} end * The offset within the text to stop tokenizing. */ protected setBounds(start: number, end: number): void; /** * Determines if text at the given position starts with the specified string. * * @param {string} value * The text to search for. * @param {boolean=} caseInsensitive * If a match should be case-insensitive. Defaults to `true`. * * @return {boolean} * If the scannable text starts with the given string `true`, otherwise * `false`. */ protected startsWith(value: string, caseInsensitive?: boolean): boolean; }