@danielx/civet 0.7.4 → 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/main.js CHANGED
@@ -418,14 +418,14 @@ var require_machine = __commonJS({
418
418
  }, [1, 1]);
419
419
  return [line, column];
420
420
  }
421
- function validate(input, result, { filename }) {
421
+ function validate(input, result, { filename: filename2 }) {
422
422
  if (result && result.pos === input.length)
423
423
  return result.value;
424
424
  const expectations = Array.from(new Set(failExpected.slice(0, failIndex)));
425
425
  let l = location(input, maxFailPos), [line, column] = l;
426
426
  if (result && result.pos > maxFailPos) {
427
427
  l = location(input, result.pos);
428
- throw new Error(`${filename}:${line}:${column} Unconsumed input at #{l}
428
+ throw new Error(`${filename2}:${line}:${column} Unconsumed input at #{l}
429
429
 
430
430
  ${input.slice(result.pos)}
431
431
  `);
@@ -437,11 +437,11 @@ ${input.slice(result.pos)}
437
437
  hint = JSON.stringify(hint);
438
438
  else
439
439
  hint = "EOF";
440
- const error = new ParseError(`${filename}:${line}:${column} Failed to parse
440
+ const error = new ParseError(`${filename2}:${line}:${column} Failed to parse
441
441
  Expected:
442
442
  ${expectations.join("\n ")}
443
443
  Found: ${hint}
444
- `, "ParseError", filename, line, column, maxFailPos);
444
+ `, "ParseError", filename2, line, column, maxFailPos);
445
445
  throw error;
446
446
  }
447
447
  if (result) {
@@ -465,11 +465,11 @@ ${input.slice(result.pos)}
465
465
  };
466
466
  }
467
467
  var ParseError = class extends Error {
468
- constructor(message, name, filename, line, column, offset) {
468
+ constructor(message, name, filename2, line, column, offset) {
469
469
  super(message);
470
470
  this.message = message;
471
471
  this.name = name;
472
- this.filename = filename;
472
+ this.filename = filename2;
473
473
  this.line = line;
474
474
  this.column = column;
475
475
  this.offset = offset;
@@ -487,6 +487,7 @@ __export(main_exports, {
487
487
  isCompileError: () => isCompileError,
488
488
  lib: () => lib_exports,
489
489
  parse: () => parse,
490
+ parseProgram: () => parseProgram,
490
491
  prune: () => prune,
491
492
  util: () => util_exports
492
493
  });
@@ -547,6 +548,7 @@ __export(lib_exports, {
547
548
  processCoffeeInterpolation: () => processCoffeeInterpolation,
548
549
  processForInOf: () => processForInOf,
549
550
  processProgram: () => processProgram,
551
+ processProgramAsync: () => processProgramAsync,
550
552
  processUnaryExpression: () => processUnaryExpression,
551
553
  quoteString: () => quoteString,
552
554
  reorderBindingRestProperty: () => reorderBindingRestProperty,
@@ -1169,14 +1171,12 @@ function processParams(f) {
1169
1171
  if (!prefix.length)
1170
1172
  return;
1171
1173
  if (isConstructor) {
1172
- const superCalls = gatherNodes(expressions, (exp) => {
1173
- return exp.type === "CallExpression" && exp.children[0]?.token === "super";
1174
- });
1174
+ const superCalls = gatherNodes(
1175
+ expressions,
1176
+ (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"
1177
+ );
1175
1178
  if (superCalls.length) {
1176
- const { child } = findAncestor(
1177
- superCalls[0],
1178
- (ancestor) => ancestor === block
1179
- );
1179
+ const { child } = findAncestor(superCalls[0], (a1) => a1 === block);
1180
1180
  const index = findChildIndex(expressions, child);
1181
1181
  if (index < 0) {
1182
1182
  throw new Error("Could not find super call within top-level expressions");
@@ -1261,7 +1261,7 @@ function blockWithPrefix(prefixStatements, block) {
1261
1261
  return block;
1262
1262
  }
1263
1263
  function braceBlock(block) {
1264
- if (block.bare) {
1264
+ if (block.bare && !block.root) {
1265
1265
  if (block.children === block.expressions) {
1266
1266
  block.children = [block.expressions];
1267
1267
  }
@@ -1313,7 +1313,7 @@ function makeBlockFragment() {
1313
1313
  function replaceBlockExpression(node, child, replacement) {
1314
1314
  let found = false;
1315
1315
  const { expressions } = node;
1316
- for (let i1 = 0, len2 = expressions.length; i1 < len2; i1++) {
1316
+ for (let i1 = 0, len3 = expressions.length; i1 < len3; i1++) {
1317
1317
  const statement = expressions[i1];
1318
1318
  const [, s] = statement;
1319
1319
  if (s === child) {
@@ -1395,7 +1395,7 @@ function needsPrecedingSemicolon(exp) {
1395
1395
  return false;
1396
1396
  }
1397
1397
  if (Array.isArray(exp)) {
1398
- for (let i3 = 0, len2 = exp.length; i3 < len2; i3++) {
1398
+ for (let i3 = 0, len22 = exp.length; i3 < len22; i3++) {
1399
1399
  const child = exp[i3];
1400
1400
  if (!(child != null)) {
1401
1401
  continue;
@@ -1438,6 +1438,9 @@ function needsPrecedingSemicolon(exp) {
1438
1438
  }
1439
1439
 
1440
1440
  // source/parser/util.civet
1441
+ function len(arr, length) {
1442
+ return arr.length === length;
1443
+ }
1441
1444
  var assert = {
1442
1445
  equal(a, b, msg) {
1443
1446
  if (a !== b) {
@@ -1461,7 +1464,7 @@ function addParentPointers(node, parent) {
1461
1464
  node.parent = parent;
1462
1465
  }
1463
1466
  if (node.children) {
1464
- for (let ref1 = node.children, i1 = 0, len2 = ref1.length; i1 < len2; i1++) {
1467
+ for (let ref1 = node.children, i1 = 0, len1 = ref1.length; i1 < len1; i1++) {
1465
1468
  const child = ref1[i1];
1466
1469
  addParentPointers(child, node);
1467
1470
  }
@@ -1518,8 +1521,7 @@ function isEmptyBareBlock(node) {
1518
1521
  if (node?.type !== "BlockStatement")
1519
1522
  return false;
1520
1523
  const { bare, expressions } = node;
1521
- return bare && //expressions is like [], [, {type: "EmptyStatement"}]
1522
- (expressions.length === 0 || expressions.length === 1 && expressions[0][1]?.type === "EmptyStatement");
1524
+ 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");
1523
1525
  }
1524
1526
  function isFunction(node) {
1525
1527
  const { type } = node;
@@ -1876,7 +1878,7 @@ function updateParentPointers(node, parent, depth = 1) {
1876
1878
  return;
1877
1879
  }
1878
1880
  if (Array.isArray(node)) {
1879
- for (let i2 = 0, len1 = node.length; i2 < len1; i2++) {
1881
+ for (let i2 = 0, len22 = node.length; i2 < len22; i2++) {
1880
1882
  const child = node[i2];
1881
1883
  updateParentPointers(child, parent, depth);
1882
1884
  }
@@ -1887,7 +1889,7 @@ function updateParentPointers(node, parent, depth = 1) {
1887
1889
  node.parent = parent;
1888
1890
  }
1889
1891
  if (depth && isParent(node)) {
1890
- for (let ref4 = node.children, i3 = 0, len2 = ref4.length; i3 < len2; i3++) {
1892
+ for (let ref4 = node.children, i3 = 0, len3 = ref4.length; i3 < len3; i3++) {
1891
1893
  const child = ref4[i3];
1892
1894
  updateParentPointers(child, node, depth - 1);
1893
1895
  }
@@ -2018,7 +2020,7 @@ function wrapWithReturn(expression) {
2018
2020
  }
2019
2021
  function flatJoin(array, separator) {
2020
2022
  const result = [];
2021
- for (let i4 = 0, len3 = array.length; i4 < len3; i4++) {
2023
+ for (let i4 = 0, len4 = array.length; i4 < len4; i4++) {
2022
2024
  const i = i4;
2023
2025
  const items = array[i4];
2024
2026
  if (i) {
@@ -2041,7 +2043,7 @@ function findChildIndex(parent, child) {
2041
2043
  if (!(children != null)) {
2042
2044
  return -1;
2043
2045
  }
2044
- for (let i1 = 0, len2 = children.length; i1 < len2; i1++) {
2046
+ for (let i1 = 0, len3 = children.length; i1 < len3; i1++) {
2045
2047
  const i = i1;
2046
2048
  const c = children[i1];
2047
2049
  if (c === child || Array.isArray(c) && arrayRecurse(c)) {
@@ -2156,7 +2158,7 @@ var precedenceOrder = [
2156
2158
  ["**"]
2157
2159
  ];
2158
2160
  var precedenceMap = /* @__PURE__ */ new Map();
2159
- for (let i1 = 0, len2 = precedenceOrder.length; i1 < len2; i1++) {
2161
+ for (let i1 = 0, len3 = precedenceOrder.length; i1 < len3; i1++) {
2160
2162
  const prec = i1;
2161
2163
  const ops = precedenceOrder[i1];
2162
2164
  for (let i2 = 0, len1 = ops.length; i2 < len1; i2++) {
@@ -2267,7 +2269,7 @@ function processBinaryOpExpression($0) {
2267
2269
  } else {
2268
2270
  b = recurse(b);
2269
2271
  }
2270
- if (op.token === "instanceof" && b.type === "Literal" && b.children?.[0]?.type === "StringLiteral") {
2272
+ 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")) {
2271
2273
  a = ["typeof ", makeLeftHandSideExpression(a)];
2272
2274
  if (op.negated) {
2273
2275
  op = { ...op, token: "!==", negated: false };
@@ -2372,7 +2374,7 @@ function expandChainedComparisons([first, binops]) {
2372
2374
  let start = 0;
2373
2375
  const chains = [];
2374
2376
  var i = 0;
2375
- for (let i3 = 0, len2 = binops.length; i3 < len2; i3++) {
2377
+ for (let i3 = 0, len22 = binops.length; i3 < len22; i3++) {
2376
2378
  var i = i3;
2377
2379
  var [, op] = binops[i3];
2378
2380
  if (isRelationalOp(op)) {
@@ -2982,7 +2984,7 @@ function aliasBinding(p, ref) {
2982
2984
  }
2983
2985
 
2984
2986
  // source/parser/declaration.civet
2985
- function len(arr, length) {
2987
+ function len2(arr, length) {
2986
2988
  return arr.length === length;
2987
2989
  }
2988
2990
  function processAssignmentDeclaration(decl, pattern, suffix, ws, assign, e) {
@@ -3180,7 +3182,7 @@ function processDeclarationConditionStatement(s) {
3180
3182
  return;
3181
3183
  }
3182
3184
  let { expression } = condition;
3183
- 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]) {
3185
+ 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]) {
3184
3186
  const { type: type1, children: [, { type: type2, expression: expression2 }] } = expression;
3185
3187
  const type = [type1, type2];
3186
3188
  expression = expression2;
@@ -3189,7 +3191,7 @@ function processDeclarationConditionStatement(s) {
3189
3191
  const { ref, pattern } = expression;
3190
3192
  if (pattern) {
3191
3193
  const conditions = getPatternConditions(pattern, ref).filter((c) => {
3192
- if (Array.isArray(c) && len(c, 2) && c[0] === ref && c[1] === " != null") {
3194
+ if (Array.isArray(c) && len2(c, 2) && c[0] === ref && c[1] === " != null") {
3193
3195
  const [,] = c;
3194
3196
  return false;
3195
3197
  } else {
@@ -4106,36 +4108,79 @@ function processComptime(statements) {
4106
4108
  if (!getInitialConfig()?.comptime) {
4107
4109
  return;
4108
4110
  }
4109
- gatherRecursive(
4111
+ let prepareEval = () => {
4112
+ };
4113
+ if (getSync()) {
4114
+ runComptime(statements, prepareEval);
4115
+ return;
4116
+ } else {
4117
+ return (async () => {
4118
+ {
4119
+ try {
4120
+ const path = (await import("node:path")).default;
4121
+ const { createRequire } = await import("node:module");
4122
+ return prepareEval = () => {
4123
+ global.__filename = path.resolve(getFilename() ?? "");
4124
+ global.__dirname = path.dirname(global.__filename);
4125
+ return global.require = createRequire(global.__filename);
4126
+ };
4127
+ } catch (e) {
4128
+ return;
4129
+ }
4130
+ }
4131
+ })().then(async () => {
4132
+ await Promise.all(runComptime(statements, prepareEval));
4133
+ });
4134
+ }
4135
+ }
4136
+ function runComptime(statements, prepareEval) {
4137
+ const sync2 = getSync();
4138
+ return gatherRecursive(
4110
4139
  statements,
4111
4140
  (node) => {
4112
4141
  return node.type === "ComptimeStatement" || node.type === "ComptimeExpression";
4113
4142
  }
4114
- ).forEach((exp) => {
4143
+ ).map((exp) => {
4115
4144
  const content = exp.type === "ComptimeStatement" ? exp.block : exp.expression;
4116
4145
  const options = { js: true };
4117
4146
  const js = generate_default(prune(content), options);
4118
4147
  if (options.errors != null) {
4119
4148
  return;
4120
4149
  }
4121
- const output = eval?.(`"use strict";${js}`);
4150
+ prepareEval();
4151
+ let output = eval?.(`"use strict";${js}`);
4152
+ let promise;
4122
4153
  if (exp.type === "ComptimeExpression") {
4123
- let string;
4124
- try {
4125
- string = serialize(output);
4126
- } catch (e) {
4127
- exp.children = [
4154
+ const finish = () => {
4155
+ let string;
4156
+ try {
4157
+ string = serialize(output);
4158
+ } catch (e) {
4159
+ exp.children = [
4160
+ {
4161
+ type: "Error",
4162
+ message: `comptime result ${output} not serializable: ${e}`
4163
+ }
4164
+ ];
4165
+ return;
4166
+ }
4167
+ return exp.children = [string];
4168
+ };
4169
+ if (sync2) {
4170
+ finish();
4171
+ } else {
4172
+ promise = (async () => {
4128
4173
  {
4129
- type: "Error",
4130
- message: `comptime result ${output} not JSON serializable: ${e}`
4174
+ output = await output;
4175
+ return finish();
4131
4176
  }
4132
- ];
4133
- return;
4177
+ })();
4134
4178
  }
4135
- return exp.children = [string];
4136
4179
  } else {
4137
- return exp.children = [];
4180
+ promise = output;
4181
+ exp.children = [];
4138
4182
  }
4183
+ return promise;
4139
4184
  });
4140
4185
  }
4141
4186
  function serialize(value) {
@@ -4243,7 +4288,7 @@ function serialize(value) {
4243
4288
  case Object.prototype: {
4244
4289
  let objStr = "{";
4245
4290
  let descStr = "";
4246
- for (let ref2 = Object.getOwnPropertyNames(val).concat(Object.getOwnPropertySymbols(val)), i = 0, len2 = ref2.length; i < len2; i++) {
4291
+ for (let ref2 = Object.getOwnPropertyNames(val).concat(Object.getOwnPropertySymbols(val)), i = 0, len3 = ref2.length; i < len3; i++) {
4247
4292
  const prop = ref2[i];
4248
4293
  const desc = Object.getOwnPropertyDescriptor(val, prop);
4249
4294
  if (desc.enumerable && desc.configurable && desc.writable) {
@@ -4332,7 +4377,7 @@ function getIndentLevel(str, tab) {
4332
4377
  }
4333
4378
  function reduceIndentLevel(str, dedent, tab) {
4334
4379
  if (tab != null && tab != 1) {
4335
- for (let i1 = 0, len2 = str.length; i1 < len2; i1++) {
4380
+ for (let i1 = 0, len3 = str.length; i1 < len3; i1++) {
4336
4381
  const i = i1;
4337
4382
  const char = str[i1];
4338
4383
  if (!dedent) {
@@ -4414,7 +4459,7 @@ function dedentBlockSubstitutions($0, tab) {
4414
4459
  ;
4415
4460
  const dedent = ref1;
4416
4461
  let results = [s];
4417
- for (let i3 = 0, len2 = strWithSubstitutions.length; i3 < len2; i3++) {
4462
+ for (let i3 = 0, len22 = strWithSubstitutions.length; i3 < len22; i3++) {
4418
4463
  const i = i3;
4419
4464
  let part = strWithSubstitutions[i3];
4420
4465
  if (part.token != null) {
@@ -4544,9 +4589,8 @@ function isExpression(node) {
4544
4589
  return true;
4545
4590
  }
4546
4591
  function expressionizeBlock(blockOrExpression) {
4547
- let ref1;
4548
- if ((ref1 = blockOrExpression) && "expressions" in ref1) {
4549
- const { expressions } = ref1;
4592
+ if (blockOrExpression && typeof blockOrExpression === "object" && "expressions" in blockOrExpression) {
4593
+ const { expressions } = blockOrExpression;
4550
4594
  const l = expressions.length;
4551
4595
  const results = [];
4552
4596
  let i1 = 0;
@@ -4645,7 +4689,7 @@ function handleThisPrivateShorthands(value) {
4645
4689
  }
4646
4690
  function processCallMemberExpression(node) {
4647
4691
  const { children } = node;
4648
- if (children[0]?.parenthesizedOp?.token && children[1]?.type === "Call") {
4692
+ 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") {
4649
4693
  const op = children[0].parenthesizedOp;
4650
4694
  let call = children[1];
4651
4695
  const args = [...call.args];
@@ -4739,14 +4783,14 @@ function processCallMemberExpression(node) {
4739
4783
  });
4740
4784
  }
4741
4785
  }
4742
- let ref2;
4786
+ let ref1;
4743
4787
  let object = {
4744
4788
  type: "ObjectExpression",
4745
4789
  children: [
4746
4790
  glob.object.children[0],
4747
4791
  // {
4748
4792
  ...parts,
4749
- (ref2 = glob.object.children)[ref2.length - 1]
4793
+ (ref1 = glob.object.children)[ref1.length - 1]
4750
4794
  // whitespace and }
4751
4795
  ],
4752
4796
  properties: parts,
@@ -4798,7 +4842,7 @@ function replaceNode(node, newNode, parent) {
4798
4842
  throw new Error("replaceNode failed: node has no parent");
4799
4843
  }
4800
4844
  function recurse(children) {
4801
- for (let i3 = 0, len2 = children.length; i3 < len2; i3++) {
4845
+ for (let i3 = 0, len22 = children.length; i3 < len22; i3++) {
4802
4846
  const i = i3;
4803
4847
  const child = children[i3];
4804
4848
  if (child === node) {
@@ -4888,8 +4932,8 @@ function convertNamedImportsToObject(node, pattern) {
4888
4932
  return { type: "Error", message: "cannot use `type` in dynamic import" };
4889
4933
  } else {
4890
4934
  const { source, binding } = specifier;
4891
- let ref3;
4892
- const delim = (ref3 = specifier.children)[ref3.length - 1];
4935
+ let ref2;
4936
+ const delim = (ref2 = specifier.children)[ref2.length - 1];
4893
4937
  return {
4894
4938
  type: pattern ? "BindingProperty" : "Property",
4895
4939
  name: source,
@@ -4899,7 +4943,7 @@ function convertNamedImportsToObject(node, pattern) {
4899
4943
  };
4900
4944
  }
4901
4945
  });
4902
- let ref4;
4946
+ let ref3;
4903
4947
  return {
4904
4948
  type: pattern ? "ObjectBindingPattern" : "ObjectExpression",
4905
4949
  names: node.names,
@@ -4908,7 +4952,7 @@ function convertNamedImportsToObject(node, pattern) {
4908
4952
  node.children[0],
4909
4953
  // {
4910
4954
  properties,
4911
- (ref4 = node.children)[ref4.length - 1]
4955
+ (ref3 = node.children)[ref3.length - 1]
4912
4956
  // }
4913
4957
  ]
4914
4958
  };
@@ -5085,17 +5129,17 @@ function processAssignments(statements) {
5085
5129
  statements,
5086
5130
  (n) => n.type === "AssignmentExpression" && n.names === null,
5087
5131
  (exp) => {
5088
- let { lhs: $1, expression: $2 } = exp, tail = [], i = 0, len2 = $1.length;
5132
+ let { lhs: $1, expression: $2 } = exp, tail = [], i = 0, len3 = $1.length;
5089
5133
  let block;
5090
- let ref5;
5091
- if (exp.parent?.type === "BlockStatement" && !(ref5 = $1[$1.length - 1])?.[ref5.length - 1]?.special) {
5134
+ let ref4;
5135
+ if (exp.parent?.type === "BlockStatement" && !(ref4 = $1[$1.length - 1])?.[ref4.length - 1]?.special) {
5092
5136
  block = makeBlockFragment();
5093
- let ref6;
5094
- if (ref6 = prependStatementExpressionBlock(
5137
+ let ref5;
5138
+ if (ref5 = prependStatementExpressionBlock(
5095
5139
  { type: "Initializer", expression: $2, children: [void 0, void 0, $2] },
5096
5140
  block
5097
5141
  )) {
5098
- const ref = ref6;
5142
+ const ref = ref5;
5099
5143
  exp.children = exp.children.map(function(c) {
5100
5144
  if (c === $2)
5101
5145
  return ref;
@@ -5125,7 +5169,7 @@ function processAssignments(statements) {
5125
5169
  }
5126
5170
  }
5127
5171
  let wrapped = false;
5128
- while (i < len2) {
5172
+ while (i < len3) {
5129
5173
  const lastAssignment = $1[i++];
5130
5174
  const [, lhs, , op] = lastAssignment;
5131
5175
  if (op.token !== "=")
@@ -5138,7 +5182,7 @@ function processAssignments(statements) {
5138
5182
  }
5139
5183
  }
5140
5184
  }
5141
- i = len2 - 1;
5185
+ i = len3 - 1;
5142
5186
  while (i >= 0) {
5143
5187
  const lastAssignment = $1[i];
5144
5188
  if (lastAssignment[3].token === "=") {
@@ -5174,14 +5218,14 @@ function processAssignments(statements) {
5174
5218
  }
5175
5219
  i--;
5176
5220
  }
5177
- i = len2 - 1;
5221
+ i = len3 - 1;
5178
5222
  const optionalChainRef = makeRef();
5179
5223
  while (i >= 0) {
5180
5224
  const assignment = $1[i];
5181
5225
  const [ws1, lhs, ws2, op] = assignment;
5182
5226
  if (lhs.type === "MemberExpression" || lhs.type === "CallExpression") {
5183
5227
  const newMemberExp = unchainOptionalMemberExpression(lhs, optionalChainRef, (children) => {
5184
- const assigns = $1.splice(i + 1, len2 - 1 - i);
5228
+ const assigns = $1.splice(i + 1, len3 - 1 - i);
5185
5229
  $1.pop();
5186
5230
  return [ws1, ...children, ws2, op, ...assigns, $2];
5187
5231
  });
@@ -5263,9 +5307,9 @@ function unchainOptionalMemberExpression(exp, ref, innerExp) {
5263
5307
  }
5264
5308
  j++;
5265
5309
  }
5266
- let ref7;
5267
- if (ref7 = conditions.length) {
5268
- const l = ref7;
5310
+ let ref6;
5311
+ if (ref6 = conditions.length) {
5312
+ const l = ref6;
5269
5313
  const cs = flatJoin(conditions, " && ");
5270
5314
  return {
5271
5315
  ...exp,
@@ -5302,8 +5346,8 @@ function processTypes(node) {
5302
5346
  return gatherRecursiveAll(node, (n) => n.type === "UnaryType").forEach((unary) => {
5303
5347
  let last;
5304
5348
  let count = 0;
5305
- let ref8;
5306
- while (unary.suffix.length && (ref8 = unary.suffix)[ref8.length - 1]?.token === "?") {
5349
+ let ref7;
5350
+ while (unary.suffix.length && (ref7 = unary.suffix)[ref7.length - 1]?.token === "?") {
5307
5351
  last = unary.suffix.pop();
5308
5352
  count++;
5309
5353
  }
@@ -5337,11 +5381,11 @@ function processStatementExpressions(statements) {
5337
5381
  gatherRecursiveAll(statements, ($) => $.type === "StatementExpression").forEach((_exp) => {
5338
5382
  const exp = _exp;
5339
5383
  const { statement } = exp;
5340
- let ref9;
5384
+ let ref8;
5341
5385
  switch (statement.type) {
5342
5386
  case "IfStatement": {
5343
- if (ref9 = expressionizeIfStatement(statement)) {
5344
- const expression = ref9;
5387
+ if (ref8 = expressionizeIfStatement(statement)) {
5388
+ const expression = ref8;
5345
5389
  return replaceNode(statement, expression, exp);
5346
5390
  } else {
5347
5391
  return replaceNode(statement, wrapIIFE([["", statement]]), exp);
@@ -5349,7 +5393,8 @@ function processStatementExpressions(statements) {
5349
5393
  }
5350
5394
  case "IterationExpression": {
5351
5395
  if (statement.subtype === "ComptimeStatement") {
5352
- const expression = wrapIIFE(statement.statement.block.expressions);
5396
+ const { expressions } = statement.statement.block;
5397
+ const expression = wrapIIFE(expressions, hasAwait(expressions));
5353
5398
  return replaceNode(statement, makeNode({
5354
5399
  type: "ComptimeExpression",
5355
5400
  expression,
@@ -5427,7 +5472,13 @@ function processProgram(root) {
5427
5472
  processBlocks(statements);
5428
5473
  populateRefs(statements);
5429
5474
  adjustAtBindings(statements);
5430
- processComptime(statements);
5475
+ if (getSync()) {
5476
+ processComptime(statements);
5477
+ }
5478
+ }
5479
+ async function processProgramAsync(root) {
5480
+ const { expressions: statements } = root;
5481
+ await processComptime(statements);
5431
5482
  }
5432
5483
  function populateRefs(statements) {
5433
5484
  const refNodes = gatherRecursive(statements, ({ type }) => type === "Ref");
@@ -5551,8 +5602,8 @@ function processPlaceholders(statements) {
5551
5602
  for (let i4 = 0, len3 = placeholders.length; i4 < len3; i4++) {
5552
5603
  const placeholder = placeholders[i4];
5553
5604
  typeSuffix ??= placeholder.typeSuffix;
5554
- let ref10;
5555
- replaceNode((ref10 = placeholder.children)[ref10.length - 1], ref);
5605
+ let ref9;
5606
+ replaceNode((ref9 = placeholder.children)[ref9.length - 1], ref);
5556
5607
  }
5557
5608
  const { parent } = ancestor;
5558
5609
  const body = maybeUnwrap(ancestor);
@@ -5583,9 +5634,9 @@ function processPlaceholders(statements) {
5583
5634
  fnExp = makeLeftHandSideExpression(fnExp);
5584
5635
  }
5585
5636
  replaceNode(ancestor, fnExp, parent);
5586
- let ref11;
5587
- if (ref11 = getTrimmingSpace(body)) {
5588
- const ws = ref11;
5637
+ let ref10;
5638
+ if (ref10 = getTrimmingSpace(body)) {
5639
+ const ws = ref10;
5589
5640
  inplaceInsertTrimmingSpace(body, "");
5590
5641
  inplacePrepend(ws, fnExp);
5591
5642
  }
@@ -5630,8 +5681,8 @@ function reorderBindingRestProperty(props) {
5630
5681
  }
5631
5682
  ];
5632
5683
  }
5633
- let ref12;
5634
- if (Array.isArray(rest.delim) && (ref12 = rest.delim)[ref12.length - 1]?.token === ",") {
5684
+ let ref11;
5685
+ if (Array.isArray(rest.delim) && (ref11 = rest.delim)[ref11.length - 1]?.token === ",") {
5635
5686
  rest.delim = rest.delim.slice(0, -1);
5636
5687
  rest.children = [...rest.children.slice(0, -1), rest.delim];
5637
5688
  }
@@ -6829,15 +6880,20 @@ var $R92 = (0, import_lib2.$R)(new RegExp("(?=[ \\t\\r\\n\\/#]|$)", "suy"));
6829
6880
  var $R93 = (0, import_lib2.$R)(new RegExp("\\r\\n|\\n|\\r|$", "suy"));
6830
6881
  var $R94 = (0, import_lib2.$R)(new RegExp("[^]*", "suy"));
6831
6882
  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) {
6883
+ var reset = $1;
6884
+ var init = $2;
6885
+ var ws1 = $3;
6832
6886
  var statements = $4;
6833
- processProgram({
6887
+ var ws2 = $5;
6888
+ const program = {
6834
6889
  type: "BlockStatement",
6835
6890
  expressions: statements,
6836
- children: [statements],
6891
+ children: [reset, init, ws1, statements, ws2],
6837
6892
  bare: true,
6838
6893
  root: true
6839
- }, ReservedWord);
6840
- return $0;
6894
+ };
6895
+ processProgram(program);
6896
+ return program;
6841
6897
  });
6842
6898
  function Program(ctx, state2) {
6843
6899
  return (0, import_lib2.$EVENT)(ctx, state2, "Program", Program$0);
@@ -8435,11 +8491,11 @@ var PropertyAccess$1 = (0, import_lib2.$TS)((0, import_lib2.$S)(AccessStart, (0,
8435
8491
  var dot = $1;
8436
8492
  var neg = $2;
8437
8493
  var num = $3;
8438
- const len2 = {
8494
+ const len3 = {
8439
8495
  children: []
8440
8496
  }, children = [
8441
8497
  adjustIndexAccess(dot),
8442
- len2,
8498
+ len3,
8443
8499
  neg,
8444
8500
  num,
8445
8501
  "]"
@@ -8447,7 +8503,7 @@ var PropertyAccess$1 = (0, import_lib2.$TS)((0, import_lib2.$S)(AccessStart, (0,
8447
8503
  return {
8448
8504
  type: "NegativeIndex",
8449
8505
  children,
8450
- len: len2
8506
+ len: len3
8451
8507
  };
8452
8508
  });
8453
8509
  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) {
@@ -16119,26 +16175,29 @@ var parser = function() {
16119
16175
  const parser2 = options.startRule != null ? grammar[options.startRule] : Object.values(grammar)[0];
16120
16176
  if (!parser2)
16121
16177
  throw new Error(`Could not find rule with name '${options.startRule}'`);
16122
- const filename = options.filename || "<anonymous>";
16178
+ const filename2 = options.filename || "<anonymous>";
16123
16179
  reset();
16124
16180
  Object.assign(ctx, { ...options.events, tokenize: options.tokenize });
16125
16181
  return validate(input, parser2(ctx, {
16126
16182
  input,
16127
16183
  pos: 0
16128
16184
  }), {
16129
- filename
16185
+ filename: filename2
16130
16186
  });
16131
16187
  }
16132
16188
  };
16133
16189
  }();
16134
16190
  var { parse } = parser;
16191
+ var filename;
16135
16192
  var initialConfig;
16136
16193
  var config;
16194
+ var sync;
16137
16195
  var state = {};
16138
16196
  var getState = () => state;
16139
16197
  var getConfig = () => config;
16140
16198
  var getInitialConfig = () => initialConfig;
16141
- var setInitialConfig = (c) => initialConfig = c;
16199
+ var getFilename = () => filename;
16200
+ var getSync = () => sync;
16142
16201
  Object.defineProperties(state, {
16143
16202
  currentIndent: {
16144
16203
  get() {
@@ -16183,6 +16242,21 @@ Object.defineProperties(state, {
16183
16242
  }
16184
16243
  }
16185
16244
  });
16245
+ function parseProgram(input, options) {
16246
+ filename = options?.filename;
16247
+ initialConfig = options?.parseOptions;
16248
+ sync = options?.sync;
16249
+ const root = parse(input, options);
16250
+ if (sync) {
16251
+ filename = initialConfig = sync = null;
16252
+ return root;
16253
+ } else {
16254
+ return processProgramAsync(root).then(() => {
16255
+ filename = initialConfig = sync = null;
16256
+ return root;
16257
+ });
16258
+ }
16259
+ }
16186
16260
 
16187
16261
  // source/util.civet
16188
16262
  var util_exports = {};
@@ -16609,15 +16683,15 @@ var uncacheable = /* @__PURE__ */ new Set([
16609
16683
  "RestoreTrailingMemberProperty",
16610
16684
  "RestoreNewlineBinaryOp"
16611
16685
  ]);
16612
- var compile = function(src, options) {
16686
+ function compile(src, options) {
16613
16687
  if (!options) {
16614
16688
  options = {};
16615
16689
  } else {
16616
16690
  options = { ...options };
16617
16691
  }
16618
16692
  options.parseOptions = { ...options.parseOptions };
16619
- const filename = options.filename || "unknown";
16620
- if (filename.endsWith(".coffee") && !/^(#![^\r\n]*(\r\n|\n|\r))?\s*['"]civet/.test(src)) {
16693
+ const filename2 = options.filename || "unknown";
16694
+ if (filename2.endsWith(".coffee") && !/^(#![^\r\n]*(\r\n|\n|\r))?\s*['"]civet/.test(src)) {
16621
16695
  options.parseOptions.coffeeCompat = true;
16622
16696
  }
16623
16697
  const { hits, trace, noCache } = options;
@@ -16629,21 +16703,18 @@ var compile = function(src, options) {
16629
16703
  });
16630
16704
  }
16631
16705
  let ast;
16632
- setInitialConfig(options.parseOptions);
16633
16706
  try {
16634
- ast = parse(src, {
16635
- filename,
16707
+ ast = parseProgram(src, {
16708
+ parseOptions: options.parseOptions,
16709
+ sync: options.sync,
16710
+ filename: filename2,
16636
16711
  events
16637
16712
  });
16638
- if (!(options.ast === "raw")) {
16639
- ast = prune(ast);
16640
- }
16641
16713
  } finally {
16642
- setInitialConfig(void 0);
16643
16714
  if (hits || trace) {
16644
- import("fs").then(function({ writeFileSync }) {
16715
+ import("node:fs").then(function({ writeFileSync }) {
16645
16716
  let ref;
16646
- if ((ref = events?.meta) && "logs" in ref) {
16717
+ if ((ref = events?.meta) && typeof ref === "object" && "logs" in ref) {
16647
16718
  const { logs } = ref;
16648
16719
  if (trace) {
16649
16720
  writeFileSync(trace, logs.join("\n"));
@@ -16672,34 +16743,45 @@ ${counts}`;
16672
16743
  });
16673
16744
  }
16674
16745
  }
