qasm-ts - v2.0.0
    Preparing search index...

    Class default

    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.

    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();
    Index

    Constructors

    • Creates a lexer.

      Parameters

      • input: string

        The string to lex.

      • cursor: number = 0

        The starting cursor position.

      Returns default

    Methods

    • Returns the current line of code where the cursor is located.

      Parameters

      • cursor: number

        The current cursor position in the input string.

      Returns string

      The specific line where the cursor is located.

    • Returns the line number where the current cursor is located.

      Parameters

      • cursor: number

        The current cursor position in the input string.

      Returns number

      The line number.

    • Calling this method lexes the code represented by the provided string.

      Returns [Token, (string | number)?][]

      An array of tokens and their corresponding values.

    • Lexes the next token.

      Returns [Token, (string | number)?]

      The next token and its corresponding value.

    • Determines whether the next character to process equals a given character.

      Parameters

      • c: string

        The given character.

      Returns boolean

      Whether the next character equals the given character.

    • Reads a character and advances the cursor.

      Parameters

      • num: number = 1

        Optional cursor position modifier.

      Returns string

    • Reads an identifier.

      Returns string

      The identifier as a string.

    • Reads a numeric value.

      Returns string

      The numeric value as a string.

    • Reads a string literal.

      Parameters

      • terminator: string

        The literal's termination character.

      Returns string

      The literal as a string.

    • Advances the cusor past the next block of whitespace.

      Returns null

    • Verifies that all appropriate lines end with a semicolon.

      Returns [boolean, number, string]

      A tuple of the status and if False, returns the problematic line.

    Properties

    cursor: number

    The lexer's current cursor location.

    input: string

    The string to lex.