solve-engine 1.0.0-beta.0 → 1.0.0-beta.2

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.
Files changed (253) hide show
  1. package/dist/BytecodeBuilder-3egMyuDi.d.cts +274 -0
  2. package/dist/BytecodeBuilder-3egMyuDi.d.ts +274 -0
  3. package/dist/Configuration-fVpd-HC-.d.cts +228 -0
  4. package/dist/Configuration-fVpd-HC-.d.ts +228 -0
  5. package/dist/EngineError-DvPSnhZi.d.cts +304 -0
  6. package/dist/EngineError-DvPSnhZi.d.ts +304 -0
  7. package/dist/Lexer-BJdhlnej.d.cts +531 -0
  8. package/dist/Lexer-D9l4Yrk2.d.ts +531 -0
  9. package/dist/NormalizerRule-BrVoVjmP.d.cts +163 -0
  10. package/dist/NormalizerRule-CEjf1FyD.d.ts +163 -0
  11. package/dist/PackageRegistry-D-Tv_7ca.d.cts +2572 -0
  12. package/dist/PackageRegistry-DaH4aIKP.d.ts +2572 -0
  13. package/dist/Parselet-CK8cQu2a.d.cts +299 -0
  14. package/dist/Parselet-ConOIdRO.d.ts +299 -0
  15. package/dist/ScopeManager-B5k0nk05.d.ts +559 -0
  16. package/dist/ScopeManager-CKfQtl7l.d.cts +559 -0
  17. package/dist/Token-0jpvUdPY.d.cts +245 -0
  18. package/dist/Token-0jpvUdPY.d.ts +245 -0
  19. package/dist/VMCheckpoints-BmDwIXgK.d.ts +171 -0
  20. package/dist/VMCheckpoints-CqFuLwrT.d.cts +171 -0
  21. package/dist/Value-KtAQNHur.d.cts +295 -0
  22. package/dist/Value-KtAQNHur.d.ts +295 -0
  23. package/dist/chunk-2RWAXT6O.js +3 -0
  24. package/dist/chunk-2RWAXT6O.js.map +1 -0
  25. package/dist/chunk-34RRD7PC.js +1492 -0
  26. package/dist/chunk-34RRD7PC.js.map +1 -0
  27. package/dist/chunk-3PPFLFH4.js +5097 -0
  28. package/dist/chunk-3PPFLFH4.js.map +1 -0
  29. package/dist/chunk-3VGDNEE3.js +84 -0
  30. package/dist/chunk-3VGDNEE3.js.map +1 -0
  31. package/dist/chunk-3YNVWKR2.cjs +370 -0
  32. package/dist/chunk-3YNVWKR2.cjs.map +1 -0
  33. package/dist/chunk-47LRVGOT.cjs +316 -0
  34. package/dist/chunk-47LRVGOT.cjs.map +1 -0
  35. package/dist/chunk-4MG4XKO2.js +1644 -0
  36. package/dist/chunk-4MG4XKO2.js.map +1 -0
  37. package/dist/chunk-4QADQTWS.js +2392 -0
  38. package/dist/chunk-4QADQTWS.js.map +1 -0
  39. package/dist/chunk-53B6KDDJ.cjs +1651 -0
  40. package/dist/chunk-53B6KDDJ.cjs.map +1 -0
  41. package/dist/chunk-5X2PTP6F.cjs +104 -0
  42. package/dist/chunk-5X2PTP6F.cjs.map +1 -0
  43. package/dist/chunk-5YEMOYSE.js +264 -0
  44. package/dist/chunk-5YEMOYSE.js.map +1 -0
  45. package/dist/chunk-64W6GLLZ.js +481 -0
  46. package/dist/chunk-64W6GLLZ.js.map +1 -0
  47. package/dist/chunk-6BKTCEUP.cjs +233 -0
  48. package/dist/chunk-6BKTCEUP.cjs.map +1 -0
  49. package/dist/chunk-6NTVRDQV.cjs +1642 -0
  50. package/dist/chunk-6NTVRDQV.cjs.map +1 -0
  51. package/dist/chunk-6RDSQURE.js +290 -0
  52. package/dist/chunk-6RDSQURE.js.map +1 -0
  53. package/dist/chunk-75JP4BZX.js +93 -0
  54. package/dist/chunk-75JP4BZX.js.map +1 -0
  55. package/dist/chunk-B7LXJ6LC.cjs +36 -0
  56. package/dist/chunk-B7LXJ6LC.cjs.map +1 -0
  57. package/dist/chunk-BVAR5J2F.js +257 -0
  58. package/dist/chunk-BVAR5J2F.js.map +1 -0
  59. package/dist/chunk-C4XZV6E7.cjs +269 -0
  60. package/dist/chunk-C4XZV6E7.cjs.map +1 -0
  61. package/dist/chunk-CCJPSHGI.cjs +40 -0
  62. package/dist/chunk-CCJPSHGI.cjs.map +1 -0
  63. package/dist/chunk-CLL7RUQV.cjs +316 -0
  64. package/dist/chunk-CLL7RUQV.cjs.map +1 -0
  65. package/dist/chunk-DM3LMRBC.js +329 -0
  66. package/dist/chunk-DM3LMRBC.js.map +1 -0
  67. package/dist/chunk-DUEZFTK2.js +38 -0
  68. package/dist/chunk-DUEZFTK2.js.map +1 -0
  69. package/dist/chunk-EBSPLUW4.cjs +489 -0
  70. package/dist/chunk-EBSPLUW4.cjs.map +1 -0
  71. package/dist/chunk-EHAHVROS.cjs +261 -0
  72. package/dist/chunk-EHAHVROS.cjs.map +1 -0
  73. package/dist/chunk-EIGTWK5N.js +167 -0
  74. package/dist/chunk-EIGTWK5N.js.map +1 -0
  75. package/dist/chunk-EPOXXJBK.js +96 -0
  76. package/dist/chunk-EPOXXJBK.js.map +1 -0
  77. package/dist/chunk-FEAIOR7N.cjs +91 -0
  78. package/dist/chunk-FEAIOR7N.cjs.map +1 -0
  79. package/dist/chunk-G535KJEG.js +312 -0
  80. package/dist/chunk-G535KJEG.js.map +1 -0
  81. package/dist/chunk-GCM5JHMX.js +88 -0
  82. package/dist/chunk-GCM5JHMX.js.map +1 -0
  83. package/dist/chunk-GOLDJNMZ.js +368 -0
  84. package/dist/chunk-GOLDJNMZ.js.map +1 -0
  85. package/dist/chunk-GW32KPCU.cjs +360 -0
  86. package/dist/chunk-GW32KPCU.cjs.map +1 -0
  87. package/dist/chunk-HTXVVJRA.cjs +1549 -0
  88. package/dist/chunk-HTXVVJRA.cjs.map +1 -0
  89. package/dist/chunk-HWSZJQCI.js +311 -0
  90. package/dist/chunk-HWSZJQCI.js.map +1 -0
  91. package/dist/chunk-IF532O7C.js +101 -0
  92. package/dist/chunk-IF532O7C.js.map +1 -0
  93. package/dist/chunk-J73SJHR3.cjs +2415 -0
  94. package/dist/chunk-J73SJHR3.cjs.map +1 -0
  95. package/dist/chunk-JBSYC7BB.cjs +7473 -0
  96. package/dist/chunk-JBSYC7BB.cjs.map +1 -0
  97. package/dist/chunk-KVILKGMS.js +228 -0
  98. package/dist/chunk-KVILKGMS.js.map +1 -0
  99. package/dist/chunk-LIPPNDBE.js +258 -0
  100. package/dist/chunk-LIPPNDBE.js.map +1 -0
  101. package/dist/chunk-LR7YASZF.cjs +172 -0
  102. package/dist/chunk-LR7YASZF.cjs.map +1 -0
  103. package/dist/chunk-M4N5GA5Z.cjs +309 -0
  104. package/dist/chunk-M4N5GA5Z.cjs.map +1 -0
  105. package/dist/chunk-M5LX5AOO.js +7459 -0
  106. package/dist/chunk-M5LX5AOO.js.map +1 -0
  107. package/dist/chunk-MZOVP3WI.cjs +96 -0
  108. package/dist/chunk-MZOVP3WI.cjs.map +1 -0
  109. package/dist/chunk-N5YFR4UP.cjs +10 -0
  110. package/dist/chunk-N5YFR4UP.cjs.map +1 -0
  111. package/dist/chunk-NGBBNL4K.cjs +4 -0
  112. package/dist/chunk-NGBBNL4K.cjs.map +1 -0
  113. package/dist/chunk-NH2O2AUR.js +146 -0
  114. package/dist/chunk-NH2O2AUR.js.map +1 -0
  115. package/dist/chunk-NLNDMWZC.js +31 -0
  116. package/dist/chunk-NLNDMWZC.js.map +1 -0
  117. package/dist/chunk-NMCRQP3Z.cjs +778 -0
  118. package/dist/chunk-NMCRQP3Z.cjs.map +1 -0
  119. package/dist/chunk-NMD5VRN4.cjs +1327 -0
  120. package/dist/chunk-NMD5VRN4.cjs.map +1 -0
  121. package/dist/chunk-OC6LTXYS.cjs +286 -0
  122. package/dist/chunk-OC6LTXYS.cjs.map +1 -0
  123. package/dist/chunk-OT6OJY7C.cjs +114 -0
  124. package/dist/chunk-OT6OJY7C.cjs.map +1 -0
  125. package/dist/chunk-PFSDGGYL.js +8 -0
  126. package/dist/chunk-PFSDGGYL.js.map +1 -0
  127. package/dist/chunk-QNJ4ACRT.cjs +155 -0
  128. package/dist/chunk-QNJ4ACRT.cjs.map +1 -0
  129. package/dist/chunk-QWY3VEZN.js +1637 -0
  130. package/dist/chunk-QWY3VEZN.js.map +1 -0
  131. package/dist/chunk-RFYD5TJE.js +111 -0
  132. package/dist/chunk-RFYD5TJE.js.map +1 -0
  133. package/dist/chunk-RIN643A3.js +354 -0
  134. package/dist/chunk-RIN643A3.js.map +1 -0
  135. package/dist/chunk-RZCWSXTA.cjs +5122 -0
  136. package/dist/chunk-RZCWSXTA.cjs.map +1 -0
  137. package/dist/chunk-SDGRK7EP.js +774 -0
  138. package/dist/chunk-SDGRK7EP.js.map +1 -0
  139. package/dist/chunk-VEKIWQ3F.cjs +39 -0
  140. package/dist/chunk-VEKIWQ3F.cjs.map +1 -0
  141. package/dist/chunk-WQTTOGXC.cjs +99 -0
  142. package/dist/chunk-WQTTOGXC.cjs.map +1 -0
  143. package/dist/chunk-X6K3HUOY.cjs +86 -0
  144. package/dist/chunk-X6K3HUOY.cjs.map +1 -0
  145. package/dist/chunk-XJCMXB2E.cjs +333 -0
  146. package/dist/chunk-XJCMXB2E.cjs.map +1 -0
  147. package/dist/chunk-XVWCOTR6.js +1324 -0
  148. package/dist/chunk-XVWCOTR6.js.map +1 -0
  149. package/dist/chunk-YLJ4XMA6.js +33 -0
  150. package/dist/chunk-YLJ4XMA6.js.map +1 -0
  151. package/dist/chunk-Z2XP767T.cjs +35 -0
  152. package/dist/chunk-Z2XP767T.cjs.map +1 -0
  153. package/dist/chunk-Z4X2RWOF.js +37 -0
  154. package/dist/chunk-Z4X2RWOF.js.map +1 -0
  155. package/dist/constants.cjs +22 -0
  156. package/dist/constants.cjs.map +1 -0
  157. package/dist/constants.d.cts +12 -0
  158. package/dist/constants.d.ts +12 -0
  159. package/dist/constants.js +5 -0
  160. package/dist/constants.js.map +1 -0
  161. package/dist/engine.cjs +79 -0
  162. package/dist/engine.cjs.map +1 -0
  163. package/dist/engine.d.cts +587 -0
  164. package/dist/engine.d.ts +587 -0
  165. package/dist/engine.js +34 -0
  166. package/dist/engine.js.map +1 -0
  167. package/dist/errors.cjs +81 -0
  168. package/dist/errors.cjs.map +1 -0
  169. package/dist/errors.d.cts +95 -0
  170. package/dist/errors.d.ts +95 -0
  171. package/dist/errors.js +4 -0
  172. package/dist/errors.js.map +1 -0
  173. package/dist/format.cjs +171 -0
  174. package/dist/format.cjs.map +1 -0
  175. package/dist/format.d.cts +51 -0
  176. package/dist/format.d.ts +51 -0
  177. package/dist/format.js +168 -0
  178. package/dist/format.js.map +1 -0
  179. package/dist/index.cjs +97 -0
  180. package/dist/index.cjs.map +1 -0
  181. package/dist/index.d.cts +152 -0
  182. package/dist/index.d.ts +152 -0
  183. package/dist/index.js +75 -0
  184. package/dist/index.js.map +1 -0
  185. package/dist/language.cjs +308 -0
  186. package/dist/language.cjs.map +1 -0
  187. package/dist/language.d.cts +110 -0
  188. package/dist/language.d.ts +110 -0
  189. package/dist/language.js +287 -0
  190. package/dist/language.js.map +1 -0
  191. package/dist/lexer.cjs +64 -0
  192. package/dist/lexer.cjs.map +1 -0
  193. package/dist/lexer.d.cts +56 -0
  194. package/dist/lexer.d.ts +56 -0
  195. package/dist/lexer.js +11 -0
  196. package/dist/lexer.js.map +1 -0
  197. package/dist/normalizer.cjs +40 -0
  198. package/dist/normalizer.cjs.map +1 -0
  199. package/dist/normalizer.d.cts +364 -0
  200. package/dist/normalizer.d.ts +364 -0
  201. package/dist/normalizer.js +11 -0
  202. package/dist/normalizer.js.map +1 -0
  203. package/dist/packages.cjs +121 -0
  204. package/dist/packages.cjs.map +1 -0
  205. package/dist/packages.d.cts +691 -0
  206. package/dist/packages.d.ts +691 -0
  207. package/dist/packages.js +24 -0
  208. package/dist/packages.js.map +1 -0
  209. package/dist/parser.cjs +55 -0
  210. package/dist/parser.cjs.map +1 -0
  211. package/dist/parser.d.cts +46 -0
  212. package/dist/parser.d.ts +46 -0
  213. package/dist/parser.js +10 -0
  214. package/dist/parser.js.map +1 -0
  215. package/dist/pipeline-CrsRPmsv.d.ts +285 -0
  216. package/dist/pipeline-DPUQt7yL.d.cts +285 -0
  217. package/dist/resolvers.cjs +13 -0
  218. package/dist/resolvers.cjs.map +1 -0
  219. package/dist/resolvers.d.cts +111 -0
  220. package/dist/resolvers.d.ts +111 -0
  221. package/dist/resolvers.js +4 -0
  222. package/dist/resolvers.js.map +1 -0
  223. package/dist/services.cjs +21 -0
  224. package/dist/services.cjs.map +1 -0
  225. package/dist/services.d.cts +30 -0
  226. package/dist/services.d.ts +30 -0
  227. package/dist/services.js +4 -0
  228. package/dist/services.js.map +1 -0
  229. package/dist/uom.cjs +56 -0
  230. package/dist/uom.cjs.map +1 -0
  231. package/dist/uom.d.cts +224 -0
  232. package/dist/uom.d.ts +224 -0
  233. package/dist/uom.js +11 -0
  234. package/dist/uom.js.map +1 -0
  235. package/dist/utilities.cjs +32 -0
  236. package/dist/utilities.cjs.map +1 -0
  237. package/dist/utilities.d.cts +113 -0
  238. package/dist/utilities.d.ts +113 -0
  239. package/dist/utilities.js +7 -0
  240. package/dist/utilities.js.map +1 -0
  241. package/dist/variables.cjs +17 -0
  242. package/dist/variables.cjs.map +1 -0
  243. package/dist/variables.d.cts +87 -0
  244. package/dist/variables.d.ts +87 -0
  245. package/dist/variables.js +4 -0
  246. package/dist/variables.js.map +1 -0
  247. package/dist/vm.cjs +284 -0
  248. package/dist/vm.cjs.map +1 -0
  249. package/dist/vm.d.cts +68 -0
  250. package/dist/vm.d.ts +68 -0
  251. package/dist/vm.js +194 -0
  252. package/dist/vm.js.map +1 -0
  253. package/package.json +4 -4