16675
- if (options.ast) {
16676
- return ast;
16677
- }
16678
- function checkErrors() {
16679
- if (options.errors?.length) {
16680
- throw new Error(`Parse errors: ${options.errors.map(($) => $.message).join("\n")} `);
16746
+ function rest(ast2) {
16747
+ options = options;
16748
+ if (!(options.ast === "raw")) {
16749
+ ast2 = prune(ast2);
16681
16750
  }
16682
- ;
16683
- return;
16684
- }
16685
- if (options.sourceMap || options.inlineMap) {
16686
- const sm = SourceMap2(src);
16687
- options.updateSourceMap = sm.updateSourceMap;
16688
- const code = generate_default(ast, options);
16689
- checkErrors();
16690
- if (options.inlineMap) {
16691
- return SourceMap2.remap(code, sm, filename, filename + ".tsx");
16692
- } else {
16693
- return {
16694
- code,
16695
- sourceMap: sm
16696
- };
16751
+ if (options.ast) {
16752
+ return ast2;
16753
+ }
16754
+ function checkErrors() {
16755
+ if (options.errors?.length) {
16756
+ throw new Error(`Parse errors: ${options.errors.map(($) => $.message).join("\n")} `);
16757
+ }
16758
+ ;
16759
+ return;
16697
16760
  }
16761
+ if (options.sourceMap || options.inlineMap) {
16762
+ const sm = SourceMap2(src);
16763
+ options.updateSourceMap = sm.updateSourceMap;
16764
+ const code = generate_default(ast2, options);
16765
+ checkErrors();
16766
+ if (options.inlineMap) {
16767
+ return SourceMap2.remap(code, sm, filename2, filename2 + ".tsx");
16768
+ } else {
16769
+ return {
16770
+ code,
16771
+ sourceMap: sm
16772
+ };
16773
+ }
16774
+ }
16775
+ const result = generate_default(ast2, options);
16776
+ checkErrors();
16777
+ return result;
16698
16778
  }
16699
- const result = generate_default(ast, options);
16700
- checkErrors();
16701
- return result;
16702
- };
16779
+ if (ast.then != null) {
16780
+ return ast.then(rest);
16781
+ } else {
16782
+ return rest(ast);
16783
+ }
16784
+ }
16703
16785
  var makeCache = function({ hits, trace } = {}) {
16704
16786
  const meta = {};
16705
16787
  let hitCount;
@@ -16766,7 +16848,7 @@ var isCompileError = function(err) {
16766
16848
  return err instanceof Error && //@ts-ignore
16767
16849
  [err.message, err.name, err.filename, err.line, err.column, err.offset].every(($1) => $1 !== void 0);
16768
16850
  };
16769
- var main_default = { parse, generate: generate_default, util: util_exports, compile, isCompileError };
16851
+ var main_default = { parse, parseProgram, generate: generate_default, util: util_exports, compile, isCompileError };
16770
16852
  // Annotate the CommonJS export names for ESM import in node:
16771
16853
  0 && (module.exports = {
16772
16854
  compile,
@@ -16774,6 +16856,7 @@ var main_default = { parse, generate: generate_default, util: util_exports, comp
16774
16856
  isCompileError,
16775
16857
  lib,
16776
16858
  parse,
16859
+ parseProgram,
16777
16860
  prune,
16778
16861
  util
16779
16862
  });