@jplorg/jpl 2.0.0 → 2.1.1

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 (320) hide show
  1. package/dist/esm/main.js +3474 -0
  2. package/dist/types/applyDefaults.d.ts +2 -0
  3. package/dist/types/builtins/funcContains.d.ts +3 -0
  4. package/dist/types/builtins/funcEndsWith.d.ts +3 -0
  5. package/dist/types/builtins/funcError.d.ts +3 -0
  6. package/dist/types/builtins/funcFromJSON.d.ts +3 -0
  7. package/dist/types/builtins/funcHas.d.ts +3 -0
  8. package/dist/types/builtins/funcIn.d.ts +3 -0
  9. package/dist/types/builtins/funcKeys.d.ts +3 -0
  10. package/dist/types/builtins/funcLength.d.ts +3 -0
  11. package/dist/types/builtins/funcNow.d.ts +3 -0
  12. package/dist/types/builtins/funcStartsWith.d.ts +3 -0
  13. package/dist/types/builtins/funcToJSON.d.ts +3 -0
  14. package/dist/types/builtins/funcToLowerCase.d.ts +3 -0
  15. package/dist/types/builtins/funcToNumber.d.ts +3 -0
  16. package/dist/types/builtins/funcToString.d.ts +3 -0
  17. package/dist/types/builtins/funcToUpperCase.d.ts +3 -0
  18. package/dist/types/builtins/funcTrim.d.ts +3 -0
  19. package/dist/types/builtins/funcTrimEnd.d.ts +3 -0
  20. package/dist/types/builtins/funcTrimStart.d.ts +3 -0
  21. package/dist/types/builtins/funcType.d.ts +3 -0
  22. package/dist/types/builtins/funcVoid.d.ts +3 -0
  23. package/dist/types/builtins/index.d.ts +2 -0
  24. package/dist/types/builtins/internals.d.ts +1 -0
  25. package/dist/types/builtins/math.d.ts +17 -0
  26. package/dist/types/createSingleton.d.ts +1 -0
  27. package/dist/types/interpreter/index.d.ts +21 -0
  28. package/dist/types/interpreter/parse.d.ts +194 -0
  29. package/{src/interpreter/parseFunction.js → dist/types/interpreter/parseFunction.d.ts} +2 -10
  30. package/dist/types/interpreter/system.d.ts +3 -0
  31. package/dist/types/interpreter/util.d.ts +89 -0
  32. package/dist/types/library/apply.d.ts +32 -0
  33. package/dist/types/library/definition.d.ts +79 -0
  34. package/dist/types/library/errors/adaptErrors.d.ts +6 -0
  35. package/dist/types/library/errors/enclosure.d.ts +6 -0
  36. package/dist/types/library/errors/error.d.ts +6 -0
  37. package/dist/types/library/errors/execution.d.ts +12 -0
  38. package/dist/types/library/errors/fatal.d.ts +4 -0
  39. package/dist/types/library/errors/runtime.d.ts +46 -0
  40. package/dist/types/library/errors/syntax.d.ts +7 -0
  41. package/{src/library/functions.js → dist/types/library/functions.d.ts} +9 -30
  42. package/dist/types/library/index.d.ts +19 -0
  43. package/dist/types/library/mux.d.ts +19 -0
  44. package/{src/library/ops/op.js → dist/types/library/ops/op.d.ts} +25 -49
  45. package/{src/library/ops/opa.js → dist/types/library/ops/opa.d.ts} +4 -7
  46. package/{src/library/ops/opc.js → dist/types/library/ops/opc.d.ts} +6 -11
  47. package/{src/library/ops/opm.js → dist/types/library/ops/opm.d.ts} +5 -9
  48. package/{src/library/ops/opu.js → dist/types/library/ops/opu.d.ts} +8 -15
  49. package/dist/types/library/runtimeScope.d.ts +23 -0
  50. package/dist/types/library/runtimeSignal.d.ts +25 -0
  51. package/dist/types/library/types.d.ts +97 -0
  52. package/dist/types/main.d.ts +19 -0
  53. package/dist/types/program/handler.d.ts +11 -0
  54. package/dist/types/program/index.d.ts +40 -0
  55. package/dist/types/program/ops/index.d.ts +5 -0
  56. package/dist/types/program/ops/opAccess/index.d.ts +10 -0
  57. package/dist/types/program/ops/opAccess/opaField.d.ts +10 -0
  58. package/dist/types/program/ops/opAccess/opaFunction.d.ts +10 -0
  59. package/dist/types/program/ops/opAccess/opaIter.d.ts +10 -0
  60. package/dist/types/program/ops/opAccess/opaSlice.d.ts +10 -0
  61. package/dist/types/program/ops/opAnd.d.ts +10 -0
  62. package/dist/types/program/ops/opArrayConstructor.d.ts +10 -0
  63. package/dist/types/program/ops/opAssignment/index.d.ts +10 -0
  64. package/dist/types/program/ops/opAssignment/opaAssignField.d.ts +10 -0
  65. package/dist/types/program/ops/opAssignment/opaAssignIter.d.ts +10 -0
  66. package/dist/types/program/ops/opAssignment/opaAssignSlice.d.ts +10 -0
  67. package/dist/types/program/ops/opAssignment/opuAddition.d.ts +10 -0
  68. package/dist/types/program/ops/opAssignment/opuDivision.d.ts +10 -0
  69. package/dist/types/program/ops/opAssignment/opuMultiplication.d.ts +10 -0
  70. package/dist/types/program/ops/opAssignment/opuNullCoalescence.d.ts +10 -0
  71. package/dist/types/program/ops/opAssignment/opuRemainder.d.ts +10 -0
  72. package/dist/types/program/ops/opAssignment/opuSet.d.ts +10 -0
  73. package/dist/types/program/ops/opAssignment/opuSubtraction.d.ts +10 -0
  74. package/dist/types/program/ops/opAssignment/opuUpdate.d.ts +10 -0
  75. package/dist/types/program/ops/opCalculation/index.d.ts +10 -0
  76. package/dist/types/program/ops/opCalculation/opmAddition.d.ts +10 -0
  77. package/dist/types/program/ops/opCalculation/opmDivision.d.ts +10 -0
  78. package/dist/types/program/ops/opCalculation/opmMultiplication.d.ts +10 -0
  79. package/dist/types/program/ops/opCalculation/opmRemainder.d.ts +10 -0
  80. package/dist/types/program/ops/opCalculation/opmSubtraction.d.ts +10 -0
  81. package/dist/types/program/ops/opComparison/index.d.ts +10 -0
  82. package/dist/types/program/ops/opComparison/opcEqual.d.ts +10 -0
  83. package/dist/types/program/ops/opComparison/opcGreater.d.ts +10 -0
  84. package/dist/types/program/ops/opComparison/opcGreaterEqual.d.ts +10 -0
  85. package/dist/types/program/ops/opComparison/opcLess.d.ts +10 -0
  86. package/dist/types/program/ops/opComparison/opcLessEqual.d.ts +10 -0
  87. package/dist/types/program/ops/opComparison/opcUnequal.d.ts +10 -0
  88. package/dist/types/program/ops/opConstant.d.ts +10 -0
  89. package/dist/types/program/ops/opConstantFalse.d.ts +10 -0
  90. package/dist/types/program/ops/opConstantNull.d.ts +10 -0
  91. package/dist/types/program/ops/opConstantTrue.d.ts +10 -0
  92. package/dist/types/program/ops/opFunctionDefinition.d.ts +10 -0
  93. package/dist/types/program/ops/opIf.d.ts +10 -0
  94. package/dist/types/program/ops/opInterpolatedString.d.ts +10 -0
  95. package/dist/types/program/ops/opNegation.d.ts +10 -0
  96. package/dist/types/program/ops/opNot.d.ts +10 -0
  97. package/dist/types/program/ops/opNullCoalescence.d.ts +10 -0
  98. package/dist/types/program/ops/opNumber.d.ts +10 -0
  99. package/dist/types/program/ops/opObjectConstructor.d.ts +10 -0
  100. package/dist/types/program/ops/opOr.d.ts +10 -0
  101. package/dist/types/program/ops/opOutputConcat.d.ts +10 -0
  102. package/dist/types/program/ops/opString.d.ts +10 -0
  103. package/dist/types/program/ops/opTry.d.ts +10 -0
  104. package/dist/types/program/ops/opVariable.d.ts +10 -0
  105. package/dist/types/program/ops/opVariableDefinition.d.ts +10 -0
  106. package/dist/types/program/ops/opVoid.d.ts +10 -0
  107. package/dist/types/program/ops/utils.d.ts +5 -0
  108. package/dist/types/program/params.d.ts +67 -0
  109. package/dist/types/runtime/index.d.ts +82 -0
  110. package/package.json +59 -49
  111. package/{repl.js → repl.ts} +85 -39
  112. package/lib/applyDefaults.js +0 -34
  113. package/lib/builtins/funcContains.js +0 -25
  114. package/lib/builtins/funcEndsWith.js +0 -21
  115. package/lib/builtins/funcError.js +0 -11
  116. package/lib/builtins/funcFromJSON.js +0 -26
  117. package/lib/builtins/funcHas.js +0 -24
  118. package/lib/builtins/funcIn.js +0 -24
  119. package/lib/builtins/funcKeys.js +0 -20
  120. package/lib/builtins/funcLength.js +0 -24
  121. package/lib/builtins/funcNow.js +0 -10
  122. package/lib/builtins/funcStartsWith.js +0 -21
  123. package/lib/builtins/funcToJSON.js +0 -10
  124. package/lib/builtins/funcToLowerCase.js +0 -17
  125. package/lib/builtins/funcToNumber.js +0 -26
  126. package/lib/builtins/funcToString.js +0 -10
  127. package/lib/builtins/funcToUpperCase.js +0 -17
  128. package/lib/builtins/funcTrim.js +0 -17
  129. package/lib/builtins/funcTrimEnd.js +0 -17
  130. package/lib/builtins/funcTrimStart.js +0 -17
  131. package/lib/builtins/funcType.js +0 -10
  132. package/lib/builtins/funcVoid.js +0 -10
  133. package/lib/builtins/index.js +0 -205
  134. package/lib/builtins/internals.js +0 -8
  135. package/lib/builtins/math.js +0 -40
  136. package/lib/builtins/native.js +0 -180
  137. package/lib/createSingleton.js +0 -14
  138. package/lib/index.js +0 -79
  139. package/lib/interpreter/index.js +0 -56
  140. package/lib/interpreter/parse.js +0 -2377
  141. package/lib/interpreter/parseFunction.js +0 -24
  142. package/lib/interpreter/system.js +0 -10
  143. package/lib/interpreter/util.js +0 -349
  144. package/lib/library/apply.js +0 -142
  145. package/lib/library/definition.js +0 -9
  146. package/lib/library/errors/adaptErrors.js +0 -34
  147. package/lib/library/errors/enclosure.js +0 -16
  148. package/lib/library/errors/error.js +0 -18
  149. package/lib/library/errors/execution.js +0 -20
  150. package/lib/library/errors/fatal.js +0 -15
  151. package/lib/library/errors/runtime.js +0 -78
  152. package/lib/library/errors/syntax.js +0 -17
  153. package/lib/library/functions.js +0 -82
  154. package/lib/library/index.js +0 -335
  155. package/lib/library/mux.js +0 -106
  156. package/lib/library/ops/index.js +0 -60
  157. package/lib/library/ops/op.js +0 -180
  158. package/lib/library/ops/opa.js +0 -41
  159. package/lib/library/ops/opc.js +0 -59
  160. package/lib/library/ops/opm.js +0 -50
  161. package/lib/library/ops/opu.js +0 -77
  162. package/lib/library/runtimeScope.js +0 -38
  163. package/lib/library/runtimeSignal.js +0 -72
  164. package/lib/library/types.js +0 -304
  165. package/lib/program/index.js +0 -101
  166. package/lib/program/ops/index.js +0 -61
  167. package/lib/program/ops/opAccess/index.js +0 -55
  168. package/lib/program/ops/opAccess/opaField.js +0 -50
  169. package/lib/program/ops/opAccess/opaFunction.js +0 -50
  170. package/lib/program/ops/opAccess/opaIter.js +0 -33
  171. package/lib/program/ops/opAccess/opaSlice.js +0 -46
  172. package/lib/program/ops/opAnd.js +0 -30
  173. package/lib/program/ops/opArrayConstructor.js +0 -20
  174. package/lib/program/ops/opAssignment/index.js +0 -90
  175. package/lib/program/ops/opAssignment/opaAssignField.js +0 -125
  176. package/lib/program/ops/opAssignment/opaAssignIter.js +0 -71
  177. package/lib/program/ops/opAssignment/opaAssignSlice.js +0 -101
  178. package/lib/program/ops/opAssignment/opuAddition.js +0 -31
  179. package/lib/program/ops/opAssignment/opuDivision.js +0 -31
  180. package/lib/program/ops/opAssignment/opuMultiplication.js +0 -31
  181. package/lib/program/ops/opAssignment/opuNullCoalescence.js +0 -25
  182. package/lib/program/ops/opAssignment/opuRemainder.js +0 -31
  183. package/lib/program/ops/opAssignment/opuSet.js +0 -19
  184. package/lib/program/ops/opAssignment/opuSubtraction.js +0 -31
  185. package/lib/program/ops/opAssignment/opuUpdate.js +0 -19
  186. package/lib/program/ops/opCalculation/index.js +0 -57
  187. package/lib/program/ops/opCalculation/opmAddition.js +0 -49
  188. package/lib/program/ops/opCalculation/opmDivision.js +0 -42
  189. package/lib/program/ops/opCalculation/opmMultiplication.js +0 -40
  190. package/lib/program/ops/opCalculation/opmRemainder.js +0 -34
  191. package/lib/program/ops/opCalculation/opmSubtraction.js +0 -53
  192. package/lib/program/ops/opComparison/index.js +0 -59
  193. package/lib/program/ops/opComparison/opcEqual.js +0 -19
  194. package/lib/program/ops/opComparison/opcGreater.js +0 -19
  195. package/lib/program/ops/opComparison/opcGreaterEqual.js +0 -19
  196. package/lib/program/ops/opComparison/opcLess.js +0 -19
  197. package/lib/program/ops/opComparison/opcLessEqual.js +0 -19
  198. package/lib/program/ops/opComparison/opcUnequal.js +0 -19
  199. package/lib/program/ops/opConstant.js +0 -18
  200. package/lib/program/ops/opConstantFalse.js +0 -16
  201. package/lib/program/ops/opConstantNull.js +0 -16
  202. package/lib/program/ops/opConstantTrue.js +0 -16
  203. package/lib/program/ops/opFunctionDefinition.js +0 -21
  204. package/lib/program/ops/opIf.js +0 -38
  205. package/lib/program/ops/opInterpolatedString.js +0 -24
  206. package/lib/program/ops/opNegation.js +0 -25
  207. package/lib/program/ops/opNot.js +0 -16
  208. package/lib/program/ops/opNullCoalescence.js +0 -34
  209. package/lib/program/ops/opNumber.js +0 -18
  210. package/lib/program/ops/opObjectConstructor.js +0 -37
  211. package/lib/program/ops/opOr.js +0 -30
  212. package/lib/program/ops/opOutputConcat.js +0 -19
  213. package/lib/program/ops/opString.js +0 -18
  214. package/lib/program/ops/opTry.js +0 -37
  215. package/lib/program/ops/opVariable.js +0 -20
  216. package/lib/program/ops/opVariableDefinition.js +0 -24
  217. package/lib/program/ops/opVoid.js +0 -16
  218. package/lib/program/ops/utils.js +0 -35
  219. package/lib/runtime/index.js +0 -258
  220. package/src/applyDefaults.js +0 -35
  221. package/src/builtins/funcContains.js +0 -27
  222. package/src/builtins/funcEndsWith.js +0 -21
  223. package/src/builtins/funcError.js +0 -7
  224. package/src/builtins/funcFromJSON.js +0 -24
  225. package/src/builtins/funcHas.js +0 -25
  226. package/src/builtins/funcIn.js +0 -25
  227. package/src/builtins/funcKeys.js +0 -20
  228. package/src/builtins/funcLength.js +0 -26
  229. package/src/builtins/funcNow.js +0 -5
  230. package/src/builtins/funcStartsWith.js +0 -21
  231. package/src/builtins/funcToJSON.js +0 -5
  232. package/src/builtins/funcToLowerCase.js +0 -20
  233. package/src/builtins/funcToNumber.js +0 -25
  234. package/src/builtins/funcToString.js +0 -5
  235. package/src/builtins/funcToUpperCase.js +0 -20
  236. package/src/builtins/funcTrim.js +0 -16
  237. package/src/builtins/funcTrimEnd.js +0 -16
  238. package/src/builtins/funcTrimStart.js +0 -16
  239. package/src/builtins/funcType.js +0 -5
  240. package/src/builtins/funcVoid.js +0 -5
  241. package/src/builtins/index.js +0 -193
  242. package/src/builtins/internals.js +0 -9
  243. package/src/builtins/math.js +0 -44
  244. package/src/createSingleton.js +0 -9
  245. package/src/index.js +0 -44
  246. package/src/interpreter/index.js +0 -48
  247. package/src/interpreter/parse.js +0 -1473
  248. package/src/interpreter/system.js +0 -5
  249. package/src/interpreter/util.js +0 -237
  250. package/src/library/apply.js +0 -135
  251. package/src/library/definition.js +0 -3
  252. package/src/library/errors/adaptErrors.js +0 -28
  253. package/src/library/errors/enclosure.js +0 -11
  254. package/src/library/errors/error.js +0 -14
  255. package/src/library/errors/execution.js +0 -15
  256. package/src/library/errors/fatal.js +0 -10
  257. package/src/library/errors/runtime.js +0 -71
  258. package/src/library/errors/syntax.js +0 -12
  259. package/src/library/index.js +0 -41
  260. package/src/library/mux.js +0 -98
  261. package/src/library/runtimeScope.js +0 -35
  262. package/src/library/runtimeSignal.js +0 -67
  263. package/src/library/types.js +0 -299
  264. package/src/program/index.js +0 -114
  265. package/src/program/ops/index.js +0 -82
  266. package/src/program/ops/opAccess/index.js +0 -55
  267. package/src/program/ops/opAccess/opaField.js +0 -58
  268. package/src/program/ops/opAccess/opaFunction.js +0 -52
  269. package/src/program/ops/opAccess/opaIter.js +0 -34
  270. package/src/program/ops/opAccess/opaSlice.js +0 -61
  271. package/src/program/ops/opAnd.js +0 -32
  272. package/src/program/ops/opArrayConstructor.js +0 -16
  273. package/src/program/ops/opAssignment/index.js +0 -102
  274. package/src/program/ops/opAssignment/opaAssignField.js +0 -156
  275. package/src/program/ops/opAssignment/opaAssignIter.js +0 -77
  276. package/src/program/ops/opAssignment/opaAssignSlice.js +0 -160
  277. package/src/program/ops/opAssignment/opuAddition.js +0 -29
  278. package/src/program/ops/opAssignment/opuDivision.js +0 -29
  279. package/src/program/ops/opAssignment/opuMultiplication.js +0 -29
  280. package/src/program/ops/opAssignment/opuNullCoalescence.js +0 -26
  281. package/src/program/ops/opAssignment/opuRemainder.js +0 -29
  282. package/src/program/ops/opAssignment/opuSet.js +0 -15
  283. package/src/program/ops/opAssignment/opuSubtraction.js +0 -29
  284. package/src/program/ops/opAssignment/opuUpdate.js +0 -15
  285. package/src/program/ops/opCalculation/index.js +0 -64
  286. package/src/program/ops/opCalculation/opmAddition.js +0 -63
  287. package/src/program/ops/opCalculation/opmDivision.js +0 -47
  288. package/src/program/ops/opCalculation/opmMultiplication.js +0 -52
  289. package/src/program/ops/opCalculation/opmRemainder.js +0 -49
  290. package/src/program/ops/opCalculation/opmSubtraction.js +0 -72
  291. package/src/program/ops/opComparison/index.js +0 -67
  292. package/src/program/ops/opComparison/opcEqual.js +0 -17
  293. package/src/program/ops/opComparison/opcGreater.js +0 -17
  294. package/src/program/ops/opComparison/opcGreaterEqual.js +0 -17
  295. package/src/program/ops/opComparison/opcLess.js +0 -17
  296. package/src/program/ops/opComparison/opcLessEqual.js +0 -17
  297. package/src/program/ops/opComparison/opcUnequal.js +0 -17
  298. package/src/program/ops/opConstant.js +0 -13
  299. package/src/program/ops/opConstantFalse.js +0 -11
  300. package/src/program/ops/opConstantNull.js +0 -11
  301. package/src/program/ops/opConstantTrue.js +0 -11
  302. package/src/program/ops/opFunctionDefinition.js +0 -17
  303. package/src/program/ops/opIf.js +0 -44
  304. package/src/program/ops/opInterpolatedString.js +0 -27
  305. package/src/program/ops/opNegation.js +0 -26
  306. package/src/program/ops/opNot.js +0 -11
  307. package/src/program/ops/opNullCoalescence.js +0 -36
  308. package/src/program/ops/opNumber.js +0 -13
  309. package/src/program/ops/opObjectConstructor.js +0 -40
  310. package/src/program/ops/opOr.js +0 -32
  311. package/src/program/ops/opOutputConcat.js +0 -17
  312. package/src/program/ops/opString.js +0 -13
  313. package/src/program/ops/opTry.js +0 -38
  314. package/src/program/ops/opVariable.js +0 -17
  315. package/src/program/ops/opVariableDefinition.js +0 -18
  316. package/src/program/ops/opVoid.js +0 -11
  317. package/src/program/ops/utils.js +0 -19
  318. package/src/runtime/index.js +0 -309
  319. /package/{src/builtins/native.js → dist/types/builtins/native.d.ts} +0 -0
  320. /package/{src/library/ops/index.js → dist/types/library/ops/index.d.ts} +0 -0
