qasm-ts 1.1.3 → 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
@@ -0,0 +1,615 @@
1
+ "use strict";
2
+ /* eslint-disable no-useless-escape */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ var token_1 = require("./token");
5
+ var errors_1 = require("../errors");
6
+ /**
7
+ * Handles throwing lexer errors with basic stack trace.
8
+ * @param error - The error to throw.
9
+ * @param number - The line number in the source code.
10
+ * @param code - The source code that the error is about.
11
+ */
12
+ function throwLexerError(error, line, code) {
13
+ throw new error("Line ".concat(line, ": ").concat(code));
14
+ }
15
+ /**
16
+ * Returns whether a given character could be an element of a numeric value.
17
+ * @param c - The character.
18
+ * @return Whether the character is numeric.
19
+ */
20
+ function isNumeric(c) {
21
+ return c == "." || c == "e" || c == "_" || !isNaN(parseInt(c));
22
+ }
23
+ /**
24
+ * Returns whether a given character is a letter.
25
+ * @param c - The character.
26
+ * @param matchCase - Whether to check for a letter that is upper case, lower case, or either. (optional)
27
+ * @return Whether the character is a letter.
28
+ */
29
+ function isLetter(c, matchCase) {
30
+ switch (matchCase) {
31
+ case "upper":
32
+ return /^[A-Z]$/.test(c);
33
+ case "lower":
34
+ return /^[a-z]$/.test(c);
35
+ default:
36
+ return /^[A-Za-z]$/.test(c);
37
+ }
38
+ }
39
+ /**
40
+ * Returns whether a given character is unicode.
41
+ * @param c - The character.
42
+ * @param excludePi - Whether to exclude the Pi symbol from consideration.
43
+ * @return - Whether the given character is valid unicode.
44
+ */
45
+ function isUnicode(c, excludePi) {
46
+ var isBasicUnicode = /^\u0000-\u00ff/.test(c);
47
+ switch (excludePi) {
48
+ case true:
49
+ return isBasicUnicode && c !== "\u03C0";
50
+ case false:
51
+ return isBasicUnicode;
52
+ default:
53
+ return isBasicUnicode;
54
+ }
55
+ }
56
+ /**
57
+ * Returns whether a given character is alphanumeric.
58
+ * @param c - The character.
59
+ * @return Whether the character is alphanumeric.
60
+ */
61
+ function isAlpha(c) {
62
+ return /^[0-9a-zA-Z]+$/.test(c);
63
+ }
64
+ /**
65
+ * Returns whether a given character is a newline character.
66
+ * @param c - The character.
67
+ * @return Whether the character is a newline.
68
+ */
69
+ function isNewline(c) {
70
+ return /\n|\r(?!\n)|\u2028|\u2029|\r\n/.test(c);
71
+ }
72
+ /** Class representing a lexer. */
73
+ var Lexer = /** @class */ (function () {
74
+ /**
75
+ * Creates a lexer.
76
+ * @param input - The string to lex.
77
+ * @param cursor - The starting cursor position.
78
+ */
79
+ function Lexer(input, cursor) {
80
+ if (cursor === void 0) { cursor = 0; }
81
+ var _this = this;
82
+ /**
83
+ * Verifies that all appropriate lines end with a semicolon.
84
+ * @return A tuple of the status and if False, returns the problematic line.
85
+ */
86
+ this.verifyInput = function () {
87
+ var lines = _this.input.split(/\n|\r(?!\n)|\u2028|\u2029|\r\n/g);
88
+ for (var i = 0; i < lines.length; i++) {
89
+ var trimmedLine = lines[i].trim();
90
+ if (!trimmedLine.startsWith("//") &&
91
+ !trimmedLine.startsWith("/*") &&
92
+ !trimmedLine.startsWith("*") &&
93
+ !trimmedLine.startsWith("*/") &&
94
+ trimmedLine.length > 0 &&
95
+ !trimmedLine.startsWith("gate") &&
96
+ trimmedLine !== "{" &&
97
+ trimmedLine !== "}" &&
98
+ !trimmedLine.includes(";") &&
99
+ !trimmedLine.startsWith("def") &&
100
+ !trimmedLine.startsWith("if") &&
101
+ !trimmedLine.startsWith("else") &&
102
+ !trimmedLine.startsWith("for") &&
103
+ !trimmedLine.startsWith("while") &&
104
+ !trimmedLine.startsWith("switch") &&
105
+ !trimmedLine.startsWith("case") &&
106
+ !trimmedLine.startsWith("box") &&
107
+ !trimmedLine.startsWith("array") &&
108
+ !trimmedLine.startsWith("{") &&
109
+ !trimmedLine.startsWith("default")) {
110
+ return [false, i + 1, lines[i]];
111
+ }
112
+ }
113
+ return [true, null, null];
114
+ };
115
+ /**
116
+ * Calling this method lexes the code represented by the provided string.
117
+ * @return An array of tokens and their corresponding values.
118
+ */
119
+ this.lex = function () {
120
+ var tokens = [];
121
+ var token;
122
+ var verifyInputResult = _this.verifyInput();
123
+ if (!verifyInputResult[0]) {
124
+ throwLexerError(errors_1.MissingSemicolonError, verifyInputResult[1], verifyInputResult[2]);
125
+ }
126
+ while (_this.cursor < _this.input.length) {
127
+ token = _this.nextToken();
128
+ if (token) {
129
+ tokens.push(token);
130
+ }
131
+ }
132
+ return tokens;
133
+ };
134
+ /**
135
+ * Reads a character and advances the cursor.
136
+ * @param num - Optional cursor position modifier.
137
+ */
138
+ this.readChar = function (num) {
139
+ if (num === void 0) { num = 1; }
140
+ _this.cursor += num;
141
+ return _this.input[_this.cursor - num];
142
+ };
143
+ /**
144
+ * Advances the cusor past the next comment.
145
+ */
146
+ this.skipComment = function () {
147
+ var char = "";
148
+ while (!isNewline(char)) {
149
+ char = _this.readChar();
150
+ }
151
+ };
152
+ /**
153
+ * Advances the cursor past a multiline comment.
154
+ */
155
+ this.skipMultiLineComment = function () {
156
+ var char = "";
157
+ var nextChar = "";
158
+ var multiLineCommentTerminator = "*/";
159
+ while ("".concat(char).concat(nextChar) !== multiLineCommentTerminator) {
160
+ char = _this.readChar();
161
+ nextChar = _this.peek();
162
+ }
163
+ _this.readChar();
164
+ };
165
+ /**
166
+ * Determines whether the next character to process equals a given character.
167
+ * @param c - The given character.
168
+ * @return Whether the next character equals the given character.
169
+ */
170
+ this.peekEq = function (c) { return _this.peek() == c; };
171
+ /**
172
+ * Reads a character without advancing the cursor.
173
+ * @param index - Optional peek position offset.
174
+ */
175
+ this.peek = function () { return _this.input[_this.cursor]; };
176
+ /**
177
+ * Reads a numeric value.
178
+ * @return The numeric value as a string.
179
+ */
180
+ this.readNumeric = function () {
181
+ var num = "";
182
+ var char = _this.peek();
183
+ while (isNumeric(char) || char === "e") {
184
+ num += _this.readChar();
185
+ if (char === "e" && _this.peek() === "-") {
186
+ num += _this.readChar();
187
+ }
188
+ char = _this.peek();
189
+ }
190
+ return num;
191
+ };
192
+ /**
193
+ * Reads an identifier.
194
+ * @return The identifier as a string.
195
+ */
196
+ this.readIdentifier = function () {
197
+ var id = "";
198
+ var next = _this.peek();
199
+ while (isAlpha(next) || next == "_" || isUnicode(next)) {
200
+ id += _this.readChar();
201
+ next = _this.peek();
202
+ }
203
+ return id;
204
+ };
205
+ /**
206
+ * Reds a keyword or identifier.
207
+ * If the character sequence matches a keyword, returns the corresponding token.
208
+ * Otherwise, treats the sequence as an identifier.
209
+ * @param char - The first character of the keyword or identifier.
210
+ * @return The corresponding token or identifier.
211
+ */
212
+ this.readKeywordOrIdentifier = function (char) {
213
+ var identifier = char + _this.readIdentifier();
214
+ return [(0, token_1.lookup)(identifier), identifier];
215
+ };
216
+ /**
217
+ * Reads a string literal.
218
+ * @param terminator - The literal's termination character.
219
+ * @return The literal as a string.
220
+ */
221
+ this.readStringLiteral = function (terminator) {
222
+ var lit = "";
223
+ var char = "";
224
+ while (!(terminator == char)) {
225
+ char = _this.readChar();
226
+ lit += char;
227
+ }
228
+ return lit;
229
+ };
230
+ /**
231
+ * Advances the cusor past the next block of whitespace.
232
+ */
233
+ this.skipWhitespace = function () {
234
+ while (" \t\n\r\v".indexOf(_this.peek()) > -1) {
235
+ _this.cursor += 1;
236
+ }
237
+ return null;
238
+ };
239
+ /**
240
+ * Lexes the next token.
241
+ * @return The next token and its corresponding value.
242
+ */
243
+ this.nextToken = function () {
244
+ _this.skipWhitespace();
245
+ if (_this.cursor == _this.input.length) {
246
+ return [token_1.Token.EndOfFile];
247
+ }
248
+ var char = _this.peek();
249
+ _this.readChar();
250
+ switch (char) {
251
+ case "π":
252
+ return [token_1.Token.Pi];
253
+ case "ℇ":
254
+ return [token_1.Token.Euler];
255
+ case "τ":
256
+ return [token_1.Token.Tau];
257
+ case "@":
258
+ return [token_1.Token.At];
259
+ case "θ":
260
+ return _this.readKeywordOrIdentifier(char);
261
+ case "!":
262
+ if (_this.peekEq("=")) {
263
+ _this.readChar();
264
+ return [token_1.Token.BinaryOp, "!="];
265
+ }
266
+ else {
267
+ return [token_1.Token.UnaryOp, "!"];
268
+ }
269
+ case "~":
270
+ return [token_1.Token.UnaryOp, "~"];
271
+ case "*":
272
+ if (_this.peekEq("*")) {
273
+ _this.readChar();
274
+ if (_this.peekEq("=")) {
275
+ _this.readChar();
276
+ return [token_1.Token.CompoundArithmeticOp, "**="];
277
+ }
278
+ else {
279
+ return [token_1.Token.ArithmeticOp, "**"];
280
+ }
281
+ }
282
+ else if (_this.peekEq("=")) {
283
+ _this.readChar();
284
+ return [token_1.Token.CompoundArithmeticOp, "*="];
285
+ }
286
+ else {
287
+ return [token_1.Token.ArithmeticOp, "*"];
288
+ }
289
+ case "/":
290
+ if (_this.peekEq("/")) {
291
+ _this.skipComment();
292
+ return;
293
+ }
294
+ else if (_this.peekEq("*")) {
295
+ _this.skipMultiLineComment();
296
+ return;
297
+ }
298
+ else if (_this.peekEq("=")) {
299
+ _this.readChar();
300
+ return [token_1.Token.CompoundArithmeticOp, "/="];
301
+ }
302
+ else {
303
+ return [token_1.Token.ArithmeticOp, "/"];
304
+ }
305
+ case "%":
306
+ if (_this.peekEq("=")) {
307
+ _this.readChar();
308
+ return [token_1.Token.CompoundArithmeticOp, "%="];
309
+ }
310
+ else {
311
+ return [token_1.Token.ArithmeticOp, "%"];
312
+ }
313
+ case "+":
314
+ if (_this.peekEq("=")) {
315
+ _this.readChar();
316
+ return [token_1.Token.CompoundArithmeticOp, "+="];
317
+ }
318
+ else if (_this.peekEq("+")) {
319
+ _this.readChar();
320
+ return [token_1.Token.ArithmeticOp, "++"];
321
+ }
322
+ return [token_1.Token.ArithmeticOp, "+"];
323
+ case "-": {
324
+ if (_this.peekEq(">")) {
325
+ _this.readChar();
326
+ return [token_1.Token.Arrow];
327
+ }
328
+ else if (_this.peekEq("=")) {
329
+ _this.readChar();
330
+ return [token_1.Token.CompoundArithmeticOp, "-="];
331
+ }
332
+ return [token_1.Token.UnaryOp, "-"];
333
+ }
334
+ case "&":
335
+ if (_this.peekEq("&")) {
336
+ _this.readChar();
337
+ return [token_1.Token.BinaryOp, "&&"];
338
+ }
339
+ else {
340
+ return [token_1.Token.BinaryOp, "&"];
341
+ }
342
+ case "|":
343
+ if (_this.peekEq("|")) {
344
+ _this.readChar();
345
+ return [token_1.Token.BinaryOp, "||"];
346
+ }
347
+ else {
348
+ return [token_1.Token.BinaryOp, "|"];
349
+ }
350
+ case "^":
351
+ if (_this.peekEq("=")) {
352
+ _this.readChar();
353
+ return [token_1.Token.CompoundBinaryOp, "^="];
354
+ }
355
+ return [token_1.Token.BinaryOp, "^"];
356
+ case "<":
357
+ if (_this.peekEq("=")) {
358
+ _this.readChar();
359
+ return [token_1.Token.BinaryOp, "<="];
360
+ }
361
+ else if (_this.input[_this.cursor] == "<" &&
362
+ _this.input[_this.cursor + 1] == "=") {
363
+ _this.readChar(2);
364
+ return [token_1.Token.CompoundBinaryOp, "<<="];
365
+ }
366
+ else if (_this.peekEq("<")) {
367
+ _this.readChar();
368
+ return [token_1.Token.BinaryOp, "<<"];
369
+ }
370
+ else {
371
+ return [token_1.Token.BinaryOp, "<"];
372
+ }
373
+ case ">":
374
+ if (_this.peekEq("=")) {
375
+ _this.readChar();
376
+ return [token_1.Token.BinaryOp, ">="];
377
+ }
378
+ else if (_this.input[_this.cursor] == ">" &&
379
+ _this.input[_this.cursor + 1] == "=") {
380
+ _this.readChar(2);
381
+ return [token_1.Token.CompoundBinaryOp, ">>="];
382
+ }
383
+ else if (_this.peekEq(">")) {
384
+ _this.readChar();
385
+ return [token_1.Token.BinaryOp, ">>"];
386
+ }
387
+ else {
388
+ return [token_1.Token.BinaryOp, ">"];
389
+ }
390
+ case "=":
391
+ if (_this.peekEq("=")) {
392
+ _this.readChar();
393
+ return [token_1.Token.BinaryOp, "=="];
394
+ }
395
+ else {
396
+ return [token_1.Token.EqualsAssmt];
397
+ }
398
+ case ";":
399
+ return [token_1.Token.Semicolon];
400
+ case ",":
401
+ return [token_1.Token.Comma];
402
+ case ":":
403
+ return [token_1.Token.Colon];
404
+ case "(":
405
+ return [token_1.Token.LParen];
406
+ case "[":
407
+ return [token_1.Token.LSParen];
408
+ case "{":
409
+ return [token_1.Token.LCParen];
410
+ case ")":
411
+ return [token_1.Token.RParen];
412
+ case "]":
413
+ return [token_1.Token.RSParen];
414
+ case "}":
415
+ return [token_1.Token.RCParen];
416
+ case "$":
417
+ return [token_1.Token.Dollar];
418
+ case "#":
419
+ if (_this.input[_this.cursor] == "d" &&
420
+ _this.input[_this.cursor + 1] == "i" &&
421
+ _this.input[_this.cursor + 2] == "m") {
422
+ _this.readChar(3);
423
+ return [token_1.Token.Dim];
424
+ }
425
+ return _this.readKeywordOrIdentifier(char);
426
+ case "c":
427
+ if (_this.input[_this.cursor] == "t" &&
428
+ _this.input[_this.cursor + 1] == "r" &&
429
+ _this.input[_this.cursor + 2] == "l") {
430
+ _this.readChar(3);
431
+ return _this.lexGateModifier("ctrl");
432
+ }
433
+ return _this.readKeywordOrIdentifier(char);
434
+ case "n":
435
+ if (_this.input[_this.cursor] == "e" &&
436
+ _this.input[_this.cursor + 1] == "g" &&
437
+ _this.input[_this.cursor + 2] == "c" &&
438
+ _this.input[_this.cursor + 3] == "t" &&
439
+ _this.input[_this.cursor + 4] == "r" &&
440
+ _this.input[_this.cursor + 5] == "l") {
441
+ _this.readChar(6);
442
+ return _this.lexGateModifier("negctrl");
443
+ }
444
+ return _this.readKeywordOrIdentifier(char);
445
+ case "i":
446
+ if (_this.input[_this.cursor] == "n" &&
447
+ _this.input[_this.cursor + 1] == "v") {
448
+ _this.readChar(2);
449
+ return _this.lexGateModifier("inv");
450
+ }
451
+ return _this.readKeywordOrIdentifier(char);
452
+ case "p":
453
+ if (_this.input[_this.cursor] == "o" &&
454
+ _this.input[_this.cursor + 1] == "w" &&
455
+ !isLetter(_this.input[_this.cursor + 2])) {
456
+ _this.readChar(2);
457
+ return _this.lexGateModifier("pow");
458
+ }
459
+ return _this.readKeywordOrIdentifier(char);
460
+ case "0":
461
+ if (_this.peekEq("b") || _this.peekEq("B")) {
462
+ var char_1 = _this.readChar(1);
463
+ var binaryLit = _this.readIdentifier();
464
+ return [token_1.Token.BinaryLiteral, "0".concat(char_1).concat(binaryLit)];
465
+ }
466
+ else if (_this.peekEq("o") || _this.peekEq("O")) {
467
+ var char_2 = _this.readChar(1);
468
+ var octalLit = _this.readIdentifier();
469
+ return [token_1.Token.OctalLiteral, "0".concat(char_2).concat(octalLit)];
470
+ }
471
+ else if (_this.peekEq("x") || _this.peekEq("X")) {
472
+ var char_3 = _this.readChar(1);
473
+ var hexLit = _this.readIdentifier();
474
+ return [token_1.Token.HexLiteral, "0".concat(char_3).concat(hexLit)];
475
+ }
476
+ else {
477
+ var num = char + _this.readNumeric();
478
+ if (num.indexOf(".") != -1) {
479
+ return [token_1.Token.Real, parseFloat(num)];
480
+ }
481
+ else if (num.indexOf("_") != -1) {
482
+ return [token_1.Token.Integer, num];
483
+ }
484
+ else {
485
+ return [token_1.Token.NNInteger, parseInt(num)];
486
+ }
487
+ }
488
+ return _this.readKeywordOrIdentifier(char);
489
+ case "O":
490
+ if (_this.input[_this.cursor].toLowerCase() == "p" &&
491
+ _this.input[_this.cursor + 1].toLowerCase() == "e" &&
492
+ _this.input[_this.cursor + 2].toLowerCase() == "n" &&
493
+ _this.input[_this.cursor + 3] == "Q" &&
494
+ _this.input[_this.cursor + 4] == "A" &&
495
+ _this.input[_this.cursor + 5] == "S" &&
496
+ _this.input[_this.cursor + 6] == "M") {
497
+ _this.readChar(7);
498
+ var offset = 0;
499
+ while (_this.cursor + offset < _this.input.length &&
500
+ " \t".indexOf(_this.input[_this.cursor + offset]) > -1) {
501
+ offset++;
502
+ }
503
+ // Read the major version
504
+ var majorVersion = "";
505
+ while (_this.cursor + offset < _this.input.length &&
506
+ !isNaN(parseInt(_this.input[_this.cursor + offset], 10))) {
507
+ majorVersion += _this.input[_this.cursor + offset];
508
+ offset++;
509
+ }
510
+ // Attempt to read the minor version
511
+ var minorVersion = undefined;
512
+ if (_this.input[_this.cursor + offset] == ".") {
513
+ offset++;
514
+ minorVersion = "";
515
+ while (_this.cursor + offset < _this.input.length &&
516
+ !isNaN(parseInt(_this.input[_this.cursor + offset], 10))) {
517
+ minorVersion += _this.input[_this.cursor + offset];
518
+ offset++;
519
+ }
520
+ }
521
+ // Parse major and minor versions
522
+ var major = parseInt(majorVersion, 10);
523
+ var minor = minorVersion ? parseInt(minorVersion, 10) : undefined;
524
+ if (major !== 3) {
525
+ throw new errors_1.UnsupportedOpenQASMVersionError("Unsupported OpenQASM version detected: ".concat(majorVersion, ".").concat(minor !== null && minor !== void 0 ? minor : 0));
526
+ }
527
+ return [token_1.Token.OpenQASM];
528
+ }
529
+ return _this.readKeywordOrIdentifier(char);
530
+ case '"': {
531
+ var stringLiteral = char + _this.readStringLiteral('"');
532
+ return [token_1.Token.String, stringLiteral];
533
+ }
534
+ case "’": {
535
+ var singleStringLiteral = char + _this.readStringLiteral("’");
536
+ return [token_1.Token.String, singleStringLiteral];
537
+ }
538
+ default:
539
+ if (isLetter(char)) {
540
+ return _this.readKeywordOrIdentifier(char);
541
+ }
542
+ else if (isNumeric(char)) {
543
+ var num = char + _this.readNumeric();
544
+ if (num.indexOf("e") != -1) {
545
+ return [token_1.Token.ScientificNotation, num];
546
+ }
547
+ else if (num.indexOf(".") != -1) {
548
+ return [token_1.Token.Real, parseFloat(num)];
549
+ }
550
+ else if (num.indexOf("_") != -1) {
551
+ return [token_1.Token.Integer, num];
552
+ }
553
+ else {
554
+ return [token_1.Token.NNInteger, parseInt(num)];
555
+ }
556
+ }
557
+ else {
558
+ return [token_1.Token.Illegal];
559
+ }
560
+ }
561
+ };
562
+ /**
563
+ * Returns the line number where the current cursor is located.
564
+ * @param cursor - The current cursor position in the input string.
565
+ * @return The line number.
566
+ */
567
+ this.getLineNumber = function (cursor) {
568
+ return _this.input
569
+ .substring(0, cursor)
570
+ .split(/\n|\r(?!\n)|\u2028|\u2029|\r\n/).length;
571
+ };
572
+ /**
573
+ * Returns the current line of code where the cursor is located.
574
+ * @param cursor - The current cursor position in the input string.
575
+ * @return The specific line where the cursor is located.
576
+ */
577
+ this.getCurrentLine = function (cursor) {
578
+ var lines = _this.input.split(/\n|\r(?!\n)|\u2028|\u2029|\r\n/);
579
+ var lineNumber = _this.getLineNumber(cursor);
580
+ return lines[lineNumber - 1];
581
+ };
582
+ /**
583
+ * Retruns an identifier or gate modifier.
584
+ */
585
+ this.lexGateModifier = function (keyword) {
586
+ var offset = 1;
587
+ var currChar = "";
588
+ while (_this.cursor + offset < _this.input.length &&
589
+ _this.input[_this.cursor + offset] !== ";") {
590
+ currChar = _this.input[_this.cursor + offset];
591
+ if (currChar === "@") {
592
+ break;
593
+ }
594
+ offset++;
595
+ }
596
+ if (currChar === "@") {
597
+ switch (keyword) {
598
+ case "ctrl":
599
+ return [token_1.Token.Ctrl];
600
+ case "negctrl":
601
+ return [token_1.Token.NegCtrl];
602
+ case "inv":
603
+ return [token_1.Token.Inv];
604
+ case "pow":
605
+ return [token_1.Token.PowM];
606
+ }
607
+ }
608
+ return _this.readKeywordOrIdentifier(keyword);
609
+ };
610
+ this.input = input;
611
+ this.cursor = cursor;
612
+ }
613
+ return Lexer;
614
+ }());
615
+ exports.default = Lexer;