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.
@@ -0,0 +1,156 @@
1
+ /**
2
+ * OpenQASM 3.0 Lexical Analyzer
3
+ *
4
+ * This module implements the lexer for OpenQASM 3.0, which transforms source code
5
+ * into a stream of tokens. The lexer handles the significantly expanded syntax of
6
+ * OpenQASM 3.0, including classical programming constructs, control flow, and
7
+ * advanced quantum features.
8
+ *
9
+ * Key features of the OpenQASM 3.0 lexer:
10
+ * - **Extended token set**: Classical types, control flow, functions
11
+ * - **Complex operators**: Compound assignment, bitwise operations
12
+ * - **Advanced literals**: Scientific notation, binary/hex/octal, durations
13
+ * - **Gate modifiers**: ctrl, negctrl, inv, pow with @ syntax
14
+ * - **Unicode support**: Mathematical constants (π, ℇ, τ)
15
+ * - **Robust error handling**: Detailed syntax error reporting
16
+ *
17
+ * The lexer performs several validation passes:
18
+ * - Semicolon verification for statement termination
19
+ * - Comment handling (single-line // and multi-line /* *\/)
20
+ * - String literal parsing with multiple quote styles
21
+ * - Number format validation and conversion
22
+ *
23
+ * @module
24
+ *
25
+ * @example Basic lexing process
26
+ * ```typescript
27
+ * const lexer = new Lexer('qubit[2] q; h q[0];');
28
+ * const tokens = lexer.lex();
29
+ * // Returns: [
30
+ * // [Token.Id, 'qubit'], [Token.LSParen], [Token.NNInteger, 2],
31
+ * // [Token.RSParen], [Token.Id, 'q'], [Token.Semicolon], ...
32
+ * // ]
33
+ * ```
34
+ */
35
+ import { Token } from "./token";
36
+ /**
37
+ * OpenQASM 3.0 Lexical Analyzer
38
+ *
39
+ * The main lexer class that processes OpenQASM 3.0 source code character by
40
+ * character and produces a stream of tokens for the parser to consume.
41
+ *
42
+ * The lexer maintains state including:
43
+ * - Current cursor position in the input
44
+ * - Input validation status
45
+ * - Error reporting context
46
+ *
47
+ * @example Creating and using a lexer
48
+ * ```typescript
49
+ * const source = `
50
+ * OPENQASM 3.0;
51
+ * include "stdgates.inc";
52
+ * qubit[2] q;
53
+ * h q[0];
54
+ * cx q[0], q[1];
55
+ * `;
56
+ *
57
+ * const lexer = new Lexer(source);
58
+ * const tokens = lexer.lex();
59
+ * ```
60
+ */
61
+ declare class Lexer {
62
+ /** The string to lex. */
63
+ input: string;
64
+ /** The lexer's current cursor location. */
65
+ cursor: number;
66
+ /**
67
+ * Creates a lexer.
68
+ * @param input - The string to lex.
69
+ * @param cursor - The starting cursor position.
70
+ */
71
+ constructor(input: string, cursor?: number);
72
+ /**
73
+ * Verifies that all appropriate lines end with a semicolon.
74
+ * @return A tuple of the status and if False, returns the problematic line.
75
+ */
76
+ verifyInput: () => [boolean, number | null, string | null];
77
+ /**
78
+ * Calling this method lexes the code represented by the provided string.
79
+ * @return An array of tokens and their corresponding values.
80
+ */
81
+ lex: () => Array<[Token, (number | string)?]>;
82
+ /**
83
+ * Reads a character and advances the cursor.
84
+ * @param num - Optional cursor position modifier.
85
+ */
86
+ readChar: (num?: number) => string;
87
+ /**
88
+ * Advances the cusor past the next comment.
89
+ */
90
+ skipComment: () => void;
91
+ /**
92
+ * Advances the cursor past a multiline comment.
93
+ */
94
+ skipMultiLineComment: () => void;
95
+ /**
96
+ * Determines whether the next character to process equals a given character.
97
+ * @param c - The given character.
98
+ * @return Whether the next character equals the given character.
99
+ */
100
+ peekEq: (c: string) => boolean;
101
+ /**
102
+ * Reads a character without advancing the cursor.
103
+ * @param index - Optional peek position offset.
104
+ */
105
+ peek: () => string;
106
+ /**
107
+ * Reads a numeric value.
108
+ * @return The numeric value as a string.
109
+ */
110
+ readNumeric: () => string;
111
+ /**
112
+ * Reads an identifier.
113
+ * @return The identifier as a string.
114
+ */
115
+ readIdentifier: () => string;
116
+ /**
117
+ * Reds a keyword or identifier.
118
+ * If the character sequence matches a keyword, returns the corresponding token.
119
+ * Otherwise, treats the sequence as an identifier.
120
+ * @param char - The first character of the keyword or identifier.
121
+ * @return The corresponding token or identifier.
122
+ */
123
+ readKeywordOrIdentifier: (char: string) => [Token, string];
124
+ /**
125
+ * Reads a string literal.
126
+ * @param terminator - The literal's termination character.
127
+ * @return The literal as a string.
128
+ */
129
+ readStringLiteral: (terminator: string) => string;
130
+ /**
131
+ * Advances the cusor past the next block of whitespace.
132
+ */
133
+ skipWhitespace: () => null;
134
+ /**
135
+ * Lexes the next token.
136
+ * @return The next token and its corresponding value.
137
+ */
138
+ nextToken: () => [Token, (number | string)?];
139
+ /**
140
+ * Returns the line number where the current cursor is located.
141
+ * @param cursor - The current cursor position in the input string.
142
+ * @return The line number.
143
+ */
144
+ getLineNumber: (cursor: number) => number;
145
+ /**
146
+ * Returns the current line of code where the cursor is located.
147
+ * @param cursor - The current cursor position in the input string.
148
+ * @return The specific line where the cursor is located.
149
+ */
150
+ getCurrentLine: (cursor: number) => string;
151
+ /**
152
+ * Retruns an identifier or gate modifier.
153
+ */
154
+ lexGateModifier: (keyword: string) => [Token, (number | string)?];
155
+ }
156
+ export default Lexer;