@danielx/civet 0.7.4 → 0.7.6

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
@@ -28,9 +28,9 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  mod
29
29
  ));
30
30
 
31
- // node_modules/@danielx/hera/dist/machine.js
31
+ // ../Hera/dist/machine.js
32
32
  var require_machine = __commonJS({
33
- "node_modules/@danielx/hera/dist/machine.js"(exports, module) {
33
+ "../Hera/dist/machine.js"(exports, module) {
34
34
  "use strict";
35
35
  var __defProp2 = Object.defineProperty;
36
36
  var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
@@ -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,
@@ -945,6 +946,9 @@ function assignResults(node, collect) {
945
946
  wrapIterationReturningResults(exp, outer, collect);
946
947
  return;
947
948
  case "BlockStatement":
949
+ if (node.expressions.some(isExit)) {
950
+ return;
951
+ }
948
952
  assignResults(exp.expressions[exp.expressions.length - 1], collect);
949
953
  return;
950
954
  case "IfStatement":
@@ -979,6 +983,9 @@ function insertReturn(node, outerNode = node) {
979
983
  switch (node.type) {
980
984
  case "BlockStatement":
981
985
  if (node.expressions.length) {
986
+ if (node.expressions.some(([, exp2]) => isExit(exp2))) {
987
+ return;
988
+ }
982
989
  const last = node.expressions[node.expressions.length - 1];
983
990
  insertReturn(last);
984
991
  } else {
@@ -1153,14 +1160,12 @@ function processParams(f) {
1153
1160
  if (!prefix.length)
1154
1161
  return;
1155
1162
  if (isConstructor) {
1156
- const superCalls = gatherNodes(expressions, (exp) => {
1157
- return exp.type === "CallExpression" && exp.children[0]?.token === "super";
1158
- });
1163
+ const superCalls = gatherNodes(
1164
+ expressions,
1165
+ (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"
1166
+ );
1159
1167
  if (superCalls.length) {
1160
- const { child } = findAncestor(
1161
- superCalls[0],
1162
- (ancestor) => ancestor === block
1163
- );
1168
+ const { child } = findAncestor(superCalls[0], (a1) => a1 === block);
1164
1169
  const index = findChildIndex(expressions, child);
1165
1170
  if (index < 0) {
1166
1171
  throw new Error("Could not find super call within top-level expressions");
@@ -1226,12 +1231,6 @@ function skipImplicitArguments(args) {
1226
1231
  // source/parser/block.civet
1227
1232
  function blockWithPrefix(prefixStatements, block) {
1228
1233
  if (prefixStatements && prefixStatements.length) {
1229
- const indent = getIndent(block.expressions[0]);
1230
- if (indent) {
1231
- prefixStatements = prefixStatements.map((statement) => {
1232
- return [indent, ...statement.slice(1)];
1233
- });
1234
- }
1235
1234
  const expressions = [...prefixStatements, ...block.expressions];
1236
1235
  updateParentPointers(prefixStatements, block);
1237
1236
  block = {
@@ -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)) {
@@ -2587,6 +2588,11 @@ function getHelperRef(base) {
2587
2588
  declareHelper[base](ref);
2588
2589
  return state.helperRefs[base] = ref;
2589
2590
  }
2591
+ function extractPreludeFor(node) {
2592
+ let helpers = new Set(Object.values(state.helperRefs));
2593
+ helpers = new Set(gatherRecursive(node, helpers.has.bind(helpers)));
2594
+ return state.prelude.filter((s) => gatherRecursive(s, helpers.has.bind(helpers)).length);
2595
+ }
2590
2596
 
2591
2597
  // source/parser/pattern-matching.civet
2592
2598
  function processPatternTest(lhs, patterns) {
@@ -2966,7 +2972,7 @@ function aliasBinding(p, ref) {
2966
2972
  }
2967
2973
 
2968
2974
  // source/parser/declaration.civet
2969
- function len(arr, length) {
2975
+ function len2(arr, length) {
2970
2976
  return arr.length === length;
2971
2977
  }
2972
2978
  function processAssignmentDeclaration(decl, pattern, suffix, ws, assign, e) {
@@ -3164,7 +3170,7 @@ function processDeclarationConditionStatement(s) {
3164
3170
  return;
3165
3171
  }
3166
3172
  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]) {
3173
+ 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
3174
  const { type: type1, children: [, { type: type2, expression: expression2 }] } = expression;
3169
3175
  const type = [type1, type2];
3170
3176
  expression = expression2;
@@ -3173,7 +3179,7 @@ function processDeclarationConditionStatement(s) {
3173
3179
  const { ref, pattern } = expression;
3174
3180
  if (pattern) {
3175
3181
  const conditions = getPatternConditions(pattern, ref).filter((c) => {
3176
- if (Array.isArray(c) && len(c, 2) && c[0] === ref && c[1] === " != null") {
3182
+ if (Array.isArray(c) && len2(c, 2) && c[0] === ref && c[1] === " != null") {
3177
3183
  const [,] = c;
3178
3184
  return false;
3179
3185
  } else {
@@ -4005,6 +4011,11 @@ function createVarDecs(block, scopes, pushVar) {
4005
4011
  scopes.pop();
4006
4012
  }
4007
4013
 
4014
+ // source/parser/comptime.civet
4015
+ import { resolve, dirname } from "node:path";
4016
+ import { createRequire } from "node:module";
4017
+ import vm from "node:vm";
4018
+
4008
4019
  // source/generate.civet
4009
4020
  function stringify(node) {
4010
4021
  try {
@@ -4090,36 +4101,122 @@ function processComptime(statements) {
4090
4101
  if (!getInitialConfig()?.comptime) {
4091
4102
  return;
4092
4103
  }
4093
- gatherRecursive(
4104
+ const promises = runComptime(statements);
4105
+ if (getSync()) {
4106
+ return;
4107
+ } else {
4108
+ return (async () => {
4109
+ {
4110
+ await Promise.all(promises);
4111
+ }
4112
+ })();
4113
+ }
4114
+ }
4115
+ function runComptime(statements) {
4116
+ const sync2 = getSync();
4117
+ return gatherRecursive(
4094
4118
  statements,
4095
4119
  (node) => {
4096
4120
  return node.type === "ComptimeStatement" || node.type === "ComptimeExpression";
4097
4121
  }
4098
- ).forEach((exp) => {
4099
- const content = exp.type === "ComptimeStatement" ? exp.block : exp.expression;
4122
+ ).map((exp) => {
4123
+ let content = exp.type === "ComptimeStatement" ? exp.block : exp.expression;
4124
+ content = [
4125
+ ...extractPreludeFor(content),
4126
+ content
4127
+ ];
4100
4128
  const options = { js: true };
4101
- const js = generate_default(prune(content), options);
4129
+ let js = generate_default(prune(content), options);
4130
+ js = `"use strict";${js}`;
4102
4131
  if (options.errors != null) {
4103
4132
  return;
4104
4133
  }
4105
- const output = eval?.(`"use strict";${js}`);
4134
+ let output, context;
4135
+ try {
4136
+ context = vm.createContext?.() ?? globalThis;
4137
+ const filename2 = context.__filename = resolve(getFilename() ?? "");
4138
+ context.__dirname = dirname(filename2);
4139
+ context.require = createRequire(filename2);
4140
+ if (vm.runInContext != null) {
4141
+ for (const global of [
4142
+ "RegExp",
4143
+ "Date",
4144
+ "Set",
4145
+ "Map",
4146
+ "URL",
4147
+ "Int8Array",
4148
+ "Uint8Array",
4149
+ "Int16Array",
4150
+ "Uint16Array",
4151
+ "Int32Array",
4152
+ "Uint32Array",
4153
+ "Float32Array",
4154
+ "Float64Array",
4155
+ "Uint8ClampedArray",
4156
+ "BigInt64Array",
4157
+ "BigUint64Array",
4158
+ "Buffer"
4159
+ ]) {
4160
+ context[global] = globalThis[global];
4161
+ }
4162
+ context.Object2 = Object;
4163
+ output = vm.runInContext(js, context, {
4164
+ filename: filename2,
4165
+ importModuleDynamically: vm.constants?.USE_MAIN_CONTEXT_DEFAULT_LOADER
4166
+ });
4167
+ } else {
4168
+ output = eval?.(js);
4169
+ }
4170
+ } catch (e) {
4171
+ exp.children = [
4172
+ {
4173
+ type: "Error",
4174
+ message: `comptime block failed to execute: ${e}
4175
+ ${js}`
4176
+ }
4177
+ ];
4178
+ return;
4179
+ }
4180
+ let promise;
4106
4181
  if (exp.type === "ComptimeExpression") {
4107
- let string;
4108
- try {
4109
- string = serialize(output);
4110
- } catch (e) {
4111
- exp.children = [
4182
+ const finish = () => {
4183
+ let string;
4184
+ try {
4185
+ if (vm.runInContext != null) {
4186
+ context.output = output;
4187
+ string = vm.runInContext(
4188
+ `${serialize(serialize)}serialize(output)`,
4189
+ context
4190
+ );
4191
+ } else {
4192
+ string = serialize(output);
4193
+ }
4194
+ } catch (e) {
4195
+ exp.children = [
4196
+ {
4197
+ type: "Error",
4198
+ message: `comptime result ${output} not serializable: ${e}`
4199
+ }
4200
+ ];
4201
+ return;
4202
+ }
4203
+ return exp.children = [string];
4204
+ };
4205
+ if (sync2) {
4206
+ finish();
4207
+ } else {
4208
+ promise = (async () => {
4112
4209
  {
4113
- type: "Error",
4114
- message: `comptime result ${output} not JSON serializable: ${e}`
4210
+ output = await output;
4211
+ return finish();
4115
4212
  }
4116
- ];
4117
- return;
4213
+ })();
4118
4214
  }
4119
- return exp.children = [string];
4120
4215
  } else {
4121
- return exp.children = [];
4216
+ promise = output;
4217
+ exp.children = [];
4122
4218
  }
4219
+ return promise;
4123
4220
  });
4124
4221
  }
4125
4222
  function serialize(value) {
@@ -4220,14 +4317,11 @@ function serialize(value) {
4220
4317
  })().join(",") + "])";
4221
4318
  break;
4222
4319
  }
4223
- case Array.prototype: {
4224
- ref1 = `[${val.map(recurse).join(",")}]`;
4225
- break;
4226
- }
4227
- case Object.prototype: {
4320
+ case Object.prototype:
4321
+ case globalThis.Object2?.prototype: {
4228
4322
  let objStr = "{";
4229
4323
  let descStr = "";
4230
- for (let ref2 = Object.getOwnPropertyNames(val).concat(Object.getOwnPropertySymbols(val)), i = 0, len2 = ref2.length; i < len2; i++) {
4324
+ for (let ref2 = Object.getOwnPropertyNames(val).concat(Object.getOwnPropertySymbols(val)), i = 0, len3 = ref2.length; i < len3; i++) {
4231
4325
  const prop = ref2[i];
4232
4326
  const desc = Object.getOwnPropertyDescriptor(val, prop);
4233
4327
  if (desc.enumerable && desc.configurable && desc.writable) {
@@ -4289,7 +4383,11 @@ function serialize(value) {
4289
4383
  break;
4290
4384
  }
4291
4385
  default: {
4292
- throw new TypeError(`cannot serialize object with prototype ${Object.getPrototypeOf(val)}`);
4386
+ if (Array.isArray(val)) {
4387
+ ref1 = `[${val.map(recurse).join(",")}]`;
4388
+ } else {
4389
+ throw new TypeError(`cannot serialize object with prototype ${val.constructor?.name ?? Object.getPrototypeOf(val)}`);
4390
+ }
4293
4391
  }
4294
4392
  }
4295
4393
  ;
@@ -4316,7 +4414,7 @@ function getIndentLevel(str, tab) {
4316
4414
  }
4317
4415
  function reduceIndentLevel(str, dedent, tab) {
4318
4416
  if (tab != null && tab != 1) {
4319
- for (let i1 = 0, len2 = str.length; i1 < len2; i1++) {
4417
+ for (let i1 = 0, len3 = str.length; i1 < len3; i1++) {
4320
4418
  const i = i1;
4321
4419
  const char = str[i1];
4322
4420
  if (!dedent) {
@@ -4398,7 +4496,7 @@ function dedentBlockSubstitutions($0, tab) {
4398
4496
  ;
4399
4497
  const dedent = ref1;
4400
4498
  let results = [s];
4401
- for (let i3 = 0, len2 = strWithSubstitutions.length; i3 < len2; i3++) {
4499
+ for (let i3 = 0, len22 = strWithSubstitutions.length; i3 < len22; i3++) {
4402
4500
  const i = i3;
4403
4501
  let part = strWithSubstitutions[i3];
4404
4502
  if (part.token != null) {
@@ -4528,9 +4626,8 @@ function isExpression(node) {
4528
4626
  return true;
4529
4627
  }
4530
4628
  function expressionizeBlock(blockOrExpression) {
4531
- let ref1;
4532
- if ((ref1 = blockOrExpression) && "expressions" in ref1) {
4533
- const { expressions } = ref1;
4629
+ if (blockOrExpression && typeof blockOrExpression === "object" && "expressions" in blockOrExpression) {
4630
+ const { expressions } = blockOrExpression;
4534
4631
  const l = expressions.length;
4535
4632
  const results = [];
4536
4633
  let i1 = 0;
@@ -4629,7 +4726,7 @@ function handleThisPrivateShorthands(value) {
4629
4726
  }
4630
4727
  function processCallMemberExpression(node) {
4631
4728
  const { children } = node;
4632
- if (children[0]?.parenthesizedOp?.token && children[1]?.type === "Call") {
4729
+ 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
4730
  const op = children[0].parenthesizedOp;
4634
4731
  let call = children[1];
4635
4732
  const args = [...call.args];
@@ -4676,11 +4773,23 @@ function processCallMemberExpression(node) {
4676
4773
  }
4677
4774
  prefix = prefix.concat(glob.dot);
4678
4775
  for (const part of glob.object.properties) {
4776
+ if (part.type === "Error") {
4777
+ parts.push(part);
4778
+ continue;
4779
+ }
4679
4780
  if (part.type === "MethodDefinition") {
4680
- throw new Error("Glob pattern cannot have method definition");
4781
+ parts.push({
4782
+ type: "Error",
4783
+ message: "Glob pattern cannot have method definition"
4784
+ });
4785
+ continue;
4681
4786
  }
4682
4787
  if (part.value && !["CallExpression", "MemberExpression", "Identifier"].includes(part.value.type)) {
4683
- throw new Error(`Glob pattern must have call or member expression value, found ${JSON.stringify(part.value)}`);
4788
+ parts.push({
4789
+ type: "Error",
4790
+ message: `Glob pattern must have call or member expression value, found ${JSON.stringify(part.value)}`
4791
+ });
4792
+ continue;
4684
4793
  }
4685
4794
  let suppressPrefix = false;
4686
4795
  let name = part.name;
@@ -4723,14 +4832,14 @@ function processCallMemberExpression(node) {
4723
4832
  });
4724
4833
  }
4725
4834
  }
4726
- let ref2;
4835
+ let ref1;
4727
4836
  let object = {
4728
4837
  type: "ObjectExpression",
4729
4838
  children: [
4730
4839
  glob.object.children[0],
4731
4840
  // {
4732
4841
  ...parts,
4733
- (ref2 = glob.object.children)[ref2.length - 1]
4842
+ (ref1 = glob.object.children)[ref1.length - 1]
4734
4843
  // whitespace and }
4735
4844
  ],
4736
4845
  properties: parts,
@@ -4782,7 +4891,7 @@ function replaceNode(node, newNode, parent) {
4782
4891
  throw new Error("replaceNode failed: node has no parent");
4783
4892
  }
4784
4893
  function recurse(children) {
4785
- for (let i3 = 0, len2 = children.length; i3 < len2; i3++) {
4894
+ for (let i3 = 0, len22 = children.length; i3 < len22; i3++) {
4786
4895
  const i = i3;
4787
4896
  const child = children[i3];
4788
4897
  if (child === node) {
@@ -4829,6 +4938,10 @@ function lastAccessInCallExpression(exp) {
4829
4938
  }
4830
4939
  let children, i;
4831
4940
  do {
4941
+ if (!(exp.children != null)) {
4942
+ return;
4943
+ }
4944
+ ;
4832
4945
  ({ children } = exp);
4833
4946
  i = children.length - 1;
4834
4947
  while (i >= 0 && (children[i].type === "Call" || children[i].type === "NonNullAssertion" || children[i].type === "Optional"))
@@ -4872,8 +4985,8 @@ function convertNamedImportsToObject(node, pattern) {
4872
4985
  return { type: "Error", message: "cannot use `type` in dynamic import" };
4873
4986
  } else {
4874
4987
  const { source, binding } = specifier;
4875
- let ref3;
4876
- const delim = (ref3 = specifier.children)[ref3.length - 1];
4988
+ let ref2;
4989
+ const delim = (ref2 = specifier.children)[ref2.length - 1];
4877
4990
  return {
4878
4991
  type: pattern ? "BindingProperty" : "Property",
4879
4992
  name: source,
@@ -4883,7 +4996,7 @@ function convertNamedImportsToObject(node, pattern) {
4883
4996
  };
4884
4997
  }
4885
4998
  });
4886
- let ref4;
4999
+ let ref3;
4887
5000
  return {
4888
5001
  type: pattern ? "ObjectBindingPattern" : "ObjectExpression",
4889
5002
  names: node.names,
@@ -4892,7 +5005,7 @@ function convertNamedImportsToObject(node, pattern) {
4892
5005
  node.children[0],
4893
5006
  // {
4894
5007
  properties,
4895
- (ref4 = node.children)[ref4.length - 1]
5008
+ (ref3 = node.children)[ref3.length - 1]
4896
5009
  // }
4897
5010
  ]
4898
5011
  };
@@ -5069,17 +5182,17 @@ function processAssignments(statements) {
5069
5182
  statements,
5070
5183
  (n) => n.type === "AssignmentExpression" && n.names === null,
5071
5184
  (exp) => {
5072
- let { lhs: $1, expression: $2 } = exp, tail = [], i = 0, len2 = $1.length;
5185
+ let { lhs: $1, expression: $2 } = exp, tail = [], i = 0, len3 = $1.length;
5073
5186
  let block;
5074
- let ref5;
5075
- if (exp.parent?.type === "BlockStatement" && !(ref5 = $1[$1.length - 1])?.[ref5.length - 1]?.special) {
5187
+ let ref4;
5188
+ if (exp.parent?.type === "BlockStatement" && !(ref4 = $1[$1.length - 1])?.[ref4.length - 1]?.special) {
5076
5189
  block = makeBlockFragment();
5077
- let ref6;
5078
- if (ref6 = prependStatementExpressionBlock(
5190
+ let ref5;
5191
+ if (ref5 = prependStatementExpressionBlock(
5079
5192
  { type: "Initializer", expression: $2, children: [void 0, void 0, $2] },
5080
5193
  block
5081
5194
  )) {
5082
- const ref = ref6;
5195
+ const ref = ref5;
5083
5196
  exp.children = exp.children.map(function(c) {
5084
5197
  if (c === $2)
5085
5198
  return ref;
@@ -5109,7 +5222,7 @@ function processAssignments(statements) {
5109
5222
  }
5110
5223
  }
5111
5224
  let wrapped = false;
5112
- while (i < len2) {
5225
+ while (i < len3) {
5113
5226
  const lastAssignment = $1[i++];
5114
5227
  const [, lhs, , op] = lastAssignment;
5115
5228
  if (op.token !== "=")
@@ -5122,7 +5235,7 @@ function processAssignments(statements) {
5122
5235
  }
5123
5236
  }
5124
5237
  }
5125
- i = len2 - 1;
5238
+ i = len3 - 1;
5126
5239
  while (i >= 0) {
5127
5240
  const lastAssignment = $1[i];
5128
5241
  if (lastAssignment[3].token === "=") {
@@ -5158,14 +5271,14 @@ function processAssignments(statements) {
5158
5271
  }
5159
5272
  i--;
5160
5273
  }
5161
- i = len2 - 1;
5274
+ i = len3 - 1;
5162
5275
  const optionalChainRef = makeRef();
5163
5276
  while (i >= 0) {
5164
5277
  const assignment = $1[i];
5165
5278
  const [ws1, lhs, ws2, op] = assignment;
5166
5279
  if (lhs.type === "MemberExpression" || lhs.type === "CallExpression") {
5167
5280
  const newMemberExp = unchainOptionalMemberExpression(lhs, optionalChainRef, (children) => {
5168
- const assigns = $1.splice(i + 1, len2 - 1 - i);
5281
+ const assigns = $1.splice(i + 1, len3 - 1 - i);
5169
5282
  $1.pop();
5170
5283
  return [ws1, ...children, ws2, op, ...assigns, $2];
5171
5284
  });
@@ -5247,9 +5360,9 @@ function unchainOptionalMemberExpression(exp, ref, innerExp) {
5247
5360
  }
5248
5361
  j++;
5249
5362
  }
5250
- let ref7;
5251
- if (ref7 = conditions.length) {
5252
- const l = ref7;
5363
+ let ref6;
5364
+ if (ref6 = conditions.length) {
5365
+ const l = ref6;
5253
5366
  const cs = flatJoin(conditions, " && ");
5254
5367
  return {
5255
5368
  ...exp,
@@ -5286,8 +5399,8 @@ function processTypes(node) {
5286
5399
  return gatherRecursiveAll(node, (n) => n.type === "UnaryType").forEach((unary) => {
5287
5400
  let last;
5288
5401
  let count = 0;
5289
- let ref8;
5290
- while (unary.suffix.length && (ref8 = unary.suffix)[ref8.length - 1]?.token === "?") {
5402
+ let ref7;
5403
+ while (unary.suffix.length && (ref7 = unary.suffix)[ref7.length - 1]?.token === "?") {
5291
5404
  last = unary.suffix.pop();
5292
5405
  count++;
5293
5406
  }
@@ -5321,11 +5434,11 @@ function processStatementExpressions(statements) {
5321
5434
  gatherRecursiveAll(statements, ($) => $.type === "StatementExpression").forEach((_exp) => {
5322
5435
  const exp = _exp;
5323
5436
  const { statement } = exp;
5324
- let ref9;
5437
+ let ref8;
5325
5438
  switch (statement.type) {
5326
5439
  case "IfStatement": {
5327
- if (ref9 = expressionizeIfStatement(statement)) {
5328
- const expression = ref9;
5440
+ if (ref8 = expressionizeIfStatement(statement)) {
5441
+ const expression = ref8;
5329
5442
  return replaceNode(statement, expression, exp);
5330
5443
  } else {
5331
5444
  return replaceNode(statement, wrapIIFE([["", statement]]), exp);
@@ -5333,7 +5446,8 @@ function processStatementExpressions(statements) {
5333
5446
  }
5334
5447
  case "IterationExpression": {
5335
5448
  if (statement.subtype === "ComptimeStatement") {
5336
- const expression = wrapIIFE(statement.statement.block.expressions);
5449
+ const { expressions } = statement.statement.block;
5450
+ const expression = wrapIIFE(expressions, hasAwait(expressions));
5337
5451
  return replaceNode(statement, makeNode({
5338
5452
  type: "ComptimeExpression",
5339
5453
  expression,
@@ -5411,7 +5525,13 @@ function processProgram(root) {
5411
5525
  processBlocks(statements);
5412
5526
  populateRefs(statements);
5413
5527
  adjustAtBindings(statements);
5414
- processComptime(statements);
5528
+ if (getSync()) {
5529
+ processComptime(statements);
5530
+ }
5531
+ }
5532
+ async function processProgramAsync(root) {
5533
+ const { expressions: statements } = root;
5534
+ await processComptime(statements);
5415
5535
  }
5416
5536
  function populateRefs(statements) {
5417
5537
  const refNodes = gatherRecursive(statements, ({ type }) => type === "Ref");
@@ -5535,8 +5655,8 @@ function processPlaceholders(statements) {
5535
5655
  for (let i4 = 0, len3 = placeholders.length; i4 < len3; i4++) {
5536
5656
  const placeholder = placeholders[i4];
5537
5657
  typeSuffix ??= placeholder.typeSuffix;
5538
- let ref10;
5539
- replaceNode((ref10 = placeholder.children)[ref10.length - 1], ref);
5658
+ let ref9;
5659
+ replaceNode((ref9 = placeholder.children)[ref9.length - 1], ref);
5540
5660
  }
5541
5661
  const { parent } = ancestor;
5542
5662
  const body = maybeUnwrap(ancestor);
@@ -5567,9 +5687,9 @@ function processPlaceholders(statements) {
5567
5687
  fnExp = makeLeftHandSideExpression(fnExp);
5568
5688
  }
5569
5689
  replaceNode(ancestor, fnExp, parent);
5570
- let ref11;
5571
- if (ref11 = getTrimmingSpace(body)) {
5572
- const ws = ref11;
5690
+ let ref10;
5691
+ if (ref10 = getTrimmingSpace(body)) {
5692
+ const ws = ref10;
5573
5693
  inplaceInsertTrimmingSpace(body, "");
5574
5694
  inplacePrepend(ws, fnExp);
5575
5695
  }
@@ -5614,8 +5734,8 @@ function reorderBindingRestProperty(props) {
5614
5734
  }
5615
5735
  ];
5616
5736
  }
5617
- let ref12;
5618
- if (Array.isArray(rest.delim) && (ref12 = rest.delim)[ref12.length - 1]?.token === ",") {
5737
+ let ref11;
5738
+ if (Array.isArray(rest.delim) && (ref11 = rest.delim)[ref11.length - 1]?.token === ",") {
5619
5739
  rest.delim = rest.delim.slice(0, -1);
5620
5740
  rest.children = [...rest.children.slice(0, -1), rest.delim];
5621
5741
  }
@@ -6813,15 +6933,20 @@ var $R92 = (0, import_lib2.$R)(new RegExp("(?=[ \\t\\r\\n\\/#]|$)", "suy"));
6813
6933
  var $R93 = (0, import_lib2.$R)(new RegExp("\\r\\n|\\n|\\r|$", "suy"));
6814
6934
  var $R94 = (0, import_lib2.$R)(new RegExp("[^]*", "suy"));
6815
6935
  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) {
6936
+ var reset = $1;
6937
+ var init = $2;
6938
+ var ws1 = $3;
6816
6939
  var statements = $4;
6817
- processProgram({
6940
+ var ws2 = $5;
6941
+ const program = {
6818
6942
  type: "BlockStatement",
6819
6943
  expressions: statements,
6820
- children: [statements],
6944
+ children: [reset, init, ws1, statements, ws2],
6821
6945
  bare: true,
6822
6946
  root: true
6823
- }, ReservedWord);
6824
- return $0;
6947
+ };
6948
+ processProgram(program);
6949
+ return program;
6825
6950
  });
6826
6951
  function Program(ctx, state2) {
6827
6952
  return (0, import_lib2.$EVENT)(ctx, state2, "Program", Program$0);
@@ -8419,11 +8544,11 @@ var PropertyAccess$1 = (0, import_lib2.$TS)((0, import_lib2.$S)(AccessStart, (0,
8419
8544
  var dot = $1;
8420
8545
  var neg = $2;
8421
8546
  var num = $3;
8422
- const len2 = {
8547
+ const len3 = {
8423
8548
  children: []
8424
8549
  }, children = [
8425
8550
  adjustIndexAccess(dot),
8426
- len2,
8551
+ len3,
8427
8552
  neg,
8428
8553
  num,
8429
8554
  "]"
@@ -8431,7 +8556,7 @@ var PropertyAccess$1 = (0, import_lib2.$TS)((0, import_lib2.$S)(AccessStart, (0,
8431
8556
  return {
8432
8557
  type: "NegativeIndex",
8433
8558
  children,
8434
- len: len2
8559
+ len: len3
8435
8560
  };
8436
8561
  });
8437
8562
  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) {
@@ -10921,6 +11046,7 @@ var _BinaryOp$1 = (0, import_lib2.$TV)(Identifier, function($skip, $loc, $0, $1)
10921
11046
  if (!state.operators.has(id.name))
10922
11047
  return $skip;
10923
11048
  return {
11049
+ token: id.name,
10924
11050
  call: id,
10925
11051
  special: true,
10926
11052
  ...state.operators.get(id.name)
@@ -10931,6 +11057,7 @@ var _BinaryOp$2 = (0, import_lib2.$TS)((0, import_lib2.$S)(OmittedNegation, __,
10931
11057
  if (!state.operators.has(id.name))
10932
11058
  return $skip;
10933
11059
  return {
11060
+ token: id.name,
10934
11061
  call: id,
10935
11062
  special: true,
10936
11063
  negated: true,
@@ -16103,26 +16230,29 @@ var parser = function() {
16103
16230
  const parser2 = options.startRule != null ? grammar[options.startRule] : Object.values(grammar)[0];
16104
16231
  if (!parser2)
16105
16232
  throw new Error(`Could not find rule with name '${options.startRule}'`);
16106
- const filename = options.filename || "<anonymous>";
16233
+ const filename2 = options.filename || "<anonymous>";
16107
16234
  reset();
16108
16235
  Object.assign(ctx, { ...options.events, tokenize: options.tokenize });
16109
16236
  return validate(input, parser2(ctx, {
16110
16237
  input,
16111
16238
  pos: 0
16112
16239
  }), {
16113
- filename
16240
+ filename: filename2
16114
16241
  });
16115
16242
  }
16116
16243
  };
16117
16244
  }();
16118
16245
  var { parse } = parser;
16246
+ var filename;
16119
16247
  var initialConfig;
16120
16248
  var config;
16249
+ var sync;
16121
16250
  var state = {};
16122
16251
  var getState = () => state;
16123
16252
  var getConfig = () => config;
16124
16253
  var getInitialConfig = () => initialConfig;
16125
- var setInitialConfig = (c) => initialConfig = c;
16254
+ var getFilename = () => filename;
16255
+ var getSync = () => sync;
16126
16256
  Object.defineProperties(state, {
16127
16257
  currentIndent: {
16128
16258
  get() {
@@ -16167,6 +16297,21 @@ Object.defineProperties(state, {
16167
16297
  }
16168
16298
  }
16169
16299
  });
16300
+ function parseProgram(input, options) {
16301
+ filename = options?.filename;
16302
+ initialConfig = options?.parseOptions;
16303
+ sync = options?.sync;
16304
+ const root = parse(input, options);
16305
+ if (sync) {
16306
+ filename = initialConfig = sync = null;
16307
+ return root;
16308
+ } else {
16309
+ return processProgramAsync(root).then(() => {
16310
+ filename = initialConfig = sync = null;
16311
+ return root;
16312
+ });
16313
+ }
16314
+ }
16170
16315
 
16171
16316
  // source/util.civet
16172
16317
  var util_exports = {};
@@ -16554,6 +16699,21 @@ var StateCache = class {
16554
16699
 
16555
16700
  // source/main.civet
16556
16701
  var { SourceMap: SourceMap2 } = util_exports;
16702
+ var ParseErrors = class extends Error {
16703
+ name = "ParseErrors";
16704
+ filename;
16705
+ line;
16706
+ column;
16707
+ offset;
16708
+ constructor(message, filename1, line1, column1, offset1) {
16709
+ super(message);
16710
+ this.filename = filename1;
16711
+ this.line = line1;
16712
+ this.column = column1;
16713
+ this.offset = offset1;
16714
+ this.message = message;
16715
+ }
16716
+ };
16557
16717
  var uncacheable = /* @__PURE__ */ new Set([
16558
16718
  // Meta
16559
16719
  "DebugHere",
@@ -16593,15 +16753,15 @@ var uncacheable = /* @__PURE__ */ new Set([
16593
16753
  "RestoreTrailingMemberProperty",
16594
16754
  "RestoreNewlineBinaryOp"
16595
16755
  ]);
16596
- var compile = function(src, options) {
16756
+ function compile(src, options) {
16597
16757
  if (!options) {
16598
16758
  options = {};
16599
16759
  } else {
16600
16760
  options = { ...options };
16601
16761
  }
16602
16762
  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)) {
16763
+ const filename2 = options.filename || "unknown";
16764
+ if (filename2.endsWith(".coffee") && !/^(#![^\r\n]*(\r\n|\n|\r))?\s*['"]civet/.test(src)) {
16605
16765
  options.parseOptions.coffeeCompat = true;
16606
16766
  }
16607
16767
  const { hits, trace, noCache } = options;
@@ -16613,21 +16773,18 @@ var compile = function(src, options) {
16613
16773
  });
16614
16774
  }
16615
16775
  let ast;
16616
- setInitialConfig(options.parseOptions);
16617
16776
  try {
16618
- ast = parse(src, {
16619
- filename,
16777
+ ast = parseProgram(src, {
16778
+ parseOptions: options.parseOptions,
16779
+ sync: options.sync,
16780
+ filename: filename2,
16620
16781
  events
16621
16782
  });
16622
- if (!(options.ast === "raw")) {
16623
- ast = prune(ast);
16624
- }
16625
16783
  } finally {
16626
- setInitialConfig(void 0);
16627
16784
  if (hits || trace) {
16628
- import("fs").then(function({ writeFileSync }) {
16785
+ import("node:fs").then(function({ writeFileSync }) {
16629
16786
  let ref;
16630
- if ((ref = events?.meta) && "logs" in ref) {
16787
+ if ((ref = events?.meta) && typeof ref === "object" && "logs" in ref) {
16631
16788
  const { logs } = ref;
16632
16789
  if (trace) {
16633
16790
  writeFileSync(trace, logs.join("\n"));
@@ -16656,34 +16813,45 @@ ${counts}`;
16656
16813
  });
16657
16814
  }
16658
16815
  }
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")} `);
16816
+ function rest(ast2) {
16817
+ options = options;
16818
+ if (!(options.ast === "raw")) {
16819
+ ast2 = prune(ast2);
16665
16820
  }
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
- };
16821
+ if (options.ast) {
16822
+ return ast2;
16823
+ }
16824
+ function checkErrors() {
16825
+ if (options.errors?.length) {
16826
+ throw new ParseErrors(options.errors.map(($) => $.message).join("\n"));
16827
+ }
16828
+ ;
16829
+ return;
16830
+ }
16831
+ if (options.sourceMap || options.inlineMap) {
16832
+ const sm = SourceMap2(src);
16833
+ options.updateSourceMap = sm.updateSourceMap;
16834
+ const code = generate_default(ast2, options);
16835
+ checkErrors();
16836
+ if (options.inlineMap) {
16837
+ return SourceMap2.remap(code, sm, filename2, filename2 + ".tsx");
16838
+ } else {
16839
+ return {
16840
+ code,
16841
+ sourceMap: sm
16842
+ };
16843
+ }
16681
16844
  }
16845
+ const result = generate_default(ast2, options);
16846
+ checkErrors();
16847
+ return result;
16682
16848
  }
16683
- const result = generate_default(ast, options);
16684
- checkErrors();
16685
- return result;
16686
- };
16849
+ if (ast.then != null) {
16850
+ return ast.then(rest);
16851
+ } else {
16852
+ return rest(ast);
16853
+ }
16854
+ }
16687
16855
  var makeCache = function({ hits, trace } = {}) {
16688
16856
  const meta = {};
16689
16857
  let hitCount;
@@ -16750,14 +16918,16 @@ var isCompileError = function(err) {
16750
16918
  return err instanceof Error && //@ts-ignore
16751
16919
  [err.message, err.name, err.filename, err.line, err.column, err.offset].every(($1) => $1 !== void 0);
16752
16920
  };
16753
- var main_default = { parse, generate: generate_default, util: util_exports, compile, isCompileError };
16921
+ var main_default = { parse, parseProgram, generate: generate_default, util: util_exports, compile, isCompileError };
16754
16922
  export {
16923
+ ParseErrors,
16755
16924
  compile,
16756
16925
  main_default as default,
16757
16926
  generate_default as generate,
16758
16927
  isCompileError,
16759
16928
  lib_exports as lib,
16760
16929
  parse,
16930
+ parseProgram,
16761
16931
  prune,
16762
16932
  util_exports as util
16763
16933
  };