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/errors.d.ts +88 -0
- package/dist/errors.js +116 -228
- package/dist/lexer.d.ts +78 -0
- package/dist/lexer.js +21 -24
- package/dist/main.d.ts +87 -0
- package/dist/main.js +10 -19
- package/dist/parser.d.ts +75 -0
- package/dist/parser.js +22 -25
- package/dist/qasm2/ast.d.ts +159 -0
- package/dist/qasm2/ast.js +117 -267
- package/dist/qasm2/lexer.d.ts +135 -0
- package/dist/qasm2/lexer.js +171 -177
- package/dist/qasm2/parser.d.ts +187 -0
- package/dist/qasm2/parser.js +254 -258
- package/dist/qasm2/token.d.ts +89 -0
- package/dist/qasm2/token.js +4 -9
- package/dist/qasm3/ast.d.ts +652 -0
- package/dist/qasm3/ast.js +397 -732
- package/dist/qasm3/lexer.d.ts +156 -0
- package/dist/qasm3/lexer.js +259 -265
- package/dist/qasm3/parser.d.ts +407 -0
- package/dist/qasm3/parser.js +892 -896
- package/dist/qasm3/token.d.ts +197 -0
- package/dist/qasm3/token.js +4 -9
- package/dist/version.d.ts +41 -0
- package/dist/version.js +12 -16
- package/docs/docs-readme.md +13 -9
- package/docs/typedoc/index.html +4 -4
- package/package.json +2 -1
- package/readme.md +4 -3
- package/tsconfig.json +7 -2
package/dist/errors.d.ts
ADDED
|
@@ -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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
-
|
|
39
|
-
}(Error));
|
|
40
|
-
exports.BadArgumentError = BadArgumentError;
|
|
17
|
+
}
|
|
41
18
|
/** Class representing a bad include statement */
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
52
|
-
}(Error));
|
|
53
|
-
exports.BadIncludeError = BadIncludeError;
|
|
25
|
+
}
|
|
54
26
|
/** Class representing a bad quantum register exception. */
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
|
65
|
-
}(Error));
|
|
66
|
-
exports.BadQregError = BadQregError;
|
|
33
|
+
}
|
|
67
34
|
/** Class representing a bad equality exception. */
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
78
|
-
}(Error));
|
|
79
|
-
exports.BadEqualsError = BadEqualsError;
|
|
41
|
+
}
|
|
80
42
|
/** Class representing a bad classical register exception. */
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
91
|
-
}(Error));
|
|
92
|
-
exports.BadCregError = BadCregError;
|
|
49
|
+
}
|
|
93
50
|
/** Class representing a bad conditional exception. */
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
|
|
104
|
-
}(Error));
|
|
105
|
-
exports.BadConditionalError = BadConditionalError;
|
|
57
|
+
}
|
|
106
58
|
/** Class representing a bad barrier exception. */
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
-
|
|
117
|
-
}(Error));
|
|
118
|
-
exports.BadBarrierError = BadBarrierError;
|
|
65
|
+
}
|
|
119
66
|
/** Class representing a bad measurement exception. */
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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
|
-
|
|
130
|
-
}(Error));
|
|
131
|
-
exports.BadMeasurementError = BadMeasurementError;
|
|
73
|
+
}
|
|
132
74
|
/** Class representing a bad gate exception. */
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
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
|
-
|
|
143
|
-
}(Error));
|
|
144
|
-
exports.BadGateError = BadGateError;
|
|
81
|
+
}
|
|
145
82
|
/** Class representing a bad parameter exception. */
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
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
|
-
|
|
156
|
-
}(Error));
|
|
157
|
-
exports.BadParameterError = BadParameterError;
|
|
89
|
+
}
|
|
158
90
|
/** Class representing a missing semicolon exception. */
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
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
|
-
|
|
169
|
-
}(Error));
|
|
170
|
-
exports.MissingSemicolonError = MissingSemicolonError;
|
|
97
|
+
}
|
|
171
98
|
/** Class representing a missing opening or closing parenthesis, bracket, or curly brakcet. */
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
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
|
-
|
|
182
|
-
}(Error));
|
|
183
|
-
exports.MissingBraceError = MissingBraceError;
|
|
105
|
+
}
|
|
184
106
|
/** Class representing an unsupported OpenQASM version exception. */
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
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
|
-
|
|
195
|
-
}(Error));
|
|
196
|
-
exports.UnsupportedOpenQASMVersionError = UnsupportedOpenQASMVersionError;
|
|
113
|
+
}
|
|
197
114
|
/** Class representing an error parsing an expected string literal. */
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
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
|
-
|
|
208
|
-
}(Error));
|
|
209
|
-
exports.BadStringLiteralError = BadStringLiteralError;
|
|
121
|
+
}
|
|
210
122
|
/** Class representing an error parsing scalar types. */
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
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
|
-
|
|
221
|
-
}(Error));
|
|
222
|
-
exports.BadClassicalTypeError = BadClassicalTypeError;
|
|
129
|
+
}
|
|
223
130
|
/** Class representing an error parsing an expression. */
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
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
|
-
|
|
234
|
-
}(Error));
|
|
235
|
-
exports.BadExpressionError = BadExpressionError;
|
|
137
|
+
}
|
|
236
138
|
/** Class representing an error in defining or calling a custom subroutine. */
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
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
|
-
|
|
247
|
-
}(Error));
|
|
248
|
-
exports.BadSubroutineError = BadSubroutineError;
|
|
145
|
+
}
|
|
249
146
|
/** Class representing a bad loop statement declaration. */
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
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
|
-
|
|
260
|
-
}(Error));
|
|
261
|
-
exports.BadLoopError = BadLoopError;
|
|
153
|
+
}
|
|
262
154
|
/** Class representing a bad quantum instruction. */
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
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, };
|
package/dist/lexer.d.ts
ADDED
|
@@ -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
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
84
|
-
if (version instanceof
|
|
79
|
+
export function lex(qasm, cursor, version) {
|
|
80
|
+
let lexer;
|
|
81
|
+
if (version instanceof OpenQASMVersion) {
|
|
85
82
|
switch (version.major) {
|
|
86
|
-
case
|
|
87
|
-
lexer = new
|
|
83
|
+
case OpenQASMMajorVersion.Version2:
|
|
84
|
+
lexer = new Qasm2Lexer(qasm, cursor);
|
|
88
85
|
break;
|
|
89
|
-
case
|
|
90
|
-
lexer = new
|
|
86
|
+
case OpenQASMMajorVersion.Version3:
|
|
87
|
+
lexer = new Qasm3Lexer(qasm, cursor);
|
|
91
88
|
break;
|
|
92
89
|
default:
|
|
93
|
-
throw new
|
|
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
|
|
96
|
+
lexer = new Qasm2Lexer(qasm, cursor);
|
|
100
97
|
break;
|
|
101
98
|
case 3:
|
|
102
|
-
lexer = new
|
|
99
|
+
lexer = new Qasm3Lexer(qasm, cursor);
|
|
103
100
|
break;
|
|
104
101
|
default:
|
|
105
|
-
throw new
|
|
102
|
+
throw new UnsupportedOpenQASMVersionError(`Unsupported OpenQASM version detected: ${version}`);
|
|
106
103
|
}
|
|
107
104
|
}
|
|
108
|
-
else if (version ===
|
|
109
|
-
lexer = new
|
|
105
|
+
else if (version === OpenQASMMajorVersion.Version2) {
|
|
106
|
+
lexer = new Qasm2Lexer(qasm, cursor);
|
|
110
107
|
}
|
|
111
|
-
else if (version ===
|
|
112
|
-
lexer = new
|
|
108
|
+
else if (version === OpenQASMMajorVersion.Version3) {
|
|
109
|
+
lexer = new Qasm3Lexer(qasm, cursor);
|
|
113
110
|
}
|
|
114
111
|
else {
|
|
115
|
-
lexer = new
|
|
112
|
+
lexer = new Qasm3Lexer(qasm, cursor);
|
|
116
113
|
}
|
|
117
|
-
|
|
114
|
+
const tokens = lexer.lex();
|
|
118
115
|
return tokens;
|
|
119
116
|
}
|