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

    Class default

    OpenQASM 3.0 Lexical Analyzer

    The main lexer class that processes OpenQASM 3.0 source code character by character and produces a stream of tokens for the parser to consume.

    The lexer maintains state including:

    • Current cursor position in the input
    • Input validation status
    • Error reporting context
    const source = `
    OPENQASM 3.0;
    include "stdgates.inc";
    qubit[2] q;
    h q[0];
    cx q[0], q[1];
    `;

    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.

    • Retruns an identifier or gate modifier.

      Parameters

      • keyword: string

      Returns [Token, (string | number)?]

    • 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.

    • Reds a keyword or identifier. If the character sequence matches a keyword, returns the corresponding token. Otherwise, treats the sequence as an identifier.

      Parameters

      • char: string

        The first character of the keyword or identifier.

      Returns [Token, string]

      The corresponding token or identifier.

    • 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 cursor past a multiline comment.

      Returns void

    • 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.