UNPKG

consys

Version:

consys is a flexible tool to evaluate models using generic and readable constraints.

111 lines (110 loc) 2.83 kB
import Token from './Token'; /** * The lexer is responsible for reading in source code of the dsl and turn it into a list of meaningful tokens. */ export default class Lexer { private static readonly singleCharTokens; private static readonly keywords; /** * Checks if the given symbol is a digit from 0-9. * * @param symbol character to check * @private */ private static isDigit; /** * Checks if the given symbol is an alphabetic lower- or uppercase value. * * @param symbol character to check * @private */ private static isAlpha; /** * Checks if the given symbol is either a digit or alphabetic. * * @param symbol character to check * @private */ private static isAlphaNumeric; private readonly tokens; private readonly source; private readonly offset; private start; private current; /** * Creates a new lexer instance for a given string of source code * * @param source source code * @param offset index offset into the source code */ constructor(source: string, offset?: number); /** * Scans the source code and turns it into a list of tokens. */ scan(): Token[]; /** * Scans a single token on the current index. * @private */ private scanToken; /** * Should the lexer come to a single quote char, it interprets as a string the next symbols until the next quote. * @private */ private handleString; /** * Scans a number token. * @private */ private handleNumber; /** * Scans an identifier token. * @private */ private handleIdentifier; /** * Checks if the lexer is done scanning. * @private */ private isAtEnd; /** * Consume the current symbol. * @private */ private advance; /** * Consume the current symbol if it matches a given character, else return false. * @param expected expected character * @private */ private match; /** * Returns the current symbol. * @private */ private peek; /** * Returns the symbol after the current symbol. * @private */ private peekNext; /** * Push a new token to the output list. * * @param type token type * @param literal token value * @param begin start index in source * @param end end index in source * @param position position in source (may be different from begin) * @private */ private emitToken; /** * Prints an error message to the console, then throws an error. * * @param message error message * @param position position in source where the error occurred * @private */ private errorOnPosition; }