@@ -1,1473 +0,0 @@
1
- import {
2
- OPA_FIELD,
3
- OPA_FUNCTION,
4
- OPA_ITER,
5
- OPA_SLICE,
6
- OPC_EQUAL,
7
- OPC_GREATER,
8
- OPC_GREATEREQUAL,
9
- OPC_LESS,
10
- OPC_LESSEQUAL,
11
- OPC_UNEQUAL,
12
- OPM_ADDITION,
13
- OPM_DIVISION,
14
- OPM_MULTIPLICATION,
15
- OPM_REMAINDER,
16
- OPM_SUBTRACTION,
17
- OPU_ADDITION,
18
- OPU_DIVISION,
19
- OPU_MULTIPLICATION,
20
- OPU_NULL_COALESCENCE,
21
- OPU_REMAINDER,
22
- OPU_SET,
23
- OPU_SUBTRACTION,
24
- OPU_UPDATE,
25
- OP_ACCESS,
26
- OP_AND,
27
- OP_ARRAY_CONSTRUCTOR,
28
- OP_ASSIGNMENT,
29
- OP_CALCULATION,
30
- OP_COMPARISON,
31
- OP_CONSTANT_FALSE,
32
- OP_CONSTANT_NULL,
33
- OP_CONSTANT_TRUE,
34
- OP_FUNCTION_DEFINITION,
35
- OP_IF,
36
- OP_INTERPOLATED_STRING,
37
- OP_NEGATION,
38
- OP_NOT,
39
- OP_NULL_COALESCENCE,
40
- OP_NUMBER,
41
- OP_OBJECT_CONSTRUCTOR,
42
- OP_OR,
43
- OP_OUTPUT_CONCAT,
44
- OP_STRING,
45
- OP_TRY,
46
- OP_VARIABLE,
47
- OP_VARIABLE_DEFINITION,
48
- OP_VOID,
49
- } from '../library';
50
- import {
51
- eot,
52
- errorGeneric,
53
- errorUnexpectedToken,
54
- hex,
55
- match,
56
- matchSet,
57
- matchWord,
58
- safeVariable,
59
- walkWhitespace,
60
- } from './util';
61
-
62
- /**
63
- * Parse a single program at i.
64
- * Throws an error if src contains additional content.
65
- */
66
- export async function parseEntrypoint(src, i, c) {
67
- let n = i;
68
-
69
- const result = await parseProgram(src, n, c);
70
- ({ i: n } = result);
71
-
72
- if (!eot(src, n, c).is)
73
- return errorUnexpectedToken(src, n, c, { operator: 'program', message: 'expected EOT' });
74
-
75
- return result;
76
- }
77
-
78
- /** Parse program at i */
79
- export function parseProgram(src, i, c) {
80
- let n = i;
81
-
82
- ({ i: n } = walkWhitespace(src, n, c));
83
-
84
- return opPipe(src, n, c);
85
- }
86
-
87
- /** Parse function header at i */
88
- export function parseFunctionHeader(src, i, c) {
89
- let n = i;
90
-
91
- let m = matchWord(src, n, c, { phrase: '(' });
92
- ({ i: n } = m);
93
- if (!m.is)
94
- return errorUnexpectedToken(src, n, c, {
95
- operator: 'function definition',
96
- message: "expected '('",
97
- });
98
-
99
- const argNames = [];
100
- m = matchWord(src, n, c, { phrase: ')' });
101
- if (m.is) ({ i: n } = m);
102
- else
103
- for (;;) {
104
- const v = safeVariable(src, n, c);
105
- if (!v.is)
106
- return errorUnexpectedToken(src, n, c, {
107
- operator: 'function definition',
108
- message: 'expected argument name',
109
- });
110
- let name;
111
- ({ i: n, value: name } = v);
112
- argNames.push(name);
113
-
114
- m = matchWord(src, n, c, { phrase: ')' });
115
- if (m.is) {
116
- ({ i: n } = m);
117
- break;
118
- }
119
-
120
- m = matchWord(src, n, c, { phrase: ',' });
121
- ({ i: n } = m);
122
- if (!m.is)
123
- return errorUnexpectedToken(src, n, c, {
124
- operator: 'function definition',
125
- message: "expected ',' or ')'",
126
- });
127
- }
128
-
129
- m = matchWord(src, n, c, { phrase: ':' });
130
- ({ i: n } = m);
131
- if (!m.is)
132
- return errorUnexpectedToken(src, n, c, {
133
- operator: 'function definition',
134
- message: "expected ':'",
135
- });
136
-
137
- return { i: n, argNames };
138
- }
139
-
140
- /** Parse access at i */
141
- export async function parseAccess(src, i, c, { identity } = {}) {
142
- let n = i;
143
-
144
- const selectors = [];
145
- let canAssign = true;
146
- for (;;) {
147
- let m = matchWord(src, n, c, { phrase: '.' });
148
- const isIdentity = identity && selectors.length === 0;
149
- if (!isIdentity && m.is) {
150
- ({ i: n } = m);
151
-
152
- const v = safeVariable(src, n, c);
153
- if (!v.is)
154
- return errorUnexpectedToken(src, n, c, {
155
- operator: 'field access operator',
156
- message: 'expected field name',
157
- });
158
- let name;
159
- ({ i: n, value: name } = v);
160
-
161
- let optional;
162
- m = matchWord(src, n, c, { phrase: '?', notBeforeSet: '?=' });
163
- if (m.is) ({ i: n, is: optional } = m);
164
- selectors.push({
165
- op: OPA_FIELD,
166
- params: { pipe: [{ op: OP_STRING, params: { string: name } }], optional },
167
- });
168
- continue;
169
- }
170
-
171
- m = matchWord(src, n, c, { phrase: '[' });
172
- if (m.is) {
173
- ({ i: n } = m);
174
-
175
- m = matchWord(src, n, c, { phrase: ']' });
176
- if (m.is) {
177
- ({ i: n } = m);
178
-
179
- let optional;
180
- m = matchWord(src, n, c, { phrase: '?', notBeforeSet: '?=' });
181
- if (m.is) ({ i: n, is: optional } = m);
182
- selectors.push({ op: OPA_ITER, params: { optional } });
183
- continue;
184
- }
185
-
186
- m = matchWord(src, n, c, { phrase: ':' });
187
- if (m.is) {
188
- ({ i: n } = m);
189
-
190
- m = matchWord(src, n, c, { phrase: ']' });
191
- if (m.is) {
192
- ({ i: n } = m);
193
-
194
- let optional;
195
- m = matchWord(src, n, c, { phrase: '?', notBeforeSet: '?=' });
196
- if (m.is) ({ i: n, is: optional } = m);
197
- selectors.push({
198
- op: OPA_SLICE,
199
- params: { from: [{ op: OP_CONSTANT_NULL }], to: [{ op: OP_CONSTANT_NULL }], optional },
200
- });
201
- continue;
202
- }
203
-
204
- let opsRight;
205
- ({ i: n, ops: opsRight } = await opPipe(src, n, c));
206
-
207
- m = matchWord(src, n, c, { phrase: ']' });
208
- ({ i: n } = m);
209
- if (!m.is)
210
- return errorUnexpectedToken(src, n, c, {
211
- operator: 'array slice operator',
212
- message: "expected ']'",
213
- });
214
-
215
- let optional;
216
- m = matchWord(src, n, c, { phrase: '?', notBeforeSet: '?=' });
217
- if (m.is) ({ i: n, is: optional } = m);
218
- selectors.push({
219
- op: OPA_SLICE,
220
- params: { from: [{ op: OP_CONSTANT_NULL }], to: opsRight, optional },
221
- });
222
- continue;
223
- }
224
-
225
- let opsLeft;
226
- ({ i: n, ops: opsLeft } = await opPipe(src, n, c));
227
-
228
- m = matchWord(src, n, c, { phrase: ']' });
229
- if (m.is) {
230
- ({ i: n } = m);
231
-
232
- let optional;
233
- m = matchWord(src, n, c, { phrase: '?', notBeforeSet: '?=' });
234
- if (m.is) ({ i: n, is: optional } = m);
235
- selectors.push({ op: OPA_FIELD, params: { pipe: opsLeft, optional } });
236
- continue;
237
- }
238
-
239
- m = matchWord(src, n, c, { phrase: ':' });
240
- if (!m.is)
241
- return errorUnexpectedToken(src, n, c, {
242
- operator: 'variable access operator',
243
- message: "expected ':' or ']'",
244
- });
245
- ({ i: n } = m);
246
-
247
- m = matchWord(src, n, c, { phrase: ']' });
248
- if (m.is) {
249
- ({ i: n } = m);
250
-
251
- let optional;
252
- m = matchWord(src, n, c, { phrase: '?', notBeforeSet: '?=' });
253
- if (m.is) ({ i: n, is: optional } = m);
254
- selectors.push({
255
- op: OPA_SLICE,
256
- params: { from: opsLeft, to: [{ op: OP_CONSTANT_NULL }], optional },
257
- });
258
- continue;
259
- }
260
-
261
- let opsRight;
262
- ({ i: n, ops: opsRight } = await opPipe(src, n, c));
263
-
264
- m = matchWord(src, n, c, { phrase: ']' });
265
- ({ i: n } = m);
266
- if (!m.is)
267
- return errorUnexpectedToken(src, n, c, {
268
- operator: 'array slice operator',
269
- message: "expected ']'",
270
- });
271
-
272
- let optional;
273
- m = matchWord(src, n, c, { phrase: '?', notBeforeSet: '?=' });
274
- if (m.is) ({ i: n, is: optional } = m);
275
- selectors.push({ op: OPA_SLICE, params: { from: opsLeft, to: opsRight, optional } });
276
- continue;
277
- }
278
-
279
- let bound;
280
- m = matchWord(src, n, c, { phrase: '->' });
281
- if (m.is) ({ i: n, is: bound } = m);
282
-
283
- m = matchWord(src, n, c, { phrase: '(' });
284
- if (!m.is && bound) {
285
- return errorUnexpectedToken(src, n, c, {
286
- operator: 'bound function call',
287
- message: "expected '('",
288
- });
289
- }
290
- if (m.is) {
291
- ({ i: n } = m);
292
-
293
- const args = [];
294
- m = matchWord(src, n, c, { phrase: ')' });
295
- if (m.is) ({ i: n } = m);
296
- else
297
- for (;;) {
298
- let opsArg;
299
- ({ i: n, ops: opsArg } = await opSubPipe(src, n, c));
300
- args.push(opsArg);
301
-
302
- m = matchWord(src, n, c, { phrase: ')' });
303
- if (m.is) {
304
- ({ i: n } = m);
305
- break;
306
- }
307
-
308
- m = matchWord(src, n, c, { phrase: ',' });
309
- ({ i: n } = m);
310
- if (!m.is)
311
- return errorUnexpectedToken(src, n, c, {
312
- operator: 'function call',
313
- message: "expected ',' or ')'",
314
- });
315
- }
316
-
317
- let optional;
318
- m = matchWord(src, n, c, { phrase: '?', notBeforeSet: '?=' });
319
- if (m.is) ({ i: n, is: optional } = m);
320
- selectors.push({ op: OPA_FUNCTION, params: { args, bound, optional } });
321
- canAssign = false;
322
- continue;
323
- }
324
-
325
- break;
326
- }
327
-
328
- return { i: n, is: selectors.length > 0, selectors, canAssign };
329
- }
330
-
331
- /** Parse assignment at i */
332
- export async function parseAssignment(src, i, c) {
333
- let n = i;
334
-
335
- let m = matchWord(src, n, c, { phrase: '=', notBeforeSet: '=' });
336
- if (m.is) {
337
- ({ i: n } = m);
338
-
339
- let ops;
340
- ({ i: n, ops } = await opSubRoute(src, n, c));
341
- return { i: n, is: true, assignment: { op: OPU_SET, params: { pipe: ops } } };
342
- }
343
-
344
- m = matchWord(src, n, c, { phrase: '|=' });
345
- if (m.is) {
346
- ({ i: n } = m);
347
-
348
- let ops;
349
- ({ i: n, ops } = await opSubRoute(src, n, c));
350
- return { i: n, is: true, assignment: { op: OPU_UPDATE, params: { pipe: ops } } };
351
- }
352
-
353
- m = matchWord(src, n, c, { phrase: '+=' });
354
- if (m.is) {
355
- ({ i: n } = m);
356
-
357
- let ops;
358
- ({ i: n, ops } = await opSubRoute(src, n, c));
359
- return { i: n, is: true, assignment: { op: OPU_ADDITION, params: { pipe: ops } } };
360
- }
361
-
362
- m = matchWord(src, n, c, { phrase: '-=' });
363
- if (m.is) {
364
- ({ i: n } = m);
365
-
366
- let ops;
367
- ({ i: n, ops } = await opSubRoute(src, n, c));
368
- return { i: n, is: true, assignment: { op: OPU_SUBTRACTION, params: { pipe: ops } } };
369
- }
370
-
371
- m = matchWord(src, n, c, { phrase: '*=' });
372
- if (m.is) {
373
- ({ i: n } = m);
374
-
375
- let ops;
376
- ({ i: n, ops } = await opSubRoute(src, n, c));
377
- return { i: n, is: true, assignment: { op: OPU_MULTIPLICATION, params: { pipe: ops } } };
378
- }
379
-
380
- m = matchWord(src, n, c, { phrase: '/=' });
381
- if (m.is) {
382
- ({ i: n } = m);
383
-
384
- let ops;
385
- ({ i: n, ops } = await opSubRoute(src, n, c));
386
- return { i: n, is: true, assignment: { op: OPU_DIVISION, params: { pipe: ops } } };
387
- }
388
-
389
- m = matchWord(src, n, c, { phrase: '%=' });
390
- if (m.is) {
391
- ({ i: n } = m);
392
-
393
- let ops;
394
- ({ i: n, ops } = await opSubRoute(src, n, c));
395
- return { i: n, is: true, assignment: { op: OPU_REMAINDER, params: { pipe: ops } } };
396
- }
397
-
398
- m = matchWord(src, n, c, { phrase: '?=' });
399
- if (m.is) {
400
- ({ i: n } = m);
401
-
402
- let ops;
403
- ({ i: n, ops } = await opSubRoute(src, n, c));
404
- return { i: n, is: true, assignment: { op: OPU_NULL_COALESCENCE, params: { pipe: ops } } };
405
- }
406
-
407
- return { i: n, is: false };
408
- }
409
-
410
- /** Parse number at i */
411
- export function parseNumber(src, i, c) {
412
- let n = i;
413
- let is = false;
414
- let value = '';
415
-
416
- for (;;) {
417
- const set = matchSet(src, n, c, { set: '0123456789' });
418
- if (!set.is) break;
419
- ({ i: n } = set);
420
- is = true;
421
- value += set.value;
422
- }
423
-
424
- if (!is) return { i: n, is: false };
425
-
426
- const m = match(src, n, c, { phrase: '.' });
427
- if (m.is) {
428
- ({ i: n } = m);
429
- value += '.';
430
-
431
- for (;;) {
432
- const set = matchSet(src, n, c, { set: '0123456789' });
433
- if (!set.is) break;
434
- ({ i: n } = set);
435
- value += set.value;
436
- }
437
- }
438
-
439
- let set = matchSet(src, n, c, { set: 'eE' });
440
- if (set.is) {
441
- ({ i: n } = set);
442
- value += set.value;
443
-
444
- set = matchSet(src, n, c, { set: '+-' });
445
- if (set.is) {
446
- ({ i: n } = set);
447
- value += set.value;
448
- }
449
-
450
- let isE;
451
- for (;;) {
452
- set = matchSet(src, n, c, { set: '0123456789' });
453
- if (!set.is) break;
454
- ({ i: n } = set);
455
- isE = true;
456
- value += set.value;
457
- }
458
- if (!isE)
459
- return errorUnexpectedToken(src, n, c, { operator: 'number', message: 'expected digit' });
460
- }
461
-
462
- const number = +value;
463
- if (!Number.isFinite(number)) {
464
- return errorGeneric(src, n, c, { operator: 'number', message: `invalid number ${number}` });
465
- }
466
-
467
- ({ i: n } = walkWhitespace(src, n, c));
468
-
469
- return { i: n, is: true, ops: [{ op: OP_NUMBER, params: { number } }] };
470
- }
471
-
472
- /** Parse string at i */
473
- export async function parseString(src, i, c) {
474
- let n = i;
475
- let value = '';
476
-
477
- const set = matchSet(src, n, c, { set: '"\'`' });
478
- if (!set.is) return { i: n, is: false };
479
- let boundary;
480
- ({ i: n, value: boundary } = set);
481
-
482
- const multilineString = boundary === '`';
483
-
484
- const interpolations = [];
485
- for (;;) {
486
- let m = match(src, n, c, { phrase: boundary });
487
- if (m.is) {
488
- ({ i: n } = m);
489
- break;
490
- }
491
-
492
- const end = eot(src, n, c);
493
- if (end.is) {
494
- ({ i: n } = end);
495
- return errorUnexpectedToken(src, n, c, {
496
- operator: 'string',
497
- message: 'incomplete string literal',
498
- });
499
- }
500
-
501
- m = match(src, n, c, { phrase: '\\' });
502
- if (m.is) {
503
- ({ i: n } = m);
504
-
505
- m = matchWord(src, n, c, { phrase: '(' });
506
- if (m.is) {
507
- ({ i: n } = m);
508
-
509
- let ops;
510
- ({ i: n, ops } = await opPipe(src, n, c));
511
-
512
- m = match(src, n, c, { phrase: ')' });
513
- ({ i: n } = m);
514
- if (!m.is)
515
- return errorUnexpectedToken(src, n, c, {
516
- operator: 'string interpolation',
517
- message: "expected ')'",
518
- });
519
- interpolations.push({ before: value, pipe: ops });
520
- value = '';
521
- continue;
522
- }
523
-
524
- if (multilineString) {
525
- switch (src[n]) {
526
- case '\n':
527
- n += 1;
528
- continue;
529
-
530
- case '\r':
531
- n += 1;
532
- if (!eot(src, n, c).is && src[n] === '\n') n += 1;
533
- continue;
534
-
535
- default:
536
- }
537
- }
538
-
539
- switch (src[n]) {
540
- case '"':
541
- value += '"';
542
- n += 1;
543
- continue;
544
-
545
- case "'":
546
- value += "'";
547
- n += 1;
548
- continue;
549
-
550
- case '`':
551
- value += '`';
552
- n += 1;
553
- continue;
554
-
555
- case '\\':
556
- value += '\\';
557
- n += 1;
558
- continue;
559
-
560
- case '/':
561
- value += '/';
562
- n += 1;
563
- continue;
564
-
565
- case 'b':
566
- value += '\b';
567
- n += 1;
568
- continue;
569
-
570
- case 'f':
571
- value += '\f';
572
- n += 1;
573
- continue;
574
-
575
- case 'n':
576
- value += '\n';
577
- n += 1;
578
- continue;
579
-
580
- case 'r':
581
- value += '\r';
582
- n += 1;
583
- continue;
584
-
585
- case 't':
586
- value += '\t';
587
- n += 1;
588
- continue;
589
-
590
- case 'u': {
591
- n += 1;
592
- let hexVal = '';
593
- for (let j = 0; j < 4; j += 1) {
594
- m = hex(src, n, c);
595
- ({ i: n } = m);
596
- if (!m.is)
597
- return errorUnexpectedToken(src, n, c, {
598
- operator: 'string',
599
- message: 'incomplete unicode escape sequence: expected hex digit',
600
- });
601
- hexVal += m.value;
602
- }
603
- value += String.fromCodePoint(parseInt(hexVal, 16));
604
- continue;
605
- }
606
-
607
- default:
608
- return errorUnexpectedToken(src, n, c, {
609
- operator: 'string',
610
- message: 'invalid escape sequence',
611
- });
612
- }
613
- }
614
-
615
- if (multilineString) {
616
- switch (src[n]) {
617
- case '\n':
618
- case '\r':
619
- case '\t':
620
- value += src[n];
621
- n += 1;
622
- continue;
623
-
624
- default:
625
- }
626
- }
627
-
628
- if (src.charCodeAt(n) < 0x20) return errorUnexpectedToken(src, n, c, { operator: 'string' });
629
-
630
- value += src[n];
631
- n += 1;
632
- }
633
-
634
- ({ i: n } = walkWhitespace(src, n, c));
635
-
636
- if (interpolations.length === 0)
637
- return { i: n, is: true, ops: [{ op: OP_STRING, params: { string: value } }] };
638
-
639
- return {
640
- i: n,
641
- is: true,
642
- ops: [{ op: OP_INTERPOLATED_STRING, params: { interpolations, after: value } }],
643
- };
644
- }
645
-
646
- /** Parse pipe at i */
647
- export async function opPipe(src, i, c) {
648
- // Call stack decoupling - This is necessary as some browsers (i.e. Safari) have very limited call stack sizes which result in stack overflow exceptions in certain situations.
649
- await undefined;
650
-
651
- let n = i;
652
-
653
- const pipe = [];
654
- for (;;) {
655
- let ops;
656
- ({ i: n, ops } = await opOutputConcat(src, n, c));
657
- pipe.push(...ops);
658
-
659
- const m = matchWord(src, n, c, { phrase: '|', notBeforeSet: '=' });
660
- if (!m.is) break;
661
- ({ i: n } = m);
662
- }
663
-
664
- return { i: n, ops: pipe };
665
- }
666
-
667
- /** Parse subpipe at i */
668
- export async function opSubPipe(src, i, c) {
669
- // Call stack decoupling - This is necessary as some browsers (i.e. Safari) have very limited call stack sizes which result in stack overflow exceptions in certain situations.
670
- await undefined;
671
-
672
- let n = i;
673
-
674
- const pipe = [];
675
- for (;;) {
676
- let ops;
677
- ({ i: n, ops } = await opTry(src, n, c));
678
- pipe.push(...ops);
679
-
680
- const m = matchWord(src, n, c, { phrase: '|', notBeforeSet: '=' });
681
- if (!m.is) break;
682
- ({ i: n } = m);
683
- }
684
-
685
- return { i: n, ops: pipe };
686
- }
687
-
688
- /** Parse subroute at i */
689
- export async function opSubRoute(src, i, c) {
690
- // Call stack decoupling - This is necessary as some browsers (i.e. Safari) have very limited call stack sizes which result in stack overflow exceptions in certain situations.
691
- await undefined;
692
-
693
- return opTry(src, i, c);
694
- }
695
-
696
- /** Parse output concat at i */
697
- export async function opOutputConcat(src, i, c) {
698
- let n = i;
699
-
700
- const pipes = [];
701
- for (;;) {
702
- let ops;
703
- ({ i: n, ops } = await opTry(src, n, c));
704
- pipes.push(ops);
705
-
706
- const m = matchWord(src, n, c, { phrase: ',' });
707
- if (!m.is) break;
708
- ({ i: n } = m);
709
- }
710
-
711
- if (pipes.length === 1) return { i: n, ops: pipes[0] };
712
-
713
- return { i: n, ops: [{ op: OP_OUTPUT_CONCAT, params: { pipes } }] };
714
- }
715
-
716
- /** Parse try at i */
717
- export async function opTry(src, i, c) {
718
- let n = i;
719
-
720
- let m = matchWord(src, n, c, { phrase: 'try', spaceAfter: true });
721
- if (!m.is) return opOr(src, n, c);
722
- ({ i: n } = m);
723
-
724
- let opsTry;
725
- ({ i: n, ops: opsTry } = await opOr(src, n, c));
726
-
727
- let opsCatch;
728
- m = matchWord(src, n, c, { spaceBefore: true, phrase: 'catch', spaceAfter: true });
729
- if (m.is) {
730
- ({ i: n } = m);
731
-
732
- ({ i: n, ops: opsCatch } = await opOr(src, n, c));
733
- } else opsCatch = [{ op: OP_VOID }];
734
-
735
- return { i: n, ops: [{ op: OP_TRY, params: { try: opsTry, catch: opsCatch } }] };
736
- }
737
-
738
- /** Parse or at i */
739
- export async function opOr(src, i, c) {
740
- let n = i;
741
-
742
- const pipes = [];
743
- for (;;) {
744
- let ops;
745
- ({ i: n, ops } = await opAnd(src, n, c));
746
- pipes.push(ops);
747
-
748
- const m = matchWord(src, n, c, { spaceBefore: true, phrase: 'or', spaceAfter: true });
749
- if (!m.is) break;
750
- ({ i: n } = m);
751
- }
752
-
753
- if (pipes.length === 1) return { i: n, ops: pipes[0] };
754
-
755
- return { i: n, ops: [{ op: OP_OR, params: { pipes } }] };
756
- }
757
-
758
- /** Parse and at i */
759
- export async function opAnd(src, i, c) {
760
- let n = i;
761
-
762
- const pipes = [];
763
- for (;;) {
764
- let ops;
765
- ({ i: n, ops } = await opEquality(src, n, c));
766
- pipes.push(ops);
767
-
768
- const m = matchWord(src, n, c, { spaceBefore: true, phrase: 'and', spaceAfter: true });
769
- if (!m.is) break;
770
- ({ i: n } = m);
771
- }
772
-
773
- if (pipes.length === 1) return { i: n, ops: pipes[0] };
774
-
775
- return { i: n, ops: [{ op: OP_AND, params: { pipes } }] };
776
- }
777
-
778
- /** Parse equality at i */
779
- export async function opEquality(src, i, c) {
780
- let n = i;
781
-
782
- let ops;
783
- ({ i: n, ops } = await opComparison(src, n, c));
784
-
785
- const comparisons = [];
786
- for (;;) {
787
- let m = matchWord(src, n, c, { phrase: '==' });
788
- if (m.is) {
789
- ({ i: n } = m);
790
-
791
- let opsBy;
792
- ({ i: n, ops: opsBy } = await opComparison(src, n, c));
793
- comparisons.push({ op: OPC_EQUAL, params: { by: opsBy } });
794
- continue;
795
- }
796
-
797
- m = matchWord(src, n, c, { phrase: '!=' });
798
- if (m.is) {
799
- ({ i: n } = m);
800
-
801
- let opsBy;
802
- ({ i: n, ops: opsBy } = await opComparison(src, n, c));
803
- comparisons.push({ op: OPC_UNEQUAL, params: { by: opsBy } });
804
- continue;
805
- }
806
-
807
- break;
808
- }
809
-
810
- if (comparisons.length === 0) return { i: n, ops };
811
-
812
- return { i: n, ops: [{ op: OP_COMPARISON, params: { pipe: ops, comparisons } }] };
813
- }
814
-
815
- /** Parse comparison at i */
816
- export async function opComparison(src, i, c) {
817
- let n = i;
818
-
819
- let ops;
820
- ({ i: n, ops } = await opNot(src, n, c));
821
-
822
- const comparisons = [];
823
- for (;;) {
824
- let m = matchWord(src, n, c, { phrase: '<=' });
825
- if (m.is) {
826
- ({ i: n } = m);
827
-
828
- let opsBy;
829
- ({ i: n, ops: opsBy } = await opNot(src, n, c));
830
- comparisons.push({ op: OPC_LESSEQUAL, params: { by: opsBy } });
831
- continue;
832
- }
833
-
834
- m = matchWord(src, n, c, { phrase: '<' });
835
- if (m.is) {
836
- ({ i: n } = m);
837
-
838
- let opsBy;
839
- ({ i: n, ops: opsBy } = await opNot(src, n, c));
840
- comparisons.push({ op: OPC_LESS, params: { by: opsBy } });
841
- continue;
842
- }
843
-
844
- m = matchWord(src, n, c, { phrase: '>=' });
845
- if (m.is) {
846
- ({ i: n } = m);
847
-
848
- let opsBy;
849
- ({ i: n, ops: opsBy } = await opNot(src, n, c));
850
- comparisons.push({ op: OPC_GREATEREQUAL, params: { by: opsBy } });
851
- continue;
852
- }
853
-
854
- m = matchWord(src, n, c, { phrase: '>' });
855
- if (m.is) {
856
- ({ i: n } = m);
857
-
858
- let opsBy;
859
- ({ i: n, ops: opsBy } = await opNot(src, n, c));
860
- comparisons.push({ op: OPC_GREATER, params: { by: opsBy } });
861
- continue;
862
- }
863
-
864
- break;
865
- }
866
-
867
- if (comparisons.length === 0) return { i: n, ops };
868
-
869
- return { i: n, ops: [{ op: OP_COMPARISON, params: { pipe: ops, comparisons } }] };
870
- }
871
-
872
- /** Parse not at i */
873
- export async function opNot(src, i, c) {
874
- let n = i;
875
-
876
- const m = matchWord(src, n, c, { phrase: 'not', spaceAfter: true });
877
- if (!m.is) return opErrorSuppression(src, n, c);
878
- ({ i: n } = m);
879
-
880
- let ops;
881
- ({ i: n, ops } = await opErrorSuppression(src, n, c));
882
-
883
- return { i: n, ops: [...ops, { op: OP_NOT }] };
884
- }
885
-
886
- /** Parse error suppression at i */
887
- export async function opErrorSuppression(src, i, c) {
888
- let n = i;
889
-
890
- const result = await opDifference(src, n, c);
891
- ({ i: n } = result);
892
-
893
- const m = matchWord(src, n, c, { phrase: '?', notBeforeSet: '?=' });
894
- if (!m.is) return result;
895
- ({ i: n } = m);
896
-
897
- return {
898
- i: n,
899
- ops: [{ op: OP_TRY, params: { try: result.ops, catch: [{ op: OP_VOID }] } }],
900
- };
901
- }
902
-
903
- /** Parse difference at i */
904
- export async function opDifference(src, i, c) {
905
- let n = i;
906
-
907
- let ops;
908
- ({ i: n, ops } = await opMultiplication(src, n, c));
909
-
910
- const operations = [];
911
- for (;;) {
912
- let m = matchWord(src, n, c, { phrase: '+', notBeforeSet: '=' });
913
- if (m.is) {
914
- ({ i: n } = m);
915
-
916
- let opsBy;
917
- ({ i: n, ops: opsBy } = await opMultiplication(src, n, c));
918
- operations.push({ op: OPM_ADDITION, params: { by: opsBy } });
919
- continue;
920
- }
921
-
922
- m = matchWord(src, n, c, { phrase: '-', notBeforeSet: '=>' });
923
- if (m.is) {
924
- ({ i: n } = m);
925
-
926
- let opsBy;
927
- ({ i: n, ops: opsBy } = await opMultiplication(src, n, c));
928
- operations.push({ op: OPM_SUBTRACTION, params: { by: opsBy } });
929
- continue;
930
- }
931
-
932
- break;
933
- }
934
-
935
- if (operations.length === 0) return { i: n, ops };
936
-
937
- return { i: n, ops: [{ op: OP_CALCULATION, params: { pipe: ops, operations } }] };
938
- }
939
-
940
- /** Parse multiplication at i */
941
- export async function opMultiplication(src, i, c) {
942
- let n = i;
943
-
944
- let ops;
945
- ({ i: n, ops } = await opNullCoalescence(src, n, c));
946
-
947
- const operations = [];
948
- for (;;) {
949
- let m = matchWord(src, n, c, { phrase: '*', notBeforeSet: '=' });
950
- if (m.is) {
951
- ({ i: n } = m);
952
-
953
- let opsBy;
954
- ({ i: n, ops: opsBy } = await opNullCoalescence(src, n, c));
955
- operations.push({ op: OPM_MULTIPLICATION, params: { by: opsBy } });
956
- continue;
957
- }
958
-
959
- m = matchWord(src, n, c, { phrase: '/', notBeforeSet: '=' });
960
- if (m.is) {
961
- ({ i: n } = m);
962
-
963
- let opsBy;
964
- ({ i: n, ops: opsBy } = await opNullCoalescence(src, n, c));
965
- operations.push({ op: OPM_DIVISION, params: { by: opsBy } });
966
- continue;
967
- }
968
-
969
- m = matchWord(src, n, c, { phrase: '%', notBeforeSet: '=' });
970
- if (m.is) {
971
- ({ i: n } = m);
972
-
973
- let opsBy;
974
- ({ i: n, ops: opsBy } = await opNullCoalescence(src, n, c));
975
- operations.push({ op: OPM_REMAINDER, params: { by: opsBy } });
976
- continue;
977
- }
978
-
979
- break;
980
- }
981
-
982
- if (operations.length === 0) return { i: n, ops };
983
-
984
- return { i: n, ops: [{ op: OP_CALCULATION, params: { pipe: ops, operations } }] };
985
- }
986
-
987
- /** Parse null coalescence at i */
988
- export async function opNullCoalescence(src, i, c) {
989
- let n = i;
990
-
991
- const pipes = [];
992
- for (;;) {
993
- let ops;
994
- ({ i: n, ops } = await opNegation(src, n, c));
995
- pipes.push(ops);
996
-
997
- const m = matchWord(src, n, c, { phrase: '??' });
998
- if (!m.is) break;
999
- ({ i: n } = m);
1000
- }
1001
-
1002
- if (pipes.length === 1) return { i: n, ops: pipes[0] };
1003
-
1004
- return { i: n, ops: [{ op: OP_NULL_COALESCENCE, params: { pipes } }] };
1005
- }
1006
-
1007
- /** Parse negation at i */
1008
- export async function opNegation(src, i, c) {
1009
- let n = i;
1010
-
1011
- const m = matchWord(src, n, c, { phrase: '-', notBeforeSet: '=>' });
1012
- if (!m.is) return opIf(src, n, c);
1013
- ({ i: n } = m);
1014
-
1015
- let ops;
1016
- ({ i: n, ops } = await opIf(src, n, c));
1017
-
1018
- return { i: n, ops: [...ops, { op: OP_NEGATION }] };
1019
- }
1020
-
1021
- /** Parse if at i */
1022
- export async function opIf(src, i, c) {
1023
- let n = i;
1024
-
1025
- let m = matchWord(src, n, c, { phrase: 'if', spaceAfter: true });
1026
- if (!m.is) return opConstant(src, n, c);
1027
- ({ i: n } = m);
1028
-
1029
- const ifs = [];
1030
- for (;;) {
1031
- let opsIf;
1032
- ({ i: n, ops: opsIf } = await opPipe(src, n, c));
1033
-
1034
- m = matchWord(src, n, c, { spaceBefore: true, phrase: 'then', spaceAfter: true });
1035
- ({ i: n } = m);
1036
- if (!m.is)
1037
- return errorUnexpectedToken(src, n, c, {
1038
- operator: 'if statement',
1039
- message: "expected 'then'",
1040
- });
1041
-
1042
- let opsThen;
1043
- ({ i: n, ops: opsThen } = await opPipe(src, n, c));
1044
- ifs.push({ if: opsIf, then: opsThen });
1045
-
1046
- m = matchWord(src, n, c, { spaceBefore: true, phrase: 'elif', spaceAfter: true });
1047
- if (!m.is) break;
1048
- ({ i: n } = m);
1049
- }
1050
-
1051
- let opsElse;
1052
- m = matchWord(src, n, c, { spaceBefore: true, phrase: 'else', spaceAfter: true });
1053
- if (m.is) {
1054
- ({ i: n } = m);
1055
-
1056
- ({ i: n, ops: opsElse } = await opPipe(src, n, c));
1057
- } else opsElse = [];
1058
-
1059
- m = matchWord(src, n, c, { spaceBefore: true, phrase: 'end' });
1060
- ({ i: n } = m);
1061
- if (!m.is)
1062
- return errorUnexpectedToken(src, n, c, {
1063
- operator: 'if statement',
1064
- message: "expected 'end'",
1065
- });
1066
-
1067
- return { i: n, ops: [{ op: OP_IF, params: { ifs, else: opsElse } }] };
1068
- }
1069
-
1070
- /** Parse constant at i */
1071
- export function opConstant(src, i, c) {
1072
- let n = i;
1073
-
1074
- let m = matchWord(src, n, c, { phrase: 'true', spaceAfter: true });
1075
- if (m.is) {
1076
- ({ i: n } = m);
1077
- return { i: n, ops: [{ op: OP_CONSTANT_TRUE }] };
1078
- }
1079
-
1080
- m = matchWord(src, n, c, { phrase: 'false', spaceAfter: true });
1081
- if (m.is) {
1082
- ({ i: n } = m);
1083
- return { i: n, ops: [{ op: OP_CONSTANT_FALSE }] };
1084
- }
1085
-
1086
- m = matchWord(src, n, c, { phrase: 'null', spaceAfter: true });
1087
- if (m.is) {
1088
- ({ i: n } = m);
1089
- return { i: n, ops: [{ op: OP_CONSTANT_NULL }] };
1090
- }
1091
-
1092
- return opNumber(src, n, c);
1093
- }
1094
-
1095
- /** Parse number at i */
1096
- export async function opNumber(src, i, c) {
1097
- let n = i;
1098
-
1099
- const result = await parseNumber(src, n, c);
1100
- if (!result.is) return opNamedFunctionDefinition(src, n, c);
1101
- ({ i: n } = result);
1102
-
1103
- return { i: n, ops: result.ops };
1104
- }
1105
-
1106
- /** Parse named function definition at i */
1107
- export async function opNamedFunctionDefinition(src, i, c) {
1108
- let n = i;
1109
-
1110
- const m = matchWord(src, n, c, { phrase: 'func', spaceAfter: true });
1111
- if (!m.is) return opFunctionDefinition(src, n, c);
1112
- ({ i: n } = m);
1113
-
1114
- const v = safeVariable(src, n, c);
1115
- if (!v.is) return opFunctionDefinition(src, i, c);
1116
- let name;
1117
- ({ i: n, value: name } = v);
1118
-
1119
- let argNames;
1120
- ({ i: n, argNames } = await parseFunctionHeader(src, n, c));
1121
-
1122
- let ops;
1123
- ({ i: n, ops } = await opSubRoute(src, n, c));
1124
-
1125
- return {
1126
- i: n,
1127
- ops: [
1128
- {
1129
- op: OP_VARIABLE_DEFINITION,
1130
- params: { name, pipe: [{ op: OP_FUNCTION_DEFINITION, params: { argNames, pipe: ops } }] },
1131
- },
1132
- ],
1133
- };
1134
- }
1135
-
1136
- /** Parse function definition at i */
1137
- export async function opFunctionDefinition(src, i, c) {
1138
- let n = i;
1139
-
1140
- const m = matchWord(src, n, c, { phrase: 'func', spaceAfter: true });
1141
- if (!m.is) return opVariableAccess(src, n, c);
1142
- ({ i: n } = m);
1143
-
1144
- let argNames;
1145
- ({ i: n, argNames } = await parseFunctionHeader(src, n, c));
1146
-
1147
- let ops;
1148
- ({ i: n, ops } = await opSubRoute(src, n, c));
1149
-
1150
- return { i: n, ops: [{ op: OP_FUNCTION_DEFINITION, params: { argNames, pipe: ops } }] };
1151
- }
1152
-
1153
- /** Parse variable definition at i */
1154
- export async function opVariableAccess(src, i, c) {
1155
- let n = i;
1156
-
1157
- const v = safeVariable(src, n, c);
1158
- if (!v.is) return opValueAccess(src, n, c);
1159
- let name;
1160
- ({ i: n, value: name } = v);
1161
-
1162
- let selectors;
1163
- let canAssign;
1164
- const ac = await parseAccess(src, n, c);
1165
- if (ac.is) ({ i: n, selectors, canAssign } = ac);
1166
- else {
1167
- selectors = [];
1168
- canAssign = true;
1169
- }
1170
-
1171
- if (!canAssign) {
1172
- const ops = [{ op: OP_VARIABLE, params: { name } }];
1173
-
1174
- if (selectors.length === 0) return { i: n, ops };
1175
- return { i: n, ops: [{ op: OP_ACCESS, params: { pipe: ops, selectors } }] };
1176
- }
1177
-
1178
- const as = await parseAssignment(src, n, c);
1179
- if (!as.is) {
1180
- const ops = [{ op: OP_VARIABLE, params: { name } }];
1181
-
1182
- if (selectors.length === 0) return { i: n, ops };
1183
- return { i: n, ops: [{ op: OP_ACCESS, params: { pipe: ops, selectors } }] };
1184
- }
1185
- let opAssignment;
1186
- ({ i: n, assignment: opAssignment } = as);
1187
-
1188
- if (selectors.length === 0 && opAssignment.op === OPU_SET) {
1189
- return {
1190
- i: n,
1191
- ops: [{ op: OP_VARIABLE_DEFINITION, params: { name, pipe: opAssignment.params.pipe } }],
1192
- };
1193
- }
1194
-
1195
- return {
1196
- i: n,
1197
- ops: [
1198
- {
1199
- op: OP_VARIABLE_DEFINITION,
1200
- params: {
1201
- name,
1202
- pipe: [
1203
- {
1204
- op: OP_ASSIGNMENT,
1205
- params: {
1206
- pipe: [{ op: OP_VARIABLE, params: { name } }],
1207
- selectors,
1208
- assignment: opAssignment,
1209
- },
1210
- },
1211
- ],
1212
- },
1213
- },
1214
- ],
1215
- };
1216
- }
1217
-
1218
- /** Parse variable access at i */
1219
- export async function opValueAccess(src, i, c) {
1220
- let n = i;
1221
-
1222
- const selectors = [];
1223
-
1224
- let ops;
1225
- let m = matchWord(src, n, c, { phrase: '.' });
1226
- if (!m.is) ({ i: n, ops } = await opObjectConstructor(src, n, c));
1227
- else {
1228
- ({ i: n } = m);
1229
- ops = [];
1230
-
1231
- const v = safeVariable(src, n, c);
1232
- if (v.is) {
1233
- let name;
1234
- ({ i: n, value: name } = v);
1235
-
1236
- let optional;
1237
- m = matchWord(src, n, c, { phrase: '?', notBeforeSet: '?=' });
1238
- if (m.is) ({ i: n, is: optional } = m);
1239
-
1240
- selectors.push({
1241
- op: OPA_FIELD,
1242
- params: { pipe: [{ op: OP_STRING, params: { string: name } }], optional },
1243
- });
1244
- }
1245
- }
1246
-
1247
- const ac = await parseAccess(src, n, c, {
1248
- identity: ops.length === 0 && selectors.length === 0,
1249
- });
1250
- let canAssign;
1251
- if (ac.is) {
1252
- ({ i: n, canAssign } = ac);
1253
- selectors.push(...ac.selectors);
1254
- } else canAssign = selectors.length > 0;
1255
-
1256
- if (selectors.length === 0) return { i: n, ops };
1257
-
1258
- if (!canAssign) return { i: n, ops: [{ op: OP_ACCESS, params: { pipe: ops, selectors } }] };
1259
-
1260
- const as = await parseAssignment(src, n, c);
1261
- if (!as.is) return { i: n, ops: [{ op: OP_ACCESS, params: { pipe: ops, selectors } }] };
1262
- let opAssignment;
1263
- ({ i: n, assignment: opAssignment } = as);
1264
-
1265
- return {
1266
- i: n,
1267
- ops: [{ op: OP_ASSIGNMENT, params: { pipe: ops, selectors, assignment: opAssignment } }],
1268
- };
1269
- }
1270
-
1271
- /** Parse object constructor at i */
1272
- export async function opObjectConstructor(src, i, c) {
1273
- let n = i;
1274
-
1275
- let m = matchWord(src, n, c, { phrase: '{' });
1276
- if (!m.is) return opArrayConstructor(src, n, c);
1277
- ({ i: n } = m);
1278
-
1279
- const fields = [];
1280
-
1281
- m = matchWord(src, n, c, { phrase: '}' });
1282
- if (m.is) ({ i: n } = m);
1283
- else
1284
- for (;;) {
1285
- m = matchWord(src, n, c, { phrase: '(' });
1286
- if (m.is) {
1287
- ({ i: n } = m);
1288
-
1289
- let opsKey;
1290
- ({ i: n, ops: opsKey } = await opPipe(src, n, c));
1291
-
1292
- m = matchWord(src, n, c, { phrase: ')' });
1293
- ({ i: n } = m);
1294
- if (!m.is)
1295
- return errorUnexpectedToken(src, n, c, { operator: 'object', message: "expected ')'" });
1296
-
1297
- let optional;
1298
- ({ i: n, is: optional } = matchWord(src, n, c, { phrase: '?' }));
1299
-
1300
- m = matchWord(src, n, c, { phrase: ':' });
1301
- ({ i: n } = m);
1302
- if (!m.is)
1303
- return errorUnexpectedToken(src, n, c, { operator: 'object', message: "expected ':'" });
1304
-
1305
- let opsValue;
1306
- ({ i: n, ops: opsValue } = await opSubPipe(src, n, c));
1307
-
1308
- fields.push({ key: opsKey, value: opsValue, optional });
1309
-
1310
- m = matchWord(src, n, c, { phrase: '}' });
1311
- if (m.is) {
1312
- ({ i: n } = m);
1313
- break;
1314
- }
1315
-
1316
- m = matchWord(src, n, c, { phrase: ',' });
1317
- ({ i: n } = m);
1318
- if (!m.is)
1319
- return errorUnexpectedToken(src, n, c, {
1320
- operator: 'object',
1321
- message: "expected ',' or '}'",
1322
- });
1323
-
1324
- continue;
1325
- }
1326
-
1327
- const s = await parseString(src, n, c);
1328
- if (s.is) {
1329
- let opsKey;
1330
- ({ i: n, ops: opsKey } = s);
1331
-
1332
- m = matchWord(src, n, c, { phrase: ':' });
1333
- ({ i: n } = m);
1334
- if (!m.is)
1335
- return errorUnexpectedToken(src, n, c, { operator: 'object', message: "expected ':'" });
1336
-
1337
- let opsValue;
1338
- ({ i: n, ops: opsValue } = await opSubPipe(src, n, c));
1339
-
1340
- fields.push({ key: opsKey, value: opsValue, optional: false });
1341
-
1342
- m = matchWord(src, n, c, { phrase: '}' });
1343
- if (m.is) {
1344
- ({ i: n } = m);
1345
- break;
1346
- }
1347
-
1348
- m = matchWord(src, n, c, { phrase: ',' });
1349
- ({ i: n } = m);
1350
- if (!m.is)
1351
- return errorUnexpectedToken(src, n, c, {
1352
- operator: 'object',
1353
- message: "expected ',' or '}'",
1354
- });
1355
-
1356
- continue;
1357
- }
1358
-
1359
- const v = safeVariable(src, n, c);
1360
- if (v.is) {
1361
- let name;
1362
- ({ i: n, value: name } = v);
1363
-
1364
- let opsValue;
1365
- m = matchWord(src, n, c, { phrase: ':' });
1366
- if (m.is) {
1367
- ({ i: n } = m);
1368
-
1369
- ({ i: n, ops: opsValue } = await opSubPipe(src, n, c));
1370
- } else {
1371
- let optional;
1372
- ({ i: n, is: optional } = matchWord(src, n, c, { phrase: '?' }));
1373
-
1374
- opsValue = optional
1375
- ? [
1376
- {
1377
- op: OP_TRY,
1378
- params: {
1379
- try: [{ op: OP_VARIABLE, params: { name } }],
1380
- catch: [{ op: OP_VOID }],
1381
- },
1382
- },
1383
- ]
1384
- : [{ op: OP_VARIABLE, params: { name } }];
1385
- }
1386
-
1387
- fields.push({
1388
- key: [{ op: OP_STRING, params: { string: name } }],
1389
- value: opsValue,
1390
- optional: false,
1391
- });
1392
-
1393
- m = matchWord(src, n, c, { phrase: '}' });
1394
- if (m.is) {
1395
- ({ i: n } = m);
1396
- break;
1397
- }
1398
-
1399
- m = matchWord(src, n, c, { phrase: ',' });
1400
- ({ i: n } = m);
1401
- if (!m.is)
1402
- return errorUnexpectedToken(src, n, c, {
1403
- operator: 'object',
1404
- message: "expected ',' or '}'",
1405
- });
1406
-
1407
- continue;
1408
- }
1409
-
1410
- return errorUnexpectedToken(src, n, c, {
1411
- operator: 'object',
1412
- message: 'expected field declaration',
1413
- });
1414
- }
1415
-
1416
- return { i: n, ops: [{ op: OP_OBJECT_CONSTRUCTOR, params: { fields } }] };
1417
- }
1418
-
1419
- /** Parse array constructor at i */
1420
- export async function opArrayConstructor(src, i, c) {
1421
- let n = i;
1422
-
1423
- let m = matchWord(src, n, c, { phrase: '[' });
1424
- ({ i: n } = m);
1425
- if (!m.is) return opStringLiteral(src, n, c);
1426
-
1427
- let ops;
1428
-
1429
- m = matchWord(src, n, c, { phrase: ']' });
1430
- if (m.is) {
1431
- ({ i: n } = m);
1432
-
1433
- ops = [{ op: OP_VOID }];
1434
- } else {
1435
- ({ i: n, ops } = await opPipe(src, n, c));
1436
-
1437
- m = matchWord(src, n, c, { phrase: ']' });
1438
- ({ i: n } = m);
1439
- if (!m.is)
1440
- return errorUnexpectedToken(src, n, c, { operator: 'array', message: "expected ']'" });
1441
- }
1442
-
1443
- return { i: n, ops: [{ op: OP_ARRAY_CONSTRUCTOR, params: { pipe: ops } }] };
1444
- }
1445
-
1446
- /** Parse string literal at i */
1447
- export async function opStringLiteral(src, i, c) {
1448
- let n = i;
1449
-
1450
- const s = await parseString(src, n, c);
1451
- if (!s.is) return opGroup(src, n, c);
1452
- ({ i: n } = s);
1453
-
1454
- return { i: n, ops: s.ops };
1455
- }
1456
-
1457
- /** Parse group at i */
1458
- export async function opGroup(src, i, c) {
1459
- let n = i;
1460
-
1461
- let m = matchWord(src, n, c, { phrase: '(' });
1462
- ({ i: n } = m);
1463
- if (!m.is) return errorUnexpectedToken(src, n, c);
1464
-
1465
- let ops;
1466
- ({ i: n, ops } = await opPipe(src, n, c));
1467
-
1468
- m = matchWord(src, n, c, { phrase: ')' });
1469
- ({ i: n } = m);
1470
- if (!m.is) return errorUnexpectedToken(src, n, c, { operator: 'group', message: "expected ')'" });
1471
-
1472
- return { i: n, ops };
1473
- }