qasm-ts 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/lexer.js ADDED
@@ -0,0 +1,300 @@
1
+ "use strict";
2
+ exports.__esModule = true;
3
+ var token_1 = require("./token");
4
+ 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;
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;
32
+ }
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;
43
+ }
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;
297
+ }
298
+ return Lexer;
299
+ }());
300
+ exports["default"] = Lexer;
package/dist/main.js ADDED
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ exports.__esModule = true;
3
+ var parser_1 = require("./parser");
4
+ var lexer_1 = require("./lexer");
5
+ var fs = require("fs");
6
+ /**
7
+ * Returns the abstract syntax tree for a given string of QASM code.
8
+ * @param qasm - The code string.
9
+ * @return The corresponding AST.
10
+ */
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();
16
+ return ast;
17
+ }
18
+ /**
19
+ * Returns the abstract syntax tree for a given QASM file.
20
+ * @param file - The file location.
21
+ * @return The corresponding AST.
22
+ */
23
+ exports.parse = function (file) {
24
+ return parseString(fs.readFileSync(file, 'utf8'));
25
+ };
26
+ exports.parseString = parseString;