qasm-ts 2.1.2 → 2.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.d.ts ADDED
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Main parsing functions for qasm-ts - the primary entry points for parsing OpenQASM code
3
+ * @module Main Functions
4
+ */
5
+ import type * as qasm2 from "./qasm2/ast";
6
+ import type * as qasm3 from "./qasm3/ast";
7
+ import { OpenQASMVersion, OpenQASMMajorVersion } from "./version";
8
+ /**
9
+ * Parses OpenQASM code from a string and returns the abstract syntax tree.
10
+ *
11
+ * This is the primary entry point for parsing OpenQASM code. It handles both
12
+ * OpenQASM 2.0 and 3.0 syntax, automatically selecting the appropriate lexer
13
+ * and parser based on the version parameter.
14
+ *
15
+ * @group Main Functions
16
+ * @param qasm - The OpenQASM code string to parse
17
+ * @param version - The OpenQASM version to use (defaults to 3.0)
18
+ * @param verbose - Whether to include class names in the output (defaults to false)
19
+ * @param stringify - Whether to return stringified JSON (defaults to false)
20
+ * @returns The corresponding AST as an array of nodes, or stringified JSON if stringify is true
21
+ *
22
+ * @example Basic OpenQASM 3.0 parsing
23
+ * ```typescript
24
+ * import { parseString } from 'qasm-ts';
25
+ *
26
+ * const qasmCode = `
27
+ * OPENQASM 3.0;
28
+ * include "stdgates.inc";
29
+ * qubit[2] q;
30
+ * h q[0];
31
+ * cx q[0], q[1];
32
+ * `;
33
+ *
34
+ * const ast = parseString(qasmCode);
35
+ * console.log(ast);
36
+ * ```
37
+ *
38
+ * @example OpenQASM 2.0 parsing
39
+ * ```typescript
40
+ * const qasm2Code = `
41
+ * OPENQASM 2.0;
42
+ * include "qelib1.inc";
43
+ * qreg q[2];
44
+ * creg c[2];
45
+ * h q[0];
46
+ * cx q[0],q[1];
47
+ * measure q -> c;
48
+ * `;
49
+ *
50
+ * const ast = parseString(qasm2Code, 2);
51
+ * ```
52
+ *
53
+ * @example Verbose output with class names
54
+ * ```typescript
55
+ * const ast = parseString(qasmCode, 3, true);
56
+ * // Output will include __className__ properties for each node
57
+ * ```
58
+ */
59
+ export declare function parseString(qasm: string, version?: number | OpenQASMVersion | OpenQASMMajorVersion, verbose?: boolean, stringify?: boolean): string | qasm2.AstNode[] | qasm3.AstNode[];
60
+ /**
61
+ * Parses OpenQASM code from a file and returns the abstract syntax tree.
62
+ *
63
+ * @group Main Functions
64
+ * @param file - The path to the .qasm file to parse
65
+ * @param version - The OpenQASM version to use (defaults to 3.0)
66
+ * @param verbose - Whether to include class names in the output (defaults to false)
67
+ * @param stringify - Whether to return stringified JSON (defaults to false)
68
+ * @returns The corresponding AST as an array of nodes, or stringified JSON if stringify is true
69
+ *
70
+ * @example Parse a QASM file
71
+ * ```typescript
72
+ * import { parseFile } from 'qasm-ts';
73
+ *
74
+ * // Parse OpenQASM 3.0 file
75
+ * const ast = parseFile('./my-circuit.qasm');
76
+ *
77
+ * // Parse OpenQASM 2.0 file
78
+ * const ast2 = parseFile('./legacy-circuit.qasm', 2);
79
+ * ```
80
+ *
81
+ * @example Parse with verbose output
82
+ * ```typescript
83
+ * const ast = parseFile('./circuit.qasm', 3, true);
84
+ * // Includes detailed class information for each AST node
85
+ * ```
86
+ */
87
+ export declare function parseFile(file: string, version?: number | OpenQASMVersion | OpenQASMMajorVersion, verbose?: boolean, stringify?: boolean): string | qasm2.AstNode[] | qasm3.AstNode[];
package/dist/main.js CHANGED
@@ -1,15 +1,7 @@
1
- "use strict";
2
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.parseString = parseString;
5
- exports.parseFile = parseFile;
6
- /**
7
- * Main parsing functions for qasm-ts - the primary entry points for parsing OpenQASM code
8
- * @module Main Functions
9
- */
10
- var fs = require("fs");
11
- var lexer_1 = require("./lexer");
12
- var parser_1 = require("./parser");
2
+ import * as fs from "fs";
3
+ import { lex } from "./lexer";
4
+ import { parse } from "./parser";
13
5
  /**
14
6
  * Parses OpenQASM code from a string and returns the abstract syntax tree.
15
7
  *
@@ -61,10 +53,10 @@ var parser_1 = require("./parser");
61
53
  * // Output will include __className__ properties for each node
62
54
  * ```
63
55
  */
