@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.mjs CHANGED
@@ -416,14 +416,14 @@ var require_machine = __commonJS({
416
416
  }, [1, 1]);
417
417
  return [line, column];
418
418
  }
419
- function validate(input, result, { filename }) {
419
+ function validate(input, result, { filename: filename2 }) {
420
420
  if (result && result.pos === input.length)
421
421
  return result.value;
422
422
  const expectations = Array.from(new Set(failExpected.slice(0, failIndex)));
423
423
  let l = location(input, maxFailPos), [line, column] = l;
424
424
  if (result && result.pos > maxFailPos) {
425
425
  l = location(input, result.pos);
426
- throw new Error(`${filename}:${line}:${column} Unconsumed input at #{l}
426
+ throw new Error(`${filename2}:${line}:${column} Unconsumed input at #{l}
427
427
 
428
428
  ${input.slice(result.pos)}
429
429
  `);
@@ -435,11 +435,11 @@ ${input.slice(result.pos)}
435
435
  hint = JSON.stringify(hint);
436
436
  else
437
437
  hint = "EOF";
438
- const error = new ParseError(`${filename}:${line}:${column} Failed to parse
438
+ const error = new ParseError(`${filename2}:${line}:${column} Failed to parse
439
439
  Expected:
440
440
  ${expectations.join("\n ")}
441
441
  Found: ${hint}
442
- `, "ParseError", filename, line, column, maxFailPos);
442
+ `, "ParseError", filename2, line, column, maxFailPos);
443
443
  throw error;
444
444
  }
445
445
  if (result) {
@@ -463,11 +463,11 @@ ${input.slice(result.pos)}
463
463
  };
464
464
  }
465
465
  var ParseError = class extends Error {
466
- constructor(message, name, filename, line, column, offset) {
466
+ constructor(message, name, filename2, line, column, offset) {
467
467
  super(message);
468
468
  this.message = message;
469
469
  this.name = name;
470
- this.filename = filename;
470
+ this.filename = filename2;
471
471
  this.line = line;
472
472
  this.column = column;
473
473
  this.offset = offset;
@@ -531,6 +531,7 @@ __export(lib_exports, {
531
531
  processCoffeeInterpolation: () => processCoffeeInterpolation,
532
532
  processForInOf: () => processForInOf,
533
533
  processProgram: () => processProgram,
534
+ processProgramAsync: () => processProgramAsync,
534
535
  processUnaryExpression: () => processUnaryExpression,
535
536
  quoteString: () => quoteString,
536
537
  reorderBindingRestProperty: () => reorderBindingRestProperty,
@@ -1153,14 +1154,12 @@ function processParams(f) {
1153
1154
  if (!prefix.length)
1154
1155
  return;
1155
1156
  if (isConstructor) {
1156
- const superCalls = gatherNodes(expressions, (exp) => {
1157
- return exp.type === "CallExpression" && exp.children[0]?.token === "super";
1158
- });
1157
+ const superCalls = gatherNodes(
1158
+ expressions,
1159
+ (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"
1160
+ );
1159
1161
  if (superCalls.length) {
1160
- const { child } = findAncestor(
1161
- superCalls[0],
1162
- (ancestor) => ancestor === block
1163
- );
1162
+ const { child } = findAncestor(superCalls[0], (a1) => a1 === block);
1164
1163
  const index = findChildIndex(expressions, child);
1165
1164
  if (index < 0) {
1166
1165
  throw new Error("Could not find super call within top-level expressions");
@@ -1245,7 +1244,7 @@ function blockWithPrefix(prefixStatements, block) {
1245
1244
  return block;
1246
1245
  }
1247
1246
  function braceBlock(block) {
1248
- if (block.bare) {
1247
+ if (block.bare && !block.root) {
1249
1248
  if (block.children === block.expressions) {
1250
1249
  block.children = [block.expressions];
1251
1250
  }
@@ -1297,7 +1296,7 @@ function makeBlockFragment() {
1297
1296
  function replaceBlockExpression(node, child, replacement) {
1298
1297
  let found = false;
1299
1298
  const { expressions } = node;
1300
- for (let i1 = 0, len2 = expressions.length; i1 < len2; i1++) {
1299
+ for (let i1 = 0, len3 = expressions.length; i1 < len3; i1++) {
1301
1300
  const statement = expressions[i1];
1302
1301
  const [, s] = statement;
1303
1302
  if (s === child) {
@@ -1379,7 +1378,7 @@ function needsPrecedingSemicolon(exp) {
1379
1378
  return false;
1380
1379
  }
1381
1380
  if (Array.isArray(exp)) {
1382
- for (let i3 = 0, len2 = exp.length; i3 < len2; i3++) {
1381
+ for (let i3 = 0, len22 = exp.length; i3 < len22; i3++) {
1383
1382
  const child = exp[i3];
1384
1383
  if (!(child != null)) {
1385
1384
  continue;
@@ -1422,6 +1421,9 @@ function needsPrecedingSemicolon(exp) {
1422
1421
  }
1423
1422
 
1424
1423
  // source/parser/util.civet
1424
+ function len(arr, length) {
1425
+ return arr.length === length;
1426
+ }
1425
1427
  var assert = {
1426
1428
  equal(a, b, msg) {
1427
1429
  if (a !== b) {
@@ -1445,7 +1447,7 @@ function addParentPointers(node, parent) {
1445
1447
  node.parent = parent;
1446
1448
  }
1447
1449
  if (node.children) {
1448
- for (let ref1 = node.children, i1 = 0, len2 = ref1.length; i1 < len2; i1++) {
1450
+ for (let ref1 = node.children, i1 = 0, len1 = ref1.length; i1 < len1; i1++) {
1449
1451
  const child = ref1[i1];
1450
1452
  addParentPointers(child, node);
1451
1453
  }
@@ -1502,8 +1504,7 @@ function isEmptyBareBlock(node) {
1502
1504
  if (node?.type !== "BlockStatement")
1503
1505
  return false;
1504
1506
  const { bare, expressions } = node;
1505
- return bare && //expressions is like [], [, {type: "EmptyStatement"}]
1506
- (expressions.length === 0 || expressions.length === 1 && expressions[0][1]?.type === "EmptyStatement");
1507
+ 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");
1507
1508
  }
1508
1509
  function isFunction(node) {
1509
1510
  const { type } = node;
@@ -1860,7 +1861,7 @@ function updateParentPointers(node, parent, depth = 1) {
1860
1861
  return;
1861
1862
  }
1862
1863
  if (Array.isArray(node)) {
1863
- for (let i2 = 0, len1 = node.length; i2 < len1; i2++) {
1864
+ for (let i2 = 0, len22 = node.length; i2 < len22; i2++) {
1864
1865
  const child = node[i2];
1865
1866
  updateParentPointers(child, parent, depth);
1866
1867
  }
@@ -1871,7 +1872,7 @@ function updateParentPointers(node, parent, depth = 1) {
1871
1872
  node.parent = parent;
1872
1873
  }
1873
1874
  if (depth && isParent(node)) {
1874
- for (let ref4 = node.children, i3 = 0, len2 = ref4.length; i3 < len2; i3++) {
1875
+ for (let ref4 = node.children, i3 = 0, len3 = ref4.length; i3 < len3; i3++) {
1875
1876
  const child = ref4[i3];
1876
1877
  updateParentPointers(child, node, depth - 1);
1877
1878
  }
@@ -2002,7 +2003,7 @@ function wrapWithReturn(expression) {
2002
2003
  }
2003
2004
  function flatJoin(array, separator) {
2004
2005
  const result = [];
2005
- for (let i4 = 0, len3 = array.length; i4 < len3; i4++) {
2006
+ for (let i4 = 0, len4 = array.length; i4 < len4; i4++) {
2006
2007
  const i = i4;
2007
2008
  const items = array[i4];
2008
2009
  if (i) {
@@ -2025,7 +2026,7 @@ function findChildIndex(parent, child) {
2025
2026
  if (!(children != null)) {
2026
2027
  return -1;
2027
2028
  }
2028
- for (let i1 = 0, len2 = children.length; i1 < len2; i1++) {
2029
+ for (let i1 = 0, len3 = children.length; i1 < len3; i1++) {
2029
2030
  const i = i1;
2030
2031
  const c = children[i1];
2031
2032
  if (c === child || Array.isArray(c) && arrayRecurse(c)) {
@@ -2140,7 +2141,7 @@ var precedenceOrder = [
2140
2141
  ["**"]
2141
2142
  ];
2142
2143
  var precedenceMap = /* @__PURE__ */ new Map();
2143
- for (let i1 = 0, len2 = precedenceOrder.length; i1 < len2; i1++) {
2144
+ for (let i1 = 0, len3 = precedenceOrder.length; i1 < len3; i1++) {
2144
2145
  const prec = i1;
2145
2146
  const ops = precedenceOrder[i1];
2146
2147
  for (let i2 = 0, len1 = ops.length; i2 < len1; i2++) {
@@ -2251,7 +2252,7 @@ function processBinaryOpExpression($0) {
2251
2252
  } else {
2252
2253
  b = recurse(b);
2253
2254
  }
2254
- if (op.token === "instanceof" && b.type === "Literal" && b.children?.[0]?.type === "StringLiteral") {
2255
+ 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")) {
2255
2256
  a = ["typeof ", makeLeftHandSideExpression(a)];
2256
2257
  if (op.negated) {
2257
2258
  op = { ...op, token: "!==", negated: false };
@@ -2356,7 +2357,7 @@ function expandChainedComparisons([first, binops]) {
2356
2357
  let start = 0;
2357
2358
  const chains = [];
2358
2359
  var i = 0;
2359
- for (let i3 = 0, len2 = binops.length; i3 < len2; i3++) {
2360
+ for (let i3 = 0, len22 = binops.length; i3 < len22; i3++) {
2360
2361
  var i = i3;
2361
2362
  var [, op] = binops[i3];
2362
2363
  if (isRelationalOp(op)) {
@@ -2966,7 +2967,7 @@ function aliasBinding(p, ref) {
2966
2967
  }
2967
2968
 
2968
2969
  // source/parser/declaration.civet
2969
- function len(arr, length) {
2970
+ function len2(arr, length) {
2970
2971
  return arr.length === length;
2971
2972
  }
2972
2973
  function processAssignmentDeclaration(decl, pattern, suffix, ws, assign, e) {
@@ -3164,7 +3165,7 @@ function processDeclarationConditionStatement(s) {
3164
3165
  return;
3165
3166
  }
3166
3167
  let { expression } = condition;
3167
- 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]) {
3168
+ 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]) {
3168
3169
  const { type: type1, children: [, { type: type2, expression: expression2 }] } = expression;
3169
3170
  const type = [type1, type2];
3170
3171
  expression = expression2;
@@ -3173,7 +3174,7 @@ function processDeclarationConditionStatement(s) {
3173
3174
  const { ref, pattern } = expression;
3174
3175
  if (pattern) {
3175
3176
  const conditions = getPatternConditions(pattern, ref).filter((c) => {
3176
- if (Array.isArray(c) && len(c, 2) && c[0] === ref && c[1] === " != null") {
3177
+ if (Array.isArray(c) && len2(c, 2) && c[0] === ref && c[1] === " != null") {
3177
3178
  const [,] = c;
3178
3179
  return false;
3179
3180
  } else {
@@ -4090,36 +4091,79 @@ function processComptime(statements) {
4090
4091
  if (!getInitialConfig()?.comptime) {
4091
4092
  return;
4092
4093
  }
4093
- gatherRecursive(
4094
+ let prepareEval = () => {
4095
+ };
4096
+ if (getSync()) {
4097
+ runComptime(statements, prepareEval);
4098
+ return;
4099
+ } else {
4100
+ return (async () => {
4101
+ {
4102
+ try {
4103
+ const path = (await import("node:path")).default;
4104
+ const { createRequire } = await import("node:module");
4105
+ return prepareEval = () => {
4106
+ global.__filename = path.resolve(getFilename() ?? "");
4107
+ global.__dirname = path.dirname(global.__filename);
4108
+ return global.require = createRequire(global.__filename);
4109
+ };
4110
+ } catch (e) {
4111
+ return;
4112
+ }
4113
+ }
4114
+ })().then(async () => {
4115
+ await Promise.all(runComptime(statements, prepareEval));
4116
+ });
4117
+ }
4118
+ }
4119
+ function runComptime(statements, prepareEval) {
4120
+ const sync2 = getSync();
4121
+ return gatherRecursive(
4094
4122
  statements,
4095
4123
  (node) => {
4096
4124
  return node.type === "ComptimeStatement" || node.type === "ComptimeExpression";
4097
4125
  }
4098
- ).forEach((exp) => {
4126
+ ).map((exp) => {
4099
4127
  const content = exp.type === "ComptimeStatement" ? exp.block : exp.expression;
4100
4128
  const options = { js: true };
4101
4129
  const js = generate_default(prune(content), options);
4102
4130
  if (options.errors != null) {
4103
4131
  return;
4104
4132
  }
4105
- const output = eval?.(`"use strict";${js}`);
4133
+ prepareEval();
4134
+ let output = eval?.(`"use strict";${js}`);
4135
+ let promise;
4106
4136
  if (exp.type === "ComptimeExpression") {
4107
- let string;
4108
- try {
4109
- string = serialize(output);
4110
- } catch (e) {
4111
- exp.children = [
4137
+ const finish = () => {
4138
+ let string;
4139
+ try {
4140
+ string = serialize(output);
4141
+ } catch (e) {
4142
+ exp.children = [
4143
+ {
4144
+ type: "Error",
4145
+ message: `comptime result ${output} not serializable: ${e}`
4146
+ }
4147
+ ];
4148
+ return;
4149
+ }
4150
+ return exp.children = [string];
4151
+ };
4152
+ if (sync2) {
4153
+ finish();
4154
+ } else {
4155
+ promise = (async () => {
4112
4156
  {
4113
- type: "Error",
4114
- message: `comptime result ${output} not JSON serializable: ${e}`
4157
+ output = await output;
4158
+ return finish();
4115
4159
  }
4116
- ];
4117
- return;
4160
+ })();
4118
4161
  }
4119
- return exp.children = [string];
4120
4162
  } else {
4121
- return exp.children = [];
4163
+ promise = output;
4164
+ exp.children = [];
4122
4165
  }
4166
+ return promise;
4123
4167
  });
4124
4168
  }
4125
4169
  function serialize(value) {
@@ -4227,7 +4271,7 @@ function serialize(value) {
4227
4271
  case Object.prototype: {
4228
4272
  let objStr = "{";
4229
4273
  let descStr = "";
4230
- for (let ref2 = Object.getOwnPropertyNames(val).concat(Object.getOwnPropertySymbols(val)), i = 0, len2 = ref2.length; i < len2; i++) {
4274
+ for (let ref2 = Object.getOwnPropertyNames(val).concat(Object.getOwnPropertySymbols(val)), i = 0, len3 = ref2.length; i < len3; i++) {
4231
4275
  const prop = ref2[i];
4232
4276
  const desc = Object.getOwnPropertyDescriptor(val, prop);
4233
4277
  if (desc.enumerable && desc.configurable && desc.writable) {
@@ -4316,7 +4360,7 @@ function getIndentLevel(str, tab) {
4316
4360
  }
4317
4361
  function reduceIndentLevel(str, dedent, tab) {
4318
4362
  if (tab != null && tab != 1) {
4319
- for (let i1 = 0, len2 = str.length; i1 < len2; i1++) {
4363
+ for (let i1 = 0, len3 = str.length; i1 < len3; i1++) {
4320
4364
  const i = i1;
4321
4365
  const char = str[i1];
4322
4366
  if (!dedent) {
@@ -4398,7 +4442,7 @@ function dedentBlockSubstitutions($0, tab) {
4398
4442
  ;
4399
4443
  const dedent = ref1;
4400
4444
  let results = [s];
4401
- for (let i3 = 0, len2 = strWithSubstitutions.length; i3 < len2; i3++) {
4445
+ for (let i3 = 0, len22 = strWithSubstitutions.length; i3 < len22; i3++) {
4402
4446
  const i = i3;
4403
4447
  let part = strWithSubstitutions[i3];
4404
4448
  if (part.token != null) {
@@ -4528,9 +4572,8 @@ function isExpression(node) {
4528
4572
  return true;
4529
4573
  }
4530
4574
  function expressionizeBlock(blockOrExpression) {
4531
- let ref1;
4532
- if ((ref1 = blockOrExpression) && "expressions" in ref1) {
4533
- const { expressions } = ref1;
4575
+ if (blockOrExpression && typeof blockOrExpression === "object" && "expressions" in blockOrExpression) {
4576
+ const { expressions } = blockOrExpression;
4534
4577
  const l = expressions.length;
4535
4578
  const results = [];
4536
4579
  let i1 = 0;
@@ -4629,7 +4672,7 @@ function handleThisPrivateShorthands(value) {
4629
4672
  }
4630
4673
  function processCallMemberExpression(node) {
4631
4674
  const { children } = node;
4632
- if (children[0]?.parenthesizedOp?.token && children[1]?.type === "Call") {
4675
+ 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") {
4633
4676
  const op = children[0].parenthesizedOp;
4634
4677
  let call = children[1];
4635
4678
  const args = [...call.args];
@@ -4723,14 +4766,14 @@ function processCallMemberExpression(node) {
4723
4766
  });
4724
4767
  }
4725
4768
  }
4726
- let ref2;
4769
+ let ref1;
4727
4770
  let object = {
4728
4771
  type: "ObjectExpression",
4729
4772
  children: [
4730
4773
  glob.object.children[0],
4731
4774
  // {
4732
4775
  ...parts,
4733
- (ref2 = glob.object.children)[ref2.length - 1]
4776
+ (ref1 = glob.object.children)[ref1.length - 1]
4734
4777
  // whitespace and }
4735
4778
  ],
4736
4779
  properties: parts,
@@ -4782,7 +4825,7 @@ function replaceNode(node, newNode, parent) {
4782
4825
  throw new Error("replaceNode failed: node has no parent");
4783
4826
  }
4784
4827
  function recurse(children) {
4785
- for (let i3 = 0, len2 = children.length; i3 < len2; i3++) {
4828
+ for (let i3 = 0, len22 = children.length; i3 < len22; i3++) {
4786
4829
  const i = i3;
4787
4830
  const child = children[i3];
4788
4831
  if (child === node) {
@@ -4872,8 +4915,8 @@ function convertNamedImportsToObject(node, pattern) {
4872
4915
  return { type: "Error", message: "cannot use `type` in dynamic import" };
4873
4916
  } else {
4874
4917
  const { source, binding } = specifier;
4875
- let ref3;
4876
- const delim = (ref3 = specifier.children)[ref3.length - 1];
4918
+ let ref2;
4919
+ const delim = (ref2 = specifier.children)[ref2.length - 1];
4877
4920
  return {
4878
4921
  type: pattern ? "BindingProperty" : "Property",
4879
4922
  name: source,
@@ -4883,7 +4926,7 @@ function convertNamedImportsToObject(node, pattern) {
4883
4926
  };
4884
4927
  }
4885
4928
  });
4886
- let ref4;
4929
+ let ref3;
4887
4930
  return {
4888
4931
  type: pattern ? "ObjectBindingPattern" : "ObjectExpression",
4889
4932
  names: node.names,
@@ -4892,7 +4935,7 @@ function convertNamedImportsToObject(node, pattern) {
4892
4935
  node.children[0],
4893
4936
  // {
4894
4937
  properties,
4895
- (ref4 = node.children)[ref4.length - 1]
4938
+ (ref3 = node.children)[ref3.length - 1]
4896
4939
  // }
4897
4940
  ]
4898
4941
  };
@@ -5069,17 +5112,17 @@ function processAssignments(statements) {
5069
5112
  statements,
5070
5113
  (n) => n.type === "AssignmentExpression" && n.names === null,
5071
5114
  (exp) => {
5072
- let { lhs: $1, expression: $2 } = exp, tail = [], i = 0, len2 = $1.length;
5115
+ let { lhs: $1, expression: $2 } = exp, tail = [], i = 0, len3 = $1.length;
5073
5116
  let block;
5074
- let ref5;
5075
- if (exp.parent?.type === "BlockStatement" && !(ref5 = $1[$1.length - 1])?.[ref5.length - 1]?.special) {
5117
+ let ref4;
5118
+ if (exp.parent?.type === "BlockStatement" && !(ref4 = $1[$1.length - 1])?.[ref4.length - 1]?.special) {
5076
5119
  block = makeBlockFragment();
5077
- let ref6;
5078
- if (ref6 = prependStatementExpressionBlock(
5120
+ let ref5;
5121
+ if (ref5 = prependStatementExpressionBlock(
5079
5122
  { type: "Initializer", expression: $2, children: [void 0, void 0, $2] },
5080
5123
  block
5081
5124
  )) {
5082
- const ref = ref6;
5125
+ const ref = ref5;
5083
5126
  exp.children = exp.children.map(function(c) {
5084
5127
  if (c === $2)
5085
5128
  return ref;
@@ -5109,7 +5152,7 @@ function processAssignments(statements) {
5109
5152
  }
5110
5153
  }
5111
5154
  let wrapped = false;
5112
- while (i < len2) {
5155
+ while (i < len3) {
5113
5156
  const lastAssignment = $1[i++];
5114
5157
  const [, lhs, , op] = lastAssignment;
5115
5158
  if (op.token !== "=")
@@ -5122,7 +5165,7 @@ function processAssignments(statements) {
5122
5165
  }
5123
5166
  }
5124
5167
  }
5125
- i = len2 - 1;
5168
+ i = len3 - 1;
5126
5169
  while (i >= 0) {
5127
5170
  const lastAssignment = $1[i];
5128
5171
  if (lastAssignment[3].token === "=") {
@@ -5158,14 +5201,14 @@ function processAssignments(statements) {
5158
5201
  }
5159
5202
  i--;
5160
5203
  }
5161
- i = len2 - 1;
5204
+ i = len3 - 1;
5162
5205
  const optionalChainRef = makeRef();
5163
5206
  while (i >= 0) {
5164
5207
  const assignment = $1[i];
5165
5208
  const [ws1, lhs, ws2, op] = assignment;
5166
5209
  if (lhs.type === "MemberExpression" || lhs.type === "CallExpression") {
5167
5210
  const newMemberExp = unchainOptionalMemberExpression(lhs, optionalChainRef, (children) => {
5168
- const assigns = $1.splice(i + 1, len2 - 1 - i);
5211
+ const assigns = $1.splice(i + 1, len3 - 1 - i);
5169
5212
  $1.pop();
5170
5213
  return [ws1, ...children, ws2, op, ...assigns, $2];
5171
5214
  });
@@ -5247,9 +5290,9 @@ function unchainOptionalMemberExpression(exp, ref, innerExp) {
5247
5290
  }
5248
5291
  j++;
5249
5292
  }
5250
- let ref7;
5251
- if (ref7 = conditions.length) {
5252
- const l = ref7;
5293
+ let ref6;
5294
+ if (ref6 = conditions.length) {
5295
+ const l = ref6;
5253
5296
  const cs = flatJoin(conditions, " && ");
5254
5297
  return {
5255
5298
  ...exp,
@@ -5286,8 +5329,8 @@ function processTypes(node) {
5286
5329
  return gatherRecursiveAll(node, (n) => n.type === "UnaryType").forEach((unary) => {
5287
5330
  let last;
5288
5331
  let count = 0;
5289
- let ref8;
5290
- while (unary.suffix.length && (ref8 = unary.suffix)[ref8.length - 1]?.token === "?") {
5332
+ let ref7;
5333
+ while (unary.suffix.length && (ref7 = unary.suffix)[ref7.length - 1]?.token === "?") {
5291
5334
  last = unary.suffix.pop();
5292
5335
  count++;
5293
5336
  }
@@ -5321,11 +5364,11 @@ function processStatementExpressions(statements) {
5321
5364
  gatherRecursiveAll(statements, ($) => $.type === "StatementExpression").forEach((_exp) => {
5322
5365
  const exp = _exp;
5323
5366
  const { statement } = exp;
5324
- let ref9;
5367
+ let ref8;
5325
5368
  switch (statement.type) {
5326
5369
  case "IfStatement": {
5327
- if (ref9 = expressionizeIfStatement(statement)) {
5328
- const expression = ref9;
5370
+ if (ref8 = expressionizeIfStatement(statement)) {
5371
+ const expression = ref8;
5329
5372
  return replaceNode(statement, expression, exp);
5330
5373
  } else {
5331
5374
  return replaceNode(statement, wrapIIFE([["", statement]]), exp);
@@ -5333,7 +5376,8 @@ function processStatementExpressions(statements) {
5333
5376
  }
5334
5377
  case "IterationExpression": {
5335
5378
  if (statement.subtype === "ComptimeStatement") {
5336
- const expression = wrapIIFE(statement.statement.block.expressions);
5379
+ const { expressions } = statement.statement.block;
5380
+ const expression = wrapIIFE(expressions, hasAwait(expressions));
5337
5381
  return replaceNode(statement, makeNode({
5338
5382
  type: "ComptimeExpression",
5339
5383
  expression,
@@ -5411,7 +5455,13 @@ function processProgram(root) {
5411
5455
  processBlocks(statements);
5412
5456
  populateRefs(statements);
5413
5457
  adjustAtBindings(statements);
5414
- processComptime(statements);
5458
+ if (getSync()) {
5459
+ processComptime(statements);
5460
+ }
5461
+ }
5462
+ async function processProgramAsync(root) {
5463
+ const { expressions: statements } = root;
5464
+ await processComptime(statements);
5415
5465
  }
5416
5466
  function populateRefs(statements) {
5417
5467
  const refNodes = gatherRecursive(statements, ({ type }) => type === "Ref");
@@ -5535,8 +5585,8 @@ function processPlaceholders(statements) {
5535
5585
  for (let i4 = 0, len3 = placeholders.length; i4 < len3; i4++) {
5536
5586
  const placeholder = placeholders[i4];
5537
5587
  typeSuffix ??= placeholder.typeSuffix;
5538
- let ref10;
5539
- replaceNode((ref10 = placeholder.children)[ref10.length - 1], ref);
5588
+ let ref9;
5589
+ replaceNode((ref9 = placeholder.children)[ref9.length - 1], ref);
5540
5590
  }
5541
5591
  const { parent } = ancestor;
5542
5592
  const body = maybeUnwrap(ancestor);
@@ -5567,9 +5617,9 @@ function processPlaceholders(statements) {
5567
5617
  fnExp = makeLeftHandSideExpression(fnExp);
5568
5618
  }
5569
5619
  replaceNode(ancestor, fnExp, parent);
5570
- let ref11;
5571
- if (ref11 = getTrimmingSpace(body)) {
5572
- const ws = ref11;
5620
+ let ref10;
5621
+ if (ref10 = getTrimmingSpace(body)) {
5622
+ const ws = ref10;
5573
5623
  inplaceInsertTrimmingSpace(body, "");
5574
5624
  inplacePrepend(ws, fnExp);
5575
5625
  }
@@ -5614,8 +5664,8 @@ function reorderBindingRestProperty(props) {
5614
5664
  }
5615
5665
  ];
5616
5666
  }
5617
- let ref12;
5618
- if (Array.isArray(rest.delim) && (ref12 = rest.delim)[ref12.length - 1]?.token === ",") {
5667
+ let ref11;
5668
+ if (Array.isArray(rest.delim) && (ref11 = rest.delim)[ref11.length - 1]?.token === ",") {
5619
5669
  rest.delim = rest.delim.slice(0, -1);
5620
5670
  rest.children = [...rest.children.slice(0, -1), rest.delim];
5621
5671
  }
@@ -6813,15 +6863,20 @@ var $R92 = (0, import_lib2.$R)(new RegExp("(?=[ \\t\\r\\n\\/#]|$)", "suy"));
6813
6863
  var $R93 = (0, import_lib2.$R)(new RegExp("\\r\\n|\\n|\\r|$", "suy"));
6814
6864
  var $R94 = (0, import_lib2.$R)(new RegExp("[^]*", "suy"));
6815
6865
  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) {
6866
+ var reset = $1;
6867
+ var init = $2;
6868
+ var ws1 = $3;
6816
6869
  var statements = $4;
6817
- processProgram({
6870
+ var ws2 = $5;
6871
+ const program = {
6818
6872
  type: "BlockStatement",
6819
6873
  expressions: statements,
6820
- children: [statements],
6874
+ children: [reset, init, ws1, statements, ws2],
6821
6875
  bare: true,
6822
6876
  root: true
6823
- }, ReservedWord);
6824
- return $0;
6877
+ };
6878
+ processProgram(program);
6879
+ return program;
6825
6880
  });
6826
6881
  function Program(ctx, state2) {
6827
6882
  return (0, import_lib2.$EVENT)(ctx, state2, "Program", Program$0);
@@ -8419,11 +8474,11 @@ var PropertyAccess$1 = (0, import_lib2.$TS)((0, import_lib2.$S)(AccessStart, (0,
8419
8474
  var dot = $1;
8420
8475
  var neg = $2;
8421
8476
  var num = $3;
8422
- const len2 = {
8477
+ const len3 = {
8423
8478
  children: []
8424
8479
  }, children = [
8425
8480
  adjustIndexAccess(dot),
8426
- len2,
8481
+ len3,
8427
8482
  neg,
8428
8483
  num,
8429
8484
  "]"
@@ -8431,7 +8486,7 @@ var PropertyAccess$1 = (0, import_lib2.$TS)((0, import_lib2.$S)(AccessStart, (0,
8431
8486
  return {
8432
8487
  type: "NegativeIndex",
8433
8488
  children,
8434
- len: len2
8489
+ len: len3
8435
8490
  };
8436
8491
  });
8437
8492
  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) {
@@ -16103,26 +16158,29 @@ var parser = function() {
16103
16158
  const parser2 = options.startRule != null ? grammar[options.startRule] : Object.values(grammar)[0];
16104
16159
  if (!parser2)
16105
16160
  throw new Error(`Could not find rule with name '${options.startRule}'`);
16106
- const filename = options.filename || "<anonymous>";
16161
+ const filename2 = options.filename || "<anonymous>";
16107
16162
  reset();
16108
16163
  Object.assign(ctx, { ...options.events, tokenize: options.tokenize });
16109
16164
  return validate(input, parser2(ctx, {
16110
16165
  input,
16111
16166
  pos: 0
16112
16167
  }), {
16113
- filename
16168
+ filename: filename2
16114
16169
  });
16115
16170
  }
16116
16171
  };
16117
16172
  }();
16118
16173
  var { parse } = parser;
16174
+ var filename;
16119
16175
  var initialConfig;
16120
16176
  var config;
16177
+ var sync;
16121
16178
  var state = {};
16122
16179
  var getState = () => state;
16123
16180
  var getConfig = () => config;
16124
16181
  var getInitialConfig = () => initialConfig;
16125
- var setInitialConfig = (c) => initialConfig = c;
16182
+ var getFilename = () => filename;
16183
+ var getSync = () => sync;
16126
16184
  Object.defineProperties(state, {
16127
16185
  currentIndent: {
16128
16186
  get() {
@@ -16167,6 +16225,21 @@ Object.defineProperties(state, {
16167
16225
  }
16168
16226
  }
16169
16227
  });
16228
+ function parseProgram(input, options) {
16229
+ filename = options?.filename;
16230
+ initialConfig = options?.parseOptions;
16231
+ sync = options?.sync;
16232
+ const root = parse(input, options);
16233
+ if (sync) {
16234
+ filename = initialConfig = sync = null;
16235
+ return root;
16236
+ } else {
16237
+ return processProgramAsync(root).then(() => {
16238
+ filename = initialConfig = sync = null;
16239
+ return root;
16240
+ });
16241
+ }
16242
+ }
16170
16243
 
16171
16244
  // source/util.civet
16172
16245
  var util_exports = {};
@@ -16593,15 +16666,15 @@ var uncacheable = /* @__PURE__ */ new Set([
16593
16666
  "RestoreTrailingMemberProperty",
16594
16667
  "RestoreNewlineBinaryOp"
16595
16668
  ]);
16596
- var compile = function(src, options) {
16669
+ function compile(src, options) {
16597
16670
  if (!options) {
16598
16671
  options = {};
16599
16672
  } else {
16600
16673
  options = { ...options };
16601
16674
  }
16602
16675
  options.parseOptions = { ...options.parseOptions };
16603
- const filename = options.filename || "unknown";
16604
- if (filename.endsWith(".coffee") && !/^(#![^\r\n]*(\r\n|\n|\r))?\s*['"]civet/.test(src)) {
16676
+ const filename2 = options.filename || "unknown";
16677
+ if (filename2.endsWith(".coffee") && !/^(#![^\r\n]*(\r\n|\n|\r))?\s*['"]civet/.test(src)) {
16605
16678
  options.parseOptions.coffeeCompat = true;
16606
16679
  }
16607
16680
  const { hits, trace, noCache } = options;
@@ -16613,21 +16686,18 @@ var compile = function(src, options) {
16613
16686
  });
16614
16687
  }
16615
16688
  let ast;
16616
- setInitialConfig(options.parseOptions);
16617
16689
  try {
16618
- ast = parse(src, {
16619
- filename,
16690
+ ast = parseProgram(src, {
16691
+ parseOptions: options.parseOptions,
16692
+ sync: options.sync,
16693
+ filename: filename2,
16620
16694
  events
16621
16695
  });
16622
- if (!(options.ast === "raw")) {
16623
- ast = prune(ast);
16624
- }
16625
16696
  } finally {
16626
- setInitialConfig(void 0);
16627
16697
  if (hits || trace) {
16628
- import("fs").then(function({ writeFileSync }) {
16698
+ import("node:fs").then(function({ writeFileSync }) {
16629
16699
  let ref;
16630
- if ((ref = events?.meta) && "logs" in ref) {
16700
+ if ((ref = events?.meta) && typeof ref === "object" && "logs" in ref) {
16631
16701
  const { logs } = ref;
16632
16702
  if (trace) {
16633
16703
  writeFileSync(trace, logs.join("\n"));
@@ -16656,34 +16726,45 @@ ${counts}`;
16656
16726
  });
16657
16727
  }
16658
16728
  }
