@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.js CHANGED
@@ -30,9 +30,9 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
30
30
  ));
31
31
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
32
 
33
- // node_modules/@danielx/hera/dist/machine.js
33
+ // ../Hera/dist/machine.js
34
34
  var require_machine = __commonJS({
35
- "node_modules/@danielx/hera/dist/machine.js"(exports2, module2) {
35
+ "../Hera/dist/machine.js"(exports2, module2) {
36
36
  "use strict";
37
37
  var __defProp2 = Object.defineProperty;
38
38
  var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
@@ -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;
@@ -481,12 +481,14 @@ ${input.slice(result.pos)}
481
481
  // source/main.civet
482
482
  var main_exports = {};
483
483
  __export(main_exports, {
484
+ ParseErrors: () => ParseErrors,
484
485
  compile: () => compile,
485
486
  default: () => main_default,
486
487
  generate: () => generate_default,
487
488
  isCompileError: () => isCompileError,
488
489
  lib: () => lib_exports,
489
490
  parse: () => parse,
491
+ parseProgram: () => parseProgram,
490
492
  prune: () => prune,
491
493
  util: () => util_exports
492
494
  });
@@ -547,6 +549,7 @@ __export(lib_exports, {
547
549
  processCoffeeInterpolation: () => processCoffeeInterpolation,
548
550
  processForInOf: () => processForInOf,
549
551
  processProgram: () => processProgram,
552
+ processProgramAsync: () => processProgramAsync,
550
553
  processUnaryExpression: () => processUnaryExpression,
551
554
  quoteString: () => quoteString,
552
555
  reorderBindingRestProperty: () => reorderBindingRestProperty,
@@ -961,6 +964,9 @@ function assignResults(node, collect) {
961
964
  wrapIterationReturningResults(exp, outer, collect);
962
965
  return;
963
966
  case "BlockStatement":
967
+ if (node.expressions.some(isExit)) {
968
+ return;
969
+ }
964
970
  assignResults(exp.expressions[exp.expressions.length - 1], collect);
965
971
  return;
966
972
  case "IfStatement":
@@ -995,6 +1001,9 @@ function insertReturn(node, outerNode = node) {
995
1001
  switch (node.type) {
996
1002
  case "BlockStatement":
997
1003
  if (node.expressions.length) {
1004
+ if (node.expressions.some(([, exp2]) => isExit(exp2))) {
1005
+ return;
1006
+ }
998
1007
  const last = node.expressions[node.expressions.length - 1];
999
1008
  insertReturn(last);
1000
1009
  } else {
@@ -1169,14 +1178,12 @@ function processParams(f) {
1169
1178
  if (!prefix.length)
1170
1179
  return;
1171
1180
  if (isConstructor) {
1172
- const superCalls = gatherNodes(expressions, (exp) => {
1173
- return exp.type === "CallExpression" && exp.children[0]?.token === "super";
1174
- });
1181
+ const superCalls = gatherNodes(
1182
+ expressions,
1183
+ (a) => typeof a === "object" && a != null && "type" in a && a.type === "CallExpression" && "children" in a && Array.isArray(a.children) && a.children.length >= 1 && typeof a.children[0] === "object" && a.children[0] != null && "token" in a.children[0] && a.children[0].token === "super"
1184
+ );
1175
1185
  if (superCalls.length) {
1176
- const { child } = findAncestor(
1177
- superCalls[0],
1178
- (ancestor) => ancestor === block
1179
- );
1186
+ const { child } = findAncestor(superCalls[0], (a1) => a1 === block);
1180
1187
  const index = findChildIndex(expressions, child);
1181
1188
  if (index < 0) {
1182
1189
  throw new Error("Could not find super call within top-level expressions");
@@ -1242,12 +1249,6 @@ function skipImplicitArguments(args) {
1242
1249
  // source/parser/block.civet
1243
1250
  function blockWithPrefix(prefixStatements, block) {
1244
1251
  if (prefixStatements && prefixStatements.length) {
1245
- const indent = getIndent(block.expressions[0]);
1246
- if (indent) {
1247
- prefixStatements = prefixStatements.map((statement) => {
1248
- return [indent, ...statement.slice(1)];
1249
- });
1250
- }
1251
1252
  const expressions = [...prefixStatements, ...block.expressions];
1252
1253
  updateParentPointers(prefixStatements, block);
1253
1254
  block = {
@@ -1261,7 +1262,7 @@ function blockWithPrefix(prefixStatements, block) {
1261
1262
  return block;
1262
1263
  }
1263
1264
  function braceBlock(block) {
1264
- if (block.bare) {
1265
+ if (block.bare && !block.root) {
1265
1266
  if (block.children === block.expressions) {
1266
1267
  block.children = [block.expressions];
1267
1268
  }
@@ -1313,7 +1314,7 @@ function makeBlockFragment() {
1313
1314
  function replaceBlockExpression(node, child, replacement) {
1314
1315
  let found = false;
1315
1316
  const { expressions } = node;
1316
- for (let i1 = 0, len2 = expressions.length; i1 < len2; i1++) {
1317
+ for (let i1 = 0, len3 = expressions.length; i1 < len3; i1++) {
1317
1318
  const statement = expressions[i1];
1318
1319
  const [, s] = statement;
1319
1320
  if (s === child) {
@@ -1395,7 +1396,7 @@ function needsPrecedingSemicolon(exp) {
1395
1396
  return false;
1396
1397
  }
1397
1398
  if (Array.isArray(exp)) {
1398
- for (let i3 = 0, len2 = exp.length; i3 < len2; i3++) {
1399
+ for (let i3 = 0, len22 = exp.length; i3 < len22; i3++) {
1399
1400
  const child = exp[i3];
1400
1401
  if (!(child != null)) {
1401
1402
  continue;
@@ -1438,6 +1439,9 @@ function needsPrecedingSemicolon(exp) {
1438
1439
  }
1439
1440
 
1440
1441
  // source/parser/util.civet
1442
+ function len(arr, length) {
1443
+ return arr.length === length;
1444
+ }
1441
1445
  var assert = {
1442
1446
  equal(a, b, msg) {
1443
1447
  if (a !== b) {
@@ -1461,7 +1465,7 @@ function addParentPointers(node, parent) {
1461
1465
  node.parent = parent;
1462
1466
  }
1463
1467
  if (node.children) {
1464
- for (let ref1 = node.children, i1 = 0, len2 = ref1.length; i1 < len2; i1++) {
1468
+ for (let ref1 = node.children, i1 = 0, len1 = ref1.length; i1 < len1; i1++) {
1465
1469
  const child = ref1[i1];
1466
1470
  addParentPointers(child, node);
1467
1471
  }
@@ -1518,8 +1522,7 @@ function isEmptyBareBlock(node) {
1518
1522
  if (node?.type !== "BlockStatement")
1519
1523
  return false;
1520
1524
  const { bare, expressions } = node;
1521
- return bare && //expressions is like [], [, {type: "EmptyStatement"}]
1522
- (expressions.length === 0 || expressions.length === 1 && expressions[0][1]?.type === "EmptyStatement");
1525
+ 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
1526
  }
1524
1527
  function isFunction(node) {
1525
1528
  const { type } = node;
@@ -1876,7 +1879,7 @@ function updateParentPointers(node, parent, depth = 1) {
1876
1879
  return;
1877
1880
  }
1878
1881
  if (Array.isArray(node)) {
1879
- for (let i2 = 0, len1 = node.length; i2 < len1; i2++) {
1882
+ for (let i2 = 0, len22 = node.length; i2 < len22; i2++) {
1880
1883
  const child = node[i2];
1881
1884
  updateParentPointers(child, parent, depth);
1882
1885
  }
@@ -1887,7 +1890,7 @@ function updateParentPointers(node, parent, depth = 1) {
1887
1890
  node.parent = parent;
1888
1891
  }
1889
1892
  if (depth && isParent(node)) {
1890
- for (let ref4 = node.children, i3 = 0, len2 = ref4.length; i3 < len2; i3++) {
1893
+ for (let ref4 = node.children, i3 = 0, len3 = ref4.length; i3 < len3; i3++) {
1891
1894
  const child = ref4[i3];
1892
1895
  updateParentPointers(child, node, depth - 1);
1893
1896
  }
@@ -2018,7 +2021,7 @@ function wrapWithReturn(expression) {
2018
2021
  }
2019
2022
  function flatJoin(array, separator) {
2020
2023
  const result = [];
2021
- for (let i4 = 0, len3 = array.length; i4 < len3; i4++) {
2024
+ for (let i4 = 0, len4 = array.length; i4 < len4; i4++) {
2022
2025
  const i = i4;
2023
2026
  const items = array[i4];
2024
2027
  if (i) {
@@ -2041,7 +2044,7 @@ function findChildIndex(parent, child) {
2041
2044
  if (!(children != null)) {
2042
2045
  return -1;
2043
2046
  }
2044
- for (let i1 = 0, len2 = children.length; i1 < len2; i1++) {
2047
+ for (let i1 = 0, len3 = children.length; i1 < len3; i1++) {
2045
2048
  const i = i1;
2046
2049
  const c = children[i1];
2047
2050
  if (c === child || Array.isArray(c) && arrayRecurse(c)) {
@@ -2156,7 +2159,7 @@ var precedenceOrder = [
2156
2159
  ["**"]
2157
2160
  ];
2158
2161
  var precedenceMap = /* @__PURE__ */ new Map();
2159
- for (let i1 = 0, len2 = precedenceOrder.length; i1 < len2; i1++) {
2162
+ for (let i1 = 0, len3 = precedenceOrder.length; i1 < len3; i1++) {
2160
2163
  const prec = i1;
2161
2164
  const ops = precedenceOrder[i1];
2162
2165
  for (let i2 = 0, len1 = ops.length; i2 < len1; i2++) {
@@ -2267,7 +2270,7 @@ function processBinaryOpExpression($0) {
2267
2270
  } else {
2268
2271
  b = recurse(b);
2269
2272
  }
2270
- if (op.token === "instanceof" && b.type === "Literal" && b.children?.[0]?.type === "StringLiteral") {
2273
+ 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
2274
  a = ["typeof ", makeLeftHandSideExpression(a)];
2272
2275
  if (op.negated) {
2273
2276
  op = { ...op, token: "!==", negated: false };
@@ -2372,7 +2375,7 @@ function expandChainedComparisons([first, binops]) {
2372
2375
  let start = 0;
2373
2376
  const chains = [];
2374
2377
  var i = 0;
2375
- for (let i3 = 0, len2 = binops.length; i3 < len2; i3++) {
2378
+ for (let i3 = 0, len22 = binops.length; i3 < len22; i3++) {
2376
2379
  var i = i3;
2377
2380
  var [, op] = binops[i3];
2378
2381
  if (isRelationalOp(op)) {
@@ -2603,6 +2606,11 @@ function getHelperRef(base) {
2603
2606
  declareHelper[base](ref);
2604
2607
  return state.helperRefs[base] = ref;
2605
2608
  }
2609
+ function extractPreludeFor(node) {
2610
+ let helpers = new Set(Object.values(state.helperRefs));
2611
+ helpers = new Set(gatherRecursive(node, helpers.has.bind(helpers)));
2612
+ return state.prelude.filter((s) => gatherRecursive(s, helpers.has.bind(helpers)).length);
2613
+ }
2606
2614
 
2607
2615
  // source/parser/pattern-matching.civet
2608
2616
  function processPatternTest(lhs, patterns) {
@@ -2982,7 +2990,7 @@ function aliasBinding(p, ref) {
2982
2990
  }
2983
2991
 
2984
2992
  // source/parser/declaration.civet
2985
- function len(arr, length) {
2993
+ function len2(arr, length) {
2986
2994
  return arr.length === length;
2987
2995
  }
2988
2996
  function processAssignmentDeclaration(decl, pattern, suffix, ws, assign, e) {
@@ -3180,7 +3188,7 @@ function processDeclarationConditionStatement(s) {
3180
3188
  return;
3181
3189
  }
3182
3190
  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]) {
3191
+ 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
3192
  const { type: type1, children: [, { type: type2, expression: expression2 }] } = expression;
3185
3193
  const type = [type1, type2];
3186
3194
  expression = expression2;
@@ -3189,7 +3197,7 @@ function processDeclarationConditionStatement(s) {
3189
3197
  const { ref, pattern } = expression;
3190
3198
  if (pattern) {
3191
3199
  const conditions = getPatternConditions(pattern, ref).filter((c) => {
3192
- if (Array.isArray(c) && len(c, 2) && c[0] === ref && c[1] === " != null") {
3200
+ if (Array.isArray(c) && len2(c, 2) && c[0] === ref && c[1] === " != null") {
3193
3201
  const [,] = c;
3194
3202
  return false;
3195
3203
  } else {
@@ -4021,6 +4029,11 @@ function createVarDecs(block, scopes, pushVar) {
4021
4029
  scopes.pop();
4022
4030
  }
4023
4031
 
4032
+ // source/parser/comptime.civet
4033
+ var import_node_path = require("node:path");
4034
+ var import_node_module = require("node:module");
4035
+ var import_node_vm = __toESM(require("node:vm"));
4036
+
4024
4037
  // source/generate.civet
4025
4038
  function stringify(node) {
4026
4039
  try {
@@ -4106,36 +4119,122 @@ function processComptime(statements) {
4106
4119
  if (!getInitialConfig()?.comptime) {
4107
4120
  return;
4108
4121
  }
4109
- gatherRecursive(
4122
+ const promises = runComptime(statements);
4123
+ if (getSync()) {
4124
+ return;
4125
+ } else {
4126
+ return (async () => {
4127
+ {
4128
+ await Promise.all(promises);
4129
+ }
4130
+ })();
4131
+ }
4132
+ }
4133
+ function runComptime(statements) {
4134
+ const sync2 = getSync();
4135
+ return gatherRecursive(
4110
4136
  statements,
4111
4137
  (node) => {
4112
4138
  return node.type === "ComptimeStatement" || node.type === "ComptimeExpression";
4113
4139
  }
4114
- ).forEach((exp) => {
4115
- const content = exp.type === "ComptimeStatement" ? exp.block : exp.expression;
4140
+ ).map((exp) => {
4141
+ let content = exp.type === "ComptimeStatement" ? exp.block : exp.expression;
4142
+ content = [
4143
+ ...extractPreludeFor(content),
4144
+ content
4145
+ ];
4116
4146
  const options = { js: true };
4117
- const js = generate_default(prune(content), options);
4147
+ let js = generate_default(prune(content), options);
4148
+ js = `"use strict";${js}`;
4118
4149
  if (options.errors != null) {
4119
4150
  return;
4120
4151
  }
4121
- const output = eval?.(`"use strict";${js}`);
4152
+ let output, context;
4153
+ try {
4154
+ context = import_node_vm.default.createContext?.() ?? globalThis;
4155
+ const filename2 = context.__filename = (0, import_node_path.resolve)(getFilename() ?? "");
4156
+ context.__dirname = (0, import_node_path.dirname)(filename2);
4157
+ context.require = (0, import_node_module.createRequire)(filename2);
4158
+ if (import_node_vm.default.runInContext != null) {
4159
+ for (const global of [
4160
+ "RegExp",
4161
+ "Date",
4162
+ "Set",
4163
+ "Map",
4164
+ "URL",
4165
+ "Int8Array",
4166
+ "Uint8Array",
4167
+ "Int16Array",
4168
+ "Uint16Array",
4169
+ "Int32Array",
4170
+ "Uint32Array",
4171
+ "Float32Array",
4172
+ "Float64Array",
4173
+ "Uint8ClampedArray",
4174
+ "BigInt64Array",
4175
+ "BigUint64Array",
4176
+ "Buffer"
4177
+ ]) {
4178
+ context[global] = globalThis[global];
4179
+ }
4180
+ context.Object2 = Object;
4181
+ output = import_node_vm.default.runInContext(js, context, {
4182
+ filename: filename2,
4183
+ importModuleDynamically: import_node_vm.default.constants?.USE_MAIN_CONTEXT_DEFAULT_LOADER
4184
+ });
4185
+ } else {
4186
+ output = eval?.(js);
4187
+ }
4188
+ } catch (e) {
4189
+ exp.children = [
4190
+ {
4191
+ type: "Error",
4192
+ message: `comptime block failed to execute: ${e}
4193
+ ${js}`
4194
+ }
4195
+ ];
4196
+ return;
4197
+ }
4198
+ let promise;
4122
4199
  if (exp.type === "ComptimeExpression") {
4123
- let string;
4124
- try {
4125
- string = serialize(output);
4126
- } catch (e) {
4127
- exp.children = [
4200
+ const finish = () => {
4201
+ let string;
4202
+ try {
4203
+ if (import_node_vm.default.runInContext != null) {
4204
+ context.output = output;
4205
+ string = import_node_vm.default.runInContext(
4206
+ `${serialize(serialize)}serialize(output)`,
4207
+ context
4208
+ );
4209
+ } else {
4210
+ string = serialize(output);
4211
+ }
4212
+ } catch (e) {
4213
+ exp.children = [
4214
+ {
4215
+ type: "Error",
4216
+ message: `comptime result ${output} not serializable: ${e}`
4217
+ }
4218
+ ];
4219
+ return;
4220
+ }
4221
+ return exp.children = [string];
4222
+ };
4223
+ if (sync2) {
4224
+ finish();
4225
+ } else {
4226
+ promise = (async () => {
4128
4227
  {
4129
- type: "Error",
4130
- message: `comptime result ${output} not JSON serializable: ${e}`
4228
+ output = await output;
4229
+ return finish();
4131
4230
  }
4132
- ];
4133
- return;
4231
+ })();
4134
4232
  }
4135
- return exp.children = [string];
4136
4233
  } else {
4137
- return exp.children = [];
4234
+ promise = output;
4235
+ exp.children = [];
4138
4236
  }
4237
+ return promise;
4139
4238
  });
4140
4239
  }
4141
4240
  function serialize(value) {
@@ -4236,14 +4335,11 @@ function serialize(value) {
4236
4335
  })().join(",") + "])";
4237
4336
  break;
4238
4337
  }
4239
- case Array.prototype: {
4240
- ref1 = `[${val.map(recurse).join(",")}]`;
4241
- break;
4242
- }
4243
- case Object.prototype: {
4338
+ case Object.prototype:
4339
+ case globalThis.Object2?.prototype: {
4244
4340
  let objStr = "{";
4245
4341
  let descStr = "";
4246
- for (let ref2 = Object.getOwnPropertyNames(val).concat(Object.getOwnPropertySymbols(val)), i = 0, len2 = ref2.length; i < len2; i++) {
4342
+ for (let ref2 = Object.getOwnPropertyNames(val).concat(Object.getOwnPropertySymbols(val)), i = 0, len3 = ref2.length; i < len3; i++) {
4247
4343
  const prop = ref2[i];
4248
4344
  const desc = Object.getOwnPropertyDescriptor(val, prop);
4249
4345
  if (desc.enumerable && desc.configurable && desc.writable) {
@@ -4305,7 +4401,11 @@ function serialize(value) {
4305
4401
  break;
4306
4402
  }
4307
4403
  default: {
4308
- throw new TypeError(`cannot serialize object with prototype ${Object.getPrototypeOf(val)}`);
4404
+ if (Array.isArray(val)) {
4405
+ ref1 = `[${val.map(recurse).join(",")}]`;
4406
+ } else {
4407
+ throw new TypeError(`cannot serialize object with prototype ${val.constructor?.name ?? Object.getPrototypeOf(val)}`);
4408
+ }
4309
4409
  }
4310
4410
  }
4311
4411
  ;
@@ -4332,7 +4432,7 @@ function getIndentLevel(str, tab) {
4332
4432
  }
4333
4433
  function reduceIndentLevel(str, dedent, tab) {
4334
4434
  if (tab != null && tab != 1) {
4335
- for (let i1 = 0, len2 = str.length; i1 < len2; i1++) {
4435
+ for (let i1 = 0, len3 = str.length; i1 < len3; i1++) {
4336
4436
  const i = i1;
4337
4437
  const char = str[i1];
4338
4438
  if (!dedent) {
@@ -4414,7 +4514,7 @@ function dedentBlockSubstitutions($0, tab) {
4414
4514
  ;
4415
4515
  const dedent = ref1;
4416
4516
  let results = [s];
4417
- for (let i3 = 0, len2 = strWithSubstitutions.length; i3 < len2; i3++) {
4517
+ for (let i3 = 0, len22 = strWithSubstitutions.length; i3 < len22; i3++) {
4418
4518
  const i = i3;
4419
4519
  let part = strWithSubstitutions[i3];
4420
4520
  if (part.token != null) {
@@ -4544,9 +4644,8 @@ function isExpression(node) {
4544
4644
  return true;
4545
4645
  }
4546
4646
  function expressionizeBlock(blockOrExpression) {
4547
- let ref1;
4548
- if ((ref1 = blockOrExpression) && "expressions" in ref1) {
4549
- const { expressions } = ref1;
4647
+ if (blockOrExpression && typeof blockOrExpression === "object" && "expressions" in blockOrExpression) {
4648
+ const { expressions } = blockOrExpression;
4550
4649
  const l = expressions.length;
4551
4650
  const results = [];
4552
4651
  let i1 = 0;
@@ -4645,7 +4744,7 @@ function handleThisPrivateShorthands(value) {
4645
4744
  }
4646
4745
  function processCallMemberExpression(node) {
4647
4746
  const { children } = node;
4648
- if (children[0]?.parenthesizedOp?.token && children[1]?.type === "Call") {
4747
+ 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
4748
  const op = children[0].parenthesizedOp;
4650
4749
  let call = children[1];
4651
4750
  const args = [...call.args];
@@ -4692,11 +4791,23 @@ function processCallMemberExpression(node) {
4692
4791
  }
4693
4792
  prefix = prefix.concat(glob.dot);
4694
4793
  for (const part of glob.object.properties) {
4794
+ if (part.type === "Error") {
4795
+ parts.push(part);
4796
+ continue;
4797
+ }
4695
4798
  if (part.type === "MethodDefinition") {
4696
- throw new Error("Glob pattern cannot have method definition");
4799
+ parts.push({
4800
+ type: "Error",
4801
+ message: "Glob pattern cannot have method definition"
4802
+ });
4803
+ continue;
4697
4804
  }
4698
4805
  if (part.value && !["CallExpression", "MemberExpression", "Identifier"].includes(part.value.type)) {
4699
- throw new Error(`Glob pattern must have call or member expression value, found ${JSON.stringify(part.value)}`);
4806
+ parts.push({
4807
+ type: "Error",
4808
+ message: `Glob pattern must have call or member expression value, found ${JSON.stringify(part.value)}`
4809
+ });
4810
+ continue;
4700
4811
  }
4701
4812
  let suppressPrefix = false;
4702
4813
  let name = part.name;
@@ -4739,14 +4850,14 @@ function processCallMemberExpression(node) {
4739
4850
  });
4740
4851
  }
4741
4852
  }
4742
- let ref2;
4853
+ let ref1;
4743
4854
  let object = {
4744
4855
  type: "ObjectExpression",
4745
4856
  children: [
4746
4857
  glob.object.children[0],
4747
4858
  // {
4748
4859
  ...parts,
4749
- (ref2 = glob.object.children)[ref2.length - 1]
4860
+ (ref1 = glob.object.children)[ref1.length - 1]
4750
4861
  // whitespace and }
4751
4862
  ],
4752
4863
  properties: parts,
@@ -4798,7 +4909,7 @@ function replaceNode(node, newNode, parent) {
4798
4909
  throw new Error("replaceNode failed: node has no parent");
4799
4910
  }
4800
4911
  function recurse(children) {
4801
- for (let i3 = 0, len2 = children.length; i3 < len2; i3++) {
4912
+ for (let i3 = 0, len22 = children.length; i3 < len22; i3++) {
4802
4913
  const i = i3;
4803
4914
  const child = children[i3];
4804
4915
  if (child === node) {
@@ -4845,6 +4956,10 @@ function lastAccessInCallExpression(exp) {
4845
4956
  }
4846
4957
  let children, i;
4847
4958
  do {
4959
+ if (!(exp.children != null)) {
4960
+ return;
4961
+ }
4962
+ ;
4848
4963
  ({ children } = exp);
4849
4964
  i = children.length - 1;
4850
4965
  while (i >= 0 && (children[i].type === "Call" || children[i].type === "NonNullAssertion" || children[i].type === "Optional"))
@@ -4888,8 +5003,8 @@ function convertNamedImportsToObject(node, pattern) {
4888
5003
  return { type: "Error", message: "cannot use `type` in dynamic import" };
4889
5004
  } else {
4890
5005
  const { source, binding } = specifier;
4891
- let ref3;
4892
- const delim = (ref3 = specifier.children)[ref3.length - 1];
5006
+ let ref2;
5007
+ const delim = (ref2 = specifier.children)[ref2.length - 1];
4893
5008
  return {
4894
5009
  type: pattern ? "BindingProperty" : "Property",
4895
5010
  name: source,
@@ -4899,7 +5014,7 @@ function convertNamedImportsToObject(node, pattern) {
4899
5014
  };
4900
5015
  }
4901
5016
  });
4902
- let ref4;
5017
+ let ref3;
4903
5018
  return {
4904
5019
  type: pattern ? "ObjectBindingPattern" : "ObjectExpression",
4905
5020
  names: node.names,
@@ -4908,7 +5023,7 @@ function convertNamedImportsToObject(node, pattern) {
4908
5023
  node.children[0],
4909
5024
  // {
4910
5025
  properties,
4911
- (ref4 = node.children)[ref4.length - 1]
5026
+ (ref3 = node.children)[ref3.length - 1]
4912
5027
  // }
4913
5028
  ]
4914
5029
  };
@@ -5085,17 +5200,17 @@ function processAssignments(statements) {
5085
5200
  statements,
5086
5201
  (n) => n.type === "AssignmentExpression" && n.names === null,
5087
5202
  (exp) => {
5088
- let { lhs: $1, expression: $2 } = exp, tail = [], i = 0, len2 = $1.length;
5203
+ let { lhs: $1, expression: $2 } = exp, tail = [], i = 0, len3 = $1.length;
5089
5204
  let block;
5090
- let ref5;
5091
- if (exp.parent?.type === "BlockStatement" && !(ref5 = $1[$1.length - 1])?.[ref5.length - 1]?.special) {
5205
+ let ref4;
5206
+ if (exp.parent?.type === "BlockStatement" && !(ref4 = $1[$1.length - 1])?.[ref4.length - 1]?.special) {
5092
5207
  block = makeBlockFragment();
5093
- let ref6;
5094
- if (ref6 = prependStatementExpressionBlock(
5208
+ let ref5;
5209
+ if (ref5 = prependStatementExpressionBlock(
5095
5210
  { type: "Initializer", expression: $2, children: [void 0, void 0, $2] },
5096
5211
  block
5097
5212
  )) {
5098
- const ref = ref6;
5213
+ const ref = ref5;
5099
5214
  exp.children = exp.children.map(function(c) {
5100
5215
  if (c === $2)
5101
5216
  return ref;
@@ -5125,7 +5240,7 @@ function processAssignments(statements) {
5125
5240
  }
5126
5241
  }
5127
5242
  let wrapped = false;
5128
- while (i < len2) {
5243
+ while (i < len3) {
5129
5244
  const lastAssignment = $1[i++];
5130
5245
  const [, lhs, , op] = lastAssignment;
5131
5246
  if (op.token !== "=")
@@ -5138,7 +5253,7 @@ function processAssignments(statements) {
5138
5253
  }
5139
5254
  }
5140
5255
  }
5141
- i = len2 - 1;
5256
+ i = len3 - 1;
5142
5257
  while (i >= 0) {
5143
5258
  const lastAssignment = $1[i];
5144
5259
  if (lastAssignment[3].token === "=") {
@@ -5174,14 +5289,14 @@ function processAssignments(statements) {
5174
5289
  }
5175
5290
  i--;
5176
5291
  }
5177
- i = len2 - 1;
5292
+ i = len3 - 1;
5178
5293
  const optionalChainRef = makeRef();
5179
5294
  while (i >= 0) {
5180
5295
  const assignment = $1[i];
5181
5296
  const [ws1, lhs, ws2, op] = assignment;
5182
5297
  if (lhs.type === "MemberExpression" || lhs.type === "CallExpression") {
5183
5298
  const newMemberExp = unchainOptionalMemberExpression(lhs, optionalChainRef, (children) => {
5184
- const assigns = $1.splice(i + 1, len2 - 1 - i);
5299
+ const assigns = $1.splice(i + 1, len3 - 1 - i);
5185
5300
  $1.pop();
5186
5301
  return [ws1, ...children, ws2, op, ...assigns, $2];
5187
5302
  });
@@ -5263,9 +5378,9 @@ function unchainOptionalMemberExpression(exp, ref, innerExp) {
5263
5378
  }
5264
5379
  j++;
5265
5380
  }
5266
- let ref7;
5267
- if (ref7 = conditions.length) {
5268
- const l = ref7;
5381
+ let ref6;
5382
+ if (ref6 = conditions.length) {
5383
+ const l = ref6;
5269
5384
  const cs = flatJoin(conditions, " && ");
5270
5385
  return {
5271
5386
  ...exp,
@@ -5302,8 +5417,8 @@ function processTypes(node) {
5302
5417
  return gatherRecursiveAll(node, (n) => n.type === "UnaryType").forEach((unary) => {
5303
5418
  let last;
5304
5419
  let count = 0;
5305
- let ref8;
5306
- while (unary.suffix.length && (ref8 = unary.suffix)[ref8.length - 1]?.token === "?") {
5420
+ let ref7;
5421
+ while (unary.suffix.length && (ref7 = unary.suffix)[ref7.length - 1]?.token === "?") {
5307
5422
  last = unary.suffix.pop();
5308
5423
  count++;
5309
5424
  }
@@ -5337,11 +5452,11 @@ function processStatementExpressions(statements) {
5337
5452
  gatherRecursiveAll(statements, ($) => $.type === "StatementExpression").forEach((_exp) => {
5338
5453
  const exp = _exp;
5339
5454
  const { statement } = exp;
5340
- let ref9;
5455
+ let ref8;
5341
5456
  switch (statement.type) {
5342
5457
  case "IfStatement": {
5343
- if (ref9 = expressionizeIfStatement(statement)) {
5344
- const expression = ref9;
5458
+ if (ref8 = expressionizeIfStatement(statement)) {
5459
+ const expression = ref8;
5345
5460
  return replaceNode(statement, expression, exp);
5346
5461
  } else {
5347
5462
  return replaceNode(statement, wrapIIFE([["", statement]]), exp);
@@ -5349,7 +5464,8 @@ function processStatementExpressions(statements) {
5349
5464
  }
5350
5465
  case "IterationExpression": {
5351
5466
  if (statement.subtype === "ComptimeStatement") {
5352
- const expression = wrapIIFE(statement.statement.block.expressions);
5467
+ const { expressions } = statement.statement.block;
5468
+ const expression = wrapIIFE(expressions, hasAwait(expressions));
5353
5469
  return replaceNode(statement, makeNode({
5354
5470
  type: "ComptimeExpression",
5355
5471
  expression,
@@ -5427,7 +5543,13 @@ function processProgram(root) {
5427
5543
  processBlocks(statements);
5428
5544
  populateRefs(statements);
5429
5545
  adjustAtBindings(statements);
5430
- processComptime(statements);
5546
+ if (getSync()) {
5547
+ processComptime(statements);
5548
+ }
5549
+ }
5550
+ async function processProgramAsync(root) {
5551
+ const { expressions: statements } = root;
5552
+ await processComptime(statements);
5431
5553
  }
5432
5554
  function populateRefs(statements) {
5433
5555
  const refNodes = gatherRecursive(statements, ({ type }) => type === "Ref");
@@ -5551,8 +5673,8 @@ function processPlaceholders(statements) {
5551
5673
  for (let i4 = 0, len3 = placeholders.length; i4 < len3; i4++) {
5552
5674
  const placeholder = placeholders[i4];
5553
5675
  typeSuffix ??= placeholder.typeSuffix;
5554
- let ref10;
5555
- replaceNode((ref10 = placeholder.children)[ref10.length - 1], ref);
5676
+ let ref9;
5677
+ replaceNode((ref9 = placeholder.children)[ref9.length - 1], ref);
5556
5678
  }
5557
5679
  const { parent } = ancestor;
5558
5680
  const body = maybeUnwrap(ancestor);
@@ -5583,9 +5705,9 @@ function processPlaceholders(statements) {
5583
5705
  fnExp = makeLeftHandSideExpression(fnExp);
5584
5706
  }
5585
5707
  replaceNode(ancestor, fnExp, parent);
5586
- let ref11;
5587
- if (ref11 = getTrimmingSpace(body)) {
5588
- const ws = ref11;
5708
+ let ref10;
5709
+ if (ref10 = getTrimmingSpace(body)) {
5710
+ const ws = ref10;
5589
5711
  inplaceInsertTrimmingSpace(body, "");
5590
5712
  inplacePrepend(ws, fnExp);
5591
5713
  }
@@ -5630,8 +5752,8 @@ function reorderBindingRestProperty(props) {
5630
5752
  }
5631
5753
  ];
5632
5754
  }
5633
- let ref12;
5634
- if (Array.isArray(rest.delim) && (ref12 = rest.delim)[ref12.length - 1]?.token === ",") {
5755
+ let ref11;
5756
+ if (Array.isArray(rest.delim) && (ref11 = rest.delim)[ref11.length - 1]?.token === ",") {
5635
5757
  rest.delim = rest.delim.slice(0, -1);
5636
5758
  rest.children = [...rest.children.slice(0, -1), rest.delim];
5637
5759
  }
@@ -6829,15 +6951,20 @@ var $R92 = (0, import_lib2.$R)(new RegExp("(?=[ \\t\\r\\n\\/#]|$)", "suy"));
6829
6951
  var $R93 = (0, import_lib2.$R)(new RegExp("\\r\\n|\\n|\\r|$", "suy"));
6830
6952
  var $R94 = (0, import_lib2.$R)(new RegExp("[^]*", "suy"));
6831
6953
  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) {
6954
+ var reset = $1;
6955
+ var init = $2;
6956
+ var ws1 = $3;
6832
6957
  var statements = $4;
6833
- processProgram({
6958
+ var ws2 = $5;
6959
+ const program = {
6834
6960
  type: "BlockStatement",
6835
6961
  expressions: statements,
6836
- children: [statements],
6962
+ children: [reset, init, ws1, statements, ws2],
6837
6963
  bare: true,
6838
6964
  root: true
6839
- }, ReservedWord);
6840
- return $0;
6965
+ };
6966
+ processProgram(program);
6967
+ return program;
6841
6968
  });
6842
6969
  function Program(ctx, state2) {
6843
6970
  return (0, import_lib2.$EVENT)(ctx, state2, "Program", Program$0);
@@ -8435,11 +8562,11 @@ var PropertyAccess$1 = (0, import_lib2.$TS)((0, import_lib2.$S)(AccessStart, (0,
8435
8562
  var dot = $1;
8436
8563
  var neg = $2;
8437
8564
  var num = $3;
8438
- const len2 = {
8565
+ const len3 = {
8439
8566
  children: []
8440
8567
  }, children = [
8441
8568
  adjustIndexAccess(dot),
8442
- len2,
8569
+ len3,
8443
8570
  neg,
8444
8571
  num,
8445
8572
  "]"
@@ -8447,7 +8574,7 @@ var PropertyAccess$1 = (0, import_lib2.$TS)((0, import_lib2.$S)(AccessStart, (0,
8447
8574
  return {
8448
8575
  type: "NegativeIndex",
8449
8576
  children,
8450
- len: len2
8577
+ len: len3
8451
8578
  };
8452
8579
  });
8453
8580
  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) {
@@ -10937,6 +11064,7 @@ var _BinaryOp$1 = (0, import_lib2.$TV)(Identifier, function($skip, $loc, $0, $1)
10937
11064
  if (!state.operators.has(id.name))
10938
11065
  return $skip;
10939
11066
  return {
11067
+ token: id.name,
10940
11068
  call: id,
10941
11069
  special: true,
10942
11070
  ...state.operators.get(id.name)
@@ -10947,6 +11075,7 @@ var _BinaryOp$2 = (0, import_lib2.$TS)((0, import_lib2.$S)(OmittedNegation, __,
10947
11075
  if (!state.operators.has(id.name))
10948
11076
  return $skip;
10949
11077
  return {
11078
+ token: id.name,
10950
11079
  call: id,
10951
11080
  special: true,
10952
11081
  negated: true,
@@ -16119,26 +16248,29 @@ var parser = function() {
16119
16248
  const parser2 = options.startRule != null ? grammar[options.startRule] : Object.values(grammar)[0];
16120
16249
  if (!parser2)
16121
16250
  throw new Error(`Could not find rule with name '${options.startRule}'`);
16122
- const filename = options.filename || "<anonymous>";
16251
+ const filename2 = options.filename || "<anonymous>";
16123
16252
  reset();
16124
16253
  Object.assign(ctx, { ...options.events, tokenize: options.tokenize });
16125
16254
  return validate(input, parser2(ctx, {
16126
16255
  input,
16127
16256
  pos: 0
16128
16257
  }), {
16129
- filename
16258
+ filename: filename2
16130
16259
  });
16131
16260
  }
16132
16261
  };
16133
16262
  }();
16134
16263
  var { parse } = parser;
16264
+ var filename;
16135
16265
  var initialConfig;
16136
16266
  var config;
16267
+ var sync;
16137
16268
  var state = {};
16138
16269
  var getState = () => state;
16139
16270
  var getConfig = () => config;
16140
16271
  var getInitialConfig = () => initialConfig;
16141
- var setInitialConfig = (c) => initialConfig = c;
16272
+ var getFilename = () => filename;
16273
+ var getSync = () => sync;
16142
16274
  Object.defineProperties(state, {
16143
16275
  currentIndent: {
16144
16276
  get() {
@@ -16183,6 +16315,21 @@ Object.defineProperties(state, {
16183
16315
  }
16184
16316
  }
16185
16317
  });
16318
+ function parseProgram(input, options) {
16319
+ filename = options?.filename;
16320
+ initialConfig = options?.parseOptions;
16321
+ sync = options?.sync;
16322
+ const root = parse(input, options);
16323
+ if (sync) {
16324
+ filename = initialConfig = sync = null;
16325
+ return root;
16326
+ } else {
16327
+ return processProgramAsync(root).then(() => {
16328
+ filename = initialConfig = sync = null;
16329
+ return root;
16330
+ });
16331
+ }
16332
+ }
16186
16333
 
16187
16334
  // source/util.civet
16188
16335
  var util_exports = {};
@@ -16570,6 +16717,21 @@ var StateCache = class {
16570
16717
 
16571
16718
  // source/main.civet
16572
16719
  var { SourceMap: SourceMap2 } = util_exports;
16720
+ var ParseErrors = class extends Error {
16721
+ name = "ParseErrors";
16722
+ filename;
16723
+ line;
16724
+ column;
16725
+ offset;
16726
+ constructor(message, filename1, line1, column1, offset1) {
16727
+ super(message);
16728
+ this.filename = filename1;
16729
+ this.line = line1;
16730
+ this.column = column1;
16731
+ this.offset = offset1;
16732
+ this.message = message;
16733
+ }
16734
+ };
16573
16735
  var uncacheable = /* @__PURE__ */ new Set([
16574
16736
  // Meta
16575
16737
  "DebugHere",
@@ -16609,15 +16771,15 @@ var uncacheable = /* @__PURE__ */ new Set([
16609
16771
  "RestoreTrailingMemberProperty",
16610
16772
  "RestoreNewlineBinaryOp"
16611
16773
  ]);
16612
- var compile = function(src, options) {
16774
+ function compile(src, options) {
16613
16775
  if (!options) {
16614
16776
  options = {};
16615
16777
  } else {
16616
16778
  options = { ...options };
16617
16779
  }
16618
16780
  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)) {
16781
+ const filename2 = options.filename || "unknown";
16782
+ if (filename2.endsWith(".coffee") && !/^(#![^\r\n]*(\r\n|\n|\r))?\s*['"]civet/.test(src)) {
16621
16783
  options.parseOptions.coffeeCompat = true;
16622
16784
  }
16623
16785
  const { hits, trace, noCache } = options;
@@ -16629,21 +16791,18 @@ var compile = function(src, options) {
16629
16791
  });
16630
16792
  }
16631
16793
  let ast;
16632
- setInitialConfig(options.parseOptions);
16633
16794
  try {
16634
- ast = parse(src, {
16635
- filename,
16795
+ ast = parseProgram(src, {
16796
+ parseOptions: options.parseOptions,
16797
+ sync: options.sync,
16798
+ filename: filename2,
16636
16799
  events
16637
16800
  });
16638
- if (!(options.ast === "raw")) {
16639
- ast = prune(ast);
16640
- }
16641
16801
  } finally {
16642
- setInitialConfig(void 0);
16643
16802
  if (hits || trace) {
16644
- import("fs").then(function({ writeFileSync }) {
16803
+ import("node:fs").then(function({ writeFileSync }) {
16645
16804
  let ref;
16646
- if ((ref = events?.meta) && "logs" in ref) {
16805
+ if ((ref = events?.meta) && typeof ref === "object" && "logs" in ref) {
16647
16806
  const { logs } = ref;
16648
16807
  if (trace) {
16649
16808
  writeFileSync(trace, logs.join("\n"));
@@ -16672,34 +16831,45 @@ ${counts}`;
16672
16831
  });
16673
16832
  }
16674
16833
  }
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")} `);
16834
+ function rest(ast2) {
16835
+ options = options;
16836
+ if (!(options.ast === "raw")) {
16837
+ ast2 = prune(ast2);
16681
16838
  }
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
- };
16839
+ if (options.ast) {
16840
+ return ast2;
16841
+ }
16842
+ function checkErrors() {
16843
+ if (options.errors?.length) {
16844
+ throw new ParseErrors(options.errors.map(($) => $.message).join("\n"));
16845
+ }
16846
+ ;
16847
+ return;
16848
+ }
16849
+ if (options.sourceMap || options.inlineMap) {
16850
+ const sm = SourceMap2(src);
16851
+ options.updateSourceMap = sm.updateSourceMap;
16852
+ const code = generate_default(ast2, options);
16853
+ checkErrors();
16854
+ if (options.inlineMap) {
16855
+ return SourceMap2.remap(code, sm, filename2, filename2 + ".tsx");
16856
+ } else {
16857
+ return {
16858
+ code,
16859
+ sourceMap: sm
16860
+ };
16861
+ }
16697
16862
  }
16863
+ const result = generate_default(ast2, options);
16864
+ checkErrors();
16865
+ return result;
16698
16866
  }
16699
- const result = generate_default(ast, options);
16700
- checkErrors();
16701
- return result;
16702
- };
16867
+ if (ast.then != null) {
16868
+ return ast.then(rest);
16869
+ } else {
16870
+ return rest(ast);
16871
+ }
16872
+ }
16703
16873
  var makeCache = function({ hits, trace } = {}) {
16704
16874
  const meta = {};
16705
16875
  let hitCount;
@@ -16766,14 +16936,16 @@ var isCompileError = function(err) {
16766
16936
  return err instanceof Error && //@ts-ignore
16767
16937
  [err.message, err.name, err.filename, err.line, err.column, err.offset].every(($1) => $1 !== void 0);
16768
16938
  };
16769
- var main_default = { parse, generate: generate_default, util: util_exports, compile, isCompileError };
16939
+ var main_default = { parse, parseProgram, generate: generate_default, util: util_exports, compile, isCompileError };
16770
16940
  // Annotate the CommonJS export names for ESM import in node:
16771
16941
  0 && (module.exports = {
16942
+ ParseErrors,
16772
16943
  compile,
16773
16944
  generate,
16774
16945
  isCompileError,
16775
16946
  lib,
16776
16947
  parse,
16948
+ parseProgram,
16777
16949
  prune,
16778
16950
  util
16779
16951
  });