64
- function parseString(qasm, version, verbose, stringify) {
65
- var ast;
66
- var tokens = (0, lexer_1.lex)(qasm, undefined, version);
67
- ast = (0, parser_1.parse)(tokens, version);
56
+ export function parseString(qasm, version, verbose, stringify) {
57
+ let ast;
58
+ const tokens = lex(qasm, undefined, version);
59
+ ast = parse(tokens, version);
68
60
  if (verbose === true) {
69
61
  if (stringify === true) {
70
62
  ast = JSON.stringify(getDetailedOutput(ast), null, 2);
@@ -105,7 +97,7 @@ function parseString(qasm, version, verbose, stringify) {
105
97
  * // Includes detailed class information for each AST node
106
98
  * ```
107
99
  */
108
- function parseFile(file, version, verbose, stringify) {
100
+ export function parseFile(file, version, verbose, stringify) {
109
101
  return parseString(fs.readFileSync(file, "utf8"), version, verbose, stringify);
110
102
  }
111
103
  /**
@@ -117,10 +109,9 @@ function getDetailedOutput(object) {
117
109
  return object.map(getDetailedOutput);
118
110
  }
119
111
  else if (object !== null && typeof object === "object") {
120
- var result = {};
112
+ const result = {};
121
113
  result["__className__"] = object.constructor.name;
122
- for (var _i = 0, _a = Object.entries(object); _i < _a.length; _i++) {
123
- var _b = _a[_i], key = _b[0], value = _b[1];
114
+ for (const [key, value] of Object.entries(object)) {
124
115
  result[key] = getDetailedOutput(value);
125
116
  }
126
117
  return result;
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Main parser interface for generating AST from tokens
3
+ *
4
+ * The parser is responsible for transforming a stream of tokens into an Abstract
5
+ * Syntax Tree (AST) that represents the structure of the OpenQASM program. It acts
6
+ * as a dispatcher, selecting the appropriate version-specific parser based on the
7
+ * OpenQASM version being used.
8
+ *
9
+ * The parsing process follows these steps:
10
+ * 1. Receive tokenized input from the lexer
11
+ * 2. Determine OpenQASM version (2.0 or 3.0)
12
+ * 3. Select appropriate parser implementation
13
+ * 4. Generate version-specific AST nodes
14
+ *
15
+ * The specific Parser implementations can be found at:
16
+ * - {@link Qasm3Parser}
17
+ * - {@link Qasm2Parser}
18
+ *
19
+ * @module Parsing
20
+ *
21
+ * @example Basic parsing workflow
22
+ * ```typescript
23
+ * import { lex } from './lexer';
24
+ * import { parse } from './parser';
25
+ *
26
+ * const qasmCode = 'OPENQASM 3.0; h q[0];';
27
+ * const tokens = lex(qasmCode, undefined, 3);
28
+ * const ast = parse(tokens, 3);
29
+ * console.log(ast); // Array of AST nodes
30
+ * ```
31
+ *
32
+ * @example Version-specific parsing
33
+ * ```typescript
34
+ * // Parse as OpenQASM 2.0
35
+ * const ast2 = parse(tokens, 2);
36
+ *
37
+ * // Parse as OpenQASM 3.0
38
+ * const ast3 = parse(tokens, 3);
39
+ *
40
+ * // Use OpenQASMVersion object
41
+ * const version = new OpenQASMVersion(3, 0);
42
+ * const ast = parse(tokens, version);
43
+ * ```
44
+ */
45
+ import { Token as Qasm2Token } from "./qasm2/token";
46
+ import { Token as Qasm3Token } from "./qasm3/token";
47
+ import { OpenQASMVersion, OpenQASMMajorVersion } from "./version";
48
+ /**
49
+ * Parses an array of tokens into an Abstract Syntax Tree (AST).
50
+ *
51
+ * This is the main entry point for parsing tokenized OpenQASM code. It automatically
52
+ * selects the appropriate parser implementation based on the specified version and
53
+ * returns an AST that can be used for further analysis, compilation, or execution.
54
+ *
55
+ * @group Parsing
56
+ * @param tokens - Array of tokens generated by the lexer
57
+ * @param version - OpenQASM version to use for parsing (defaults to 3.0)
58
+ * @returns Abstract Syntax Tree representing the parsed program
59
+ * @throws {UnsupportedOpenQASMVersionError} When an unsupported version is specified
60
+ *
61
+ * @example Parse OpenQASM 3.0 tokens
62
+ * ```typescript
63
+ * const tokens = lex('OPENQASM 3.0; qubit q; h q;');
64
+ * const ast = parse(tokens, 3);
65
+ * // Returns array of OpenQASM 3.0 AST nodes
66
+ * ```
67
+ *
68
+ * @example Parse OpenQASM 2.0 tokens
69
+ * ```typescript
70
+ * const tokens = lex('OPENQASM 2.0; qreg q[1]; h q[0];');
71
+ * const ast = parse(tokens, 2);
72
+ * // Returns array of OpenQASM 2.0 AST nodes
73
+ * ```
74
+ */
75
+ export declare function parse(tokens: Array<[Qasm2Token | Qasm3Token, (number | string)?]>, version?: OpenQASMVersion | OpenQASMMajorVersion | number): import("./qasm2/ast").AstNode[];
package/dist/parser.js CHANGED
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /**
3
2
  * Main parser interface for generating AST from tokens
4
3
  *
@@ -43,12 +42,10 @@
43
42
  * const ast = parse(tokens, version);
44
43
  * ```
45
44
  */
46
- Object.defineProperty(exports, "__esModule", { value: true });
47
- exports.parse = parse;
48
- var parser_1 = require("./qasm2/parser");
49
- var parser_2 = require("./qasm3/parser");
50
- var version_1 = require("./version");
51
- var errors_1 = require("./errors");
45
+ import { default as Qasm2Parser } from "./qasm2/parser";
46
+ import { default as Qasm3Parser } from "./qasm3/parser";
47
+ import { OpenQASMVersion, OpenQASMMajorVersion } from "./version";
48
+ import { UnsupportedOpenQASMVersionError } from "./errors";
52
49
  /**
53
50
  * Parses an array of tokens into an Abstract Syntax Tree (AST).
54
51
  *
@@ -76,49 +73,49 @@ var errors_1 = require("./errors");
76
73
  * // Returns array of OpenQASM 2.0 AST nodes
77
74
  * ```
78
75
  */
79
- function parse(tokens, version) {
80
- var parser;
81
- var castTokens;
82
- if (version instanceof version_1.OpenQASMVersion) {
76
+ export function parse(tokens, version) {
77
+ let parser;
78
+ let castTokens;
79
+ if (version instanceof OpenQASMVersion) {
83
80
  switch (version.major) {
84
- case version_1.OpenQASMMajorVersion.Version2:
81
+ case OpenQASMMajorVersion.Version2:
85
82
  castTokens = tokens;
86
- parser = new parser_1.default(castTokens);
83
+ parser = new Qasm2Parser(castTokens);
87
84
  break;
88
- case version_1.OpenQASMMajorVersion.Version3:
85
+ case OpenQASMMajorVersion.Version3:
89
86
  castTokens = tokens;
90
- parser = new parser_1.default(castTokens);
87
+ parser = new Qasm2Parser(castTokens);
91
88
  break;
92
89
  default:
93
- throw new errors_1.UnsupportedOpenQASMVersionError("Unsupported OpenQASM version detected: ".concat(version.major));
90
+ throw new UnsupportedOpenQASMVersionError(`Unsupported OpenQASM version detected: ${version.major}`);
94
91
  }
95
92
  }
96
93
  else if (typeof version === "number") {
97
94
  switch (version) {
98
95
  case 2:
99
96
  castTokens = tokens;
100
- parser = new parser_1.default(castTokens);
97
+ parser = new Qasm2Parser(castTokens);
101
98
  break;
102
99
  case 3:
103
100
  castTokens = tokens;
104
- parser = new parser_2.default(castTokens);
101
+ parser = new Qasm3Parser(castTokens);
105
102
  break;
106
103
  default:
107
- throw new errors_1.UnsupportedOpenQASMVersionError("Unsupported OpenQASM version detected: ".concat(version));
104
+ throw new UnsupportedOpenQASMVersionError(`Unsupported OpenQASM version detected: ${version}`);
108
105
  }
109
106
  }
110
- else if (version === version_1.OpenQASMMajorVersion.Version2) {
107
+ else if (version === OpenQASMMajorVersion.Version2) {
111
108
  castTokens = tokens;
112
- parser = new parser_1.default(castTokens);
109
+ parser = new Qasm2Parser(castTokens);
113
110
  }
114
- else if (version === version_1.OpenQASMMajorVersion.Version3) {
111
+ else if (version === OpenQASMMajorVersion.Version3) {
115
112
  castTokens = tokens;
116
- parser = new parser_2.default(castTokens);
113
+ parser = new Qasm3Parser(castTokens);
117
114
  }
118
115
  else {
119
116
  castTokens = tokens;
120
- parser = new parser_2.default(castTokens);
117
+ parser = new Qasm3Parser(castTokens);
121
118
  }
122
- var ast = parser.parse();
119
+ const ast = parser.parse();
123
120
  return ast;
124
121
  }
@@ -0,0 +1,159 @@
1
+ /**
2
+ * OpenQASM 2.0 Abstract Syntax Tree Node Definitions
3
+ *
4
+ * This module defines the AST node classes for OpenQASM 2.0, which provides
5
+ * a simpler and more limited set of constructs compared to OpenQASM 3.0.
6
+ *
7
+ * OpenQASM 2.0 focuses on:
8
+ * - Basic quantum register (`qreg`) and classical register (`creg`) declarations
9
+ * - Gate definitions and applications
10
+ * - Measurement operations
11
+ * - Simple conditional statements
12
+ * - Basic arithmetic expressions
13
+ *
14
+ * Key limitations compared to 3.0:
15
+ * - No advanced classical types (only registers)
16
+ * - No control flow structures (loops, complex conditionals)
17
+ * - No function definitions or subroutines
18
+ * - Limited expression capabilities
19
+ *
20
+ * @module
21
+ *
22
+ * @example Basic OpenQASM 2.0 constructs
23
+ * ```typescript
24
+ * // Quantum register: qreg q[2];
25
+ * new QReg('q', 2)
26
+ *
27
+ * // Gate application: h q[0];
28
+ * new ApplyGate('h', [['q', 0]], [])
29
+ *
30
+ * // Measurement: measure q[0] -> c[0];
31
+ * new Measure('q', 'c', 0, 0)
32
+ * ```
33
+ */
34
+ import { OpenQASMVersion } from "../version";
35
+ /** Base class representing a basic AST node. */
36
+ declare class AstNode {
37
+ }
38
+ /** Class representing the version statement. */
39
+ declare class Version extends AstNode {
40
+ version: OpenQASMVersion;
41
+ constructor(version: OpenQASMVersion);
42
+ }
43
+ /** Class representing an include statement. */
44
+ declare class Include extends AstNode {
45
+ filename: string;
46
+ constructor(filename: string);
47
+ }
48
+ /** Class representing a qubit register. */
49
+ declare class QReg extends AstNode {
50
+ size: number;
51
+ id: string;
52
+ constructor(id: string, size: number);
53
+ }
54
+ /** Class representing a classical register. */
55
+ declare class CReg extends AstNode {
56
+ size: number;
57
+ id: string;
58
+ constructor(id: string, size: number);
59
+ }
60
+ /** Class representing an identifier. */
61
+ declare class Id extends AstNode {
62
+ id: string;
63
+ constructor(id: string);
64
+ }
65
+ /** Class representing a barrier. */
66
+ declare class Barrier extends AstNode {
67
+ index: number;
68
+ register: string;
69
+ constructor(register: string, index?: number);
70
+ }
71
+ /** Class representing a variable. */
72
+ declare class Variable extends AstNode {
73
+ value: string;
74
+ constructor(value: string);
75
+ }
76
+ /** Class representing a measurement. */
77
+ declare class Measure extends AstNode {
78
+ src_index: number;
79
+ src_register: string;
80
+ dest_index: number;
81
+ dest_register: string;
82
+ constructor(src_register: string, dest_register: string, src_index?: number, dest_index?: number);
83
+ }
84
+ /** Class representing a gate application. */
85
+ declare class ApplyGate extends AstNode {
86
+ name: string;
87
+ qubits: Array<[string, number?]>;
88
+ params: Array<AstNode>;
89
+ constructor(name: string, qubits: Array<[string, number?]>, params: Array<AstNode>);
90
+ }
91
+ /** Class representing a gate. */
92
+ declare class Gate extends AstNode {
93
+ name: string;
94
+ registers: Array<string>;
95
+ params: Array<string>;
96
+ nodes: Array<AstNode>;
97
+ constructor(name: string, registers: Array<string>, params: Array<string>, nodes: Array<AstNode>);
98
+ }
99
+ /** Class representing an opaque gate declaration (only available in OpenQASM 2.x versions) */
100
+ declare class Opaque extends AstNode {
101
+ name: string;
102
+ qubits: Array<[string, number?]>;
103
+ params: Array<string>;
104
+ constructor(name: string, qubits: Array<[string, number?]>, params?: Array<string>);
105
+ }
106
+ /** Class representing conditional. */
107
+ declare class If extends AstNode {
108
+ register: string;
109
+ param: number;
110
+ gate: AstNode;
111
+ constructor(register: string, param: number, gate: AstNode);
112
+ }
113
+ /** Class representing minus. */
114
+ declare class Minus extends AstNode {
115
+ }
116
+ /** Class representing plus. */
117
+ declare class Plus extends AstNode {
118
+ }
119
+ /** Class representing times. */
120
+ declare class Times extends AstNode {
121
+ }
122
+ /** Class representing power. */
123
+ declare class Power extends AstNode {
124
+ }
125
+ /** Class representing division. */
126
+ declare class Divide extends AstNode {
127
+ }
128
+ /** Class representing pi. */
129
+ declare class Pi extends AstNode {
130
+ }
131
+ /** Class representing the square root. */
132
+ declare class Sqrt extends AstNode {
133
+ }
134
+ /** Class representing natural logarithm. */
135
+ declare class Ln extends AstNode {
136
+ }
137
+ /** Class representing exponentiation. */
138
+ declare class Exp extends AstNode {
139
+ }
140
+ /** Class representing tagnent. */
141
+ declare class Tan extends AstNode {
142
+ }
143
+ /** Class representing cosine. */
144
+ declare class Cos extends AstNode {
145
+ }
146
+ /** Class representing sine. */
147
+ declare class Sin extends AstNode {
148
+ }
149
+ /** Class representing an integer. */
150
+ declare class NNInteger extends AstNode {
151
+ value: number;
152
+ constructor(value: number);
153
+ }
154
+ /** Class representing a real. */
155
+ declare class Real extends AstNode {
156
+ value: number;
157
+ constructor(value: number);
158
+ }
159
+ export { AstNode, Version, Include, QReg, CReg, Barrier, Measure, ApplyGate, Gate, Opaque, If, Id, Divide, Plus, Minus, Times, Power, Sin, Cos, Tan, Exp, Ln, Sqrt, Pi, NNInteger, Real, Variable, };