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

    Function lex

    • Tokenizes OpenQASM source code into an array of tokens.

      This is the main entry point for lexical analysis. It automatically selects the appropriate lexer implementation based on the OpenQASM version and returns an array of tokens that can be consumed by the parser.

      Each token is represented as a tuple containing:

      • Token type: An enum value indicating the kind of token
      • Token value: The associated value (for literals, identifiers, operators)

      Parameters

      • qasm: string

        The OpenQASM source code to tokenize

      • Optionalcursor: number

        Starting position in the input string (defaults to 0)

      • Optionalversion: number | OpenQASMVersion

        OpenQASM version to use for lexing (defaults to 3.0)

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

      Array of token tuples [TokenType, value?]

      When an unsupported version is specified

      const tokens = lex('qubit[2] q; h q[0];', 0, 3);
      // Returns tokens using OpenQASM 3.0 syntax rules
      const tokens = lex('qreg q[2]; h q[0];', 0, 2);
      // Returns tokens using OpenQASM 2.0 syntax rules
      const code = 'OPENQASM 3.0; qubit q;';
      const tokens = lex(code, 12); // Start after "OPENQASM 3.0"