qasm-ts 2.1.1 → 2.1.3

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