qasm-ts 2.1.2 → 2.1.4
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/errors.d.ts +88 -0
- package/dist/errors.js +116 -228
- package/dist/lexer.d.ts +78 -0
- package/dist/lexer.js +21 -24
- package/dist/main.d.ts +87 -0
- package/dist/main.js +10 -19
- package/dist/parser.d.ts +75 -0
- package/dist/parser.js +22 -25
- package/dist/qasm2/ast.d.ts +159 -0
- package/dist/qasm2/ast.js +117 -267
- package/dist/qasm2/lexer.d.ts +135 -0
- package/dist/qasm2/lexer.js +171 -177
- package/dist/qasm2/parser.d.ts +187 -0
- package/dist/qasm2/parser.js +254 -258
- package/dist/qasm2/token.d.ts +89 -0
- package/dist/qasm2/token.js +4 -9
- package/dist/qasm3/ast.d.ts +652 -0
- package/dist/qasm3/ast.js +397 -732
- package/dist/qasm3/lexer.d.ts +156 -0
- package/dist/qasm3/lexer.js +259 -265
- package/dist/qasm3/parser.d.ts +407 -0
- package/dist/qasm3/parser.js +892 -896
- package/dist/qasm3/token.d.ts +197 -0
- package/dist/qasm3/token.js +4 -9
- package/dist/version.d.ts +41 -0
- package/dist/version.js +12 -16
- package/docs/docs-readme.md +13 -9
- package/docs/typedoc/index.html +4 -4
- package/package.json +2 -1
- package/readme.md +4 -3
- package/tsconfig.json +7 -2
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenQASM 2.0 Parser Implementation
|
|
3
|
+
*
|
|
4
|
+
* This module implements a parser for OpenQASM 2.0 that focuses on the core
|
|
5
|
+
* quantum circuit description language without the advanced features of version 3.0.
|
|
6
|
+
* The parser handles quantum and classical register declarations, gate definitions
|
|
7
|
+
* and applications, measurements, and basic control structures.
|
|
8
|
+
*
|
|
9
|
+
* OpenQASM 2.0 parsing capabilities:
|
|
10
|
+
* - **Register declarations**: qreg and creg with size specifications
|
|
11
|
+
* - **Gate definitions**: Custom gate definitions with parameters and bodies
|
|
12
|
+
* - **Gate applications**: Built-in and custom gate applications
|
|
13
|
+
* - **Measurements**: Quantum measurements with classical result storage
|
|
14
|
+
* - **Basic conditionals**: Simple if statements based on classical register values
|
|
15
|
+
* - **Arithmetic expressions**: Parameter expressions for gate operations
|
|
16
|
+
* - **Opaque gates**: External gate declarations
|
|
17
|
+
*
|
|
18
|
+
* The parser maintains a list of known gates and validates gate applications
|
|
19
|
+
* against declared gates and built-in operations.
|
|
20
|
+
*
|
|
21
|
+
* @module
|
|
22
|
+
*
|
|
23
|
+
* @example Parsing OpenQASM 2.0 code
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const tokens = lexer.lex();
|
|
26
|
+
* const parser = new Parser(tokens);
|
|
27
|
+
* const ast = parser.parse();
|
|
28
|
+
*
|
|
29
|
+
* // AST contains simplified node structure for OpenQASM 2.0
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
import { Token } from "./token";
|
|
33
|
+
import { AstNode, Version, Include } from "./ast";
|
|
34
|
+
/**
|
|
35
|
+
* OpenQASM 2.0 Parser
|
|
36
|
+
*
|
|
37
|
+
* A straightforward recursive descent parser for OpenQASM 2.0 that produces
|
|
38
|
+
* a simplified AST structure appropriate for the more limited feature set
|
|
39
|
+
* of the 2.0 language specification.
|
|
40
|
+
*
|
|
41
|
+
* @example Basic parsing workflow
|
|
42
|
+
* ```typescript
|
|
43
|
+
* const parser = new Parser(tokens);
|
|
44
|
+
* const ast = parser.parse();
|
|
45
|
+
*
|
|
46
|
+
* // Process the resulting AST nodes
|
|
47
|
+
* ast.forEach(node => {
|
|
48
|
+
* if (node instanceof QReg) {
|
|
49
|
+
* console.log(`Quantum register: ${node.id}[${node.size}]`);
|
|
50
|
+
* }
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
declare class Parser {
|
|
55
|
+
/** The tokens to parse. */
|
|
56
|
+
tokens: Array<[Token, (number | string)?]>;
|
|
57
|
+
/** The allowed gates. */
|
|
58
|
+
gates: Array<string>;
|
|
59
|
+
/**
|
|
60
|
+
* Creates a parser.
|
|
61
|
+
* @param tokens - Tokens to parse.
|
|
62
|
+
*/
|
|
63
|
+
constructor(tokens: Array<[Token, (number | string)?]>);
|
|
64
|
+
/**
|
|
65
|
+
* Calling this method parses the code represented by the provided tokens.
|
|
66
|
+
* @return The abstract syntax tree.
|
|
67
|
+
*/
|
|
68
|
+
parse(): Array<AstNode>;
|
|
69
|
+
/**
|
|
70
|
+
* Delegates the parsing of the next set of tokens to the appropriate method.
|
|
71
|
+
* @param tokens - Remaining tokens to parse.
|
|
72
|
+
* @param allowVariables - Whether encountered identifiers should be consider
|
|
73
|
+
variable initializations or references.
|
|
74
|
+
* @return A set of AST nodes.
|
|
75
|
+
*/
|
|
76
|
+
parseNode(tokens: Array<[Token, (number | string)?]>, allowVariables?: boolean): Array<AstNode>;
|
|
77
|
+
/**
|
|
78
|
+
* Checks if the next tokens match those expected.
|
|
79
|
+
* @param tokens - Remaining tokens to parse.
|
|
80
|
+
* @param expectedTokens - Expected tokens.
|
|
81
|
+
* @return Whether these is a match.
|
|
82
|
+
*/
|
|
83
|
+
matchNext(tokens: Array<[Token, (number | string)?]>, expectedTokens: Array<Token>): boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Parses a quantum register.
|
|
86
|
+
* @param tokens - Remaining tokens to parse.
|
|
87
|
+
* @return An AST node representing the quantum register.
|
|
88
|
+
*/
|
|
89
|
+
qreg(tokens: Array<[Token, (number | string)?]>): AstNode;
|
|
90
|
+
/**
|
|
91
|
+
* Parses a classical register.
|
|
92
|
+
* @param tokens - Remaining tokens to parse.
|
|
93
|
+
* @return An AST node representing the classical register.
|
|
94
|
+
*/
|
|
95
|
+
creg(tokens: Array<[Token, (number | string)?]>): AstNode;
|
|
96
|
+
/**
|
|
97
|
+
* Parses a conditional.
|
|
98
|
+
* @param tokens - Remaining tokens to parse.
|
|
99
|
+
* @return An AST node representing the conditional.
|
|
100
|
+
*/
|
|
101
|
+
conditional(tokens: Array<[Token, (number | string)?]>): AstNode;
|
|
102
|
+
/**
|
|
103
|
+
* Parses a barrier.
|
|
104
|
+
* @param tokens - Remaining tokens to parse.
|
|
105
|
+
* @return An AST node representing the barrier.
|
|
106
|
+
*/
|
|
107
|
+
barrier(tokens: Array<[Token, (number | string)?]>): AstNode;
|
|
108
|
+
/**
|
|
109
|
+
* Parses a measurement.
|
|
110
|
+
* @param tokens - Remaining tokens to parse.
|
|
111
|
+
* @return An AST node representing the measurement.
|
|
112
|
+
*/
|
|
113
|
+
measure(tokens: Array<[Token, (number | string)?]>): AstNode;
|
|
114
|
+
/**
|
|
115
|
+
* Parses an application of one of the allowed gates.
|
|
116
|
+
* @param tokens - Remaining tokens to parse.
|
|
117
|
+
* @return An AST node representing the gate application.
|
|
118
|
+
*/
|
|
119
|
+
application(tokens: Array<[Token, (number | string)?]>, op: string): Array<AstNode>;
|
|
120
|
+
/**
|
|
121
|
+
* Parses a subroutine used in a custom gate definition.
|
|
122
|
+
* @param tokens - Expression tokens to parse.
|
|
123
|
+
* @return A parsed subroutine.
|
|
124
|
+
*/
|
|
125
|
+
sub(tokens: Array<[Token, (number | string)?]>): Array<AstNode>;
|
|
126
|
+
/**
|
|
127
|
+
* Parses a parameter value.
|
|
128
|
+
* @param tokens - Tokens to parse.
|
|
129
|
+
* @return An AST node representing the parameter value.
|
|
130
|
+
*/
|
|
131
|
+
matchParam(tokens: Array<[Token, (number | string)?]>): AstNode;
|
|
132
|
+
/**
|
|
133
|
+
* Parses a list of parameter values.
|
|
134
|
+
* @param tokens - Tokens to parse.
|
|
135
|
+
* @return An array of AST nodes representing the parameter values.
|
|
136
|
+
*/
|
|
137
|
+
matchParamList(tokens: Array<[Token, (number | string)?]>): Array<Array<AstNode>>;
|
|
138
|
+
/**
|
|
139
|
+
* Parses an argument value.
|
|
140
|
+
* @param tokens - Tokens to parse.
|
|
141
|
+
* @return An AST node representing the argument value.
|
|
142
|
+
*/
|
|
143
|
+
matchArg(tokens: Array<[Token, (number | string)?]>): number;
|
|
144
|
+
/**
|
|
145
|
+
* Parses a list of argument values.
|
|
146
|
+
* @param tokens - Tokens to parse.
|
|
147
|
+
* @return An array of AST nodes representing the argument values.
|
|
148
|
+
*/
|
|
149
|
+
matchArgList(tokens: Array<[Token, (number | string)?]>): Array<[string, number?]>;
|
|
150
|
+
/**
|
|
151
|
+
* Parses an include statement.
|
|
152
|
+
* @param tokens - Tokens to parse.
|
|
153
|
+
* @return An Include node representing the include statement.
|
|
154
|
+
*/
|
|
155
|
+
include(tokens: Array<[Token, (number | string)?]>): Include;
|
|
156
|
+
/**
|
|
157
|
+
* Parses the version header and sets the parser version.
|
|
158
|
+
* @param tokens - Tokens to parse.
|
|
159
|
+
* @return A Version node representing the version statement.
|
|
160
|
+
*/
|
|
161
|
+
versionHeader(tokens: Array<[Token, (number | string)?]>): Version;
|
|
162
|
+
/**
|
|
163
|
+
* Parses a list of identifiers.
|
|
164
|
+
* @param tokens - Tokens to parse.
|
|
165
|
+
* @return An array of AST nodes representing the identifiers.
|
|
166
|
+
*/
|
|
167
|
+
matchIdList(tokens: Array<[Token, (number | string)?]>): Array<string>;
|
|
168
|
+
/**
|
|
169
|
+
* Parses a gate.
|
|
170
|
+
* @param tokens - Remaining tokens to parse.
|
|
171
|
+
* @return An AST node representing the gate.
|
|
172
|
+
*/
|
|
173
|
+
gate(tokens: Array<[Token, (number | string)?]>): AstNode;
|
|
174
|
+
/**
|
|
175
|
+
* Parses an opaque declaration if using OpenQASM 2. If using OpenQASM 3 it skips the line.
|
|
176
|
+
* @param tokens - Remaining tokens to parse.
|
|
177
|
+
* @return An AST node representing the opaque declaration.
|
|
178
|
+
*/
|
|
179
|
+
opaque(tokens: Array<[Token, (number | string)?]>): AstNode;
|
|
180
|
+
/**
|
|
181
|
+
* Validates whether a register or gate identifier.
|
|
182
|
+
* @param identifier - The identifier to validate.
|
|
183
|
+
* @return Boolean indicating successful validation or not.
|
|
184
|
+
*/
|
|
185
|
+
private validateIdentifier;
|
|
186
|
+
}
|
|
187
|
+
export default Parser;
|