16659
- if (options.ast) {
16660
- return ast;
16661
- }
16662
- function checkErrors() {
16663
- if (options.errors?.length) {
16664
- throw new Error(`Parse errors: ${options.errors.map(($) => $.message).join("\n")} `);
16729
+ function rest(ast2) {
16730
+ options = options;
16731
+ if (!(options.ast === "raw")) {
16732
+ ast2 = prune(ast2);
16665
16733
  }
16666
- ;
16667
- return;
16668
- }
16669
- if (options.sourceMap || options.inlineMap) {
16670
- const sm = SourceMap2(src);
16671
- options.updateSourceMap = sm.updateSourceMap;
16672
- const code = generate_default(ast, options);
16673
- checkErrors();
16674
- if (options.inlineMap) {
16675
- return SourceMap2.remap(code, sm, filename, filename + ".tsx");
16676
- } else {
16677
- return {
16678
- code,
16679
- sourceMap: sm
16680
- };
16734
+ if (options.ast) {
16735
+ return ast2;
16736
+ }
16737
+ function checkErrors() {
16738
+ if (options.errors?.length) {
16739
+ throw new Error(`Parse errors: ${options.errors.map(($) => $.message).join("\n")} `);
16740
+ }
16741
+ ;
16742
+ return;
16681
16743
  }
16744
+ if (options.sourceMap || options.inlineMap) {
16745
+ const sm = SourceMap2(src);
16746
+ options.updateSourceMap = sm.updateSourceMap;
16747
+ const code = generate_default(ast2, options);
16748
+ checkErrors();
16749
+ if (options.inlineMap) {
16750
+ return SourceMap2.remap(code, sm, filename2, filename2 + ".tsx");
16751
+ } else {
16752
+ return {
16753
+ code,
16754
+ sourceMap: sm
16755
+ };
16756
+ }
16757
+ }
16758
+ const result = generate_default(ast2, options);
16759
+ checkErrors();
16760
+ return result;
16682
16761
  }
16683
- const result = generate_default(ast, options);
16684
- checkErrors();
16685
- return result;
16686
- };
16762
+ if (ast.then != null) {
16763
+ return ast.then(rest);
16764
+ } else {
16765
+ return rest(ast);
16766
+ }
16767
+ }
16687
16768
  var makeCache = function({ hits, trace } = {}) {
16688
16769
  const meta = {};
16689
16770
  let hitCount;
@@ -16750,7 +16831,7 @@ var isCompileError = function(err) {
16750
16831
  return err instanceof Error && //@ts-ignore
16751
16832
  [err.message, err.name, err.filename, err.line, err.column, err.offset].every(($1) => $1 !== void 0);
16752
16833
  };
16753
- var main_default = { parse, generate: generate_default, util: util_exports, compile, isCompileError };
16834
+ var main_default = { parse, parseProgram, generate: generate_default, util: util_exports, compile, isCompileError };
16754
16835
  export {
16755
16836
  compile,
16756
16837
  main_default as default,
@@ -16758,6 +16839,7 @@ export {
16758
16839
  isCompileError,
16759
16840
  lib_exports as lib,
16760
16841
  parse,
16842
+ parseProgram,
16761
16843
  prune,
16762
16844
  util_exports as util
16763
16845
  };