@danielx/civet 0.7.3 → 0.7.5

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/browser.js CHANGED
@@ -38,9 +38,9 @@ var Civet = (() => {
38
38
  ));
39
39
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
40
40
 
41
- // ../Hera/dist/machine.js
41
+ // node_modules/@danielx/hera/dist/machine.js
42
42
  var require_machine = __commonJS({
43
- "../Hera/dist/machine.js"(exports, module) {
43
+ "node_modules/@danielx/hera/dist/machine.js"(exports, module) {
44
44
  "use strict";
45
45
  var __defProp2 = Object.defineProperty;
46
46
  var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
@@ -426,14 +426,14 @@ var Civet = (() => {
426
426
  }, [1, 1]);
427
427
  return [line, column];
428
428
  }
429
- function validate(input, result, { filename }) {
429
+ function validate(input, result, { filename: filename2 }) {
430
430
  if (result && result.pos === input.length)
431
431
  return result.value;
432
432
  const expectations = Array.from(new Set(failExpected.slice(0, failIndex)));
433
433
  let l = location(input, maxFailPos), [line, column] = l;
434
434
  if (result && result.pos > maxFailPos) {
435
435
  l = location(input, result.pos);
436
- throw new Error(`${filename}:${line}:${column} Unconsumed input at #{l}
436
+ throw new Error(`${filename2}:${line}:${column} Unconsumed input at #{l}
437
437
 
438
438
  ${input.slice(result.pos)}
439
439
  `);
@@ -445,11 +445,11 @@ ${input.slice(result.pos)}
445
445
  hint = JSON.stringify(hint);
446
446
  else
447
447
  hint = "EOF";
448
- const error = new ParseError(`${filename}:${line}:${column} Failed to parse
448
+ const error = new ParseError(`${filename2}:${line}:${column} Failed to parse
449
449
  Expected:
450
450
  ${expectations.join("\n ")}
451
451
  Found: ${hint}
452
- `, "ParseError", filename, line, column, maxFailPos);
452
+ `, "ParseError", filename2, line, column, maxFailPos);
453
453
  throw error;
454
454
  }
455
455
  if (result) {
@@ -473,11 +473,11 @@ ${input.slice(result.pos)}
473
473
  };
474
474
  }
475
475
  var ParseError = class extends Error {
476
- constructor(message, name, filename, line, column, offset) {
476
+ constructor(message, name, filename2, line, column, offset) {
477
477
  super(message);
478
478
  this.message = message;
479
479
  this.name = name;
480
- this.filename = filename;
480
+ this.filename = filename2;
481
481
  this.line = line;
482
482
  this.column = column;
483
483
  this.offset = offset;
@@ -495,6 +495,7 @@ ${input.slice(result.pos)}
495
495
  isCompileError: () => isCompileError,
496
496
  lib: () => lib_exports,
497
497
  parse: () => parse,
498
+ parseProgram: () => parseProgram,
498
499
  prune: () => prune,
499
500
  util: () => util_exports
500
501
  });
@@ -554,6 +555,7 @@ ${input.slice(result.pos)}
554
555
  processCoffeeInterpolation: () => processCoffeeInterpolation,
555
556
  processForInOf: () => processForInOf,
556
557
  processProgram: () => processProgram,
558
+ processProgramAsync: () => processProgramAsync,
557
559
  processUnaryExpression: () => processUnaryExpression,
558
560
  quoteString: () => quoteString,
559
561
  reorderBindingRestProperty: () => reorderBindingRestProperty,
@@ -1176,14 +1178,12 @@ ${input.slice(result.pos)}
1176
1178
  if (!prefix.length)
1177
1179
  return;
1178
1180
  if (isConstructor) {
1179
- const superCalls = gatherNodes(expressions, (exp) => {
1180
- return exp.type === "CallExpression" && exp.children[0]?.token === "super";
1181
- });
1181
+ const superCalls = gatherNodes(
1182
+ expressions,
1183
+ (a) => typeof a === "object" && a != null && "type" in a && a.type === "CallExpression" && "children" in a && Array.isArray(a.children) && a.children.length >= 1 && typeof a.children[0] === "object" && a.children[0] != null && "token" in a.children[0] && a.children[0].token === "super"
1184
+ );
1182
1185
  if (superCalls.length) {
1183
- const { child } = findAncestor(
1184
- superCalls[0],
1185
- (ancestor) => ancestor === block
1186
- );
1186
+ const { child } = findAncestor(superCalls[0], (a1) => a1 === block);
1187
1187
  const index = findChildIndex(expressions, child);
1188
1188
  if (index < 0) {
1189
1189
  throw new Error("Could not find super call within top-level expressions");
@@ -1268,7 +1268,7 @@ ${input.slice(result.pos)}
1268
1268
  return block;
1269
1269
  }
1270
1270
  function braceBlock(block) {
1271
- if (block.bare) {
1271
+ if (block.bare && !block.root) {
1272
1272
  if (block.children === block.expressions) {
1273
1273
  block.children = [block.expressions];
1274
1274
  }
@@ -1320,8 +1320,8 @@ ${input.slice(result.pos)}
1320
1320
  function replaceBlockExpression(node, child, replacement) {
1321
1321
  let found = false;
1322
1322
  const { expressions } = node;
1323
- for (let i = 0, l = expressions.length; i < l; i++) {
1324
- const statement = expressions[i];
1323
+ for (let i1 = 0, len3 = expressions.length; i1 < len3; i1++) {
1324
+ const statement = expressions[i1];
1325
1325
  const [, s] = statement;
1326
1326
  if (s === child) {
1327
1327
  statement[1] = replacement;
@@ -1382,9 +1382,9 @@ ${input.slice(result.pos)}
1382
1382
  }
1383
1383
  function insertSemicolon(statements) {
1384
1384
  const l = statements.length;
1385
- for (let i1 = 0, len2 = statements.length; i1 < len2; i1++) {
1386
- const i = i1;
1387
- const s = statements[i1];
1385
+ for (let i2 = 0, len1 = statements.length; i2 < len1; i2++) {
1386
+ const i = i2;
1387
+ const s = statements[i2];
1388
1388
  if (i < l - 1) {
1389
1389
  if (needsPrecedingSemicolon(statements[i + 1][1])) {
1390
1390
  const delim = s[2];
@@ -1402,8 +1402,8 @@ ${input.slice(result.pos)}
1402
1402
  return false;
1403
1403
  }
1404
1404
  if (Array.isArray(exp)) {
1405
- for (let i2 = 0, len1 = exp.length; i2 < len1; i2++) {
1406
- const child = exp[i2];
1405
+ for (let i3 = 0, len22 = exp.length; i3 < len22; i3++) {
1406
+ const child = exp[i3];
1407
1407
  if (!(child != null)) {
1408
1408
  continue;
1409
1409
  }
@@ -1445,6 +1445,9 @@ ${input.slice(result.pos)}
1445
1445
  }
1446
1446
 
1447
1447
  // source/parser/util.civet
1448
+ function len(arr, length) {
1449
+ return arr.length === length;
1450
+ }
1448
1451
  var assert = {
1449
1452
  equal(a, b, msg) {
1450
1453
  if (a !== b) {
@@ -1464,9 +1467,12 @@ ${input.slice(result.pos)}
1464
1467
  return;
1465
1468
  }
1466
1469
  node = node;
1467
- node.parent = parent;
1470
+ if (parent != null) {
1471
+ node.parent = parent;
1472
+ }
1468
1473
  if (node.children) {
1469
- for (const child of node.children) {
1474
+ for (let ref1 = node.children, i1 = 0, len1 = ref1.length; i1 < len1; i1++) {
1475
+ const child = ref1[i1];
1470
1476
  addParentPointers(child, node);
1471
1477
  }
1472
1478
  }
@@ -1476,10 +1482,9 @@ ${input.slice(result.pos)}
1476
1482
  return deepCopy(node);
1477
1483
  }
1478
1484
  function removeParentPointers(node) {
1479
- if (node == null)
1480
- return;
1481
- if (typeof node !== "object")
1485
+ if (!(node != null && typeof node === "object")) {
1482
1486
  return;
1487
+ }
1483
1488
  if (Array.isArray(node)) {
1484
1489
  for (const child of node) {
1485
1490
  removeParentPointers(child);
@@ -1523,7 +1528,7 @@ ${input.slice(result.pos)}
1523
1528
  if (node?.type !== "BlockStatement")
1524
1529
  return false;
1525
1530
  const { bare, expressions } = node;
1526
- return bare && (expressions.length === 0 || expressions.length === 1 && expressions[0][1]?.type === "EmptyStatement");
1531
+ return bare && (Array.isArray(expressions) && len(expressions, 0) || Array.isArray(expressions) && len(expressions, 1) && Array.isArray(expressions[0]) && expressions[0].length >= 2 && typeof expressions[0][1] === "object" && expressions[0][1] != null && "type" in expressions[0][1] && expressions[0][1].type === "EmptyStatement");
1527
1532
  }
1528
1533
  function isFunction(node) {
1529
1534
  const { type } = node;
@@ -1571,8 +1576,8 @@ ${input.slice(result.pos)}
1571
1576
  if (!(node != null)) {
1572
1577
  return false;
1573
1578
  }
1574
- let ref1;
1575
1579
  let ref2;
1580
+ let ref3;
1576
1581
  switch (node.type) {
1577
1582
  case "ReturnStatement":
1578
1583
  case "ThrowStatement":
@@ -1581,10 +1586,10 @@ ${input.slice(result.pos)}
1581
1586
  return true;
1582
1587
  }
1583
1588
  case "IfStatement": {
1584
- return isExit(node.then) && isExit((ref1 = node.else)?.[ref1.length - 1]);
1589
+ return isExit(node.then) && isExit((ref2 = node.else)?.[ref2.length - 1]);
1585
1590
  }
1586
1591
  case "BlockStatement": {
1587
- return isExit((ref2 = node.expressions)[ref2.length - 1]?.[1]);
1592
+ return isExit((ref3 = node.expressions)[ref3.length - 1]?.[1]);
1588
1593
  }
1589
1594
  case "IterationStatement": {
1590
1595
  return node.condition?.type === "ParenthesizedExpression" && node.condition.expression?.type === "Literal" && node.condition.expression?.raw === "true" && gatherRecursiveWithinFunction(
@@ -1880,8 +1885,8 @@ ${input.slice(result.pos)}
1880
1885
  return;
1881
1886
  }
1882
1887
  if (Array.isArray(node)) {
1883
- for (let i1 = 0, len2 = node.length; i1 < len2; i1++) {
1884
- const child = node[i1];
1888
+ for (let i2 = 0, len22 = node.length; i2 < len22; i2++) {
1889
+ const child = node[i2];
1885
1890
  updateParentPointers(child, parent, depth);
1886
1891
  }
1887
1892
  return;
@@ -1891,8 +1896,8 @@ ${input.slice(result.pos)}
1891
1896
  node.parent = parent;
1892
1897
  }
1893
1898
  if (depth && isParent(node)) {
1894
- for (let ref3 = node.children, i2 = 0, len1 = ref3.length; i2 < len1; i2++) {
1895
- const child = ref3[i2];
1899
+ for (let ref4 = node.children, i3 = 0, len3 = ref4.length; i3 < len3; i3++) {
1900
+ const child = ref4[i3];
1896
1901
  updateParentPointers(child, node, depth - 1);
1897
1902
  }
1898
1903
  }
@@ -2022,9 +2027,9 @@ ${input.slice(result.pos)}
2022
2027
  }
2023
2028
  function flatJoin(array, separator) {
2024
2029
  const result = [];
2025
- for (let i3 = 0, len2 = array.length; i3 < len2; i3++) {
2026
- const i = i3;
2027
- const items = array[i3];
2030
+ for (let i4 = 0, len4 = array.length; i4 < len4; i4++) {
2031
+ const i = i4;
2032
+ const items = array[i4];
2028
2033
  if (i) {
2029
2034
  result.push(separator);
2030
2035
  }
@@ -2045,7 +2050,7 @@ ${input.slice(result.pos)}
2045
2050
  if (!(children != null)) {
2046
2051
  return -1;
2047
2052
  }
2048
- for (let i1 = 0, len2 = children.length; i1 < len2; i1++) {
2053
+ for (let i1 = 0, len3 = children.length; i1 < len3; i1++) {
2049
2054
  const i = i1;
2050
2055
  const c = children[i1];
2051
2056
  if (c === child || Array.isArray(c) && arrayRecurse(c)) {
@@ -2075,8 +2080,9 @@ ${input.slice(result.pos)}
2075
2080
  return { ancestor: void 0, child: node };
2076
2081
  }
2077
2082
  function gatherNodes(node, predicate) {
2078
- if (node == null)
2083
+ if (node == null || typeof node === "string") {
2079
2084
  return [];
2085
+ }
2080
2086
  if (Array.isArray(node)) {
2081
2087
  return node.flatMap((n) => gatherNodes(n, predicate));
2082
2088
  }
@@ -2084,24 +2090,30 @@ ${input.slice(result.pos)}
2084
2090
  return [node];
2085
2091
  }
2086
2092
  switch (node.type) {
2087
- case "BlockStatement":
2093
+ case "BlockStatement": {
2088
2094
  return [];
2089
- case "ForStatement":
2095
+ }
2096
+ case "ForStatement": {
2090
2097
  const isDec = node.declaration?.type === "Declaration";
2091
2098
  return node.children.flatMap((n) => {
2092
- if (isDec && n === node.declaration)
2099
+ if (isDec && n === node.declaration) {
2093
2100
  return [];
2094
- return gatherNodes(n, predicate);
2101
+ } else {
2102
+ return gatherNodes(n, predicate);
2103
+ }
2095
2104
  });
2096
- default:
2105
+ }
2106
+ default: {
2097
2107
  return gatherNodes(node.children, predicate);
2108
+ }
2098
2109
  }
2099
2110
  }
2100
2111
  function gatherRecursive(node, predicate, skipPredicate) {
2101
- if (node == null)
2112
+ if (node == null || typeof node === "string") {
2102
2113
  return [];
2114
+ }
2103
2115
  if (Array.isArray(node)) {
2104
- return node.flatMap((n) => gatherRecursive(n, predicate, skipPredicate));
2116
+ return node.flatMap(($) => gatherRecursive($, predicate, skipPredicate));
2105
2117
  }
2106
2118
  if (skipPredicate?.(node))
2107
2119
  return [];
@@ -2111,8 +2123,9 @@ ${input.slice(result.pos)}
2111
2123
  return gatherRecursive(node.children, predicate, skipPredicate);
2112
2124
  }
2113
2125
  function gatherRecursiveAll(node, predicate) {
2114
- if (node == null)
2126
+ if (node == null || typeof node === "string") {
2115
2127
  return [];
2128
+ }
2116
2129
  if (Array.isArray(node)) {
2117
2130
  return node.flatMap((n) => gatherRecursiveAll(n, predicate));
2118
2131
  }
@@ -2152,7 +2165,7 @@ ${input.slice(result.pos)}
2152
2165
  ["**"]
2153
2166
  ];
2154
2167
  var precedenceMap = /* @__PURE__ */ new Map();
2155
- for (let i1 = 0, len2 = precedenceOrder.length; i1 < len2; i1++) {
2168
+ for (let i1 = 0, len3 = precedenceOrder.length; i1 < len3; i1++) {
2156
2169
  const prec = i1;
2157
2170
  const ops = precedenceOrder[i1];
2158
2171
  for (let i2 = 0, len1 = ops.length; i2 < len1; i2++) {
@@ -2263,7 +2276,7 @@ ${input.slice(result.pos)}
2263
2276
  } else {
2264
2277
  b = recurse(b);
2265
2278
  }
2266
- if (op.token === "instanceof" && b.type === "Literal" && b.children?.[0]?.type === "StringLiteral") {
2279
+ if (op.token === "instanceof" && (typeof b === "object" && b != null && "type" in b && b.type === "Literal" && "children" in b && Array.isArray(b.children) && b.children.length >= 1 && typeof b.children[0] === "object" && b.children[0] != null && "type" in b.children[0] && b.children[0].type === "StringLiteral")) {
2267
2280
  a = ["typeof ", makeLeftHandSideExpression(a)];
2268
2281
  if (op.negated) {
2269
2282
  op = { ...op, token: "!==", negated: false };
@@ -2368,7 +2381,7 @@ ${input.slice(result.pos)}
2368
2381
  let start = 0;
2369
2382
  const chains = [];
2370
2383
  var i = 0;
2371
- for (let i3 = 0, len2 = binops.length; i3 < len2; i3++) {
2384
+ for (let i3 = 0, len22 = binops.length; i3 < len22; i3++) {
2372
2385
  var i = i3;
2373
2386
  var [, op] = binops[i3];
2374
2387
  if (isRelationalOp(op)) {
@@ -2978,7 +2991,7 @@ ${input.slice(result.pos)}
2978
2991
  }
2979
2992
 
2980
2993
  // source/parser/declaration.civet
2981
- function len(arr, length) {
2994
+ function len2(arr, length) {
2982
2995
  return arr.length === length;
2983
2996
  }
2984
2997
  function processAssignmentDeclaration(decl, pattern, suffix, ws, assign, e) {
@@ -3161,9 +3174,12 @@ ${input.slice(result.pos)}
3161
3174
  rootCondition.blockPrefix = getPatternBlockPrefix(pattern, ref, decl, suffix);
3162
3175
  }
3163
3176
  function processDeclarationConditions(node) {
3164
- gatherRecursiveAll(node, (n) => {
3165
- return n.type === "IfStatement" || n.type === "IterationStatement" || n.type === "SwitchStatement";
3166
- }).forEach((s) => {
3177
+ gatherRecursiveAll(
3178
+ node,
3179
+ (n) => {
3180
+ return n.type === "IfStatement" || n.type === "IterationStatement" || n.type === "SwitchStatement";
3181
+ }
3182
+ ).forEach((s) => {
3167
3183
  return processDeclarationConditionStatement(s);
3168
3184
  });
3169
3185
  }
@@ -3173,7 +3189,7 @@ ${input.slice(result.pos)}
3173
3189
  return;
3174
3190
  }
3175
3191
  let { expression } = condition;
3176
- if (typeof expression === "object" && expression != null && "type" in expression && expression.type === "UnaryExpression" && "children" in expression && Array.isArray(expression.children) && len(expression.children, 2) && expression.children[0] === "!" && typeof expression.children[1] === "object" && expression.children[1] != null && "type" in expression.children[1] && expression.children[1].type === "ParenthesizedExpression" && "expression" in expression.children[1]) {
3192
+ if (typeof expression === "object" && expression != null && "type" in expression && expression.type === "UnaryExpression" && "children" in expression && Array.isArray(expression.children) && len2(expression.children, 2) && expression.children[0] === "!" && typeof expression.children[1] === "object" && expression.children[1] != null && "type" in expression.children[1] && expression.children[1].type === "ParenthesizedExpression" && "expression" in expression.children[1]) {
3177
3193
  const { type: type1, children: [, { type: type2, expression: expression2 }] } = expression;
3178
3194
  const type = [type1, type2];
3179
3195
  expression = expression2;
@@ -3182,7 +3198,7 @@ ${input.slice(result.pos)}
3182
3198
  const { ref, pattern } = expression;
3183
3199
  if (pattern) {
3184
3200
  const conditions = getPatternConditions(pattern, ref).filter((c) => {
3185
- if (Array.isArray(c) && len(c, 2) && c[0] === ref && c[1] === " != null") {
3201
+ if (Array.isArray(c) && len2(c, 2) && c[0] === ref && c[1] === " != null") {
3186
3202
  const [,] = c;
3187
3203
  return false;
3188
3204
  } else {
@@ -4099,36 +4115,79 @@ ${input.slice(result.pos)}
4099
4115
  if (!getInitialConfig()?.comptime) {
4100
4116
  return;
4101
4117
  }
4102
- gatherRecursive(
4118
+ let prepareEval = () => {
4119
+ };
4120
+ if (getSync()) {
4121
+ runComptime(statements, prepareEval);
4122
+ return;
4123
+ } else {
4124
+ return (async () => {
4125
+ {
4126
+ try {
4127
+ const path = (await import("node:path")).default;
4128
+ const { createRequire } = await import("node:module");
4129
+ return prepareEval = () => {
4130
+ global.__filename = path.resolve(getFilename() ?? "");
4131
+ global.__dirname = path.dirname(global.__filename);
4132
+ return global.require = createRequire(global.__filename);
4133
+ };
4134
+ } catch (e) {
4135
+ return;
4136
+ }
4137
+ }
4138
+ })().then(async () => {
4139
+ await Promise.all(runComptime(statements, prepareEval));
4140
+ });
4141
+ }
4142
+ }
4143
+ function runComptime(statements, prepareEval) {
4144
+ const sync2 = getSync();
4145
+ return gatherRecursive(
4103
4146
  statements,
4104
4147
  (node) => {
4105
4148
  return node.type === "ComptimeStatement" || node.type === "ComptimeExpression";
4106
4149
  }
4107
- ).forEach((exp) => {
4150
+ ).map((exp) => {
4108
4151
  const content = exp.type === "ComptimeStatement" ? exp.block : exp.expression;
4109
4152
  const options = { js: true };
4110
4153
  const js = generate_default(prune(content), options);
4111
4154
  if (options.errors != null) {
4112
4155
  return;
4113
4156
  }
4114
- const output = eval?.(`"use strict";${js}`);
4157
+ prepareEval();
4158
+ let output = eval?.(`"use strict";${js}`);
4159
+ let promise;
4115
4160
  if (exp.type === "ComptimeExpression") {
4116
- let string;
4117
- try {
4118
- string = serialize(output);
4119
- } catch (e) {
4120
- exp.children = [
4161
+ const finish = () => {
4162
+ let string;
4163
+ try {
4164
+ string = serialize(output);
4165
+ } catch (e) {
4166
+ exp.children = [
4167
+ {
4168
+ type: "Error",
4169
+ message: `comptime result ${output} not serializable: ${e}`
4170
+ }
4171
+ ];
4172
+ return;
4173
+ }
4174
+ return exp.children = [string];
4175
+ };
4176
+ if (sync2) {
4177
+ finish();
4178
+ } else {
4179
+ promise = (async () => {
4121
4180
  {
4122
- type: "Error",
4123
- message: `comptime result ${output} not JSON serializable: ${e}`
4181
+ output = await output;
4182
+ return finish();
4124
4183
  }
4125
- ];
4126
- return;
4184
+ })();
4127
4185
  }
4128
- return exp.children = [string];
4129
4186
  } else {
4130
- return exp.children = [];
4187
+ promise = output;
4188
+ exp.children = [];
4131
4189
  }
4190
+ return promise;
4132
4191
  });
4133
4192
  }
4134
4193
  function serialize(value) {
@@ -4236,7 +4295,7 @@ ${input.slice(result.pos)}
4236
4295
  case Object.prototype: {
4237
4296
  let objStr = "{";
4238
4297
  let descStr = "";
4239
- for (let ref2 = Object.getOwnPropertyNames(val).concat(Object.getOwnPropertySymbols(val)), i = 0, len2 = ref2.length; i < len2; i++) {
4298
+ for (let ref2 = Object.getOwnPropertyNames(val).concat(Object.getOwnPropertySymbols(val)), i = 0, len3 = ref2.length; i < len3; i++) {
4240
4299
  const prop = ref2[i];
4241
4300
  const desc = Object.getOwnPropertyDescriptor(val, prop);
4242
4301
  if (desc.enumerable && desc.configurable && desc.writable) {
@@ -4325,7 +4384,7 @@ ${input.slice(result.pos)}
4325
4384
  }
4326
4385
  function reduceIndentLevel(str, dedent, tab) {
4327
4386
  if (tab != null && tab != 1) {
4328
- for (let i1 = 0, len2 = str.length; i1 < len2; i1++) {
4387
+ for (let i1 = 0, len3 = str.length; i1 < len3; i1++) {
4329
4388
  const i = i1;
4330
4389
  const char = str[i1];
4331
4390
  if (!dedent) {
@@ -4407,7 +4466,7 @@ ${input.slice(result.pos)}
4407
4466
  ;
4408
4467
  const dedent = ref1;
4409
4468
  let results = [s];
4410
- for (let i3 = 0, len2 = strWithSubstitutions.length; i3 < len2; i3++) {
4469
+ for (let i3 = 0, len22 = strWithSubstitutions.length; i3 < len22; i3++) {
4411
4470
  const i = i3;
4412
4471
  let part = strWithSubstitutions[i3];
4413
4472
  if (part.token != null) {
@@ -4537,9 +4596,8 @@ ${input.slice(result.pos)}
4537
4596
  return true;
4538
4597
  }
4539
4598
  function expressionizeBlock(blockOrExpression) {
4540
- let ref1;
4541
- if ((ref1 = blockOrExpression) && "expressions" in ref1) {
4542
- const { expressions } = ref1;
4599
+ if (blockOrExpression && typeof blockOrExpression === "object" && "expressions" in blockOrExpression) {
4600
+ const { expressions } = blockOrExpression;
4543
4601
  const l = expressions.length;
4544
4602
  const results = [];
4545
4603
  let i1 = 0;
@@ -4638,7 +4696,7 @@ ${input.slice(result.pos)}
4638
4696
  }
4639
4697
  function processCallMemberExpression(node) {
4640
4698
  const { children } = node;
4641
- if (children[0]?.parenthesizedOp?.token && children[1]?.type === "Call") {
4699
+ if (Array.isArray(children) && children.length >= 2 && typeof children[0] === "object" && children[0] != null && "parenthesizedOp" in children[0] && typeof children[0].parenthesizedOp === "object" && children[0].parenthesizedOp != null && "token" in children[0].parenthesizedOp && typeof children[1] === "object" && children[1] != null && "type" in children[1] && children[1].type === "Call") {
4642
4700
  const op = children[0].parenthesizedOp;
4643
4701
  let call = children[1];
4644
4702
  const args = [...call.args];
@@ -4732,14 +4790,14 @@ ${input.slice(result.pos)}
4732
4790
  });
4733
4791
  }
4734
4792
  }
4735
- let ref2;
4793
+ let ref1;
4736
4794
  let object = {
4737
4795
  type: "ObjectExpression",
4738
4796
  children: [
4739
4797
  glob.object.children[0],
4740
4798
  // {
4741
4799
  ...parts,
4742
- (ref2 = glob.object.children)[ref2.length - 1]
4800
+ (ref1 = glob.object.children)[ref1.length - 1]
4743
4801
  // whitespace and }
4744
4802
  ],
4745
4803
  properties: parts,
@@ -4791,7 +4849,7 @@ ${input.slice(result.pos)}
4791
4849
  throw new Error("replaceNode failed: node has no parent");
4792
4850
  }
4793
4851
  function recurse(children) {
4794
- for (let i3 = 0, len2 = children.length; i3 < len2; i3++) {
4852
+ for (let i3 = 0, len22 = children.length; i3 < len22; i3++) {
4795
4853
  const i = i3;
4796
4854
  const child = children[i3];
4797
4855
  if (child === node) {
@@ -4881,8 +4939,8 @@ ${input.slice(result.pos)}
4881
4939
  return { type: "Error", message: "cannot use `type` in dynamic import" };
4882
4940
  } else {
4883
4941
  const { source, binding } = specifier;
4884
- let ref3;
4885
- const delim = (ref3 = specifier.children)[ref3.length - 1];
4942
+ let ref2;
4943
+ const delim = (ref2 = specifier.children)[ref2.length - 1];
4886
4944
  return {
4887
4945
  type: pattern ? "BindingProperty" : "Property",
4888
4946
  name: source,
@@ -4892,7 +4950,7 @@ ${input.slice(result.pos)}
4892
4950
  };
4893
4951
  }
4894
4952
  });
4895
- let ref4;
4953
+ let ref3;
4896
4954
  return {
4897
4955
  type: pattern ? "ObjectBindingPattern" : "ObjectExpression",
4898
4956
  names: node.names,
@@ -4901,7 +4959,7 @@ ${input.slice(result.pos)}
4901
4959
  node.children[0],
4902
4960
  // {
4903
4961
  properties,
4904
- (ref4 = node.children)[ref4.length - 1]
4962
+ (ref3 = node.children)[ref3.length - 1]
4905
4963
  // }
4906
4964
  ]
4907
4965
  };
@@ -5078,17 +5136,17 @@ ${input.slice(result.pos)}
5078
5136
  statements,
5079
5137
  (n) => n.type === "AssignmentExpression" && n.names === null,
5080
5138
  (exp) => {
5081
- let { lhs: $1, expression: $2 } = exp, tail = [], i = 0, len2 = $1.length;
5139
+ let { lhs: $1, expression: $2 } = exp, tail = [], i = 0, len3 = $1.length;
5082
5140
  let block;
5083
- let ref5;
5084
- if (exp.parent?.type === "BlockStatement" && !(ref5 = $1[$1.length - 1])?.[ref5.length - 1]?.special) {
5141
+ let ref4;
5142
+ if (exp.parent?.type === "BlockStatement" && !(ref4 = $1[$1.length - 1])?.[ref4.length - 1]?.special) {
5085
5143
  block = makeBlockFragment();
5086
- let ref6;
5087
- if (ref6 = prependStatementExpressionBlock(
5144
+ let ref5;
5145
+ if (ref5 = prependStatementExpressionBlock(
5088
5146
  { type: "Initializer", expression: $2, children: [void 0, void 0, $2] },
5089
5147
  block
5090
5148
  )) {
5091
- const ref = ref6;
5149
+ const ref = ref5;
5092
5150
  exp.children = exp.children.map(function(c) {
5093
5151
  if (c === $2)
5094
5152
  return ref;
@@ -5118,7 +5176,7 @@ ${input.slice(result.pos)}
5118
5176
  }
5119
5177
  }
5120
5178
  let wrapped = false;
5121
- while (i < len2) {
5179
+ while (i < len3) {
5122
5180
  const lastAssignment = $1[i++];
5123
5181
  const [, lhs, , op] = lastAssignment;
5124
5182
  if (op.token !== "=")
@@ -5131,7 +5189,7 @@ ${input.slice(result.pos)}
5131
5189
  }
5132
5190
  }
5133
5191
  }
5134
- i = len2 - 1;
5192
+ i = len3 - 1;
5135
5193
  while (i >= 0) {
5136
5194
  const lastAssignment = $1[i];
5137
5195
  if (lastAssignment[3].token === "=") {
@@ -5167,14 +5225,14 @@ ${input.slice(result.pos)}
5167
5225
  }
5168
5226
  i--;
5169
5227
  }
5170
- i = len2 - 1;
5228
+ i = len3 - 1;
5171
5229
  const optionalChainRef = makeRef();
5172
5230
  while (i >= 0) {
5173
5231
  const assignment = $1[i];
5174
5232
  const [ws1, lhs, ws2, op] = assignment;
5175
5233
  if (lhs.type === "MemberExpression" || lhs.type === "CallExpression") {
5176
5234
  const newMemberExp = unchainOptionalMemberExpression(lhs, optionalChainRef, (children) => {
5177
- const assigns = $1.splice(i + 1, len2 - 1 - i);
5235
+ const assigns = $1.splice(i + 1, len3 - 1 - i);
5178
5236
  $1.pop();
5179
5237
  return [ws1, ...children, ws2, op, ...assigns, $2];
5180
5238
  });
@@ -5256,9 +5314,9 @@ ${input.slice(result.pos)}
5256
5314
  }
5257
5315
  j++;
5258
5316
  }
5259
- let ref7;
5260
- if (ref7 = conditions.length) {
5261
- const l = ref7;
5317
+ let ref6;
5318
+ if (ref6 = conditions.length) {
5319
+ const l = ref6;
5262
5320
  const cs = flatJoin(conditions, " && ");
5263
5321
  return {
5264
5322
  ...exp,
@@ -5295,8 +5353,8 @@ ${input.slice(result.pos)}
5295
5353
  return gatherRecursiveAll(node, (n) => n.type === "UnaryType").forEach((unary) => {
5296
5354
  let last;
5297
5355
  let count = 0;
5298
- let ref8;
5299
- while (unary.suffix.length && (ref8 = unary.suffix)[ref8.length - 1]?.token === "?") {
5356
+ let ref7;
5357
+ while (unary.suffix.length && (ref7 = unary.suffix)[ref7.length - 1]?.token === "?") {
5300
5358
  last = unary.suffix.pop();
5301
5359
  count++;
5302
5360
  }
@@ -5330,11 +5388,11 @@ ${input.slice(result.pos)}
5330
5388
  gatherRecursiveAll(statements, ($) => $.type === "StatementExpression").forEach((_exp) => {
5331
5389
  const exp = _exp;
5332
5390
  const { statement } = exp;
5333
- let ref9;
5391
+ let ref8;
5334
5392
  switch (statement.type) {
5335
5393
  case "IfStatement": {
5336
- if (ref9 = expressionizeIfStatement(statement)) {
5337
- const expression = ref9;
5394
+ if (ref8 = expressionizeIfStatement(statement)) {
5395
+ const expression = ref8;
5338
5396
  return replaceNode(statement, expression, exp);
5339
5397
  } else {
5340
5398
  return replaceNode(statement, wrapIIFE([["", statement]]), exp);
@@ -5342,7 +5400,8 @@ ${input.slice(result.pos)}
5342
5400
  }
5343
5401
  case "IterationExpression": {
5344
5402
  if (statement.subtype === "ComptimeStatement") {
5345
- const expression = wrapIIFE(statement.statement.block.expressions);
5403
+ const { expressions } = statement.statement.block;
5404
+ const expression = wrapIIFE(expressions, hasAwait(expressions));
5346
5405
  return replaceNode(statement, makeNode({
5347
5406
  type: "ComptimeExpression",
5348
5407
  expression,
@@ -5420,7 +5479,13 @@ ${input.slice(result.pos)}
5420
5479
  processBlocks(statements);
5421
5480
  populateRefs(statements);
5422
5481
  adjustAtBindings(statements);
5423
- processComptime(statements);
5482
+ if (getSync()) {
5483
+ processComptime(statements);
5484
+ }
5485
+ }
5486
+ async function processProgramAsync(root) {
5487
+ const { expressions: statements } = root;
5488
+ await processComptime(statements);
5424
5489
  }
5425
5490
  function populateRefs(statements) {
5426
5491
  const refNodes = gatherRecursive(statements, ({ type }) => type === "Ref");
@@ -5544,8 +5609,8 @@ ${input.slice(result.pos)}
5544
5609
  for (let i4 = 0, len3 = placeholders.length; i4 < len3; i4++) {
5545
5610
  const placeholder = placeholders[i4];
5546
5611
  typeSuffix ??= placeholder.typeSuffix;
5547
- let ref10;
5548
- replaceNode((ref10 = placeholder.children)[ref10.length - 1], ref);
5612
+ let ref9;
5613
+ replaceNode((ref9 = placeholder.children)[ref9.length - 1], ref);
5549
5614
  }
5550
5615
  const { parent } = ancestor;
5551
5616
  const body = maybeUnwrap(ancestor);
@@ -5576,9 +5641,9 @@ ${input.slice(result.pos)}
5576
5641
  fnExp = makeLeftHandSideExpression(fnExp);
5577
5642
  }
5578
5643
  replaceNode(ancestor, fnExp, parent);
5579
- let ref11;
5580
- if (ref11 = getTrimmingSpace(body)) {
5581
- const ws = ref11;
5644
+ let ref10;
5645
+ if (ref10 = getTrimmingSpace(body)) {
5646
+ const ws = ref10;
5582
5647
  inplaceInsertTrimmingSpace(body, "");
5583
5648
  inplacePrepend(ws, fnExp);
5584
5649
  }
@@ -5623,8 +5688,8 @@ ${input.slice(result.pos)}
5623
5688
  }
5624
5689
  ];
5625
5690
  }
5626
- let ref12;
5627
- if (Array.isArray(rest.delim) && (ref12 = rest.delim)[ref12.length - 1]?.token === ",") {
5691
+ let ref11;
5692
+ if (Array.isArray(rest.delim) && (ref11 = rest.delim)[ref11.length - 1]?.token === ",") {
5628
5693
  rest.delim = rest.delim.slice(0, -1);
5629
5694
  rest.children = [...rest.children.slice(0, -1), rest.delim];
5630
5695
  }
@@ -6822,15 +6887,20 @@ ${input.slice(result.pos)}
6822
6887
  var $R93 = (0, import_lib2.$R)(new RegExp("\\r\\n|\\n|\\r|$", "suy"));
6823
6888
  var $R94 = (0, import_lib2.$R)(new RegExp("[^]*", "suy"));
6824
6889
  var Program$0 = (0, import_lib2.$TS)((0, import_lib2.$S)(Reset, Init, (0, import_lib2.$E)(EOS), TopLevelStatements, __), function($skip, $loc, $0, $1, $2, $3, $4, $5) {
6890
+ var reset = $1;
6891
+ var init = $2;
6892
+ var ws1 = $3;
6825
6893
  var statements = $4;
6826
- processProgram({
6894
+ var ws2 = $5;
6895
+ const program = {
6827
6896
  type: "BlockStatement",
6828
6897
  expressions: statements,
6829
- children: [statements],
6898
+ children: [reset, init, ws1, statements, ws2],
6830
6899
  bare: true,
6831
6900
  root: true
6832
- }, ReservedWord);
6833
- return $0;
6901
+ };
6902
+ processProgram(program);
6903
+ return program;
6834
6904
  });
6835
6905
  function Program(ctx, state2) {
6836
6906
  return (0, import_lib2.$EVENT)(ctx, state2, "Program", Program$0);
@@ -7269,8 +7339,8 @@ ${input.slice(result.pos)}
7269
7339
  function SingleLineBinaryOpRHS(ctx, state2) {
7270
7340
  return (0, import_lib2.$EVENT)(ctx, state2, "SingleLineBinaryOpRHS", SingleLineBinaryOpRHS$0);
7271
7341
  }
7272
- var RHS$0 = UnaryExpression;
7273
- var RHS$1 = ExpressionizedStatementWithTrailingCallExpressions;
7342
+ var RHS$0 = ExpressionizedStatementWithTrailingCallExpressions;
7343
+ var RHS$1 = UnaryExpression;
7274
7344
  var RHS$$ = [RHS$0, RHS$1];
7275
7345
  function RHS(ctx, state2) {
7276
7346
  return (0, import_lib2.$EVENT_C)(ctx, state2, "RHS", RHS$$);
@@ -8428,11 +8498,11 @@ ${input.slice(result.pos)}
8428
8498
  var dot = $1;
8429
8499
  var neg = $2;
8430
8500
  var num = $3;
8431
- const len2 = {
8501
+ const len3 = {
8432
8502
  children: []
8433
8503
  }, children = [
8434
8504
  adjustIndexAccess(dot),
8435
- len2,
8505
+ len3,
8436
8506
  neg,
8437
8507
  num,
8438
8508
  "]"
@@ -8440,7 +8510,7 @@ ${input.slice(result.pos)}
8440
8510
  return {
8441
8511
  type: "NegativeIndex",
8442
8512
  children,
8443
- len: len2
8513
+ len: len3
8444
8514
  };
8445
8515
  });
8446
8516
  var PropertyAccess$2 = (0, import_lib2.$TS)((0, import_lib2.$S)(AccessStart, (0, import_lib2.$Q)(InlineComment), (0, import_lib2.$C)(IdentifierName, PrivateIdentifier, LengthShorthand)), function($skip, $loc, $0, $1, $2, $3) {
@@ -13526,7 +13596,7 @@ ${input.slice(result.pos)}
13526
13596
  function Comma(ctx, state2) {
13527
13597
  return (0, import_lib2.$EVENT)(ctx, state2, "Comma", Comma$0);
13528
13598
  }
13529
- var Comptime$0 = (0, import_lib2.$TS)((0, import_lib2.$S)((0, import_lib2.$EXPECT)($L138, 'Comptime "comptime"'), NonIdContinue), function($skip, $loc, $0, $1, $2) {
13599
+ var Comptime$0 = (0, import_lib2.$TS)((0, import_lib2.$S)((0, import_lib2.$EXPECT)($L138, 'Comptime "comptime"'), NonIdContinue, (0, import_lib2.$N)((0, import_lib2.$EXPECT)($L15, 'Comptime ":"'))), function($skip, $loc, $0, $1, $2, $3) {
13530
13600
  return { $loc, token: $1 };
13531
13601
  });
13532
13602
  function Comptime(ctx, state2) {
@@ -16112,26 +16182,29 @@ ${input.slice(result.pos)}
16112
16182
  const parser2 = options.startRule != null ? grammar[options.startRule] : Object.values(grammar)[0];
16113
16183
  if (!parser2)
16114
16184
  throw new Error(`Could not find rule with name '${options.startRule}'`);
16115
- const filename = options.filename || "<anonymous>";
16185
+ const filename2 = options.filename || "<anonymous>";
16116
16186
  reset();
16117
16187
  Object.assign(ctx, { ...options.events, tokenize: options.tokenize });
16118
16188
  return validate(input, parser2(ctx, {
16119
16189
  input,
16120
16190
  pos: 0
16121
16191
  }), {
16122
- filename
16192
+ filename: filename2
16123
16193
  });
16124
16194
  }
16125
16195
  };
16126
16196
  }();
16127
16197
  var { parse } = parser;
16198
+ var filename;
16128
16199
  var initialConfig;
16129
16200
  var config;
16201
+ var sync;
16130
16202
  var state = {};
16131
16203
  var getState = () => state;
16132
16204
  var getConfig = () => config;
16133
16205
  var getInitialConfig = () => initialConfig;
16134
- var setInitialConfig = (c) => initialConfig = c;
16206
+ var getFilename = () => filename;
16207
+ var getSync = () => sync;
16135
16208
  Object.defineProperties(state, {
16136
16209
  currentIndent: {
16137
16210
  get() {
@@ -16176,6 +16249,21 @@ ${input.slice(result.pos)}
16176
16249
  }
16177
16250
  }
16178
16251
  });
16252
+ function parseProgram(input, options) {
16253
+ filename = options?.filename;
16254
+ initialConfig = options?.parseOptions;
16255
+ sync = options?.sync;
16256
+ const root = parse(input, options);
16257
+ if (sync) {
16258
+ filename = initialConfig = sync = null;
16259
+ return root;
16260
+ } else {
16261
+ return processProgramAsync(root).then(() => {
16262
+ filename = initialConfig = sync = null;
16263
+ return root;
16264
+ });
16265
+ }
16266
+ }
16179
16267
 
16180
16268
  // source/util.civet
16181
16269
  var util_exports = {};
@@ -16602,15 +16690,15 @@ ${input.slice(result.pos)}
16602
16690
  "RestoreTrailingMemberProperty",
16603
16691
  "RestoreNewlineBinaryOp"
16604
16692
  ]);
16605
- var compile = function(src, options) {
16693
+ function compile(src, options) {
16606
16694
  if (!options) {
16607
16695
  options = {};
16608
16696
  } else {
16609
16697
  options = { ...options };
16610
16698
  }
16611
16699
  options.parseOptions = { ...options.parseOptions };
16612
- const filename = options.filename || "unknown";
16613
- if (filename.endsWith(".coffee") && !/^(#![^\r\n]*(\r\n|\n|\r))?\s*['"]civet/.test(src)) {
16700
+ const filename2 = options.filename || "unknown";
16701
+ if (filename2.endsWith(".coffee") && !/^(#![^\r\n]*(\r\n|\n|\r))?\s*['"]civet/.test(src)) {
16614
16702
  options.parseOptions.coffeeCompat = true;
16615
16703
  }
16616
16704
  const { hits, trace, noCache } = options;
@@ -16622,21 +16710,18 @@ ${input.slice(result.pos)}
16622
16710
  });
16623
16711
  }
16624
16712
  let ast;
16625
- setInitialConfig(options.parseOptions);
16626
16713
  try {
16627
- ast = parse(src, {
16628
- filename,
16714
+ ast = parseProgram(src, {
16715
+ parseOptions: options.parseOptions,
16716
+ sync: options.sync,
16717
+ filename: filename2,
16629
16718
  events
16630
16719
  });
16631
- if (!(options.ast === "raw")) {
16632
- ast = prune(ast);
16633
- }
16634
16720
  } finally {
16635
- setInitialConfig(void 0);
16636
16721
  if (hits || trace) {
16637
- import("fs").then(function({ writeFileSync }) {
16722
+ import("node:fs").then(function({ writeFileSync }) {
16638
16723
  let ref;
16639
- if ((ref = events?.meta) && "logs" in ref) {
16724
+ if ((ref = events?.meta) && typeof ref === "object" && "logs" in ref) {
16640
16725
  const { logs } = ref;
16641
16726
  if (trace) {
16642
16727
  writeFileSync(trace, logs.join("\n"));
@@ -16665,28 +16750,45 @@ ${counts}`;
16665
16750
  });
16666
16751
  }
16667
16752
  }
16668
- if (options.ast) {
16669
- return ast;
16670
- }
16671
- if (options.sourceMap || options.inlineMap) {
16672
- const sm = SourceMap2(src);
16673
- options.updateSourceMap = sm.updateSourceMap;
16674
- const code = generate_default(ast, options);
16675
- if (options.inlineMap) {
16676
- return SourceMap2.remap(code, sm, filename, filename + ".tsx");
16677
- } else {
16678
- return {
16679
- code,
16680
- sourceMap: sm
16681
- };
16753
+ function rest(ast2) {
16754
+ options = options;
16755
+ if (!(options.ast === "raw")) {
16756
+ ast2 = prune(ast2);
16757
+ }
16758
+ if (options.ast) {
16759
+ return ast2;
16760
+ }
16761
+ function checkErrors() {
16762
+ if (options.errors?.length) {
16763
+ throw new Error(`Parse errors: ${options.errors.map(($) => $.message).join("\n")} `);
16764
+ }
16765
+ ;
16766
+ return;
16767
+ }
16768
+ if (options.sourceMap || options.inlineMap) {
16769
+ const sm = SourceMap2(src);
16770
+ options.updateSourceMap = sm.updateSourceMap;
16771
+ const code = generate_default(ast2, options);
16772
+ checkErrors();
16773
+ if (options.inlineMap) {
16774
+ return SourceMap2.remap(code, sm, filename2, filename2 + ".tsx");
16775
+ } else {
16776
+ return {
16777
+ code,
16778
+ sourceMap: sm
16779
+ };
16780
+ }
16682
16781
  }
16782
+ const result = generate_default(ast2, options);
16783
+ checkErrors();
16784
+ return result;
16683
16785
  }
16684
- const result = generate_default(ast, options);
16685
- if (options.errors?.length) {
16686
- throw new Error(`Parse errors: ${options.errors.map(($) => $.message).join("\n")} `);
16786
+ if (ast.then != null) {
16787
+ return ast.then(rest);
16788
+ } else {
16789
+ return rest(ast);
16687
16790
  }
16688
- return result;
16689
- };
16791
+ }
16690
16792
  var makeCache = function({ hits, trace } = {}) {
16691
16793
  const meta = {};
16692
16794
  let hitCount;
@@ -16753,6 +16855,6 @@ ${counts}`;
16753
16855
  return err instanceof Error && //@ts-ignore
16754
16856
  [err.message, err.name, err.filename, err.line, err.column, err.offset].every(($1) => $1 !== void 0);
16755
16857
  };
16756
- var main_default = { parse, generate: generate_default, util: util_exports, compile, isCompileError };
16858
+ var main_default = { parse, parseProgram, generate: generate_default, util: util_exports, compile, isCompileError };
16757
16859
  return __toCommonJS(main_exports);
16758
16860
  })();