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.
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Error classes for QASM parsing and validation
3
+ *
4
+ * This module provides specific error types for different parsing failures,
5
+ * enabling precise error handling and debugging. Each error includes contextual
6
+ * information about where the error occurred in the source code.
7
+ *
8
+ * @module Error Handling
9
+ */
10
+ /** Class representing a bad argument exception. */
11
+ declare class BadArgumentError extends Error {
12
+ constructor(message?: string);
13
+ }
14
+ /** Class representing a bad include statement */
15
+ declare class BadIncludeError extends Error {
16
+ constructor(message?: string);
17
+ }
18
+ /** Class representing a bad quantum register exception. */
19
+ declare class BadQregError extends Error {
20
+ constructor(message?: string);
21
+ }
22
+ /** Class representing a bad equality exception. */
23
+ declare class BadEqualsError extends Error {
24
+ constructor(message?: string);
25
+ }
26
+ /** Class representing a bad classical register exception. */
27
+ declare class BadCregError extends Error {
28
+ constructor(message?: string);
29
+ }
30
+ /** Class representing a bad conditional exception. */
31
+ declare class BadConditionalError extends Error {
32
+ constructor(message?: string);
33
+ }
34
+ /** Class representing a bad barrier exception. */
35
+ declare class BadBarrierError extends Error {
36
+ constructor(message?: string);
37
+ }
38
+ /** Class representing a bad measurement exception. */
39
+ declare class BadMeasurementError extends Error {
40
+ constructor(message?: string);
41
+ }
42
+ /** Class representing a bad gate exception. */
43
+ declare class BadGateError extends Error {
44
+ constructor(message?: string);
45
+ }
46
+ /** Class representing a bad parameter exception. */
47
+ declare class BadParameterError extends Error {
48
+ constructor(message?: string);
49
+ }
50
+ /** Class representing a missing semicolon exception. */
51
+ declare class MissingSemicolonError extends Error {
52
+ constructor(message?: string);
53
+ }
54
+ /** Class representing a missing opening or closing parenthesis, bracket, or curly brakcet. */
55
+ declare class MissingBraceError extends Error {
56
+ constructor(message?: string);
57
+ }
58
+ /** Class representing an unsupported OpenQASM version exception. */
59
+ declare class UnsupportedOpenQASMVersionError extends Error {
60
+ constructor(message?: string);
61
+ }
62
+ /** Class representing an error parsing an expected string literal. */
63
+ declare class BadStringLiteralError extends Error {
64
+ constructor(message?: string);
65
+ }
66
+ /** Class representing an error parsing scalar types. */
67
+ declare class BadClassicalTypeError extends Error {
68
+ constructor(message?: string);
69
+ }
70
+ /** Class representing an error parsing an expression. */
71
+ declare class BadExpressionError extends Error {
72
+ constructor(message?: string);
73
+ }
74
+ /** Class representing an error in defining or calling a custom subroutine. */
75
+ declare class BadSubroutineError extends Error {
76
+ constructor(message?: string);
77
+ }
78
+ /** Class representing a bad loop statement declaration. */
79
+ declare class BadLoopError extends Error {
80
+ constructor(message?: string);
81
+ }
82
+ /** Class representing a bad quantum instruction. */
83
+ declare class BadQuantumInstructionError extends Error {
84
+ constructor(message?: string);
85
+ }
86
+ /** Type for returning an error constructor. */
87
+ type ReturnErrorConstructor = new (message?: string) => Error;
88
+ export { BadArgumentError, BadIncludeError, BadCregError, BadQregError, BadConditionalError, BadBarrierError, BadMeasurementError, BadGateError, BadEqualsError, BadParameterError, MissingSemicolonError, MissingBraceError, UnsupportedOpenQASMVersionError, BadStringLiteralError, BadClassicalTypeError, BadExpressionError, BadSubroutineError, BadLoopError, BadQuantumInstructionError, ReturnErrorConstructor, };
package/dist/errors.js CHANGED
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /**
3
2
  * Error classes for QASM parsing and validation
4
3
  *
@@ -8,267 +7,156 @@
8
7
  *
9
8
  * @module Error Handling
10
9
  */
