dirac-lang 0.1.88 → 0.1.90

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,323 +0,0 @@
1
- // src/runtime/braket-parser.ts
2
- var BraKetParser = class {
3
- lines = [];
4
- currentLine = 0;
5
- /**
6
- * Parse bra-ket notation and compile to XML
7
- */
8
- parse(source) {
9
- this.lines = source.split("\n");
10
- this.currentLine = 0;
11
- const xml = ["<dirac>"];
12
- this.parseBlock(xml, -1);
13
- xml.push("</dirac>");
14
- return xml.join("\n");
15
- }
16
- /**
17
- * Parse a block of lines at a given indentation level
18
- */
19
- parseBlock(output, parentIndent) {
20
- while (this.currentLine < this.lines.length) {
21
- const line = this.parseLine(this.lines[this.currentLine]);
22
- if (line.type === "empty") {
23
- this.currentLine++;
24
- continue;
25
- }
26
- if (line.indent <= parentIndent) {
27
- break;
28
- }
29
- if (line.type === "bra") {
30
- const attrs = line.attrs ? ` ${this.convertBraAttributes(line.attrs)}` : "";
31
- output.push(`${" ".repeat(line.indent)}<subroutine name="${line.tag}"${attrs}>`);
32
- this.currentLine++;
33
- this.parseBlock(output, line.indent);
34
- output.push(`${" ".repeat(line.indent)}</subroutine>`);
35
- continue;
36
- }
37
- if (line.type === "ket") {
38
- const indent = " ".repeat(line.indent);
39
- const attrs = line.attrs ? ` ${this.convertKetAttributes(line.attrs, line.tag || "")}` : "";
40
- const nextLine = this.currentLine + 1 < this.lines.length ? this.parseLine(this.lines[this.currentLine + 1]) : null;
41
- if (nextLine && nextLine.indent > line.indent && nextLine.type !== "empty") {
42
- output.push(`${indent}<${line.tag}${attrs}>`);
43
- this.currentLine++;
44
- this.parseBlock(output, line.indent);
45
- output.push(`${indent}</${line.tag}>`);
46
- } else {
47
- if (line.text) {
48
- const content = this.convertInlineKets(line.text);
49
- output.push(`${indent}<${line.tag}${attrs}>${content}</${line.tag}>`);
50
- } else {
51
- output.push(`${indent}<${line.tag}${attrs}/>`);
52
- }
53
- this.currentLine++;
54
- }
55
- continue;
56
- }
57
- if (line.type === "text") {
58
- const indent = " ".repeat(line.indent);
59
- const content = this.convertInlineKets(line.text || "");
60
- output.push(`${indent}${content}`);
61
- this.currentLine++;
62
- continue;
63
- }
64
- }
65
- }
66
- /**
67
- * Parse a single line into structured form
68
- */
69
- parseLine(raw) {
70
- const match = raw.match(/^(\s*)(.*)/);
71
- const indent = match ? Math.floor(match[1].length / 2) : 0;
72
- const content = match ? match[2] : "";
73
- if (!content.trim()) {
74
- return { indent, type: "empty", raw };
75
- }
76
- if (content.startsWith("#")) {
77
- return { indent, type: "empty", raw };
78
- }
79
- if (content.startsWith("<") && content.endsWith("|")) {
80
- const tagMatch = content.match(/^<([a-zA-Z_][a-zA-Z0-9_-]*)\s*/);
81
- if (tagMatch) {
82
- const tagName = tagMatch[1];
83
- const afterTag = content.substring(tagMatch[0].length, content.length - 1);
84
- return {
85
- indent,
86
- type: "bra",
87
- tag: tagName,
88
- attrs: afterTag.trim() || void 0,
89
- raw
90
- };
91
- }
92
- }
93
- const ketMatch = content.match(/^\|([a-zA-Z_][a-zA-Z0-9_-]*)\s*([^>]*?)>\s*(.*)/);
94
- if (ketMatch) {
95
- return {
96
- indent,
97
- type: "ket",
98
- tag: ketMatch[1],
99
- attrs: ketMatch[2].trim() || void 0,
100
- text: ketMatch[3] || void 0,
101
- raw
102
- };
103
- }
104
- return {
105
- indent,
106
- type: "text",
107
- text: content,
108
- raw
109
- };
110
- }
111
- /**
112
- * Convert bra-ket attribute syntax to XML
113
- * Examples:
114
- * name=value → name="value"
115
- * x=5 y=10 → x="5" y="10"
116
- * select=@* → select="@*"
117
- */
118
- convertAttributes(attrs) {
119
- if (!attrs) return "";
120
- const parts = [];
121
- let current = "";
122
- let inQuotes = false;
123
- let quoteChar = "";
124
- for (let i = 0; i < attrs.length; i++) {
125
- const char = attrs[i];
126
- if ((char === '"' || char === "'") && (i === 0 || attrs[i - 1] !== "\\")) {
127
- if (!inQuotes) {
128
- inQuotes = true;
129
- quoteChar = char;
130
- current += char;
131
- } else if (char === quoteChar) {
132
- inQuotes = false;
133
- current += char;
134
- } else {
135
- current += char;
136
- }
137
- } else if (char === " " && !inQuotes) {
138
- if (current.trim()) {
139
- parts.push(current.trim());
140
- current = "";
141
- }
142
- } else {
143
- current += char;
144
- }
145
- }
146
- if (current.trim()) {
147
- parts.push(current.trim());
148
- }
149
- return parts.map((part) => {
150
- const match = part.match(/^([a-zA-Z_][a-zA-Z0-9_-]*)=(.+)$/);
151
- if (!match) return part;
152
- const [, name, value] = match;
153
- if (value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'")) {
154
- return `${name}=${value}`;
155
- }
156
- return `${name}="${value}"`;
157
- }).join(" ");
158
- }
159
- /**
160
- * Convert bra-ket attribute syntax to XML for subroutine definitions
161
- * Automatically converts parameter attributes to param-* format
162
- * Reserved attributes (description, extends, visible) are kept as-is
163
- *
164
- * Examples:
165
- * name=String → param-name="String"
166
- * x=number y=string → param-x="number" param-y="string"
167
- * description=Adds → description="Adds" (reserved, no prefix)
168
- */
169
- convertBraAttributes(attrs) {
170
- if (!attrs) return "";
171
- const RESERVED = /* @__PURE__ */ new Set(["description", "extends", "visible"]);
172
- const parts = [];
173
- let current = "";
174
- let inQuotes = false;
175
- let quoteChar = "";
176
- for (let i = 0; i < attrs.length; i++) {
177
- const char = attrs[i];
178
- if ((char === '"' || char === "'") && (i === 0 || attrs[i - 1] !== "\\")) {
179
- if (!inQuotes) {
180
- inQuotes = true;
181
- quoteChar = char;
182
- current += char;
183
- } else if (char === quoteChar) {
184
- inQuotes = false;
185
- current += char;
186
- } else {
187
- current += char;
188
- }
189
- } else if (char === " " && !inQuotes) {
190
- if (current.trim()) {
191
- parts.push(current.trim());
192
- current = "";
193
- }
194
- } else {
195
- current += char;
196
- }
197
- }
198
- if (current.trim()) {
199
- parts.push(current.trim());
200
- }
201
- return parts.map((part) => {
202
- const match = part.match(/^([a-zA-Z_][a-zA-Z0-9_-]*)=(.+)$/);
203
- if (!match) return part;
204
- const [, name, value] = match;
205
- const isReserved = RESERVED.has(name);
206
- const attrName = isReserved ? name : `param-${name}`;
207
- if (value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'")) {
208
- return `${attrName}=${value}`;
209
- }
210
- return `${attrName}="${value}"`;
211
- }).join(" ");
212
- }
213
- /**
214
- * Convert ket attribute syntax to XML, supporting positional arguments
215
- * Detects unnamed values and marks them as _positional-N for runtime resolution
216
- *
217
- * Examples:
218
- * |greeting zhi> → <call name="greeting" _positional-0="zhi"/>
219
- * |add 5 10> → <call name="add" _positional-0="5" _positional-1="10"/>
220
- * |add x=5 y=10> → <call name="add" x="5" y="10"/> (named, no change)
221
- * |output>Hello → <output>Hello</output> (text content, no params)
222
- */
223
- convertKetAttributes(attrs, tagName) {
224
- if (!attrs) return "";
225
- const parts = this.parseAttributeParts(attrs);
226
- const hasPositional = parts.some((part) => !part.includes("="));
227
- if (!hasPositional) {
228
- return this.convertAttributes(attrs);
229
- }
230
- let positionalIndex = 0;
231
- return parts.map((part) => {
232
- const match = part.match(/^([a-zA-Z_][a-zA-Z0-9_-]*)=(.+)$/);
233
- if (match) {
234
- const [, name, value] = match;
235
- const quotedValue = this.quoteValue(value);
236
- return `${name}=${quotedValue}`;
237
- } else {
238
- const quotedValue = this.quoteValue(part);
239
- return `_positional-${positionalIndex++}=${quotedValue}`;
240
- }
241
- }).join(" ");
242
- }
243
- /**
244
- * Parse attribute string into parts, respecting quotes
245
- */
246
- parseAttributeParts(attrs) {
247
- const parts = [];
248
- let current = "";
249
- let inQuotes = false;
250
- let quoteChar = "";
251
- for (let i = 0; i < attrs.length; i++) {
252
- const char = attrs[i];
253
- if ((char === '"' || char === "'") && (i === 0 || attrs[i - 1] !== "\\")) {
254
- if (!inQuotes) {
255
- inQuotes = true;
256
- quoteChar = char;
257
- current += char;
258
- } else if (char === quoteChar) {
259
- inQuotes = false;
260
- current += char;
261
- } else {
262
- current += char;
263
- }
264
- } else if (char === " " && !inQuotes) {
265
- if (current.trim()) {
266
- parts.push(current.trim());
267
- current = "";
268
- }
269
- } else {
270
- current += char;
271
- }
272
- }
273
- if (current.trim()) {
274
- parts.push(current.trim());
275
- }
276
- return parts;
277
- }
278
- /**
279
- * Quote a value if not already quoted
280
- */
281
- quoteValue(value) {
282
- if (value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'")) {
283
- return value;
284
- }
285
- return `"${value}"`;
286
- }
287
- /**
288
- * Escape XML special characters in text content
289
- */
290
- escapeXml(text) {
291
- return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
292
- }
293
- /**
294
- * Convert inline kets within text content
295
- * Example: "Hello |variable name=x> world" → "Hello <variable name="x"/> world"
296
- * Example: "if x < 10 and y > 5" → "if x &lt; 10 and y &gt; 5"
297
- */
298
- convertInlineKets(text) {
299
- const parts = [];
300
- let lastIndex = 0;
301
- const ketRegex = /\|([a-zA-Z_][a-zA-Z0-9_-]*)\s*([^>]*?)>/g;
302
- let match;
303
- while ((match = ketRegex.exec(text)) !== null) {
304
- if (match.index > lastIndex) {
305
- const beforeText = text.substring(lastIndex, match.index);
306
- parts.push(this.escapeXml(beforeText));
307
- }
308
- const [, tag, attrs] = match;
309
- const attrStr = attrs.trim() ? ` ${this.convertAttributes(attrs.trim())}` : "";
310
- parts.push(`<${tag}${attrStr}/>`);
311
- lastIndex = ketRegex.lastIndex;
312
- }
313
- if (lastIndex < text.length) {
314
- const remainingText = text.substring(lastIndex);
315
- parts.push(this.escapeXml(remainingText));
316
- }
317
- return parts.join("");
318
- }
319
- };
320
-
321
- export {
322
- BraKetParser
323
- };
@@ -1,12 +0,0 @@
1
- import {
2
- integrate,
3
- integrateChildren
4
- } from "./chunk-KS5BEPQN.js";
5
- import "./chunk-HJSHCEK4.js";
6
- import "./chunk-BGG2SULN.js";
7
- import "./chunk-HRHAMPOB.js";
8
- import "./chunk-2VFQ2YBT.js";
9
- export {
10
- integrate,
11
- integrateChildren
12
- };