/** * OpenQASM 2.0 Lexical Analyzer * * This module implements the lexer for OpenQASM 2.0, which provides a simpler * token set compared to OpenQASM 3.0. The lexer focuses on basic quantum circuit * constructs without the advanced classical programming features of version 3.0. * * Key characteristics of OpenQASM 2.0 lexing: * - **Limited token set**: Basic quantum and classical registers only * - **Simple operators**: Basic arithmetic and comparison operators * - **No control flow**: No tokens for loops, conditionals, or functions * - **Gate-focused**: Emphasis on gate definitions and applications * - **Mathematical functions**: Built-in math functions (sin, cos, etc.) * * Supported constructs: * - Quantum registers (`qreg`) and classical registers (`creg`) * - Gate definitions and applications * - Measurements with arrow notation (`->`) * - Basic arithmetic expressions for gate parameters * - Include statements for library files * * @module * * @example OpenQASM 2.0 lexing * ```typescript * const lexer = new Lexer('qreg q[2]; h q[0]; measure q -> c;'); * const tokens = lexer.lex(); * // Produces tokens for register declaration, gate, and measurement * ``` */ import { Token } from "./token"; /** * OpenQASM 2.0 Lexical Analyzer * * A simpler lexer implementation focused on the core quantum circuit description * features of OpenQASM 2.0. This lexer handles the essential constructs needed * for basic quantum programming without the complexity of classical programming * language features. * * @example Basic OpenQASM 2.0 tokenization * ```typescript * const source = ` * OPENQASM 2.0; * include "qelib1.inc"; * qreg q[2]; * creg c[2]; * h q[0]; * cx q[0],q[1]; * measure q -> c; * `; * * const lexer = new Lexer(source); * const tokens = lexer.lex(); * ``` */ declare class Lexer { /** The string to lex. */ input: string; /** The lexer's current cursor location. */ cursor: number; /** * Creates a lexer. * @param input - The string to lex. * @param cursor - The starting cursor position. */ constructor(input: string, cursor?: number); /** * Verifies that all appropriate lines end with a semicolon. * @return A tuple of the status and if False, returns the problematic line. */ verifyInput: () => [boolean, number | null, string | null]; /** * Calling this method lexes the code represented by the provided string. * @return An array of tokens and their corresponding values. */ lex: () => Array<[Token, (number | string)?]>; /** * Reads a character and advances the cursor. * @param num - Optional cursor position modifier. */ readChar: (num?: number) => string; /** * Advances the cusor past the next comment. */ skipComment: () => void; /** * Determines whether the next character to process equals a given character. * @param c - The given character. * @return Whether the next character equals the given character. */ peekEq: (c: string) => boolean; /** * Reads a character without advancing the cursor. * @param index - Optional peek position offset. */ peek: () => string; /** * Reads a numeric value. * @return The numeric value as a string. */ readNumeric: () => string; /** * Reads an identifier. * @return The identifier as a string. */ readIdentifier: () => string; /** * Reads a string literal. * @param terminator - The literal's termination character. * @return The literal as a string. */ readStringLiteral: (terminator: string) => string; /** * Advances the cusor past the next block of whitespace. */ skipWhitespace: () => null; /** * Lexes the next token. * @return The next token and its corresponding value. */ nextToken: () => [Token, (number | string)?]; /** * Returns the line number where the current cursor is located. * @param cursor - The current cursor position in the input string. * @return The line number. */ getLineNumber: (cursor: number) => number; /** * Returns the current line of code where the cursor is located. * @param cursor - The current cursor position in the input string. * @return The specific line where the cursor is located. */ getCurrentLine: (cursor: number) => string; } export default Lexer;