11
- var __extends = (this && this.__extends) || (function () {
12
- var extendStatics = function (d, b) {
13
- extendStatics = Object.setPrototypeOf ||
14
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
15
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
16
- return extendStatics(d, b);
17
- };
18
- return function (d, b) {
19
- if (typeof b !== "function" && b !== null)
20
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
21
- extendStatics(d, b);
22
- function __() { this.constructor = d; }
23
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
24
- };
25
- })();
26
- Object.defineProperty(exports, "__esModule", { value: true });
27
- exports.BadQuantumInstructionError = exports.BadLoopError = exports.BadSubroutineError = exports.BadExpressionError = exports.BadClassicalTypeError = exports.BadStringLiteralError = exports.UnsupportedOpenQASMVersionError = exports.MissingBraceError = exports.MissingSemicolonError = exports.BadParameterError = exports.BadEqualsError = exports.BadGateError = exports.BadMeasurementError = exports.BadBarrierError = exports.BadConditionalError = exports.BadQregError = exports.BadCregError = exports.BadIncludeError = exports.BadArgumentError = void 0;
28
10
  /** Class representing a bad argument exception. */
29
- var BadArgumentError = /** @class */ (function (_super) {
30
- __extends(BadArgumentError, _super);
31
- function BadArgumentError(message) {
32
- var _newTarget = this.constructor;
33
- var _this = _super.call(this, message) || this;
34
- Object.setPrototypeOf(_this, _newTarget.prototype);
35
- _this.name = BadArgumentError.name;
36
- return _this;
11
+ class BadArgumentError extends Error {
12
+ constructor(message) {
13
+ super(message);
14
+ Object.setPrototypeOf(this, new.target.prototype);
15
+ this.name = BadArgumentError.name;
37
16
  }
38
- return BadArgumentError;
39
- }(Error));
40
- exports.BadArgumentError = BadArgumentError;
17
+ }
41
18
  /** Class representing a bad include statement */
42
- var BadIncludeError = /** @class */ (function (_super) {
43
- __extends(BadIncludeError, _super);
44
- function BadIncludeError(message) {
45
- var _newTarget = this.constructor;
46
- var _this = _super.call(this, message) || this;
47
- Object.setPrototypeOf(_this, _newTarget.prototype);
48
- _this.name = BadIncludeError.name;
49
- return _this;
19
+ class BadIncludeError extends Error {
20
+ constructor(message) {
21
+ super(message);
22
+ Object.setPrototypeOf(this, new.target.prototype);
23
+ this.name = BadIncludeError.name;
50
24
  }
51
- return BadIncludeError;
52
- }(Error));
53
- exports.BadIncludeError = BadIncludeError;
25
+ }
54
26
  /** Class representing a bad quantum register exception. */
55
- var BadQregError = /** @class */ (function (_super) {
56
- __extends(BadQregError, _super);
57
- function BadQregError(message) {
58
- var _newTarget = this.constructor;
59
- var _this = _super.call(this, message) || this;
60
- Object.setPrototypeOf(_this, _newTarget.prototype);
61
- _this.name = BadQregError.name;
62
- return _this;
27
+ class BadQregError extends Error {
28
+ constructor(message) {
29
+ super(message);
30
+ Object.setPrototypeOf(this, new.target.prototype);
31
+ this.name = BadQregError.name;
63
32
  }
64
- return BadQregError;
65
- }(Error));
66
- exports.BadQregError = BadQregError;
33
+ }
67
34
  /** Class representing a bad equality exception. */
68
- var BadEqualsError = /** @class */ (function (_super) {
69
- __extends(BadEqualsError, _super);
70
- function BadEqualsError(message) {
71
- var _newTarget = this.constructor;
72
- var _this = _super.call(this, message) || this;
73
- Object.setPrototypeOf(_this, _newTarget.prototype);
74
- _this.name = BadEqualsError.name;
75
- return _this;
35
+ class BadEqualsError extends Error {
36
+ constructor(message) {
37
+ super(message);
38
+ Object.setPrototypeOf(this, new.target.prototype);
39
+ this.name = BadEqualsError.name;
76
40
  }
77
- return BadEqualsError;
78
- }(Error));
79
- exports.BadEqualsError = BadEqualsError;
41
+ }
80
42
  /** Class representing a bad classical register exception. */
81
- var BadCregError = /** @class */ (function (_super) {
82
- __extends(BadCregError, _super);
83
- function BadCregError(message) {
84
- var _newTarget = this.constructor;
85
- var _this = _super.call(this, message) || this;
86
- Object.setPrototypeOf(_this, _newTarget.prototype);
87
- _this.name = BadCregError.name;
88
- return _this;
43
+ class BadCregError extends Error {
44
+ constructor(message) {
45
+ super(message);
46
+ Object.setPrototypeOf(this, new.target.prototype);
47
+ this.name = BadCregError.name;
89
48
  }
90
- return BadCregError;
91
- }(Error));
92
- exports.BadCregError = BadCregError;
49
+ }
93
50
  /** Class representing a bad conditional exception. */
94
- var BadConditionalError = /** @class */ (function (_super) {
95
- __extends(BadConditionalError, _super);
96
- function BadConditionalError(message) {
97
- var _newTarget = this.constructor;
98
- var _this = _super.call(this, message) || this;
99
- Object.setPrototypeOf(_this, _newTarget.prototype);
100
- _this.name = BadConditionalError.name;
101
- return _this;
51
+ class BadConditionalError extends Error {
52
+ constructor(message) {
53
+ super(message);
54
+ Object.setPrototypeOf(this, new.target.prototype);
55
+ this.name = BadConditionalError.name;
102
56
  }
103
- return BadConditionalError;
104
- }(Error));
105
- exports.BadConditionalError = BadConditionalError;
57
+ }
106
58
  /** Class representing a bad barrier exception. */
107
- var BadBarrierError = /** @class */ (function (_super) {
108
- __extends(BadBarrierError, _super);
109
- function BadBarrierError(message) {
110
- var _newTarget = this.constructor;
111
- var _this = _super.call(this, message) || this;
112
- Object.setPrototypeOf(_this, _newTarget.prototype);
113
- _this.name = BadBarrierError.name;
114
- return _this;
59
+ class BadBarrierError extends Error {
60
+ constructor(message) {
61
+ super(message);
62
+ Object.setPrototypeOf(this, new.target.prototype);
63
+ this.name = BadBarrierError.name;
115
64
  }
116
- return BadBarrierError;
117
- }(Error));
118
- exports.BadBarrierError = BadBarrierError;
65
+ }
119
66
  /** Class representing a bad measurement exception. */
120
- var BadMeasurementError = /** @class */ (function (_super) {
121
- __extends(BadMeasurementError, _super);
122
- function BadMeasurementError(message) {
123
- var _newTarget = this.constructor;
124
- var _this = _super.call(this, message) || this;
125
- Object.setPrototypeOf(_this, _newTarget.prototype);
126
- _this.name = BadMeasurementError.name;
127
- return _this;
67
+ class BadMeasurementError extends Error {
68
+ constructor(message) {
69
+ super(message);
70
+ Object.setPrototypeOf(this, new.target.prototype);
71
+ this.name = BadMeasurementError.name;
128
72
  }
129
- return BadMeasurementError;
130
- }(Error));
131
- exports.BadMeasurementError = BadMeasurementError;
73
+ }
132
74
  /** Class representing a bad gate exception. */
133
- var BadGateError = /** @class */ (function (_super) {
134
- __extends(BadGateError, _super);
135
- function BadGateError(message) {
136
- var _newTarget = this.constructor;
137
- var _this = _super.call(this, message) || this;
138
- Object.setPrototypeOf(_this, _newTarget.prototype);
139
- _this.name = BadGateError.name;
140
- return _this;
75
+ class BadGateError extends Error {
76
+ constructor(message) {
77
+ super(message);
78
+ Object.setPrototypeOf(this, new.target.prototype);
79
+ this.name = BadGateError.name;
141
80
  }
142
- return BadGateError;
143
- }(Error));
144
- exports.BadGateError = BadGateError;
81
+ }
145
82
  /** Class representing a bad parameter exception. */
146
- var BadParameterError = /** @class */ (function (_super) {
147
- __extends(BadParameterError, _super);
148
- function BadParameterError(message) {
149
- var _newTarget = this.constructor;
150
- var _this = _super.call(this, message) || this;
151
- Object.setPrototypeOf(_this, _newTarget.prototype);
152
- _this.name = BadParameterError.name;
153
- return _this;
83
+ class BadParameterError extends Error {
84
+ constructor(message) {
85
+ super(message);
86
+ Object.setPrototypeOf(this, new.target.prototype);
87
+ this.name = BadParameterError.name;
154
88
  }
155
- return BadParameterError;
156
- }(Error));
157
- exports.BadParameterError = BadParameterError;
89
+ }
158
90
  /** Class representing a missing semicolon exception. */
159
- var MissingSemicolonError = /** @class */ (function (_super) {
160
- __extends(MissingSemicolonError, _super);
161
- function MissingSemicolonError(message) {
162
- var _newTarget = this.constructor;
163
- var _this = _super.call(this, message) || this;
164
- Object.setPrototypeOf(_this, _newTarget.prototype);
165
- _this.name = MissingSemicolonError.name;
166
- return _this;
91
+ class MissingSemicolonError extends Error {
92
+ constructor(message) {
93
+ super(message);
94
+ Object.setPrototypeOf(this, new.target.prototype);
95
+ this.name = MissingSemicolonError.name;
167
96
  }
168
- return MissingSemicolonError;
169
- }(Error));
170
- exports.MissingSemicolonError = MissingSemicolonError;
97
+ }
171
98
  /** Class representing a missing opening or closing parenthesis, bracket, or curly brakcet. */
172
- var MissingBraceError = /** @class */ (function (_super) {
173
- __extends(MissingBraceError, _super);
174
- function MissingBraceError(message) {
175
- var _newTarget = this.constructor;
176
- var _this = _super.call(this, message) || this;
177
- Object.setPrototypeOf(_this, _newTarget.prototype);
178
- _this.name = MissingSemicolonError.name;
179
- return _this;
99
+ class MissingBraceError extends Error {
100
+ constructor(message) {
101
+ super(message);
102
+ Object.setPrototypeOf(this, new.target.prototype);
103
+ this.name = MissingSemicolonError.name;
180
104
  }
181
- return MissingBraceError;
182
- }(Error));
183
- exports.MissingBraceError = MissingBraceError;
105
+ }
184
106
  /** Class representing an unsupported OpenQASM version exception. */
185
- var UnsupportedOpenQASMVersionError = /** @class */ (function (_super) {
186
- __extends(UnsupportedOpenQASMVersionError, _super);
187
- function UnsupportedOpenQASMVersionError(message) {
188
- var _newTarget = this.constructor;
189
- var _this = _super.call(this, message) || this;
190
- Object.setPrototypeOf(_this, _newTarget.prototype);
191
- _this.name = UnsupportedOpenQASMVersionError.name;
192
- return _this;
107
+ class UnsupportedOpenQASMVersionError extends Error {
108
+ constructor(message) {
109
+ super(message);
110
+ Object.setPrototypeOf(this, new.target.prototype);
111
+ this.name = UnsupportedOpenQASMVersionError.name;
193
112
  }
194
- return UnsupportedOpenQASMVersionError;
195
- }(Error));
196
- exports.UnsupportedOpenQASMVersionError = UnsupportedOpenQASMVersionError;
113
+ }
197
114
  /** Class representing an error parsing an expected string literal. */
198
- var BadStringLiteralError = /** @class */ (function (_super) {
199
- __extends(BadStringLiteralError, _super);
200
- function BadStringLiteralError(message) {
201
- var _newTarget = this.constructor;
202
- var _this = _super.call(this, message) || this;
203
- Object.setPrototypeOf(_this, _newTarget.prototype);
204
- _this.name = BadStringLiteralError.name;
205
- return _this;
115
+ class BadStringLiteralError extends Error {
116
+ constructor(message) {
117
+ super(message);
118
+ Object.setPrototypeOf(this, new.target.prototype);
119
+ this.name = BadStringLiteralError.name;
206
120
  }
207
- return BadStringLiteralError;
208
- }(Error));
209
- exports.BadStringLiteralError = BadStringLiteralError;
121
+ }
210
122
  /** Class representing an error parsing scalar types. */
211
- var BadClassicalTypeError = /** @class */ (function (_super) {
212
- __extends(BadClassicalTypeError, _super);
213
- function BadClassicalTypeError(message) {
214
- var _newTarget = this.constructor;
215
- var _this = _super.call(this, message) || this;
216
- Object.setPrototypeOf(_this, _newTarget.prototype);
217
- _this.name = BadClassicalTypeError.name;
218
- return _this;
123
+ class BadClassicalTypeError extends Error {
124
+ constructor(message) {
125
+ super(message);
126
+ Object.setPrototypeOf(this, new.target.prototype);
127
+ this.name = BadClassicalTypeError.name;
219
128
  }
220
- return BadClassicalTypeError;
221
- }(Error));
222
- exports.BadClassicalTypeError = BadClassicalTypeError;
129
+ }
223
130
  /** Class representing an error parsing an expression. */
224
- var BadExpressionError = /** @class */ (function (_super) {
225
- __extends(BadExpressionError, _super);
226
- function BadExpressionError(message) {
227
- var _newTarget = this.constructor;
228
- var _this = _super.call(this, message) || this;
229
- Object.setPrototypeOf(_this, _newTarget.prototype);
230
- _this.name = BadExpressionError.name;
231
- return _this;
131
+ class BadExpressionError extends Error {
132
+ constructor(message) {
133
+ super(message);
134
+ Object.setPrototypeOf(this, new.target.prototype);
135
+ this.name = BadExpressionError.name;
232
136
  }
233
- return BadExpressionError;
234
- }(Error));
235
- exports.BadExpressionError = BadExpressionError;
137
+ }
236
138
  /** Class representing an error in defining or calling a custom subroutine. */
237
- var BadSubroutineError = /** @class */ (function (_super) {
238
- __extends(BadSubroutineError, _super);
239
- function BadSubroutineError(message) {
240
- var _newTarget = this.constructor;
241
- var _this = _super.call(this, message) || this;
242
- Object.setPrototypeOf(_this, _newTarget.prototype);
243
- _this.name = BadSubroutineError.name;
244
- return _this;
139
+ class BadSubroutineError extends Error {
140
+ constructor(message) {
141
+ super(message);
142
+ Object.setPrototypeOf(this, new.target.prototype);
143
+ this.name = BadSubroutineError.name;
245
144
  }
246
- return BadSubroutineError;
247
- }(Error));
248
- exports.BadSubroutineError = BadSubroutineError;
145
+ }
249
146
  /** Class representing a bad loop statement declaration. */
250
- var BadLoopError = /** @class */ (function (_super) {
251
- __extends(BadLoopError, _super);
252
- function BadLoopError(message) {
253
- var _newTarget = this.constructor;
254
- var _this = _super.call(this, message) || this;
255
- Object.setPrototypeOf(_this, _newTarget.prototype);
256
- _this.name = BadLoopError.name;
257
- return _this;
147
+ class BadLoopError extends Error {
148
+ constructor(message) {
149
+ super(message);
150
+ Object.setPrototypeOf(this, new.target.prototype);
151
+ this.name = BadLoopError.name;
258
152
  }
259
- return BadLoopError;
260
- }(Error));
261
- exports.BadLoopError = BadLoopError;
153
+ }
262
154
  /** Class representing a bad quantum instruction. */
263
- var BadQuantumInstructionError = /** @class */ (function (_super) {
264
- __extends(BadQuantumInstructionError, _super);
265
- function BadQuantumInstructionError(message) {
266
- var _newTarget = this.constructor;
267
- var _this = _super.call(this, message) || this;
268
- Object.setPrototypeOf(_this, _newTarget.prototype);
269
- _this.name = BadQuantumInstructionError.name;
270
- return _this;
271
- }
272
- return BadQuantumInstructionError;
273
- }(Error));
274
- exports.BadQuantumInstructionError = BadQuantumInstructionError;
155
+ class BadQuantumInstructionError extends Error {
156
+ constructor(message) {
157
+ super(message);
158
+ Object.setPrototypeOf(this, new.target.prototype);
159
+ this.name = BadQuantumInstructionError.name;
160
+ }
161
+ }
162
+ export { BadArgumentError, BadIncludeError, BadCregError, BadQregError, BadConditionalError, BadBarrierError, BadMeasurementError, BadGateError, BadEqualsError, BadParameterError, MissingSemicolonError, MissingBraceError, UnsupportedOpenQASMVersionError, BadStringLiteralError, BadClassicalTypeError, BadExpressionError, BadSubroutineError, BadLoopError, BadQuantumInstructionError, };
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Main lexer interface for tokenizing QASM code
3
+ *
4
+ * The lexer is responsible for breaking down QASM source code into tokens
5
+ * that can be consumed by the parser. It supports both OpenQASM 2.0 and 3.0
6
+ * syntax, with version-specific lexers handling the differences in token types
7
+ * and syntax rules.
8
+ *
9
+ * The specific Lexer implementations can be found at:
10
+ * - {@link Qasm3Lexer}
11
+ * - {@link Qasm2Lexer}
12
+ *
13
+ * @module Lexing
14
+ *
15
+ * @example Token Flow
16
+ * ```
17
+ * Source Code → Lexer → Tokens → Parser → AST
18
+ * "h q[0];" → [Id, Id, LSParen, NNInteger, RSParen, Semicolon]
19
+ * ```
20
+ *
21
+ * * @example Basic lexing workflow
22
+ * ```typescript
23
+ * import { lex } from './lexer';
24
+ *
25
+ * const qasmCode = 'OPENQASM 3.0; qubit q; h q;';
26
+ * const tokens = lex(qasmCode);
27
+ * console.log(tokens);
28
+ * // [
29
+ * // [Token.OpenQASM, undefined],
30
+ * // [Token.Id, 'qubit'],
31
+ * // [Token.Id, 'q'],
32
+ * // [Token.Semicolon, undefined],
33
+ * // [Token.Id, 'h'],
34
+ * // [Token.Id, 'q'],
35
+ * // [Token.Semicolon, undefined]
36
+ * // ]
37
+ * ```
38
+ */
39
+ import { Token as Qasm2Token } from "./qasm2/token";
40
+ import { Token as Qasm3Token } from "./qasm3/token";
41
+ import { OpenQASMVersion, OpenQASMMajorVersion } from "./version";
42
+ /**
43
+ * Tokenizes OpenQASM source code into an array of tokens.
44
+ *
45
+ * This is the main entry point for lexical analysis. It automatically selects
46
+ * the appropriate lexer implementation based on the OpenQASM version and returns
47
+ * an array of tokens that can be consumed by the parser.
48
+ *
49
+ * Each token is represented as a tuple containing:
50
+ * - **Token type**: An enum value indicating the kind of token
51
+ * - **Token value**: The associated value (for literals, identifiers, operators)
52
+ *
53
+ * @group Lexing
54
+ * @param qasm - The OpenQASM source code to tokenize
55
+ * @param cursor - Starting position in the input string (defaults to 0)
56
+ * @param version - OpenQASM version to use for lexing (defaults to 3.0)
57
+ * @returns Array of token tuples [TokenType, value?]
58
+ * @throws {UnsupportedOpenQASMVersionError} When an unsupported version is specified
59
+ *
60
+ * @example Tokenize OpenQASM 3.0 code
61
+ * ```typescript
62
+ * const tokens = lex('qubit[2] q; h q[0];', 0, 3);
63
+ * // Returns tokens using OpenQASM 3.0 syntax rules
64
+ * ```
65
+ *
66
+ * @example Tokenize OpenQASM 2.0 code
67
+ * ```typescript
68
+ * const tokens = lex('qreg q[2]; h q[0];', 0, 2);
69
+ * // Returns tokens using OpenQASM 2.0 syntax rules
70
+ * ```
71
+ *
72
+ * @example Resume lexing from specific position
73
+ * ```typescript
74
+ * const code = 'OPENQASM 3.0; qubit q;';
75
+ * const tokens = lex(code, 12); // Start after "OPENQASM 3.0"
76
+ * ```
77
+ */
78
+ export declare function lex(qasm: string, cursor?: number, version?: OpenQASMVersion | OpenQASMMajorVersion | number): Array<[Qasm2Token | Qasm3Token, (number | string)?]>;
package/dist/lexer.js CHANGED
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /**
3
2
  * Main lexer interface for tokenizing QASM code
4
3
  *
@@ -37,12 +36,10 @@
37
36
  * // ]
38
37
  * ```
39
38
  */
40
- Object.defineProperty(exports, "__esModule", { value: true });
41
- exports.lex = lex;
42
- var lexer_1 = require("./qasm2/lexer");
43
- var lexer_2 = require("./qasm3/lexer");
44
- var version_1 = require("./version");
45
- var errors_1 = require("./errors");
39
+ import { default as Qasm2Lexer } from "./qasm2/lexer";
40
+ import { default as Qasm3Lexer } from "./qasm3/lexer";
41
+ import { OpenQASMVersion, OpenQASMMajorVersion } from "./version";
42
+ import { UnsupportedOpenQASMVersionError } from "./errors";
46
43
  /**
47
44
  * Tokenizes OpenQASM source code into an array of tokens.
48
45
  *
@@ -79,41 +76,41 @@ var errors_1 = require("./errors");
79
76
  * const tokens = lex(code, 12); // Start after "OPENQASM 3.0"
80
77
  * ```
81
78
  */
82
- function lex(qasm, cursor, version) {
83
- var lexer;
84
- if (version instanceof version_1.OpenQASMVersion) {
79
+ export function lex(qasm, cursor, version) {
80
+ let lexer;
81
+ if (version instanceof OpenQASMVersion) {
85
82
  switch (version.major) {
86
- case version_1.OpenQASMMajorVersion.Version2:
87
- lexer = new lexer_1.default(qasm, cursor);
83
+ case OpenQASMMajorVersion.Version2:
84
+ lexer = new Qasm2Lexer(qasm, cursor);
88
85
  break;
89
- case version_1.OpenQASMMajorVersion.Version3:
90
- lexer = new lexer_2.default(qasm, cursor);
86
+ case OpenQASMMajorVersion.Version3:
87
+ lexer = new Qasm3Lexer(qasm, cursor);
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
- lexer = new lexer_1.default(qasm, cursor);
96
+ lexer = new Qasm2Lexer(qasm, cursor);
100
97
  break;
101
98
  case 3:
102
- lexer = new lexer_2.default(qasm, cursor);
99
+ lexer = new Qasm3Lexer(qasm, cursor);
103
100
  break;
104
101
  default:
105
- throw new errors_1.UnsupportedOpenQASMVersionError("Unsupported OpenQASM version detected: ".concat(version));
102
+ throw new UnsupportedOpenQASMVersionError(`Unsupported OpenQASM version detected: ${version}`);
106
103
  }
107
104
  }
108
- else if (version === version_1.OpenQASMMajorVersion.Version2) {
109
- lexer = new lexer_1.default(qasm, cursor);
105
+ else if (version === OpenQASMMajorVersion.Version2) {
106
+ lexer = new Qasm2Lexer(qasm, cursor);
110
107
  }
111
- else if (version === version_1.OpenQASMMajorVersion.Version3) {
112
- lexer = new lexer_2.default(qasm, cursor);
108
+ else if (version === OpenQASMMajorVersion.Version3) {
109
+ lexer = new Qasm3Lexer(qasm, cursor);
113
110
  }
114
111
  else {
115
- lexer = new lexer_2.default(qasm, cursor);
112
+ lexer = new Qasm3Lexer(qasm, cursor);
116
113
  }
117
- var tokens = lexer.lex();
114
+ const tokens = lexer.lex();
118
115
  return tokens;
119
116
  }