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,436 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var token_1 = require("./token");
4
+ var errors_1 = require("../errors");
5
+ /**
6
+ * Handles throwing lexer errors with basic stack trace.
7
+ * @param error - The error to throw.
8
+ * @param number - The line number in the source code.
9
+ * @param code - The source code that the error is about.
10
+ */
11
+ function throwLexerError(error, line, code) {
12
+ throw new error("Line ".concat(line, ": ").concat(code));
13
+ }
14
+ /**
15
+ * Returns whether a given character could be an element of a numeric value.
16
+ * @param c - The character.
17
+ * @return Whether the character is numeric.
18
+ */
19
+ function isNumeric(c) {
20
+ return c == "." || !isNaN(parseInt(c));
21
+ }
22
+ /**
23
+ * Returns whether a given character is a letter.
24
+ * @param c - The character.
25
+ * @param matchCase - Whether to check for a letter that is upper case, lower case, or either. (optional)
26
+ * @return Whether the character is a letter.
27
+ */
28
+ function isLetter(c, matchCase) {
29
+ switch (matchCase) {
30
+ case "upper":
31
+ return /^[A-Z]$/.test(c);
32
+ case "lower":
33
+ return /^[a-z]$/.test(c);
34
+ default:
35
+ return /^[A-Za-z]$/.test(c);
36
+ }
37
+ }
38
+ /**
39
+ * Returns whether a given character is unicode.
40
+ * @param c - The character.
41
+ * @param excludePi - Whether to exclude the Pi symbol from consideration.
42
+ * @return - Whether the given character is valid unicode.
43
+ */
44
+ function isUnicode(c, excludePi) {
45
+ var isBasicUnicode = /^\u0000-\u00ff/.test(c);
46
+ switch (excludePi) {
47
+ case true:
48
+ return isBasicUnicode && c !== "\u03C0";
49
+ case false:
50
+ return isBasicUnicode;
51
+ default:
52
+ return isBasicUnicode;
53
+ }
54
+ }
55
+ /**
56
+ * Returns whether a given character is alphanumeric.
57
+ * @param c - The character.
58
+ * @return Whether the character is alphanumeric.
59
+ */
60
+ function isAlpha(c) {
61
+ return /^[0-9a-zA-Z]+$/.test(c);
62
+ }
63
+ /**
64
+ * Returns whether a given character is a newline character.
65
+ * @param c - The character.
66
+ * @return Whether the character is a newline.
67
+ */
68
+ function isNewline(c) {
69
+ return /\n|\r(?!\n)|\u2028|\u2029|\r\n/.test(c);
70
+ }
71
+ /** Class representing a lexer. */
72
+ var Lexer = /** @class */ (function () {
73
+ /**
74
+ * Creates a lexer.
75
+ * @param input - The string to lex.
76
+ * @param cursor - The starting cursor position.
77
+ */
78
+ function Lexer(input, cursor) {
79
+ if (cursor === void 0) { cursor = 0; }
80
+ var _this = this;
81
+ /**
82
+ * Verifies that all appropriate lines end with a semicolon.
83
+ * @return A tuple of the status and if False, returns the problematic line.
84
+ */
85
+ this.verifyInput = function () {
86
+ var lines = _this.input.split(/\n|\r(?!\n)|\u2028|\u2029|\r\n/g);
87
+ for (var i = 0; i < lines.length; i++) {
88
+ if (!lines[i].startsWith("//") &&
89
+ !(lines[i].length == 0) &&
90
+ !lines[i].includes("gate") &&
91
+ !(lines[i].trim() == "{" || lines[i].trim() == "}") &&
92
+ !lines[i].includes(";")) {
93
+ return [false, i + 1, lines[i]];
94
+ }
95
+ }
96
+ return [true, null, null];
97
+ };
98
+ /**
99
+ * Calling this method lexes the code represented by the provided string.
100
+ * @return An array of tokens and their corresponding values.
101
+ */
102
+ this.lex = function () {
103
+ var tokens = [];
104
+ var token;
105
+ var verifyInputResult = _this.verifyInput();
106
+ if (!verifyInputResult[0]) {
107
+ throwLexerError(errors_1.MissingSemicolonError, verifyInputResult[1], verifyInputResult[2]);
108
+ }
109
+ while (_this.cursor < _this.input.length) {
110
+ token = _this.nextToken();
111
+ if (token) {
112
+ tokens.push(token);
113
+ }
114
+ }
115
+ return tokens;
116
+ };
117
+ /**
118
+ * Reads a character and advances the cursor.
119
+ * @param num - Optional cursor position modifier.
120
+ */
121
+ this.readChar = function (num) {
122
+ if (num === void 0) { num = 1; }
123
+ _this.cursor += num;
124
+ return _this.input[_this.cursor - num];
125
+ };
126
+ /**
127
+ * Advances the cusor past the next comment.
128
+ */
129
+ this.skipComment = function () {
130
+ var char = "";
131
+ while (!isNewline(char)) {
132
+ char = _this.readChar();
133
+ }
134
+ };
135
+ /**
136
+ * Determines whether the next character to process equals a given character.
137
+ * @param c - The given character.
138
+ * @return Whether the next character equals the given character.
139
+ */
140
+ this.peekEq = function (c) { return _this.peek() == c; };
141
+ /**
142
+ * Reads a character without advancing the cursor.
143
+ * @param index - Optional peek position offset.
144
+ */
145
+ this.peek = function () { return _this.input[_this.cursor]; };
146
+ /**
147
+ * Reads a numeric value.
148
+ * @return The numeric value as a string.
149
+ */
150
+ this.readNumeric = function () {
151
+ var num = "";
152
+ while (isNumeric(_this.peek())) {
153
+ num += _this.readChar();
154
+ }
155
+ return num;
156
+ };
157
+ /**
158
+ * Reads an identifier.
159
+ * @return The identifier as a string.
160
+ */
161
+ this.readIdentifier = function () {
162
+ var id = "";
163
+ var next = _this.peek();
164
+ while (isAlpha(next) || next == "_" || isUnicode(next)) {
165
+ id += _this.readChar();
166
+ next = _this.peek();
167
+ }
168
+ return id;
169
+ };
170
+ /**
171
+ * Reads a string literal.
172
+ * @param terminator - The literal's termination character.
173
+ * @return The literal as a string.
174
+ */
175
+ this.readStringLiteral = function (terminator) {
176
+ var lit = "";
177
+ var char = "";
178
+ while (!(terminator == char)) {
179
+ char = _this.readChar();
180
+ lit += char;
181
+ }
182
+ return lit;
183
+ };
184
+ /**
185
+ * Advances the cusor past the next block of whitespace.
186
+ */
187
+ this.skipWhitespace = function () {
188
+ while (" \t\n\r\v".indexOf(_this.peek()) > -1) {
189
+ _this.cursor += 1;
190
+ }
191
+ return null;
192
+ };
193
+ /**
194
+ * Lexes the next token.
195
+ * @return The next token and its corresponding value.
196
+ */
197
+ this.nextToken = function () {
198
+ _this.skipWhitespace();
199
+ if (_this.cursor == _this.input.length) {
200
+ return [token_1.Token.EndOfFile];
201
+ }
202
+ var char = _this.peek();
203
+ _this.readChar();
204
+ switch (char) {
205
+ case "=":
206
+ if (_this.peekEq("=")) {
207
+ _this.readChar();
208
+ return [token_1.Token.Equals];
209
+ }
210
+ else {
211
+ throwLexerError(errors_1.BadEqualsError, _this.getLineNumber(_this.cursor), _this.getCurrentLine(_this.cursor));
212
+ break;
213
+ }
214
+ case "-":
215
+ if (_this.peekEq(">")) {
216
+ _this.readChar();
217
+ return [token_1.Token.Arrow];
218
+ }
219
+ else {
220
+ return [token_1.Token.Minus];
221
+ }
222
+ case "+":
223
+ return [token_1.Token.Plus];
224
+ case "*":
225
+ return [token_1.Token.Times];
226
+ case "^":
227
+ return [token_1.Token.Power];
228
+ case ";":
229
+ return [token_1.Token.Semicolon];
230
+ case ",":
231
+ return [token_1.Token.Comma];
232
+ case "(":
233
+ return [token_1.Token.LParen];
234
+ case "[":
235
+ return [token_1.Token.LSParen];
236
+ case "{":
237
+ return [token_1.Token.LCParen];
238
+ case ")":
239
+ return [token_1.Token.RParen];
240
+ case "]":
241
+ return [token_1.Token.RSParen];
242
+ case "}":
243
+ return [token_1.Token.RCParen];
244
+ case "/":
245
+ if (_this.peekEq("/")) {
246
+ _this.skipComment();
247
+ return;
248
+ }
249
+ else {
250
+ return [token_1.Token.Divide];
251
+ }
252
+ case "g":
253
+ if (_this.input[_this.cursor] == "a" &&
254
+ _this.input[_this.cursor + 1] == "t" &&
255
+ _this.input[_this.cursor + 2] == "e") {
256
+ _this.readChar(3);
257
+ return [token_1.Token.Gate];
258
+ }
259
+ {
260
+ var literal = char + _this.readIdentifier();
261
+ return [(0, token_1.lookup)(literal), literal];
262
+ }
263
+ case "q":
264
+ if (_this.input[_this.cursor] == "r" &&
265
+ _this.input[_this.cursor + 1] == "e" &&
266
+ _this.input[_this.cursor + 2] == "g") {
267
+ _this.readChar(3);
268
+ return [token_1.Token.QReg];
269
+ }
270
+ {
271
+ var qregLit = char + _this.readIdentifier();
272
+ return [(0, token_1.lookup)(qregLit), qregLit];
273
+ }
274
+ case "c":
275
+ if (_this.input[_this.cursor] == "r" &&
276
+ _this.input[_this.cursor + 1] == "e" &&
277
+ _this.input[_this.cursor + 2] == "g") {
278
+ _this.readChar(3);
279
+ return [token_1.Token.CReg];
280
+ }
281
+ {
282
+ var cregLit = char + _this.readIdentifier();
283
+ return [(0, token_1.lookup)(cregLit), cregLit];
284
+ }
285
+ case "b":
286
+ if (_this.input[_this.cursor] == "a" &&
287
+ _this.input[_this.cursor + 1] == "r" &&
288
+ _this.input[_this.cursor + 2] == "r" &&
289
+ _this.input[_this.cursor + 3] == "i" &&
290
+ _this.input[_this.cursor + 4] == "e" &&
291
+ _this.input[_this.cursor + 5] == "r") {
292
+ _this.readChar(6);
293
+ return [token_1.Token.Barrier];
294
+ }
295
+ {
296
+ var barLit = char + _this.readIdentifier();
297
+ return [(0, token_1.lookup)(barLit), barLit];
298
+ }
299
+ case "m":
300
+ if (_this.input[_this.cursor] == "e" &&
301
+ _this.input[_this.cursor + 1] == "a" &&
302
+ _this.input[_this.cursor + 2] == "s" &&
303
+ _this.input[_this.cursor + 3] == "u" &&
304
+ _this.input[_this.cursor + 4] == "r" &&
305
+ _this.input[_this.cursor + 5] == "e") {
306
+ _this.readChar(6);
307
+ return [token_1.Token.Measure];
308
+ }
309
+ {
310
+ var measureLit = char + _this.readIdentifier();
311
+ return [(0, token_1.lookup)(measureLit), measureLit];
312
+ }
313
+ case "O":
314
+ if (_this.input[_this.cursor].toLowerCase() == "p" &&
315
+ _this.input[_this.cursor + 1].toLowerCase() == "e" &&
316
+ _this.input[_this.cursor + 2].toLowerCase() == "n" &&
317
+ _this.input[_this.cursor + 3] == "Q" &&
318
+ _this.input[_this.cursor + 4] == "A" &&
319
+ _this.input[_this.cursor + 5] == "S" &&
320
+ _this.input[_this.cursor + 6] == "M") {
321
+ _this.readChar(7);
322
+ var offset = 0;
323
+ while (_this.cursor + offset < _this.input.length &&
324
+ " \t".indexOf(_this.input[_this.cursor + offset]) > -1) {
325
+ offset++;
326
+ }
327
+ // Read the major version
328
+ var majorVersion = "";
329
+ while (_this.cursor + offset < _this.input.length &&
330
+ !isNaN(parseInt(_this.input[_this.cursor + offset], 10))) {
331
+ majorVersion += _this.input[_this.cursor + offset];
332
+ offset++;
333
+ }
334
+ // Attempt to read the minor version
335
+ var minorVersion = undefined;
336
+ if (_this.input[_this.cursor + offset] == ".") {
337
+ offset++;
338
+ minorVersion = "";
339
+ while (_this.cursor + offset < _this.input.length &&
340
+ !isNaN(parseInt(_this.input[_this.cursor + offset], 10))) {
341
+ minorVersion += _this.input[_this.cursor + offset];
342
+ offset++;
343
+ }
344
+ }
345
+ // Parse major and minor versions
346
+ var major = parseInt(majorVersion, 10);
347
+ var minor = minorVersion ? parseInt(minorVersion, 10) : undefined;
348
+ if (major !== 2) {
349
+ throw new errors_1.UnsupportedOpenQASMVersionError("Unsupported OpenQASM version detected: ".concat(majorVersion, ".").concat(minor !== null && minor !== void 0 ? minor : 0));
350
+ }
351
+ return [token_1.Token.OpenQASM];
352
+ }
353
+ {
354
+ var openQasmLit = char + _this.readIdentifier();
355
+ return [token_1.lookup[openQasmLit], openQasmLit];
356
+ }
357
+ case "i":
358
+ if (_this.input[_this.cursor] == "n" &&
359
+ _this.input[_this.cursor + 1] == "c" &&
360
+ _this.input[_this.cursor + 2] == "l" &&
361
+ _this.input[_this.cursor + 3] == "u" &&
362
+ _this.input[_this.cursor + 4] == "d" &&
363
+ _this.input[_this.cursor + 5] == "e") {
364
+ _this.readChar(6);
365
+ return [token_1.Token.Include];
366
+ }
367
+ {
368
+ var includeLit = char + _this.readIdentifier();
369
+ return [(0, token_1.lookup)(includeLit), includeLit];
370
+ }
371
+ case "o":
372
+ if (_this.input[_this.cursor] == "p" &&
373
+ _this.input[_this.cursor + 1] == "a" &&
374
+ _this.input[_this.cursor + 2] == "q" &&
375
+ _this.input[_this.cursor + 3] == "u" &&
376
+ _this.input[_this.cursor + 4] == "e") {
377
+ _this.readChar(5);
378
+ return [token_1.Token.Opaque];
379
+ }
380
+ {
381
+ var opaqueLit = char + _this.readIdentifier();
382
+ return [(0, token_1.lookup)(opaqueLit), opaqueLit];
383
+ }
384
+ case '"': {
385
+ var stringLiteral = char + _this.readStringLiteral('"');
386
+ return [token_1.Token.String, stringLiteral];
387
+ }
388
+ case "’": {
389
+ var singleStringLiteral = char + _this.readStringLiteral("’");
390
+ return [token_1.Token.String, singleStringLiteral];
391
+ }
392
+ default:
393
+ if (isLetter(char)) {
394
+ var literal = char + _this.readIdentifier();
395
+ return [(0, token_1.lookup)(literal), literal];
396
+ }
397
+ else if (isNumeric(char)) {
398
+ var num = char + _this.readNumeric();
399
+ if (num.indexOf(".") != -1) {
400
+ return [token_1.Token.Real, parseFloat(num)];
401
+ }
402
+ else {
403
+ return [token_1.Token.NNInteger, parseFloat(num)];
404
+ }
405
+ }
406
+ else {
407
+ return [token_1.Token.Illegal];
408
+ }
409
+ }
410
+ };
411
+ /**
412
+ * Returns the line number where the current cursor is located.
413
+ * @param cursor - The current cursor position in the input string.
414
+ * @return The line number.
415
+ */
416
+ this.getLineNumber = function (cursor) {
417
+ return _this.input
418
+ .substring(0, cursor)
419
+ .split(/\n|\r(?!\n)|\u2028|\u2029|\r\n/).length;
420
+ };
421
+ /**
422
+ * Returns the current line of code where the cursor is located.
423
+ * @param cursor - The current cursor position in the input string.
424
+ * @return The specific line where the cursor is located.
425
+ */
426
+ this.getCurrentLine = function (cursor) {
427
+ var lines = _this.input.split(/\n|\r(?!\n)|\u2028|\u2029|\r\n/);
428
+ var lineNumber = _this.getLineNumber(cursor);
429
+ return lines[lineNumber - 1];
430
+ };
431
+ this.input = input;
432
+ this.cursor = cursor;
433
+ }
434
+ return Lexer;
435
+ }());
436
+ exports.default = Lexer;