qasm-ts 1.1.2 → 2.0.0

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.
Files changed (188) hide show
  1. package/.eslintrc.json +13 -0
  2. package/dist/errors.js +119 -2
  3. package/dist/lexer.js +39 -294
  4. package/dist/main.js +42 -12
  5. package/dist/parser.js +46 -482
  6. package/dist/{ast.js → qasm2/ast.js} +38 -2
  7. package/dist/qasm2/lexer.js +436 -0
  8. package/dist/qasm2/parser.js +609 -0
  9. package/dist/{token.js → qasm2/token.js} +62 -21
  10. package/dist/qasm3/ast.js +1081 -0
  11. package/dist/qasm3/lexer.js +615 -0
  12. package/dist/qasm3/parser.js +2030 -0
  13. package/dist/qasm3/token.js +342 -0
  14. package/dist/version.js +41 -0
  15. package/docs/.nojekyll +1 -0
  16. package/docs/assets/highlight.css +99 -0
  17. package/docs/assets/icons.js +18 -0
  18. package/docs/assets/icons.svg +1 -0
  19. package/docs/assets/main.js +60 -0
  20. package/docs/assets/navigation.js +1 -0
  21. package/docs/assets/search.js +1 -0
  22. package/docs/assets/style.css +1493 -0
  23. package/docs/classes/errors.BadArgumentError.html +12 -0
  24. package/docs/classes/errors.BadBarrierError.html +12 -0
  25. package/docs/classes/errors.BadClassicalTypeError.html +12 -0
  26. package/docs/classes/errors.BadConditionalError.html +12 -0
  27. package/docs/classes/errors.BadCregError.html +12 -0
  28. package/docs/classes/errors.BadEqualsError.html +12 -0
  29. package/docs/classes/errors.BadExpressionError.html +12 -0
  30. package/docs/classes/errors.BadGateError.html +12 -0
  31. package/docs/classes/errors.BadIncludeError.html +12 -0
  32. package/docs/classes/errors.BadLoopError.html +12 -0
  33. package/docs/classes/errors.BadMeasurementError.html +12 -0
  34. package/docs/classes/errors.BadParameterError.html +12 -0
  35. package/docs/classes/errors.BadQregError.html +12 -0
  36. package/docs/classes/errors.BadQuantumInstructionError.html +12 -0
  37. package/docs/classes/errors.BadStringLiteralError.html +12 -0
  38. package/docs/classes/errors.BadSubroutineError.html +12 -0
  39. package/docs/classes/errors.MissingBraceError.html +12 -0
  40. package/docs/classes/errors.MissingSemicolonError.html +12 -0
  41. package/docs/classes/errors.UnsupportedOpenQASMVersionError.html +12 -0
  42. package/docs/classes/qasm2_ast.ApplyGate.html +6 -0
  43. package/docs/classes/qasm2_ast.AstNode.html +3 -0
  44. package/docs/classes/qasm2_ast.Barrier.html +5 -0
  45. package/docs/classes/qasm2_ast.CReg.html +5 -0
  46. package/docs/classes/qasm2_ast.Cos.html +3 -0
  47. package/docs/classes/qasm2_ast.Divide.html +3 -0
  48. package/docs/classes/qasm2_ast.Exp.html +3 -0
  49. package/docs/classes/qasm2_ast.Gate.html +7 -0
  50. package/docs/classes/qasm2_ast.Id.html +4 -0
  51. package/docs/classes/qasm2_ast.If.html +6 -0
  52. package/docs/classes/qasm2_ast.Include.html +4 -0
  53. package/docs/classes/qasm2_ast.Ln.html +3 -0
  54. package/docs/classes/qasm2_ast.Measure.html +7 -0
  55. package/docs/classes/qasm2_ast.Minus.html +3 -0
  56. package/docs/classes/qasm2_ast.NNInteger.html +4 -0
  57. package/docs/classes/qasm2_ast.Opaque.html +6 -0
  58. package/docs/classes/qasm2_ast.Pi.html +3 -0
  59. package/docs/classes/qasm2_ast.Plus.html +3 -0
  60. package/docs/classes/qasm2_ast.Power.html +3 -0
  61. package/docs/classes/qasm2_ast.QReg.html +5 -0
  62. package/docs/classes/qasm2_ast.Real.html +4 -0
  63. package/docs/classes/qasm2_ast.Sin.html +3 -0
  64. package/docs/classes/qasm2_ast.Sqrt.html +3 -0
  65. package/docs/classes/qasm2_ast.Tan.html +3 -0
  66. package/docs/classes/qasm2_ast.Times.html +3 -0
  67. package/docs/classes/qasm2_ast.Variable.html +4 -0
  68. package/docs/classes/qasm2_ast.Version.html +4 -0
  69. package/docs/classes/qasm2_lexer.default.html +50 -0
  70. package/docs/classes/qasm2_parser.default.html +87 -0
  71. package/docs/classes/qasm3_ast.AliasStatement.html +5 -0
  72. package/docs/classes/qasm3_ast.AngleType.html +4 -0
  73. package/docs/classes/qasm3_ast.Arithmetic.html +6 -0
  74. package/docs/classes/qasm3_ast.ArrayAccess.html +5 -0
  75. package/docs/classes/qasm3_ast.ArrayDeclaration.html +7 -0
  76. package/docs/classes/qasm3_ast.ArrayInitializer.html +4 -0
  77. package/docs/classes/qasm3_ast.ArrayReference.html +5 -0
  78. package/docs/classes/qasm3_ast.AssignmentStatement.html +5 -0
  79. package/docs/classes/qasm3_ast.AstNode.html +3 -0
  80. package/docs/classes/qasm3_ast.Binary.html +6 -0
  81. package/docs/classes/qasm3_ast.BitType.html +4 -0
  82. package/docs/classes/qasm3_ast.BitstringLiteral.html +4 -0
  83. package/docs/classes/qasm3_ast.BoolType.html +3 -0
  84. package/docs/classes/qasm3_ast.BooleanLiteral.html +4 -0
  85. package/docs/classes/qasm3_ast.BoxDefinition.html +7 -0
  86. package/docs/classes/qasm3_ast.BranchingStatement.html +8 -0
  87. package/docs/classes/qasm3_ast.BreakStatement.html +3 -0
  88. package/docs/classes/qasm3_ast.CalibrationGrammarDeclaration.html +6 -0
  89. package/docs/classes/qasm3_ast.CaseStatement.html +5 -0
  90. package/docs/classes/qasm3_ast.Cast.html +5 -0
  91. package/docs/classes/qasm3_ast.ClassicalDeclaration.html +7 -0
  92. package/docs/classes/qasm3_ast.ClassicalType.html +3 -0
  93. package/docs/classes/qasm3_ast.ComplexType.html +6 -0
  94. package/docs/classes/qasm3_ast.ContinueStatement.html +3 -0
  95. package/docs/classes/qasm3_ast.DefaultStatement.html +4 -0
  96. package/docs/classes/qasm3_ast.DurationLiteral.html +5 -0
  97. package/docs/classes/qasm3_ast.DurationOf.html +4 -0
  98. package/docs/classes/qasm3_ast.DurationType.html +3 -0
  99. package/docs/classes/qasm3_ast.Euler.html +3 -0
  100. package/docs/classes/qasm3_ast.Expression.html +3 -0
  101. package/docs/classes/qasm3_ast.ExternSignature.html +8 -0
  102. package/docs/classes/qasm3_ast.FloatLiteral.html +4 -0
  103. package/docs/classes/qasm3_ast.FloatType.html +6 -0
  104. package/docs/classes/qasm3_ast.ForLoopStatement.html +12 -0
  105. package/docs/classes/qasm3_ast.HardwareQubit.html +5 -0
  106. package/docs/classes/qasm3_ast.IODeclaration.html +5 -0
  107. package/docs/classes/qasm3_ast.Identifier.html +4 -0
  108. package/docs/classes/qasm3_ast.ImaginaryLiteral.html +4 -0
  109. package/docs/classes/qasm3_ast.Include.html +6 -0
  110. package/docs/classes/qasm3_ast.IndexSet.html +5 -0
  111. package/docs/classes/qasm3_ast.IntType.html +4 -0
  112. package/docs/classes/qasm3_ast.IntegerLiteral.html +4 -0
  113. package/docs/classes/qasm3_ast.MathFunction.html +5 -0
  114. package/docs/classes/qasm3_ast.NumericLiteral.html +4 -0
  115. package/docs/classes/qasm3_ast.Parameters.html +4 -0
  116. package/docs/classes/qasm3_ast.Pi.html +3 -0
  117. package/docs/classes/qasm3_ast.ProgramBlock.html +7 -0
  118. package/docs/classes/qasm3_ast.QuantumBarrier.html +6 -0
  119. package/docs/classes/qasm3_ast.QuantumBlock.html +6 -0
  120. package/docs/classes/qasm3_ast.QuantumDeclaration.html +8 -0
  121. package/docs/classes/qasm3_ast.QuantumDelay.html +5 -0
  122. package/docs/classes/qasm3_ast.QuantumGateCall.html +9 -0
  123. package/docs/classes/qasm3_ast.QuantumGateDefinition.html +9 -0
  124. package/docs/classes/qasm3_ast.QuantumGateModifier.html +5 -0
  125. package/docs/classes/qasm3_ast.QuantumMeasurement.html +6 -0
  126. package/docs/classes/qasm3_ast.QuantumMeasurementAssignment.html +8 -0
  127. package/docs/classes/qasm3_ast.QuantumReset.html +4 -0
  128. package/docs/classes/qasm3_ast.Range.html +6 -0
  129. package/docs/classes/qasm3_ast.ReturnStatement.html +4 -0
  130. package/docs/classes/qasm3_ast.SizeOf.html +5 -0
  131. package/docs/classes/qasm3_ast.Statement.html +12 -0
  132. package/docs/classes/qasm3_ast.StretchType.html +3 -0
  133. package/docs/classes/qasm3_ast.SubroutineBlock.html +6 -0
  134. package/docs/classes/qasm3_ast.SubroutineCall.html +5 -0
  135. package/docs/classes/qasm3_ast.SubroutineDefinition.html +10 -0
  136. package/docs/classes/qasm3_ast.SubscriptedIdentifier.html +5 -0
  137. package/docs/classes/qasm3_ast.SwitchStatement.html +6 -0
  138. package/docs/classes/qasm3_ast.Tau.html +3 -0
  139. package/docs/classes/qasm3_ast.TrigFunction.html +5 -0
  140. package/docs/classes/qasm3_ast.UIntType.html +4 -0
  141. package/docs/classes/qasm3_ast.Unary.html +5 -0
  142. package/docs/classes/qasm3_ast.Version.html +6 -0
  143. package/docs/classes/qasm3_ast.WhileLoopStatement.html +6 -0
  144. package/docs/classes/qasm3_lexer.default.html +60 -0
  145. package/docs/classes/qasm3_parser.default.html +227 -0
  146. package/docs/classes/version.OpenQASMVersion.html +16 -0
  147. package/docs/enums/qasm2_token.Token.html +39 -0
  148. package/docs/enums/qasm3_ast.ArithmeticOp.html +9 -0
  149. package/docs/enums/qasm3_ast.ArrayReferenceModifier.html +4 -0
  150. package/docs/enums/qasm3_ast.BinaryOp.html +15 -0
  151. package/docs/enums/qasm3_ast.DurationUnit.html +7 -0
  152. package/docs/enums/qasm3_ast.IOModifier.html +4 -0
  153. package/docs/enums/qasm3_ast.MathFunctionTypes.html +12 -0
  154. package/docs/enums/qasm3_ast.QuantumGateModifierName.html +6 -0
  155. package/docs/enums/qasm3_ast.TrigFunctionTypes.html +8 -0
  156. package/docs/enums/qasm3_ast.UnaryOp.html +5 -0
  157. package/docs/enums/qasm3_token.Token.html +101 -0
  158. package/docs/enums/version.OpenQASMMajorVersion.html +4 -0
  159. package/docs/functions/lexer.lex.html +1 -0
  160. package/docs/functions/main.parseFile.html +4 -0
  161. package/docs/functions/main.parseString.html +4 -0
  162. package/docs/functions/parser.parse.html +1 -0
  163. package/docs/functions/qasm2_token.inverseLookup.html +3 -0
  164. package/docs/functions/qasm2_token.lookup.html +4 -0
  165. package/docs/functions/qasm2_token.notParam.html +3 -0
  166. package/docs/functions/qasm3_token.inverseLookup.html +3 -0
  167. package/docs/functions/qasm3_token.lookup.html +4 -0
  168. package/docs/functions/qasm3_token.notParam.html +3 -0
  169. package/docs/hierarchy.html +1 -0
  170. package/docs/index.html +55 -0
  171. package/docs/modules/errors.html +21 -0
  172. package/docs/modules/lexer.html +2 -0
  173. package/docs/modules/main.html +3 -0
  174. package/docs/modules/parser.html +2 -0
  175. package/docs/modules/qasm2_ast.html +28 -0
  176. package/docs/modules/qasm2_lexer.html +2 -0
  177. package/docs/modules/qasm2_parser.html +2 -0
  178. package/docs/modules/qasm2_token.html +5 -0
  179. package/docs/modules/qasm3_ast.html +83 -0
  180. package/docs/modules/qasm3_lexer.html +2 -0
  181. package/docs/modules/qasm3_parser.html +2 -0
  182. package/docs/modules/qasm3_token.html +5 -0
  183. package/docs/modules/version.html +3 -0
  184. package/docs/modules.html +14 -0
  185. package/docs/types/errors.ReturnErrorConstructor.html +2 -0
  186. package/package.json +5 -4
  187. package/readme.md +67 -51
  188. package/dist/example.js +0 -22