@@ -0,0 +1,774 @@
1
+ import { DiagnosticEventType } from './chunk-G535KJEG.js';
2
+ import { buildBindingPowerTable, BindingPower, BytecodeBuilder } from './chunk-5YEMOYSE.js';
3
+ import { tokenTypeId, TokenTypes } from './chunk-RIN643A3.js';
4
+ import { getLocale } from './chunk-GOLDJNMZ.js';
5
+ import { ErrorFactory } from './chunk-6RDSQURE.js';
6
+
7
+ // src/parser/registry/ParseletRegistry.ts
8
+ function bindingPowersOf(parselet) {
9
+ return parselet;
10
+ }
11
+ var ParseletRegistry = class {
12
+ constructor() {
13
+ // String-keyed maps (kept for diagnostics + backwards compat)
14
+ this.prefixParselets = /* @__PURE__ */ new Map();
15
+ this.infixParselets = /* @__PURE__ */ new Map();
16
+ // Integer-keyed maps for parser hot path
17
+ this.prefixById = /* @__PURE__ */ new Map();
18
+ this.infixById = /* @__PURE__ */ new Map();
19
+ }
20
+ /**
21
+ * Register a prefix parselet for `tokenType`.
22
+ *
23
+ * If another parselet is already registered for this token type, it is
24
+ * silently overwritten by default (`Map.set()` semantics), the old
25
+ * parselet is simply unreachable from then on, with no error. This is
26
+ * a real footgun for third-party packages: two packages independently
27
+ * choosing the same custom token type will collide with zero signal
28
+ * about which one "won". Mirrors ResolverRegistry.register()'s and
29
+ * ExpressionEngine.registerPackage()'s existing "warn and replace"
30
+ * pattern for the same class of problem at the resolver-namespace and
31
+ * package-name levels.
32
+ *
33
+ * Note: this warns about registry-level collisions only. It does NOT
34
+ * detect the separate case where `tokenType` is one of PrecedenceParser's
35
+ * Tier-1 fast-path token types (NUMBER, STRING, IDENT, LPAREN, MINUS,
36
+ * PLUS, and the Tier-1 infix operators), those are deliberately kept
37
+ * registered here for introspection/diagnostics even though Tier-1
38
+ * always intercepts them before this registry is consulted (see
39
+ * PrecedenceParser.parsePrefix()'s docs), so warning there would
40
+ * misfire on that intentional, already-documented pattern.
41
+ */
42
+ registerPrefix(tokenType, parselet) {
43
+ const existing = this.prefixParselets.get(tokenType);
44
+ if (existing && existing !== parselet) {
45
+ console.warn(
46
+ `[ParseletRegistry] Prefix parselet for token "${tokenType}" is already registered (category: "${existing.category ?? "unknown"}"). Overwriting with a new parselet (category: "${parselet.category ?? "unknown"}") \u2014 the previous parselet is now unreachable. Two packages may be claiming the same token type.`
47
+ );
48
+ }
49
+ this.prefixParselets.set(tokenType, parselet);
50
+ this.prefixById.set(tokenTypeId(tokenType), parselet);
51
+ }
52
+ /** Register an infix parselet for `tokenType`. See {@link registerPrefix} for the collision-warning behavior this mirrors. */
53
+ registerInfix(tokenType, parselet) {
54
+ const existing = this.infixParselets.get(tokenType);
55
+ if (existing && existing !== parselet) {
56
+ console.warn(
57
+ `[ParseletRegistry] Infix parselet for token "${tokenType}" is already registered (category: "${existing.category ?? "unknown"}"). Overwriting with a new parselet (category: "${parselet.category ?? "unknown"}") \u2014 the previous parselet is now unreachable. Two packages may be claiming the same token type.`
58
+ );
59
+ }
60
+ this.infixParselets.set(tokenType, parselet);
61
+ this.infixById.set(tokenTypeId(tokenType), parselet);
62
+ }
63
+ /** Iterate all registered prefix parselets for diagnostic display. */
64
+ getAllPrefix() {
65
+ const result = [];
66
+ for (const [tokenType, parselet] of this.prefixParselets) {
67
+ result.push({
68
+ tokenType,
69
+ bindingPower: bindingPowersOf(parselet).bindingPower ?? 0,
70
+ category: parselet.category
71
+ });
72
+ }
73
+ return result;
74
+ }
75
+ /** Iterate all registered infix parselets for diagnostic display. */
76
+ getAllInfix() {
77
+ const result = [];
78
+ for (const [tokenType, parselet] of this.infixParselets) {
79
+ result.push({
80
+ tokenType,
81
+ leftBindingPower: bindingPowersOf(parselet).leftBindingPower ?? 0,
82
+ rightBindingPower: bindingPowersOf(parselet).rightBindingPower ?? 0,
83
+ category: parselet.category
84
+ });
85
+ }
86
+ return result;
87
+ }
88
+ /** Number of registered prefix parselets. */
89
+ get prefixCount() {
90
+ return this.prefixParselets.size;
91
+ }
92
+ /** Number of registered infix parselets. */
93
+ get infixCount() {
94
+ return this.infixParselets.size;
95
+ }
96
+ /**
97
+ * Get prefix parselet by string token type OR integer typeId.
98
+ * Fast path for integer IDs (Parser hot path), fallback for strings
99
+ * (diagnostics, error messages, backwards compatibility).
100
+ */
101
+ getPrefix(tokenType) {
102
+ if (typeof tokenType === "number") return this.prefixById.get(tokenType);
103
+ return this.prefixParselets.get(tokenType);
104
+ }
105
+ /**
106
+ * Get infix parselet by string token type OR integer typeId.
107
+ * Fast path for integer IDs (Parser hot path), fallback for strings.
108
+ */
109
+ getInfix(tokenType) {
110
+ if (typeof tokenType === "number") return this.infixById.get(tokenType);
111
+ return this.infixParselets.get(tokenType);
112
+ }
113
+ hasPrefix(tokenType) {
114
+ return this.prefixParselets.has(tokenType);
115
+ }
116
+ hasInfix(tokenType) {
117
+ return this.infixParselets.has(tokenType);
118
+ }
119
+ clear() {
120
+ this.prefixParselets.clear();
121
+ this.infixParselets.clear();
122
+ this.prefixById.clear();
123
+ this.infixById.clear();
124
+ }
125
+ };
126
+ var sharedParseletRegistry = new ParseletRegistry();
127
+
128
+ // src/parser/PrecedenceParser.ts
129
+ var CHAINED_DOT_THOUSANDS_GROUPS = /^\d{1,3}(\.\d{3}){2,}$/;
130
+ var _PrecedenceParser = class _PrecedenceParser {
131
+ constructor(parseletRegistry, maxDepth = 50, localeCode = "en") {
132
+ this.tokens = [];
133
+ this.current = 0;
134
+ this.depth = 0;
135
+ this.currentExpression = "";
136
+ this.registry = parseletRegistry;
137
+ this.maxDepth = maxDepth;
138
+ this.localeCode = localeCode;
139
+ }
140
+ /** Get locale code for NumberParselet to normalize separators */
141
+ getLocaleCode() {
142
+ return this.localeCode;
143
+ }
144
+ /**
145
+ * Set the diagnostic pipeline for parselet matching events.
146
+ * Cleared after each non-cached parse to avoid holding refs.
147
+ */
148
+ setDiagnosticPipeline(pipeline, expression) {
149
+ this.diagnosticPipeline = pipeline;
150
+ this.currentExpression = expression ?? "";
151
+ }
152
+ /**
153
+ * Load tokens for parsing. Identical to Parser.load().
154
+ *
155
+ * @param hasParens - if false, skips the O(n) paren balance scan (~90% of expressions)
156
+ */
157
+ load(tokens, hasParens) {
158
+ if (hasParens === false) {
159
+ this.tokens = tokens;
160
+ this.current = 0;
161
+ this.depth = 0;
162
+ return;
163
+ }
164
+ const LPAREN_ID = tokenTypeId(TokenTypes.LPAREN);
165
+ const RPAREN_ID = tokenTypeId(TokenTypes.RPAREN);
166
+ let openCount = 0;
167
+ for (let i = 0; i < tokens.length; i++) {
168
+ if (tokens[i].typeId === LPAREN_ID) openCount++;
169
+ else if (tokens[i].typeId === RPAREN_ID) openCount--;
170
+ }
171
+ this.tokens = openCount === 0 ? tokens : this.balanceParens(tokens, openCount);
172
+ this.current = 0;
173
+ this.depth = 0;
174
+ }
175
+ /**
176
+ * Auto-balance unmatched parentheses.
177
+ */
178
+ balanceParens(tokens, openCount) {
179
+ const result = tokens.slice();
180
+ if (openCount > 0) {
181
+ const RPAREN_ID = tokenTypeId(TokenTypes.RPAREN);
182
+ for (let i = 0; i < openCount; i++) {
183
+ const lastToken = tokens[tokens.length - 1];
184
+ result.push({
185
+ type: TokenTypes.RPAREN,
186
+ typeId: RPAREN_ID,
187
+ value: ")",
188
+ text: ")",
189
+ offset: lastToken ? lastToken.offset + lastToken.text.length : 0,
190
+ lineBreaks: 0,
191
+ line: lastToken ? lastToken.line : 1,
192
+ col: lastToken ? lastToken.col + lastToken.text.length : 1
193
+ });
194
+ }
195
+ } else if (openCount < 0) {
196
+ const LPAREN_ID = tokenTypeId(TokenTypes.LPAREN);
197
+ for (let i = 0; i < -openCount; i++) {
198
+ result.unshift({
199
+ type: TokenTypes.LPAREN,
200
+ typeId: LPAREN_ID,
201
+ value: "(",
202
+ text: "(",
203
+ offset: 0,
204
+ lineBreaks: 0,
205
+ line: 1,
206
+ col: 1
207
+ });
208
+ }
209
+ }
210
+ return result;
211
+ }
212
+ // ═══════════════════════════════════════════════════════════════════════════════
213
+ // Main entry point: precedence climbing parse expression
214
+ // ═══════════════════════════════════════════════════════════════════════════════
215
+ /**
216
+ * Parse a full expression starting at the current token position.
217
+ *
218
+ * @param minBp - minimum binding power (precedence climbing threshold).
219
+ * For left-associative operators, the recursive call uses `bp + 1`.
220
+ * For right-associative operators (^), it uses `bp`.
221
+ * @param _builder - accepted for parselet API compatibility; always uses `this.builder`.
222
+ */
223
+ parseExpression(minBp = 0, _builder) {
224
+ if (_builder) {
225
+ this.builder = _builder;
226
+ }
227
+ this.depth++;
228
+ if (this.depth > this.maxDepth) {
229
+ this.depth--;
230
+ throw ErrorFactory.parsing(
231
+ "NESTING_DEPTH_EXCEEDED",
232
+ `Parse nesting depth ${this.depth} exceeds maximum of ${this.maxDepth}`,
233
+ { maxDepth: this.maxDepth, currentDepth: this.depth }
234
+ );
235
+ }
236
+ const token = this.consume();
237
+ if (!token) {
238
+ this.depth--;
239
+ throw ErrorFactory.parsing("UNEXPECTED_END", "Unexpected end of expression");
240
+ }
241
+ this.parsePrefix(token);
242
+ const tokens = this.tokens;
243
+ const len = tokens.length;
244
+ const bpTable = _PrecedenceParser.BP_TABLE;
245
+ const registry = this.registry;
246
+ const builder = this.builder;
247
+ let idx = this.current;
248
+ while (idx < len) {
249
+ const lookahead = tokens[idx];
250
+ if (!lookahead) break;
251
+ const bp = bpTable[lookahead.typeId];
252
+ if (bp > 0) {
253
+ if (bp <= minBp) break;
254
+ if (this.diagnosticPipeline) {
255
+ const infixParselet = registry.getInfix(lookahead.typeId);
256
+ if (infixParselet) {
257
+ this.fireParseletMatched(infixParselet, lookahead, false, bp);
258
+ }
259
+ }
260
+ this.current = ++idx;
261
+ const typeId = lookahead.typeId;
262
+ if (typeId === _PrecedenceParser.PERCENT_ID) {
263
+ builder.emitOpcode(10 /* PUSH_NUMBER */);
264
+ builder.emitNumber(100);
265
+ builder.emitOpcode(23 /* DIV */);
266
+ } else if (typeId === _PrecedenceParser.CARET_ID && this.tryEmitMatrixCaretOp(builder)) ; else {
267
+ const rightBp = typeId === _PrecedenceParser.CARET_ID ? bp : bp + 1;
268
+ this.parseExpression(rightBp, builder);
269
+ builder.emitOpcode(_PrecedenceParser.INFIX_OPCODE[typeId]);
270
+ }
271
+ } else {
272
+ const infixParselet = registry.getInfix(lookahead.typeId);
273
+ if (!infixParselet) break;
274
+ if (infixParselet.bindingPower <= minBp) break;
275
+ this.current = ++idx;
276
+ if (this.diagnosticPipeline) {
277
+ this.fireParseletMatched(infixParselet, lookahead, false, infixParselet.bindingPower);
278
+ }
279
+ infixParselet.parse(this, token, lookahead, builder);
280
+ }
281
+ idx = this.current;
282
+ }
283
+ this.current = idx;
284
+ this.depth--;
285
+ }
286
+ // ═══════════════════════════════════════════════════════════════════════════════
287
+ // Prefix dispatch: inline switch for built-ins, parselet registry for plugins
288
+ // ═══════════════════════════════════════════════════════════════════════════════
289
+ /**
290
+ * Parse a prefix token. Built-in tokens (NUMBER, IDENT, LPAREN, etc.) are
291
+ * handled inline with zero registry lookup. Everything else falls through
292
+ * to the parselet registry (Tier 2).
293
+ */
294
+ parsePrefix(token) {
295
+ const builder = this.builder;
296
+ const typeId = token.typeId;
297
+ if (this.diagnosticPipeline) {
298
+ const parselet = this.registry.getPrefix(typeId);
299
+ if (parselet) {
300
+ this.fireParseletMatched(parselet, token, true);
301
+ }
302
+ }
303
+ switch (typeId) {
304
+ // ── Numeric literals ──────────────────────────────────────────────────
305
+ case _PrecedenceParser.NUMBER_ID: {
306
+ let v;
307
+ const raw = token.value;
308
+ if (raw.startsWith("0x") || raw.startsWith("0X")) {
309
+ v = parseInt(raw, 16);
310
+ if (Number.isNaN(v)) {
311
+ throw ErrorFactory.parsing("INVALID_NUMBER_LITERAL", `Invalid hex literal: "${raw}"`, { raw });
312
+ }
313
+ } else if (raw.startsWith("0b") || raw.startsWith("0B")) {
314
+ v = parseInt(raw.slice(2), 2);
315
+ if (Number.isNaN(v)) {
316
+ throw ErrorFactory.parsing("INVALID_NUMBER_LITERAL", `Invalid binary literal: "${raw}"`, { raw });
317
+ }
318
+ } else if (raw.startsWith("0o") || raw.startsWith("0O")) {
319
+ v = parseInt(raw.slice(2), 8);
320
+ if (Number.isNaN(v)) {
321
+ throw ErrorFactory.parsing("INVALID_NUMBER_LITERAL", `Invalid octal literal: "${raw}"`, { raw });
322
+ }
323
+ } else if (CHAINED_DOT_THOUSANDS_GROUPS.test(raw)) {
324
+ v = parseFloat(raw.split(".").join(""));
325
+ } else {
326
+ const locale = getLocale(this.localeCode);
327
+ const decimalSep = locale.display.decimalSeparator;
328
+ const thousandsSep = locale.display.thousandsSeparator;
329
+ let normalized = raw;
330
+ if (thousandsSep) {
331
+ normalized = normalized.split(thousandsSep).join("");
332
+ }
333
+ if (decimalSep && decimalSep !== ".") {
334
+ normalized = normalized.replace(decimalSep, ".");
335
+ }
336
+ v = parseFloat(normalized);
337
+ }
338
+ builder.emitOpcode(10 /* PUSH_NUMBER */);
339
+ builder.emitNumber(v);
340
+ return;
341
+ }
342
+ case _PrecedenceParser.BIGINT_ID: {
343
+ let raw = token.value;
344
+ if (raw.endsWith("n")) raw = raw.slice(0, -1);
345
+ builder.emitOpcode(11 /* PUSH_BIGINT */);
346
+ builder.emitString(raw);
347
+ return;
348
+ }
349
+ case _PrecedenceParser.STRING_ID: {
350
+ builder.emitOpcode(13 /* PUSH_STRING */);
351
+ builder.emitString(token.value);
352
+ return;
353
+ }
354
+ // ── Identifiers (variables) ────────────────────────────────────────────
355
+ case _PrecedenceParser.IDENT_ID: {
356
+ if (this.peek()?.typeId === _PrecedenceParser.LPAREN_ID) {
357
+ this.parseUserFunctionDefOrCall(token, builder);
358
+ return;
359
+ }
360
+ builder.emitOpcode(60 /* LOAD_VAR */);
361
+ builder.emitString(token.value);
362
+ return;
363
+ }
364
+ // ── Grouping / bare-tuple vector literal ─────────────────────────────────
365
+ // `(expr)` groups for precedence; `(x, y[, z[, w]])` is the bare-tuple
366
+ // vector literal documented as an alternative to vec2/vec3/vec4(...)
367
+ // (wiki: Arithmetic/Vector). This Tier-1 case is what actually runs for
368
+ // every LPAREN in production parsing, GroupParselet.ts mirrors this
369
+ // logic for registry-introspection/diagnostic-listing purposes, but a
370
+ // package can never override LPAREN's dispatch here (Tier 1 always
371
+ // wins over the ParseletRegistry fallback below), so both must be kept
372
+ // in sync by hand.
373
+ case _PrecedenceParser.LPAREN_ID: {
374
+ this.parseExpression(0, builder);
375
+ let count = 1;
376
+ while (this.match(TokenTypes.COMMA)) {
377
+ this.parseExpression(0, builder);
378
+ count++;
379
+ }
380
+ this.consume(TokenTypes.RPAREN);
381
+ if (count > 1) {
382
+ builder.emitOpcode(152 /* MAT_NEW */);
383
+ builder.emitIndex(1);
384
+ builder.emitIndex(count);
385
+ }
386
+ return;
387
+ }
388
+ // ── Unary operators ───────────────────────────────────────────────────
389
+ case _PrecedenceParser.MINUS_ID: {
390
+ this.parseExpression(BindingPower.Prefix, builder);
391
+ builder.emitOpcode(26 /* NEG */);
392
+ return;
393
+ }
394
+ case _PrecedenceParser.PLUS_ID: {
395
+ this.parseExpression(BindingPower.Prefix, builder);
396
+ builder.emitOpcode(27 /* POS */);
397
+ return;
398
+ }
399
+ // ── Keyword constants ─────────────────────────────────────────────────
400
+ case _PrecedenceParser.KEYWORD_ID: }
401
+ const prefixParselet = this.registry.getPrefix(typeId);
402
+ if (!prefixParselet) {
403
+ throw ErrorFactory.parsing(
404
+ "NO_PREFIX_PARSELET",
405
+ `No prefix parselet found for token: ${token.type} ("${token.value}")`,
406
+ { tokenType: token.type, tokenValue: token.value }
407
+ );
408
+ }
409
+ prefixParselet.parse(this, token, builder);
410
+ }
411
+ /**
412
+ * After a `^` token (already consumed by the Tier-1 infix loop above),
413
+ * checks {@link CARET_SUFFIX_RULES} in order for a shape that means
414
+ * something other than ordinary exponentiation: `^T` (transpose) and `^-1`
415
+ * (matrix inverse, LITERALLY the integer exponent `-1`; `^-2`, `^-1.5`
416
+ * etc. still mean ordinary exponentiation). On a match, the rule's `emit`
417
+ * consumes that shape's own tokens and this returns `true`. On no match,
418
+ * consumes NOTHING, returning `false` so the caller falls through to
419
+ * ordinary `EXP` parsing.
420
+ *
421
+ * Every rule dispatches purely on SHAPE, never on operand type (unknowable
422
+ * at parse time): `inv()`'s own handler (`VMBuiltins.ts` index 65) returns
423
+ * `1/x` for a plain Number, byte-identical to what `Math.pow(x, -1)`
424
+ * already computed for `x^-1` before this feature existed, and a real
425
+ * matrix inverse for a Matrix, so `5^-1` still means exactly what it always
426
+ * has; only a Matrix operand actually inverts.
427
+ *
428
+ * Adding a future `^`-suffix shape is a new table entry here, not a new
429
+ * if-block. See the "Tier-1 shape-exception" pattern write-up above for
430
+ * why this table can't instead be a package-registered parselet.
431
+ */
432
+ tryEmitMatrixCaretOp(builder) {
433
+ for (const rule of _PrecedenceParser.CARET_SUFFIX_RULES) {
434
+ if (rule.matches(this)) {
435
+ rule.emit(this, builder);
436
+ return true;
437
+ }
438
+ }
439
+ return false;
440
+ }
441
+ // ═══════════════════════════════════════════════════════════════════════════════
442
+ // User-defined, parameterized, reusable functions (f(x) = 2*x + 1, then f(5))
443
+ // ═══════════════════════════════════════════════════════════════════════════════
444
+ /**
445
+ * From an LPAREN token index, scan forward tracking paren depth and
446
+ * return the index of the matching RPAREN, or `null` if the parens never
447
+ * balance before the token stream ends. No emission, no position
448
+ * advance. Same class of technique as {@link balanceParens}'s own
449
+ * pre-scan, just exposed mid-parse instead of only at `load()` time.
450
+ * `openIdx` must point AT the LPAREN itself.
451
+ */
452
+ findMatchingRParen(openIdx) {
453
+ let depth = 0;
454
+ for (let i = openIdx; i < this.tokens.length; i++) {
455
+ const t = this.tokens[i];
456
+ if (t.typeId === _PrecedenceParser.LPAREN_ID) depth++;
457
+ else if (t.typeId === _PrecedenceParser.RPAREN_ID) {
458
+ depth--;
459
+ if (depth === 0) return i;
460
+ }
461
+ }
462
+ return null;
463
+ }
464
+ /**
465
+ * Disambiguates a bare `IDENT` immediately followed by `(` between three
466
+ * things, using ONLY a bracket-depth scan (no backtracking, nothing is
467
+ * consumed until the shape is known):
468
+ * - `f(x) = expr`, a DEFINITION: the matching `)` is followed by `=`.
469
+ * - `f(5)`, a CALL to a (possibly not-yet-defined) function: anything
470
+ * else. This was never valid syntax before this feature (a bare
471
+ * `IDENT` immediately before `(` has no pre-existing "plain variable
472
+ * read + separately grouped `(...)`" meaning to preserve, confirmed
473
+ * via `BuiltinNormalizerRules.ts`'s `implicitMultiplyRule()`, which
474
+ * only inserts an implicit `*` for `NUMBER/RPAREN` immediately before
475
+ * `IDENT/LPAREN`, never for a bare `IDENT` immediately before
476
+ * `LPAREN`). Always commits to a CALL; an unregistered name produces a
477
+ * clear `UNDEFINED_FUNCTION` error at VM-execution time, the same
478
+ * forward-reference philosophy `LOAD_VAR`/`UNDEFINED_VARIABLE` already
479
+ * uses, rather than any parse-time registry lookup.
480
+ * If the parens never balance, this falls through to the ordinary
481
+ * `LOAD_VAR` path (a malformed expression surfaces its own parse error
482
+ * shortly after, from the normal expression grammar).
483
+ */
484
+ parseUserFunctionDefOrCall(nameToken, builder) {
485
+ const closeIdx = this.findMatchingRParen(this.current);
486
+ if (closeIdx === null) {
487
+ builder.emitOpcode(60 /* LOAD_VAR */);
488
+ builder.emitString(nameToken.value);
489
+ return;
490
+ }
491
+ const afterClose = this.tokens[closeIdx + 1];
492
+ if (afterClose?.type === TokenTypes.EQUALS) {
493
+ this.parseUserFunctionDefinition(nameToken, builder);
494
+ } else {
495
+ this.parseUserFunctionCall(nameToken, builder);
496
+ }
497
+ }
498
+ /**
499
+ * A parameter name, accepted as either `IDENT` or `UNIT`, matches this
500
+ * codebase's established `:name = value` variable-name policy
501
+ * (`VariableParselet.ts` explicitly accepts `UNIT`-typed tokens too, e.g.
502
+ * `:b = 5` for the "b" bits unit) since common short parameter names
503
+ * like `h`/`l`/`b`/`t`/`s`/`m` collide with real unit abbreviations
504
+ * (hour, liter, bits, ton, second, meter, ...) and lex as `UNIT`, not
505
+ * `IDENT`.
506
+ */
507
+ consumeParamName() {
508
+ const token = this.peek();
509
+ if (token?.type === TokenTypes.IDENT || token?.type === TokenTypes.UNIT) {
510
+ this.consume();
511
+ return token.value;
512
+ }
513
+ throw ErrorFactory.parsing(
514
+ "USER_FUNCTION_INVALID_PARAM_NAME",
515
+ `Expected a parameter name but got "${token?.type ?? "end of input"}"${token ? ` ("${token.value}")` : ""}`,
516
+ { actualType: token?.type }
517
+ );
518
+ }
519
+ parseUserFunctionDefinition(nameToken, builder) {
520
+ this.consume(TokenTypes.LPAREN);
521
+ const params = [];
522
+ if (this.peek()?.type !== TokenTypes.RPAREN) {
523
+ params.push(this.consumeParamName());
524
+ while (this.match(TokenTypes.COMMA)) {
525
+ params.push(this.consumeParamName());
526
+ }
527
+ }
528
+ this.consume(TokenTypes.RPAREN);
529
+ this.consume(TokenTypes.EQUALS);
530
+ if (params.length === 0) {
531
+ throw ErrorFactory.parsing(
532
+ "USER_FUNCTION_NO_PARAMS",
533
+ `"${nameToken.value}()" has no parameters -- user-defined functions need at least one (a zero-argument definition is indistinguishable from a plain function CALL with no args, which this grammar doesn't otherwise support)`,
534
+ { name: nameToken.value }
535
+ );
536
+ }
537
+ const bodyBuilder = new BytecodeBuilder();
538
+ this.parseExpression(BindingPower.Lowest, bodyBuilder);
539
+ this.setBuilder(builder);
540
+ const bodyProgram = bodyBuilder.build();
541
+ if (bodyProgram.hasAsync) {
542
+ throw ErrorFactory.parsing(
543
+ "FUNCTION_BODY_MUST_BE_SYNCHRONOUS",
544
+ `"${nameToken.value}(...)"'s body calls an async operation (weather, stocks, currency, ...) \u2014 user-defined function bodies must be synchronous`,
545
+ { name: nameToken.value }
546
+ );
547
+ }
548
+ const bodyIdx = builder.emitUserFunctionBody(nameToken.value, params, bodyProgram);
549
+ builder.emitOpcode(150 /* DEFINE_USER_FUNCTION */);
550
+ builder.emitIndex(bodyIdx);
551
+ builder.emitOpcode(13 /* PUSH_STRING */);
552
+ builder.emitString(`${nameToken.value}(${params.join(", ")}) defined`);
553
+ }
554
+ parseUserFunctionCall(nameToken, builder) {
555
+ this.consume(TokenTypes.LPAREN);
556
+ let argCount = 0;
557
+ if (this.peek()?.type !== TokenTypes.RPAREN) {
558
+ this.parseExpression(BindingPower.Lowest, builder);
559
+ argCount++;
560
+ while (this.match(TokenTypes.COMMA)) {
561
+ this.parseExpression(BindingPower.Lowest, builder);
562
+ argCount++;
563
+ }
564
+ }
565
+ this.consume(TokenTypes.RPAREN);
566
+ builder.emitOpcode(151 /* CALL_USER_FUNCTION */);
567
+ builder.emitString(nameToken.value);
568
+ builder.emitByte(argCount);
569
+ }
570
+ // ═══════════════════════════════════════════════════════════════════════════════
571
+ // Token stream navigation (identical API to Parser)
572
+ // ═══════════════════════════════════════════════════════════════════════════════
573
+ consume(expectedType) {
574
+ const token = this.tokens[this.current];
575
+ if (!token) {
576
+ throw ErrorFactory.parsing(
577
+ "UNEXPECTED_END_OF_INPUT",
578
+ "Unexpected end of input"
579
+ );
580
+ }
581
+ if (expectedType !== void 0) {
582
+ const expectedId = tokenTypeId(expectedType);
583
+ if (token.typeId !== expectedId) {
584
+ throw ErrorFactory.parsing(
585
+ "UNEXPECTED_TOKEN_TYPE",
586
+ `Expected token type "${expectedType}" but got "${token.type}" ("${token.value}")`,
587
+ { expectedType, actualType: token.type, actualValue: token.value }
588
+ );
589
+ }
590
+ }
591
+ this.current++;
592
+ return token;
593
+ }
594
+ match(expectedType) {
595
+ const token = this.peek();
596
+ if (token && token.typeId === tokenTypeId(expectedType)) {
597
+ this.advance();
598
+ return true;
599
+ }
600
+ return false;
601
+ }
602
+ peek() {
603
+ return this.tokens[this.current];
604
+ }
605
+ /**
606
+ * Read-only lookahead `offset` tokens past the current position, without
607
+ * consuming anything, `peekAt(0)` is equivalent to {@link peek}.
608
+ * `this.tokens` is a plain in-memory array (not a stream), so this is a
609
+ * simple, safe index read; no rewind/checkpoint mechanism is needed since
610
+ * nothing is consumed.
611
+ */
612
+ peekAt(offset) {
613
+ return this.tokens[this.current + offset];
614
+ }
615
+ previous() {
616
+ return this.current > 0 ? this.tokens[this.current - 1] : void 0;
617
+ }
618
+ advance() {
619
+ this.current++;
620
+ }
621
+ // ═══════════════════════════════════════════════════════════════════════════════
622
+ // Builder access, set before parse, read by inline prefix handlers
623
+ // ═══════════════════════════════════════════════════════════════════════════════
624
+ /** Set the builder to use for the current parse. Called by ExpressionEngine. */
625
+ setBuilder(builder) {
626
+ this.builder = builder;
627
+ }
628
+ // ═══════════════════════════════════════════════════════════════════════════════
629
+ // Diagnostics
630
+ // ═══════════════════════════════════════════════════════════════════════════════
631
+ fireParseletMatched(parselet, token, isPrefix, bindingPower) {
632
+ const pipeline = this.diagnosticPipeline;
633
+ const event = {
634
+ type: DiagnosticEventType.ParseletMatched,
635
+ elapsedNs: 0,
636
+ expression: this.currentExpression,
637
+ tokenType: token.type,
638
+ tokenValue: token.value,
639
+ parseletCategory: parselet.category,
640
+ parseletType: parselet.constructor?.name ?? "unknown",
641
+ isPrefix,
642
+ bindingPower,
643
+ tokenOffset: token.offset || 0
644
+ };
645
+ pipeline.fireParseletMatched(event);
646
+ }
647
+ };
648
+ /**
649
+ * Static binding power table, built once at module load, shared across all instances.
650
+ * Index = tokenTypeId, value = binding power (0 = not a built-in infix).
651
+ */
652
+ _PrecedenceParser.BP_TABLE = buildBindingPowerTable();
653
+ // ── Pre-computed token type IDs for inline dispatch ────────────────────────
654
+ // Prefix built-ins
655
+ _PrecedenceParser.NUMBER_ID = tokenTypeId(TokenTypes.NUMBER);
656
+ _PrecedenceParser.BIGINT_ID = tokenTypeId(TokenTypes.BIGINT);
657
+ _PrecedenceParser.STRING_ID = tokenTypeId(TokenTypes.STRING);
658
+ _PrecedenceParser.IDENT_ID = tokenTypeId(TokenTypes.IDENT);
659
+ _PrecedenceParser.LPAREN_ID = tokenTypeId(TokenTypes.LPAREN);
660
+ _PrecedenceParser.RPAREN_ID = tokenTypeId(TokenTypes.RPAREN);
661
+ _PrecedenceParser.MINUS_ID = tokenTypeId(TokenTypes.MINUS);
662
+ _PrecedenceParser.PLUS_ID = tokenTypeId(TokenTypes.PLUS);
663
+ _PrecedenceParser.KEYWORD_ID = tokenTypeId(TokenTypes.KEYWORD);
664
+ // Infix built-ins (Tier 1, full inline emission)
665
+ _PrecedenceParser.STAR_ID = tokenTypeId(TokenTypes.STAR);
666
+ _PrecedenceParser.SLASH_ID = tokenTypeId(TokenTypes.SLASH);
667
+ _PrecedenceParser.MOD_ID = tokenTypeId(TokenTypes.MOD);
668
+ _PrecedenceParser.CARET_ID = tokenTypeId(TokenTypes.CARET);
669
+ _PrecedenceParser.LSHIFT_ID = tokenTypeId(TokenTypes.LSHIFT);
670
+ _PrecedenceParser.RSHIFT_ID = tokenTypeId(TokenTypes.RSHIFT);
671
+ _PrecedenceParser.BIT_AND_ID = tokenTypeId(TokenTypes.BIT_AND);
672
+ _PrecedenceParser.BIT_OR_ID = tokenTypeId(TokenTypes.BIT_OR);
673
+ _PrecedenceParser.BIT_XOR_ID = tokenTypeId(TokenTypes.BIT_XOR);
674
+ _PrecedenceParser.PERCENT_ID = tokenTypeId(TokenTypes.PERCENT);
675
+ _PrecedenceParser.OF_ID = tokenTypeId(TokenTypes.OF);
676
+ /**
677
+ * Inline opcode map for Tier 1 infix operators.
678
+ * tokenTypeId → OpCode. PERCENT and CARET are handled specially (not in this map).
679
+ */
680
+ _PrecedenceParser.INFIX_OPCODE = {
681
+ [tokenTypeId(TokenTypes.PLUS)]: 20 /* ADD */,
682
+ [tokenTypeId(TokenTypes.MINUS)]: 21 /* SUB */,
683
+ [tokenTypeId(TokenTypes.STAR)]: 22 /* MUL */,
684
+ [tokenTypeId(TokenTypes.SLASH)]: 23 /* DIV */,
685
+ [tokenTypeId(TokenTypes.MOD)]: 24 /* MOD */,
686
+ [tokenTypeId(TokenTypes.CARET)]: 25 /* EXP */,
687
+ [tokenTypeId(TokenTypes.LSHIFT)]: 30 /* LSHIFT */,
688
+ [tokenTypeId(TokenTypes.RSHIFT)]: 31 /* RSHIFT */,
689
+ [tokenTypeId(TokenTypes.BIT_AND)]: 33 /* BIT_AND */,
690
+ [tokenTypeId(TokenTypes.BIT_OR)]: 34 /* BIT_OR */,
691
+ [tokenTypeId(TokenTypes.BIT_XOR)]: 35 /* BIT_XOR */,
692
+ [tokenTypeId(TokenTypes.OF)]: 22 /* MUL */
693
+ };
694
+ // ═══════════════════════════════════════════════════════════════════════════════
695
+ // The "Tier-1 shape-exception" pattern
696
+ // ═══════════════════════════════════════════════════════════════════════════════
697
+ //
698
+ // Tier 1 exists purely for performance, avoid a Map lookup + parselet call
699
+ // on ~95% of tokens (see this file's own module doc comment above). A
700
+ // recurring consequence: any grammar shape tied to a Tier-1 token type can
701
+ // ONLY be implemented by hand-editing this switch, since a package-
702
+ // registered ParseletRegistry parselet for that same token type would
703
+ // simply never run in production (Tier 1 always wins over the registry
704
+ // fallback). Five confirmed instances of this tension exist in this file
705
+ // (plus one that had to move OUTSIDE it entirely):
706
+ // 1. NUMBER_ID, the full locale-aware number-literal parsing (hex/
707
+ // binary/octal, chained-dot thousands grouping, decimal-separator
708
+ // normalization) is inlined in parsePrefix() above; NumberParselet.ts's
709
+ // own copy is dead code for real evaluation, kept registered only for
710
+ // the "matched parselets" diagnostic view.
711
+ // 2. IDENT_ID, parseUserFunctionDefOrCall's lookahead (below) disambig-
712
+ // uates a function DEFINITION from a CALL from a plain variable load.
713
+ // 3. LPAREN_ID, the bare-tuple vector-literal sugar ("(x,y[,z[,w]])" ->
714
+ // MAT_NEW) is inlined in parsePrefix() above; GroupParselet.ts mirrors
715
+ // it for introspection only and must be kept in sync by hand.
716
+ // 4. CARET_ID, the transpose/inverse suffix table just below.
717
+ // 5. PLUS_ID, the locale word "and" lexes as PLUS (Tier-1, fixed Sum
718
+ // binding power), so a registry parselet can never intercept it the
719
+ // way LogicalParselet.ts intercepts "or"/&&/||; unlike the other four,
720
+ // there's no SHAPE to special-case on here (the token itself IS the
721
+ // operator), so the workaround lives entirely outside this file, as a
722
+ // runtime Boolean/Boolean type-check inside vm/VM.ts's OpCode.ADD
723
+ // handler instead.
724
+ //
725
+ // Deliberate non-goal: none of this is exposed as an IEnginePackage
726
+ // extension point (unlike prefixParselets/infixParselets). Letting
727
+ // third-party packages register their own Tier-1 shape-matchers would put
728
+ // per-package matcher iteration on a genuinely hot path (every CARET/
729
+ // NUMBER/IDENT/LPAREN/PLUS token, matched or not), a materially bigger
730
+ // separate product decision than "make this one table more concise," and
731
+ // not one this pattern write-up makes.
732
+ /**
733
+ * A single `^`-suffix shape: `matches` peeks ahead (consuming nothing) to
734
+ * check whether this shape starts at the current position; `emit` is only
735
+ * called immediately after `matches` returned true for that SAME position,
736
+ * and is responsible for consuming that shape's own trailing tokens and
737
+ * emitting its bytecode.
738
+ */
739
+ _PrecedenceParser.CARET_SUFFIX_RULES = [
740
+ {
741
+ name: "transpose (^T)",
742
+ matches: (p) => {
743
+ const next = p.peek();
744
+ return next?.type === TokenTypes.IDENT && next.value === "T";
745
+ },
746
+ emit: (p, builder) => {
747
+ p.advance();
748
+ builder.emitOpcode(51 /* CALL_BUILTIN */);
749
+ builder.emitIndex(63);
750
+ builder.emitIndex(1);
751
+ }
752
+ },
753
+ {
754
+ name: "inverse (^-1)",
755
+ matches: (p) => {
756
+ const next = p.peek();
757
+ if (next?.typeId !== _PrecedenceParser.MINUS_ID) return false;
758
+ const afterMinus = p.peekAt(1);
759
+ return afterMinus?.type === TokenTypes.NUMBER && afterMinus.value === "1";
760
+ },
761
+ emit: (p, builder) => {
762
+ p.advance();
763
+ p.advance();
764
+ builder.emitOpcode(51 /* CALL_BUILTIN */);
765
+ builder.emitIndex(65);
766
+ builder.emitIndex(1);
767
+ }
768
+ }
769
+ ];
770
+ var PrecedenceParser = _PrecedenceParser;
771
+
772
+ export { ParseletRegistry, PrecedenceParser, sharedParseletRegistry };
773
+ //# sourceMappingURL=chunk-SDGRK7EP.js.map
774
+ //# sourceMappingURL=chunk-SDGRK7EP.js.map