package/.eslintrc.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "parser": "@typescript-eslint/parser",
3
+ "plugins": [
4
+ "@typescript-eslint"
5
+ ],
6
+ "extends": [
7
+ "eslint:recommended",
8
+ "plugin:@typescript-eslint/recommended"
9
+ ],
10
+ "rules": {
11
+ "no-control-regex": 0
12
+ }
13
+ }
package/dist/errors.js CHANGED
@@ -14,8 +14,8 @@ var __extends = (this && this.__extends) || (function () {
14
14
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
15
  };
16
16
  })();
17
- exports.__esModule = true;
18
- exports.MissingSemicolonError = exports.BadParameterError = exports.BadEqualsError = exports.BadGateError = exports.BadMeasurementError = exports.BadBarrierError = exports.BadConditionalError = exports.BadQregError = exports.BadCregError = exports.BadArgumentError = void 0;
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ 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;
19
19
  /** Class representing a bad argument exception. */
20
20
  var BadArgumentError = /** @class */ (function (_super) {
21
21
  __extends(BadArgumentError, _super);
@@ -29,6 +29,19 @@ var BadArgumentError = /** @class */ (function (_super) {
29
29
  return BadArgumentError;
30
30
  }(Error));
31
31
  exports.BadArgumentError = BadArgumentError;
32
+ /** Class representing a bad include statement */
33
+ var BadIncludeError = /** @class */ (function (_super) {
34
+ __extends(BadIncludeError, _super);
35
+ function BadIncludeError(message) {
36
+ var _newTarget = this.constructor;
37
+ var _this = _super.call(this, message) || this;
38
+ Object.setPrototypeOf(_this, _newTarget.prototype);
39
+ _this.name = BadIncludeError.name;
40
+ return _this;
41
+ }
42
+ return BadIncludeError;
43
+ }(Error));
44
+ exports.BadIncludeError = BadIncludeError;
32
45
  /** Class representing a bad quantum register exception. */
33
46
  var BadQregError = /** @class */ (function (_super) {
34
47
  __extends(BadQregError, _super);
@@ -146,3 +159,107 @@ var MissingSemicolonError = /** @class */ (function (_super) {
146
159
  return MissingSemicolonError;
147
160
  }(Error));
148
161
  exports.MissingSemicolonError = MissingSemicolonError;
162
+ /** Class representing a missing opening or closing parenthesis, bracket, or curly brakcet. */
163
+ var MissingBraceError = /** @class */ (function (_super) {
164
+ __extends(MissingBraceError, _super);
165
+ function MissingBraceError(message) {
166
+ var _newTarget = this.constructor;
167
+ var _this = _super.call(this, message) || this;
168
+ Object.setPrototypeOf(_this, _newTarget.prototype);
169
+ _this.name = MissingSemicolonError.name;
170
+ return _this;
171
+ }
172
+ return MissingBraceError;
173
+ }(Error));
174
+ exports.MissingBraceError = MissingBraceError;
175
+ /** Class representing an unsupported OpenQASM version exception. */
176
+ var UnsupportedOpenQASMVersionError = /** @class */ (function (_super) {
177
+ __extends(UnsupportedOpenQASMVersionError, _super);
178
+ function UnsupportedOpenQASMVersionError(message) {
179
+ var _newTarget = this.constructor;
180
+ var _this = _super.call(this, message) || this;
181
+ Object.setPrototypeOf(_this, _newTarget.prototype);
182
+ _this.name = UnsupportedOpenQASMVersionError.name;
183
+ return _this;
184
+ }
185
+ return UnsupportedOpenQASMVersionError;
186
+ }(Error));
187
+ exports.UnsupportedOpenQASMVersionError = UnsupportedOpenQASMVersionError;
188
+ /** Class representing an error parsing an expected string literal. */
189
+ var BadStringLiteralError = /** @class */ (function (_super) {
190
+ __extends(BadStringLiteralError, _super);
191
+ function BadStringLiteralError(message) {
192
+ var _newTarget = this.constructor;
193
+ var _this = _super.call(this, message) || this;
194
+ Object.setPrototypeOf(_this, _newTarget.prototype);
195
+ _this.name = BadStringLiteralError.name;
196
+ return _this;
197
+ }
198
+ return BadStringLiteralError;
199
+ }(Error));
200
+ exports.BadStringLiteralError = BadStringLiteralError;
201
+ /** Class representing an error parsing scalar types. */
202
+ var BadClassicalTypeError = /** @class */ (function (_super) {
203
+ __extends(BadClassicalTypeError, _super);
204
+ function BadClassicalTypeError(message) {
205
+ var _newTarget = this.constructor;
206
+ var _this = _super.call(this, message) || this;
207
+ Object.setPrototypeOf(_this, _newTarget.prototype);
208
+ _this.name = BadClassicalTypeError.name;
209
+ return _this;
210
+ }
211
+ return BadClassicalTypeError;
212
+ }(Error));
213
+ exports.BadClassicalTypeError = BadClassicalTypeError;
214
+ /** Class representing an error parsing an expression. */
215
+ var BadExpressionError = /** @class */ (function (_super) {
216
+ __extends(BadExpressionError, _super);
217
+ function BadExpressionError(message) {
218
+ var _newTarget = this.constructor;
219
+ var _this = _super.call(this, message) || this;
220
+ Object.setPrototypeOf(_this, _newTarget.prototype);
221
+ _this.name = BadExpressionError.name;
222
+ return _this;
223
+ }
224
+ return BadExpressionError;
225
+ }(Error));
226
+ exports.BadExpressionError = BadExpressionError;
227
+ /** Class representing an error in defining or calling a custom subroutine. */
228
+ var BadSubroutineError = /** @class */ (function (_super) {
229
+ __extends(BadSubroutineError, _super);
230
+ function BadSubroutineError(message) {
231
+ var _newTarget = this.constructor;
232
+ var _this = _super.call(this, message) || this;
233
+ Object.setPrototypeOf(_this, _newTarget.prototype);
234
+ _this.name = BadSubroutineError.name;
235
+ return _this;
236
+ }
237
+ return BadSubroutineError;
238
+ }(Error));
239
+ exports.BadSubroutineError = BadSubroutineError;
240
+ /** Class representing a bad loop statement declaration. */
241
+ var BadLoopError = /** @class */ (function (_super) {
242
+ __extends(BadLoopError, _super);
243
+ function BadLoopError(message) {
244
+ var _newTarget = this.constructor;
245
+ var _this = _super.call(this, message) || this;
246
+ Object.setPrototypeOf(_this, _newTarget.prototype);
247
+ _this.name = BadLoopError.name;
248
+ return _this;
249
+ }
250
+ return BadLoopError;
251
+ }(Error));
252
+ exports.BadLoopError = BadLoopError;
253
+ /** Class representing a bad quantum instruction. */
254
+ var BadQuantumInstructionError = /** @class */ (function (_super) {
255
+ __extends(BadQuantumInstructionError, _super);
256
+ function BadQuantumInstructionError(message) {
257
+ var _newTarget = this.constructor;
258
+ var _this = _super.call(this, message) || this;
259
+ Object.setPrototypeOf(_this, _newTarget.prototype);
260
+ _this.name = BadQuantumInstructionError.name;
261
+ return _this;
262
+ }
263
+ return BadQuantumInstructionError;
264
+ }(Error));
265
+ exports.BadQuantumInstructionError = BadQuantumInstructionError;
package/dist/lexer.js CHANGED
@@ -1,300 +1,45 @@
1
1
  "use strict";
2
- exports.__esModule = true;
3
- var token_1 = require("./token");
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.lex = lex;
4
+ var lexer_1 = require("./qasm2/lexer");
5
+ var lexer_2 = require("./qasm3/lexer");
6
+ var version_1 = require("./version");
4
7
  var errors_1 = require("./errors");
5
- /**
6
- * Returns whether a given character could be an element of a numeric value.
7
- * @param c - The character.
8
- * @return Whether the character is numeric.
9
- */
10
- function isNumeric(c) {
11
- return (c == '.') || !isNaN(parseInt(c));
12
- }
13
- /**
14
- * Returns whether a given character is a letter.
15
- * @param c - The character.
16
- * @return Whether the character is a letter.
17
- */
18
- function isLetter(c) {
19
- if (c.match(/[a-z]/i)) {
20
- return true;
8
+ function lex(qasm, cursor, version) {
9
+ var lexer;
10
+ if (version instanceof version_1.OpenQASMVersion) {
11
+ switch (version.major) {
12
+ case version_1.OpenQASMMajorVersion.Version2:
13
+ lexer = new lexer_1.default(qasm, cursor);
14
+ break;
15
+ case version_1.OpenQASMMajorVersion.Version3:
16
+ lexer = new lexer_2.default(qasm, cursor);
17
+ break;
18
+ default:
19
+ throw new errors_1.UnsupportedOpenQASMVersionError("Unsupported OpenQASM version detected: ".concat(version.major));
20
+ }
21
21
  }
22
- return false;
23
- }
24
- /**
25
- * Returns whether a given character is alphanumeric.
26
- * @param c - The character.
27
- * @return Whether the character is alphanumeric.
28
- */
29
- function isAlpha(c) {
30
- if (c.match(/^[0-9a-zA-Z]+$/)) {
31
- return true;
22
+ else if (typeof version === "number") {
23
+ switch (version) {
24
+ case 2:
25
+ lexer = new lexer_1.default(qasm, cursor);
26
+ break;
27
+ case 3:
28
+ lexer = new lexer_2.default(qasm, cursor);
29
+ break;
30
+ default:
31
+ throw new errors_1.UnsupportedOpenQASMVersionError("Unsupported OpenQASM version detected: ".concat(version));
32
+ }
32
33
  }
33
- return false;
34
- }
35
- /**
36
- * Returns whether a given character is a newline character.
37
- * @param c - The character.
38
- * @return Whether the character is a newline.
39
- */
40
- function isNewline(c) {
41
- if (c.match(/\n|\r(?!\n)|\u2028|\u2029|\r\n/g)) {
42
- return true;
34
+ else if (version === version_1.OpenQASMMajorVersion.Version2) {
35
+ lexer = new lexer_1.default(qasm, cursor);
43
36
  }
44
- return false;
45
- }
46
- /** Class representing a lexer. */
47
- var Lexer = /** @class */ (function () {
48
- /**
49
- * Creates a lexer.
50
- * @param input - The string to lex.
51
- * @param cursor - The starting cursor position.
52
- */
53
- function Lexer(input, cursor) {
54
- var _this = this;
55
- if (cursor === void 0) { cursor = 0; }
56
- this.verifyInput = function () {
57
- var lines = _this.input.split(/\n|\r(?!\n)|\u2028|\u2029|\r\n/g);
58
- for (var i = 0; i < lines.length; i++) {
59
- if (!lines[i].startsWith('//')
60
- && !(lines[i].length == 0)
61
- && !(lines[i].includes('gate'))
62
- && !(lines[i].trim() == '{' || lines[i].trim() == '}')
63
- && !lines[i].includes(';')) {
64
- return false;
65
- }
66
- }
67
- return true;
68
- };
69
- /**
70
- * Calling this method lexes the code represented by the provided string.
71
- * @return An array of tokens and their corresponding values.
72
- */
73
- this.lex = function () {
74
- var tokens = [];
75
- var token;
76
- if (!_this.verifyInput()) {
77
- throw errors_1.MissingSemicolonError;
78
- }
79
- while (_this.cursor < _this.input.length) {
80
- token = _this.nextToken();
81
- if (token) {
82
- tokens.push(token);
83
- }
84
- }
85
- return tokens;
86
- };
87
- /**
88
- * Reads a character and advances the cursor.
89
- * @param num - Optional cursor position modifier.
90
- */
91
- this.readChar = function (num) {
92
- if (num === void 0) { num = 1; }
93
- _this.cursor += num;
94
- return _this.input[_this.cursor - num];
95
- };
96
- /**
97
- * Advances the cusor past the next comment.
98
- */
99
- this.skipComment = function () {
100
- var char = '';
101
- while (!isNewline(char)) {
102
- char = _this.readChar();
103
- }
104
- };
105
- /**
106
- * Determines whether the next character to process equals a given character.
107
- * @param c - The given character.
108
- * @return Whether the next character equals the given character.
109
- */
110
- this.peekEq = function (c) { return (_this.peek() == c); };
111
- /**
112
- * Reads a character without advancing the cursor.
113
- * @param index - Optional peek position offset.
114
- */
115
- this.peek = function () { return _this.input[_this.cursor]; };
116
- /**
117
- * Reads a numeric value.
118
- * @return The numeric value as a string.
119
- */
120
- this.readNumeric = function () {
121
- var num = '';
122
- while (isNumeric(_this.peek())) {
123
- num += _this.readChar();
124
- }
125
- return num;
126
- };
127
- /**
128
- * Reads an identifier.
129
- * @return The identifier as a string.
130
- */
131
- this.readIdentifier = function () {
132
- var id = '';
133
- while (isAlpha(_this.peek())) {
134
- id += _this.readChar();
135
- }
136
- return id;
137
- };
138
- /**
139
- * Reads a string literal.
140
- * @param terminator - The literal's termination character.
141
- * @return The literal as a string.
142
- */
143
- this.readStringLiteral = function (terminator) {
144
- var lit = '';
145
- var char = '';
146
- while (!(terminator == char)) {
147
- char = _this.readChar();
148
- lit += char;
149
- }
150
- return lit;
151
- };
152
- /**
153
- * Advances the cusor past the next block of whitespace.
154
- */
155
- this.skipWhitespace = function () {
156
- while (' \t\n\r\v'.indexOf(_this.peek()) > -1) {
157
- _this.cursor += 1;
158
- }
159
- return null;
160
- };
161
- /**
162
- * Lexes the next token.
163
- * @return The next token and its corresponding value.
164
- */
165
- this.nextToken = function () {
166
- _this.skipWhitespace();
167
- if (_this.cursor == _this.input.length) {
168
- return [token_1.Token.EndOfFile];
169
- }
170
- var char = _this.peek();
171
- _this.readChar();
172
- switch (char) {
173
- case '=':
174
- if (_this.peekEq('=')) {
175
- _this.readChar();
176
- return [token_1.Token.Equals];
177
- }
178
- else {
179
- throw errors_1.BadEqualsError;
180
- }
181
- case '-':
182
- if (_this.peekEq('>')) {
183
- _this.readChar();
184
- return [token_1.Token.Arrow];
185
- }
186
- else {
187
- return [token_1.Token.Minus];
188
- }
189
- case '+':
190
- return [token_1.Token.Plus];
191
- case '*':
192
- return [token_1.Token.Times];
193
- case '^':
194
- return [token_1.Token.Power];
195
- case ';':
196
- return [token_1.Token.Semicolon];
197
- case ',':
198
- return [token_1.Token.Comma];
199
- case '(':
200
- return [token_1.Token.LParen];
201
- case '[':
202
- return [token_1.Token.LSParen];
203
- case '{':
204
- return [token_1.Token.LCParen];
205
- case ')':
206
- return [token_1.Token.RParen];
207
- case ']':
208
- return [token_1.Token.RSParen];
209
- case '}':
210
- return [token_1.Token.RCParen];
211
- case '/':
212
- if (_this.peekEq('/')) {
213
- _this.skipComment();
214
- return;
215
- }
216
- else {
217
- return [token_1.Token.Divide];
218
- }
219
- case 'g':
220
- if ((_this.input[_this.cursor] == 'a')
221
- && (_this.input[_this.cursor + 1] == 't')
222
- && (_this.input[_this.cursor + 2] == 'e')) {
223
- _this.readChar(3);
224
- return [token_1.Token.Gate];
225
- }
226
- var literal = char + _this.readIdentifier();
227
- return [token_1.lookup(literal), new String(literal)];
228
- case 'q':
229
- if ((_this.input[_this.cursor] == 'r')
230
- && (_this.input[_this.cursor + 1] == 'e')
231
- && (_this.input[_this.cursor + 2] == 'g')) {
232
- _this.readChar(3);
233
- return [token_1.Token.QReg];
234
- }
235
- var qregLit = char + _this.readIdentifier();
236
- return [token_1.lookup(qregLit), new String(qregLit)];
237
- case 'c':
238
- if ((_this.input[_this.cursor] == 'r')
239
- && (_this.input[_this.cursor + 1] == 'e')
240
- && (_this.input[_this.cursor + 2] == 'g')) {
241
- _this.readChar(3);
242
- return [token_1.Token.QReg];
243
- }
244
- var cregLit = char + _this.readIdentifier();
245
- return [token_1.lookup(cregLit), new String(cregLit)];
246
- case 'b':
247
- if ((_this.input[_this.cursor] == 'a')
248
- && (_this.input[_this.cursor + 1] == 'r')
249
- && (_this.input[_this.cursor + 2] == 'r')
250
- && (_this.input[_this.cursor + 3] == 'i')
251
- && (_this.input[_this.cursor + 4] == 'e')
252
- && (_this.input[_this.cursor + 5] == 'r')) {
253
- _this.readChar(6);
254
- return [token_1.Token.Barrier];
255
- }
256
- var barLit = char + _this.readIdentifier();
257
- return [token_1.lookup(barLit), new String(barLit)];
258
- case 'm':
259
- if ((_this.input[_this.cursor] == 'e')
260
- && (_this.input[_this.cursor + 1] == 'a')
261
- && (_this.input[_this.cursor + 2] == 's')
262
- && (_this.input[_this.cursor + 3] == 'u')
263
- && (_this.input[_this.cursor + 4] == 'r')
264
- && (_this.input[_this.cursor + 5] == 'e')) {
265
- _this.readChar(6);
266
- return [token_1.Token.Measure];
267
- }
268
- var measureLit = char + _this.readIdentifier();
269
- return [token_1.lookup(measureLit), new String(measureLit)];
270
- case '\"':
271
- var stringLiteral = char + _this.readStringLiteral('\"');
272
- return [token_1.Token.String, new String(stringLiteral)];
273
- case '\’':
274
- var singleStringLiteral = char + _this.readStringLiteral('\’');
275
- return [token_1.Token.String, new String(singleStringLiteral)];
276
- default:
277
- if (isLetter(char)) {
278
- var literal_1 = char + _this.readIdentifier();
279
- return [token_1.lookup(literal_1), new String(literal_1)];
280
- }
281
- else if (isNumeric(char)) {
282
- var num = char + _this.readNumeric();
283
- if (num.indexOf('.') != -1) {
284
- return [token_1.Token.Real, parseFloat(num)];
285
- }
286
- else {
287
- return [token_1.Token.NNInteger, parseFloat(num)];
288
- }
289
- }
290
- else {
291
- return [token_1.Token.Illegal];
292
- }
293
- }
294
- };
295
- this.input = input;
296
- this.cursor = cursor;
37
+ else if (version === version_1.OpenQASMMajorVersion.Version3) {
38
+ lexer = new lexer_2.default(qasm, cursor);
39
+ }
40
+ else {
41
+ lexer = new lexer_2.default(qasm, cursor);
297
42
  }
298
- return Lexer;
299
- }());
300
- exports["default"] = Lexer;
43
+ var tokens = lexer.lex();
44
+ return tokens;
45
+ }
package/dist/main.js CHANGED
@@ -1,18 +1,31 @@
1
1
  "use strict";
2
- exports.__esModule = true;
3
- var parser_1 = require("./parser");
4
- var lexer_1 = require("./lexer");
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.parseString = parseString;
5
+ exports.parseFile = parseFile;
5
6
  var fs = require("fs");
7
+ var lexer_1 = require("./lexer");
8
+ var parser_1 = require("./parser");
6
9
  /**
7
10
  * Returns the abstract syntax tree for a given string of QASM code.
8
11
  * @param qasm - The code string.
9
12
  * @return The corresponding AST.
10
13
  */
11
- function parseString(qasm) {
12
- var lexer = new lexer_1["default"](qasm, 0);
13
- var tokens = lexer.lex();
14
- var parser = new parser_1["default"](tokens);
15
- var ast = parser.parse();
14
+ function parseString(qasm, version, verbose, stringify) {
15
+ var ast;
16
+ var tokens = (0, lexer_1.lex)(qasm, undefined, version);
17
+ ast = (0, parser_1.parse)(tokens, version);
18
+ if (verbose === true) {
19
+ if (stringify === true) {
20
+ ast = JSON.stringify(getDetailedOutput(ast), null, 2);
21
+ }
22
+ ast = getDetailedOutput(ast);
23
+ }
24
+ else {
25
+ if (stringify === true) {
26
+ ast = JSON.stringify(ast, null, 2);
27
+ }
28
+ }
16
29
  return ast;
17
30
  }
18
31
  /**
@@ -20,7 +33,24 @@ function parseString(qasm) {
20
33
  * @param file - The file location.
21
34
  * @return The corresponding AST.
22
35
  */
23
- exports.parse = function (file) {
24
- return parseString(fs.readFileSync(file, 'utf8'));
25
- };
26
- exports.parseString = parseString;
36
+ function parseFile(file, version, verbose, stringify) {
37
+ return parseString(fs.readFileSync(file, "utf8"), version, verbose, stringify);
38
+ }
39
+ /** Adds class names to the ast. */
40
+ function getDetailedOutput(object) {
41
+ if (Array.isArray(object)) {
42
+ return object.map(getDetailedOutput);
43
+ }
44
+ else if (object !== null && typeof object === "object") {
45
+ var result = {};
46
+ result["__className__"] = object.constructor.name;
47
+ for (var _i = 0, _a = Object.entries(object); _i < _a.length; _i++) {
48
+ var _b = _a[_i], key = _b[0], value = _b[1];
49
+ result[key] = getDetailedOutput(value);
50
+ }
51
+ return result;
52
+ }
53
+ else {
54
+ return object;
55
+ }
56
+ }