@futpib/parser 1.0.0 → 1.0.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 (380) hide show
  1. package/build/allSettledStream.js +32 -14
  2. package/build/allSettledStream.test.js +32 -0
  3. package/build/androidPackage.d.ts +39 -0
  4. package/build/androidPackageParser.d.ts +17 -0
  5. package/build/androidPackageParser.js +183 -0
  6. package/build/{apkParser.test.js → androidPackageParser.test.js} +7 -7
  7. package/build/androidPackageUnparser.d.ts +4 -0
  8. package/build/androidPackageUnparser.js +90 -0
  9. package/build/androidPackageUnparser.test.js +26 -0
  10. package/build/arbitrarilySlicedAsyncInterable.d.ts +3 -1
  11. package/build/arbitrarilySlicedAsyncInterable.js +3 -3
  12. package/build/arbitraryFileSystemEntry.js +1 -1
  13. package/build/arbitraryZip.d.ts +1 -1
  14. package/build/arbitraryZip.js +13 -19
  15. package/build/arbitraryZipPermissions.d.ts +1 -8
  16. package/build/arbitraryZipPermissions.js +1 -16
  17. package/build/arbitraryZipStream.d.ts +1 -1
  18. package/build/arbitraryZipStream.js +3 -3
  19. package/build/arrayParser.d.ts +1 -1
  20. package/build/arrayParser.js +2 -2
  21. package/build/arrayParser.test.js +4 -4
  22. package/build/arrayUnparser.d.ts +2 -0
  23. package/build/arrayUnparser.js +8 -0
  24. package/build/backsmali.d.ts +1 -0
  25. package/build/backsmali.js +22 -0
  26. package/build/bsonParser.js +6 -2
  27. package/build/bsonParser.test.js +2 -2
  28. package/build/{parserInvariant.d.ts → customInvariant.d.ts} +1 -1
  29. package/build/{parserInvariant.js → customInvariant.js} +1 -1
  30. package/build/dalvikBytecodeParser/formatParsers.d.ts +97 -0
  31. package/build/dalvikBytecodeParser/formatParsers.js +169 -0
  32. package/build/dalvikBytecodeParser.d.ts +107 -0
  33. package/build/dalvikBytecodeParser.js +836 -0
  34. package/build/dalvikExecutable.d.ts +158 -0
  35. package/build/dalvikExecutable.js +20 -0
  36. package/build/dalvikExecutableParser/stringSyntaxParser.d.ts +4 -0
  37. package/build/dalvikExecutableParser/stringSyntaxParser.js +76 -0
  38. package/build/dalvikExecutableParser/typeParsers.d.ts +10 -0
  39. package/build/dalvikExecutableParser/typeParsers.js +34 -0
  40. package/build/dalvikExecutableParser/typedNumbers.d.ts +90 -0
  41. package/build/dalvikExecutableParser/typedNumbers.js +19 -0
  42. package/build/dalvikExecutableParser.d.ts +5 -0
  43. package/build/dalvikExecutableParser.js +1439 -0
  44. package/build/dalvikExecutableParser.test.js +70 -0
  45. package/build/dalvikExecutableParserAgainstSmaliParser.test.js +298 -0
  46. package/build/debugLogInputParser.d.ts +4 -0
  47. package/build/debugLogInputParser.js +16 -0
  48. package/build/debugLogParser.d.ts +1 -1
  49. package/build/debugLogParser.js +15 -4
  50. package/build/disjunctionParser.d.ts +2 -1
  51. package/build/disjunctionParser.js +4 -2
  52. package/build/elementParser.d.ts +2 -2
  53. package/build/elementParser.js +1 -1
  54. package/build/elementTerminatedArrayParser.d.ts +3 -0
  55. package/build/elementTerminatedArrayParser.js +18 -0
  56. package/build/elementTerminatedArrayParser.test.js +52 -0
  57. package/build/elementTerminatedSequenceArrayParser.d.ts +3 -0
  58. package/build/elementTerminatedSequenceArrayParser.js +32 -0
  59. package/build/elementTerminatedSequenceArrayParser.test.js +34 -0
  60. package/build/elementTerminatedSequenceParser.d.ts +3 -0
  61. package/build/elementTerminatedSequenceParser.js +27 -0
  62. package/build/elementTerminatedSequenceParser.test.js +34 -0
  63. package/build/endOfInputParser.d.ts +2 -2
  64. package/build/exactElementParser.d.ts +1 -1
  65. package/build/exactElementParser.js +10 -5
  66. package/build/exactSequenceParser.d.ts +2 -1
  67. package/build/exactSequenceParser.js +12 -2
  68. package/build/fetchCid.d.ts +1 -0
  69. package/build/fetchCid.js +107 -0
  70. package/build/fixedLengthSequenceParser.d.ts +1 -0
  71. package/build/fixedLengthSequenceParser.js +18 -1
  72. package/build/fixedLengthSequenceParser.test.js +41 -0
  73. package/build/hasExecutable.d.ts +1 -0
  74. package/build/hasExecutable.js +8 -0
  75. package/build/highResolutionTimer.d.ts +16 -0
  76. package/build/highResolutionTimer.js +42 -0
  77. package/build/index.d.ts +5 -2
  78. package/build/index.js +3 -0
  79. package/build/inputReader.d.ts +14 -3
  80. package/build/inputReader.js +43 -6
  81. package/build/inputReader.test.js +172 -7
  82. package/build/inputReaderState.d.ts +10 -0
  83. package/build/inputReaderState.js +16 -0
  84. package/build/inspect.d.ts +1 -0
  85. package/build/inspect.js +7 -0
  86. package/build/javaKeyStoreParser.d.ts +2 -0
  87. package/build/javaKeyStoreParser.js +67 -0
  88. package/build/javaKeyStoreParser.test.d.ts +1 -0
  89. package/build/javaKeyStoreParser.test.js +16 -0
  90. package/build/jsonParser.d.ts +2 -0
  91. package/build/jsonParser.js +13 -15
  92. package/build/jsonParser.test.js +2 -2
  93. package/build/leb128Parser.d.ts +7 -0
  94. package/build/leb128Parser.js +82 -0
  95. package/build/leb128Parser.test.d.ts +1 -0
  96. package/build/leb128Parser.test.js +107 -0
  97. package/build/listParser.d.ts +1 -1
  98. package/build/listParser.js +5 -5
  99. package/build/lookaheadParser.d.ts +2 -0
  100. package/build/lookaheadParser.js +14 -0
  101. package/build/{negativeLookahead.d.ts → negativeLookaheadParser.d.ts} +1 -1
  102. package/build/negativeLookaheadParser.js +24 -0
  103. package/build/negativeLookaheadParser.test.d.ts +1 -0
  104. package/build/negativeLookaheadParser.test.js +30 -0
  105. package/build/nonEmptyArrayParser.d.ts +2 -0
  106. package/build/nonEmptyArrayParser.js +32 -0
  107. package/build/nonEmptyArrayParser.test.d.ts +1 -0
  108. package/build/nonEmptyArrayParser.test.js +16 -0
  109. package/build/optionalParser.d.ts +1 -1
  110. package/build/optionalParser.js +2 -2
  111. package/build/parser.d.ts +13 -4
  112. package/build/parser.js +67 -32
  113. package/build/parser.test.js +94 -27
  114. package/build/parserAccessorParser.d.ts +1 -1
  115. package/build/parserAccessorParser.js +9 -1
  116. package/build/parserConsumedSequenceParser.d.ts +2 -0
  117. package/build/parserConsumedSequenceParser.js +22 -0
  118. package/build/parserContext.d.ts +20 -6
  119. package/build/parserContext.js +70 -40
  120. package/build/parserContext.test.js +34 -7
  121. package/build/parserCreatorCompose.d.ts +4 -3
  122. package/build/parserCreatorCompose.js +9 -3
  123. package/build/parserError.d.ts +6 -0
  124. package/build/parserError.js +6 -6
  125. package/build/parserImplementationInvariant.d.ts +1 -1
  126. package/build/parserImplementationInvariant.js +2 -2
  127. package/build/parserInputCompanion.d.ts +35 -0
  128. package/build/parserInputCompanion.js +68 -0
  129. package/build/promiseCompose.d.ts +1 -1
  130. package/build/promiseCompose.js +12 -1
  131. package/build/promiseSettled.d.ts +1 -0
  132. package/build/promiseSettled.js +4 -0
  133. package/build/quantifierParser.d.ts +2 -0
  134. package/build/quantifierParser.js +17 -0
  135. package/build/separatedArrayParser.d.ts +2 -0
  136. package/build/separatedArrayParser.js +39 -0
  137. package/build/separatedArrayParser.test.d.ts +1 -0
  138. package/build/separatedArrayParser.test.js +21 -0
  139. package/build/sequenceBuffer.d.ts +13 -3
  140. package/build/sequenceBuffer.js +60 -8
  141. package/build/sequenceBuffer.test.js +59 -2
  142. package/build/sequenceTerminatedSequenceParser.d.ts +5 -0
  143. package/build/sequenceTerminatedSequenceParser.js +32 -0
  144. package/build/sequenceTerminatedSequenceParser.test.d.ts +1 -0
  145. package/build/sequenceTerminatedSequenceParser.test.js +37 -0
  146. package/build/sequenceUnparser.d.ts +2 -0
  147. package/build/sequenceUnparser.js +6 -0
  148. package/build/skipParser.d.ts +1 -1
  149. package/build/skipParser.js +4 -2
  150. package/build/skipToParser.d.ts +2 -0
  151. package/build/skipToParser.js +11 -0
  152. package/build/sliceBoundedParser.d.ts +2 -2
  153. package/build/sliceBoundedParser.js +8 -3
  154. package/build/sliceBoundedParser.test.js +37 -3
  155. package/build/smali.d.ts +1 -0
  156. package/build/smali.js +17 -0
  157. package/build/smaliParser.d.ts +12 -0
  158. package/build/smaliParser.js +656 -0
  159. package/build/smaliParser.test.d.ts +1 -0
  160. package/build/smaliParser.test.js +115 -0
  161. package/build/terminatedArrayParser.d.ts +4 -2
  162. package/build/terminatedArrayParser.js +82 -5
  163. package/build/terminatedArrayParser.test.d.ts +1 -0
  164. package/build/terminatedArrayParser.test.js +131 -0
  165. package/build/toAsyncIterable.d.ts +1 -0
  166. package/build/toAsyncIterable.js +7 -0
  167. package/build/toAsyncIterator.d.ts +1 -0
  168. package/build/toAsyncIterator.js +33 -0
  169. package/build/tupleParser.d.ts +4 -0
  170. package/build/tupleParser.js +1 -5
  171. package/build/uint8Array.d.ts +1 -0
  172. package/build/uint8Array.js +7 -0
  173. package/build/unionParser.d.ts +2 -1
  174. package/build/unionParser.js +27 -12
  175. package/build/unionParser.test.d.ts +1 -0
  176. package/build/unionParser.test.js +60 -0
  177. package/build/unparser.d.ts +8 -0
  178. package/build/unparser.js +104 -0
  179. package/build/unparser.test.d.ts +1 -0
  180. package/build/unparser.test.js +150 -0
  181. package/build/unparserContext.d.ts +31 -0
  182. package/build/unparserContext.js +74 -0
  183. package/build/unparserError.d.ts +9 -0
  184. package/build/unparserError.js +9 -0
  185. package/build/unparserImplementationInvariant.d.ts +2 -0
  186. package/build/unparserImplementationInvariant.js +5 -0
  187. package/build/unparserOutputCompanion.d.ts +15 -0
  188. package/build/unparserOutputCompanion.js +13 -0
  189. package/build/zip.d.ts +9 -17
  190. package/build/zipParser.d.ts +20 -12
  191. package/build/zipParser.js +83 -71
  192. package/build/zipParser.test.js +2 -7
  193. package/build/zipUnparser.d.ts +8 -0
  194. package/build/zipUnparser.js +200 -0
  195. package/build/zipUnparser.test.d.ts +1 -0
  196. package/build/zipUnparser.test.js +80 -0
  197. package/package.json +26 -8
  198. package/src/allSettledStream.test.ts +40 -0
  199. package/src/allSettledStream.ts +47 -15
  200. package/src/androidPackage.ts +48 -0
  201. package/src/{apkParser.test.ts → androidPackageParser.test.ts} +7 -8
  202. package/src/androidPackageParser.test.ts.md +271 -0
  203. package/src/androidPackageParser.test.ts.snap +0 -0
  204. package/src/androidPackageParser.ts +440 -0
  205. package/src/androidPackageUnparser.test.ts +36 -0
  206. package/src/androidPackageUnparser.ts +120 -0
  207. package/src/arbitrarilySlicedAsyncInterable.ts +7 -2
  208. package/src/arbitraryFileSystemEntry.ts +2 -4
  209. package/src/arbitraryZip.ts +20 -27
  210. package/src/arbitraryZipPermissions.ts +0 -25
  211. package/src/arbitraryZipStream.ts +4 -4
  212. package/src/arrayParser.test.ts +4 -4
  213. package/src/arrayParser.ts +3 -2
  214. package/src/arrayUnparser.ts +13 -0
  215. package/src/backsmali.ts +30 -0
  216. package/src/bsonParser.test.ts +2 -2
  217. package/src/bsonParser.ts +16 -5
  218. package/src/{parserInvariant.ts → customInvariant.ts} +2 -2
  219. package/src/dalvikBytecodeParser/formatParsers.ts +421 -0
  220. package/src/dalvikBytecodeParser.ts +2074 -0
  221. package/src/dalvikExecutable.ts +220 -0
  222. package/src/dalvikExecutableParser/stringSyntaxParser.ts +145 -0
  223. package/src/dalvikExecutableParser/typeParsers.ts +65 -0
  224. package/src/dalvikExecutableParser/typedNumbers.ts +57 -0
  225. package/src/dalvikExecutableParser.test.ts +89 -0
  226. package/src/dalvikExecutableParser.test.ts.md +634 -0
  227. package/src/dalvikExecutableParser.test.ts.snap +0 -0
  228. package/src/dalvikExecutableParser.ts +2768 -0
  229. package/src/dalvikExecutableParserAgainstSmaliParser.test.ts +346 -0
  230. package/src/debugLogInputParser.ts +28 -0
  231. package/src/debugLogParser.ts +20 -4
  232. package/src/disjunctionParser.ts +10 -5
  233. package/src/elementParser.ts +3 -3
  234. package/src/elementTerminatedArrayParser.test.ts +99 -0
  235. package/src/elementTerminatedArrayParser.ts +31 -0
  236. package/src/elementTerminatedSequenceArrayParser.test.ts +54 -0
  237. package/src/elementTerminatedSequenceArrayParser.ts +52 -0
  238. package/src/elementTerminatedSequenceParser.test.ts +54 -0
  239. package/src/elementTerminatedSequenceParser.ts +43 -0
  240. package/src/endOfInputParser.ts +4 -4
  241. package/src/exactElementParser.ts +18 -12
  242. package/src/exactSequenceParser.ts +24 -3
  243. package/src/fetchCid.ts +125 -0
  244. package/src/fixedLengthSequenceParser.test.ts +77 -0
  245. package/src/fixedLengthSequenceParser.ts +28 -1
  246. package/src/hasExecutable.ts +11 -0
  247. package/src/highResolutionTimer.ts +49 -0
  248. package/src/index.ts +15 -2
  249. package/src/inputReader.test.ts +216 -7
  250. package/src/inputReader.ts +80 -5
  251. package/src/inputReaderState.ts +33 -0
  252. package/src/inspect.ts +9 -0
  253. package/src/javaKeyStore.ts +0 -0
  254. package/src/javaKeyStoreParser.test.ts +22 -0
  255. package/src/javaKeyStoreParser.test.ts.md +103 -0
  256. package/src/javaKeyStoreParser.test.ts.snap +0 -0
  257. package/src/javaKeyStoreParser.ts +136 -0
  258. package/src/jsonParser.test.ts +2 -2
  259. package/src/jsonParser.ts +23 -34
  260. package/src/leb128Parser.test.ts +171 -0
  261. package/src/leb128Parser.ts +125 -0
  262. package/src/listParser.ts +6 -6
  263. package/src/lookaheadParser.ts +19 -0
  264. package/src/negativeLookaheadParser.test.ts +53 -0
  265. package/src/negativeLookaheadParser.ts +36 -0
  266. package/src/nonEmptyArrayParser.test.ts +20 -0
  267. package/src/nonEmptyArrayParser.ts +44 -0
  268. package/src/optionalParser.ts +4 -3
  269. package/src/parser.test.ts +148 -27
  270. package/src/parser.test.ts.md +21 -21
  271. package/src/parser.test.ts.snap +0 -0
  272. package/src/parser.ts +153 -49
  273. package/src/parserAccessorParser.ts +12 -2
  274. package/src/parserConsumedSequenceParser.ts +29 -0
  275. package/src/parserContext.test.ts +38 -7
  276. package/src/parserContext.ts +127 -51
  277. package/src/parserCreatorCompose.ts +25 -7
  278. package/src/parserError.ts +9 -6
  279. package/src/parserImplementationInvariant.ts +2 -2
  280. package/src/parserInputCompanion.ts +102 -0
  281. package/src/promiseCompose.ts +17 -3
  282. package/src/promiseSettled.ts +6 -0
  283. package/src/quantifierParser.ts +25 -0
  284. package/src/separatedArrayParser.test.ts +34 -0
  285. package/src/separatedArrayParser.ts +55 -0
  286. package/src/sequenceBuffer.test.ts +72 -2
  287. package/src/sequenceBuffer.ts +93 -7
  288. package/src/sequenceTerminatedSequenceParser.test.ts +60 -0
  289. package/src/sequenceTerminatedSequenceParser.ts +62 -0
  290. package/src/sequenceUnparser.ts +9 -0
  291. package/src/skipParser.ts +7 -5
  292. package/src/skipToParser.ts +16 -0
  293. package/src/sliceBoundedParser.test.ts +45 -3
  294. package/src/sliceBoundedParser.ts +21 -3
  295. package/src/smali.ts +24 -0
  296. package/src/smaliParser.test.ts +132 -0
  297. package/src/smaliParser.test.ts.md +2320 -0
  298. package/src/smaliParser.test.ts.snap +0 -0
  299. package/src/smaliParser.ts +1166 -0
  300. package/src/terminatedArrayParser.test.ts +258 -0
  301. package/src/terminatedArrayParser.ts +111 -6
  302. package/src/toAsyncIterable.ts +7 -0
  303. package/src/toAsyncIterator.ts +48 -0
  304. package/src/tupleParser.ts +8 -5
  305. package/src/uint8Array.ts +10 -0
  306. package/src/unionParser.test.ts +79 -0
  307. package/src/unionParser.ts +44 -16
  308. package/src/unparser.test.ts +221 -0
  309. package/src/unparser.ts +209 -0
  310. package/src/unparserContext.ts +127 -0
  311. package/src/unparserError.ts +12 -0
  312. package/src/unparserImplementationInvariant.ts +6 -0
  313. package/src/unparserOutputCompanion.ts +24 -0
  314. package/src/zip.ts +10 -22
  315. package/src/zipParser.test.ts +2 -8
  316. package/src/zipParser.ts +223 -146
  317. package/src/zipUnparser.test.ts +119 -0
  318. package/src/zipUnparser.ts +300 -0
  319. package/build/apk.d.ts +0 -13
  320. package/build/apkParser.d.ts +0 -3
  321. package/build/apkParser.js +0 -135
  322. package/build/arbitraryDosDate.d.ts +0 -2
  323. package/build/arbitraryDosDate.js +0 -8
  324. package/build/arbitraryZipEntry.d.ts +0 -3
  325. package/build/arbitraryZipEntry.js +0 -26
  326. package/build/createDisjunctionParser.d.ts +0 -2
  327. package/build/createDisjunctionParser.js +0 -47
  328. package/build/createExactParser.d.ts +0 -2
  329. package/build/createExactParser.js +0 -12
  330. package/build/createSequentialUnionParser.d.ts +0 -2
  331. package/build/createSequentialUnionParser.js +0 -69
  332. package/build/fixedLengthChunkParser.d.ts +0 -2
  333. package/build/fixedLengthChunkParser.js +0 -12
  334. package/build/fixedLengthParser.d.ts +0 -2
  335. package/build/fixedLengthParser.js +0 -12
  336. package/build/inputChunkBuffer.d.ts +0 -15
  337. package/build/inputChunkBuffer.js +0 -40
  338. package/build/inputChunkBuffer.test.js +0 -34
  339. package/build/inputCompanion.d.ts +0 -18
  340. package/build/inputCompanion.js +0 -28
  341. package/build/invariantDefined.d.ts +0 -1
  342. package/build/invariantDefined.js +0 -5
  343. package/build/invariantIdentity.d.ts +0 -3
  344. package/build/invariantIdentity.js +0 -5
  345. package/build/jsonParser2.d.ts +0 -3
  346. package/build/jsonParser2.js +0 -52
  347. package/build/jsonParser2.test.js +0 -22
  348. package/build/negativeLookahead.js +0 -20
  349. package/build/parserCompose.d.ts +0 -3
  350. package/build/parserCompose.js +0 -7
  351. package/build/parserImplementationInvariantInvariant.d.ts +0 -3
  352. package/build/parserImplementationInvariantInvariant.js +0 -15
  353. package/build/promiseFish.d.ts +0 -1
  354. package/build/promiseFish.js +0 -3
  355. package/build/sequenceParser.d.ts +0 -3
  356. package/build/sequenceParser.js +0 -10
  357. package/build/terminatedSequenceParser.d.ts +0 -2
  358. package/build/terminatedSequenceParser.js +0 -24
  359. package/build/zipEntry.d.ts +0 -28
  360. package/build/zipFile.d.ts +0 -32
  361. package/build/zipFileEntry.d.ts +0 -6
  362. package/src/apk.ts +0 -16
  363. package/src/apkParser.test.ts.md +0 -268
  364. package/src/apkParser.test.ts.snap +0 -0
  365. package/src/apkParser.ts +0 -327
  366. package/src/inputCompanion.ts +0 -43
  367. package/src/invariantDefined.ts +0 -6
  368. package/src/invariantIdentity.ts +0 -8
  369. package/src/negativeLookahead.ts +0 -26
  370. /package/build/{apk.js → androidPackage.js} +0 -0
  371. /package/build/{apkParser.test.d.ts → androidPackageParser.test.d.ts} +0 -0
  372. /package/build/{arbitraryDosPermissions.d.ts → androidPackageUnparser.test.d.ts} +0 -0
  373. /package/build/{arbitraryDosPermissions.js → dalvikExecutableParser.test.d.ts} +0 -0
  374. /package/build/{inputChunkBuffer.test.d.ts → dalvikExecutableParserAgainstSmaliParser.test.d.ts} +0 -0
  375. /package/build/{jsonParser2.test.d.ts → elementTerminatedArrayParser.test.d.ts} +0 -0
  376. /package/build/{parserParsingInvariant.d.ts → elementTerminatedSequenceArrayParser.test.d.ts} +0 -0
  377. /package/build/{parserParsingInvariant.js → elementTerminatedSequenceParser.test.d.ts} +0 -0
  378. /package/build/{zipEntry.js → fixedLengthSequenceParser.test.d.ts} +0 -0
  379. /package/build/{zipFile.js → javaKeyStore.d.ts} +0 -0
  380. /package/build/{zipFileEntry.js → javaKeyStore.js} +0 -0
package/src/parser.ts CHANGED
@@ -1,14 +1,17 @@
1
- import invariant from 'invariant';
2
- import { type InputCompanion } from './inputCompanion.js';
1
+ import { Class } from 'type-fest';
2
+ import { type ParserInputCompanion } from './parserInputCompanion.js';
3
3
  import { InputReaderImplementation } from './inputReader.js';
4
4
  import { type ParserContext, ParserContextImplementation } from './parserContext.js';
5
- import { DeriveSequenceElement } from './sequence.js';
5
+ import { type DeriveSequenceElement } from './sequence.js';
6
+ import { ParserError, ParserUnexpectedRemainingInputError } from './parserError.js';
7
+ import { toAsyncIterator } from './toAsyncIterator.js';
8
+ import { inputReaderStateCompanion } from './inputReaderState.js';
6
9
 
7
10
  export type Parser<
8
11
  Output,
9
12
  Sequence,
10
13
  Element = DeriveSequenceElement<Sequence>,
11
- > = (parserContext: ParserContext<Sequence, Element>) => Promise<Output>;
14
+ > = (parserContext: ParserContext<Sequence, Element>) => Output | Promise<Output>;
12
15
 
13
16
  export function getParserName(parser: Parser<any, any, any>, default_ = 'anonymous'): string {
14
17
  return parser.name || default_;
@@ -26,59 +29,152 @@ export function setParserName<
26
29
  return parser;
27
30
  }
28
31
 
32
+ const originalParserByClone = new WeakMap<Parser<any, any, any>, Parser<any, any, any>>();
33
+
34
+ export function cloneParser<
35
+ Output,
36
+ Sequence,
37
+ Element = DeriveSequenceElement<Sequence>,
38
+ >(
39
+ parser: Parser<Output, Sequence, Element>,
40
+ ): Parser<Output, Sequence, Element> {
41
+ const originalParser = originalParserByClone.get(parser) ?? parser;
42
+
43
+ const clone: Parser<Output, Sequence, Element> = (parserContext) => {
44
+ return originalParser(parserContext);
45
+ };
46
+
47
+ setParserName(clone, getParserName(parser));
48
+
49
+ originalParserByClone.set(clone, originalParser);
50
+
51
+ return clone;
52
+ }
53
+
29
54
  export type RunParserOptions<
30
55
  Output,
31
56
  Sequence,
32
57
  Element = DeriveSequenceElement<Sequence>,
33
58
  > = {
34
59
  errorJoinMode?: 'none' | 'deepest' | 'furthest' | 'all';
35
- }
60
+ parserContextClass?: Class<ParserContext<Sequence, Element>>;
61
+ };
36
62
 
37
- function isAsyncIterable<T>(value: any): value is AsyncIterable<T> {
38
- return value && typeof value[Symbol.asyncIterator] === 'function';
39
- }
63
+ export type RunParserWithRemainingInputResult<
64
+ Output,
65
+ Sequence,
66
+ Element = DeriveSequenceElement<Sequence>,
67
+ > = {
68
+ output: Output;
69
+ position: number;
70
+ remainingInput: undefined | AsyncIterable<Sequence>;
71
+ };
40
72
 
41
- function isIterable<T>(value: any): value is Iterable<T> {
42
- return value && typeof value[Symbol.iterator] === 'function';
73
+ async function withEnrichedParserError<
74
+ Output,
75
+ Sequence,
76
+ Element = DeriveSequenceElement<Sequence>,
77
+ >(
78
+ parserContext: ParserContext<Sequence, Element>,
79
+ inputReader: InputReaderImplementation<Sequence, Element>,
80
+ f: () => Promise<Output>,
81
+ ): Promise<Output> {
82
+ try {
83
+ return await f();
84
+ } catch (error) {
85
+ if (error instanceof ParserError) {
86
+ if (error.position === undefined) {
87
+ error.position = parserContext.position;
88
+ }
89
+
90
+ if (error.inputReaderSate === undefined) {
91
+ error.inputReaderSate = inputReader.toInputReaderState();
92
+ }
93
+ }
94
+
95
+ throw error;
96
+ }
43
97
  }
44
98
 
45
- function isIterator<T>(value: any): value is Iterator<T> {
46
- return value && typeof value.next === 'function';
47
- }
99
+ type RunParserInternalResult<
100
+ Output,
101
+ Sequence,
102
+ Element = DeriveSequenceElement<Sequence>,
103
+ > = {
104
+ outputPromise: Promise<Output>;
105
+ parserContext: ParserContext<Sequence, Element>;
106
+ inputReader: InputReaderImplementation<Sequence, Element>;
107
+ };
48
108
 
49
- function iteratorToAsyncIterator<T>(iterator: Iterator<T>): AsyncIterator<T> {
50
- return {
51
- next: async () => iterator.next(),
52
- };
53
- }
109
+ function runParserInternal<
110
+ Output,
111
+ Sequence,
112
+ Element = DeriveSequenceElement<Sequence>,
113
+ >(
114
+ parser: Parser<Output, Sequence, Element>,
115
+ input: AsyncIterator<Sequence> | AsyncIterable<Sequence> | Iterable<Sequence> | Sequence,
116
+ parserInputCompanion: ParserInputCompanion<Sequence, Element>,
117
+ options: RunParserOptions<Output, Sequence, Element> = {},
118
+ ): RunParserInternalResult<Output, Sequence, Element> {
119
+ const inputAsyncIterator = toAsyncIterator(input);
54
120
 
55
- function toAsyncIterator<T>(value: AsyncIterator<T> | AsyncIterable<T> | Iterable<T> | T): AsyncIterator<T> {
56
- if (
57
- typeof value === 'string'
58
- || value instanceof Uint8Array
59
- ) {
60
- return (async function*() {
61
- yield value as any;
62
- })();
63
- }
121
+ const inputReader = new InputReaderImplementation<Sequence, Element>(parserInputCompanion, inputAsyncIterator);
64
122
 
65
- if (isAsyncIterable(value)) {
66
- return value[Symbol.asyncIterator]();
67
- }
123
+ const ParserContext = options.parserContextClass ?? ParserContextImplementation;
68
124
 
69
- if (isIterable(value)) {
70
- return iteratorToAsyncIterator(value[Symbol.iterator]());
71
- }
125
+ const parserContext = new ParserContext<Sequence, Element>(parserInputCompanion, inputReader, undefined, {
126
+ ...options,
127
+ debugName: 'root',
128
+ });
72
129
 
73
- if (isIterator<T>(value)) {
74
- return iteratorToAsyncIterator(value);
75
- }
130
+ const outputPromise = (async () => {
131
+ try {
132
+ return await parser(parserContext);
133
+ } finally {
134
+ await parserContext.peek(0);
135
+ }
136
+ })();
76
137
 
77
- invariant(
78
- false,
79
- 'Value must be an async iterator, async iterable, iterable or iterator got %s.',
80
- value,
81
- );
138
+ return {
139
+ outputPromise,
140
+ parserContext,
141
+ inputReader,
142
+ };
143
+ }
144
+
145
+ export async function runParserWithRemainingInput<
146
+ Output,
147
+ Sequence,
148
+ Element = DeriveSequenceElement<Sequence>,
149
+ >(
150
+ parser: Parser<Output, Sequence, Element>,
151
+ input: AsyncIterator<Sequence> | AsyncIterable<Sequence> | Iterable<Sequence> | Sequence,
152
+ parserInputCompanion: ParserInputCompanion<Sequence, Element>,
153
+ options: RunParserOptions<Output, Sequence, Element> = {},
154
+ ): Promise<RunParserWithRemainingInputResult<Output, Sequence, Element>> {
155
+ const {
156
+ outputPromise,
157
+ parserContext,
158
+ inputReader,
159
+ } = runParserInternal(parser, input, parserInputCompanion, options);
160
+
161
+ return await withEnrichedParserError(parserContext, inputReader, async () => {
162
+ const output = await outputPromise;
163
+
164
+ const inputReaderState = inputReader.toInputReaderState();
165
+
166
+ const remainingInput = (
167
+ inputReaderStateCompanion.isDone(inputReaderState)
168
+ ? undefined
169
+ : inputReaderStateCompanion.toRemainingInputAsyncIterator(inputReaderState)
170
+ );
171
+
172
+ return {
173
+ output,
174
+ position: parserContext.position,
175
+ remainingInput,
176
+ };
177
+ });
82
178
  }
83
179
 
84
180
  export async function runParser<
@@ -88,16 +184,24 @@ export async function runParser<
88
184
  >(
89
185
  parser: Parser<Output, Sequence, Element>,
90
186
  input: AsyncIterator<Sequence> | AsyncIterable<Sequence> | Iterable<Sequence> | Sequence,
91
- inputCompanion: InputCompanion<Sequence, Element>,
187
+ parserInputCompanion: ParserInputCompanion<Sequence, Element>,
92
188
  options: RunParserOptions<Output, Sequence, Element> = {},
93
189
  ): Promise<Output> {
94
- const inputAsyncIterator = toAsyncIterator(input);
190
+ const {
191
+ outputPromise,
192
+ parserContext,
193
+ inputReader,
194
+ } = runParserInternal(parser, input, parserInputCompanion, options);
95
195
 
96
- const inputReader = new InputReaderImplementation<Sequence, Element>(inputCompanion, inputAsyncIterator);
97
- const parserContext = new ParserContextImplementation<Sequence, Element>(inputCompanion, inputReader, undefined, {
98
- ...options,
99
- debugName: 'root',
100
- });
196
+ return await withEnrichedParserError(parserContext, inputReader, async () => {
197
+ const output = await outputPromise;
198
+
199
+ const inputReaderState = inputReader.toInputReaderState();
101
200
 
102
- return parser(parserContext);
201
+ if (!inputReaderStateCompanion.isDone(inputReaderState)) {
202
+ throw new ParserUnexpectedRemainingInputError('Unexpected remaining input', 0, parserContext.position);
203
+ }
204
+
205
+ return output;
206
+ });
103
207
  }
@@ -1,3 +1,13 @@
1
- import { Parser } from "./parser.js";
1
+ import { getParserName, setParserName, type Parser } from './parser.js';
2
2
 
3
- export const createParserAccessorParser = <Output, Sequence>(parserAccessor: () => Parser<Output, Sequence>): Parser<Output, Sequence> => async parserContext => parserAccessor()(parserContext);
3
+ export const createParserAccessorParser = <Output, Sequence>(parserAccessor: () => Parser<Output, Sequence>): Parser<Output, Sequence> => {
4
+ const parserAccessorParser: Parser<Output, Sequence> = async parserContext => {
5
+ const parser = parserAccessor();
6
+
7
+ setParserName(parserAccessorParser, `parserAccessorParser(${getParserName(parser, 'anonymousParserAccessor')})`);
8
+
9
+ return parser(parserContext);
10
+ };
11
+
12
+ return parserAccessorParser;
13
+ }
@@ -0,0 +1,29 @@
1
+ import { createFixedLengthSequenceParser } from "./fixedLengthSequenceParser.js";
2
+ import { getParserName, Parser, setParserName } from "./parser.js";
3
+
4
+ export const createParserConsumedSequenceParser = <Output, Sequence>(
5
+ childParser: Parser<Output, Sequence>,
6
+ ): Parser<[Output, Sequence], Sequence> => {
7
+ const parserConsumedSequenceParser: Parser<[Output, Sequence], Sequence> = async parserContext => {
8
+ const initialPosition = parserContext.position;
9
+ const childParserContext = parserContext.lookahead();
10
+
11
+ let value: Output;
12
+ let consumedLength: number;
13
+ try {
14
+ value = await childParser(childParserContext);
15
+ consumedLength = childParserContext.position - initialPosition;
16
+ } finally {
17
+ childParserContext.dispose();
18
+ }
19
+
20
+ const consumedSequenceParser = createFixedLengthSequenceParser<Sequence>(consumedLength);
21
+ const consumedSequence = await consumedSequenceParser(parserContext);
22
+
23
+ return [value, consumedSequence];
24
+ };
25
+
26
+ setParserName(parserConsumedSequenceParser, `parserConsumedSequenceParser(${getParserName(childParser, 'anonymousParserConsumedSequenceParserChild')})`);
27
+
28
+ return parserConsumedSequenceParser;
29
+ };
@@ -1,6 +1,6 @@
1
1
  import test from 'ava';
2
2
  import { ParserContextImplementation } from './parserContext.js';
3
- import { stringInputCompanion } from './inputCompanion.js';
3
+ import { stringParserInputCompanion } from './parserInputCompanion.js';
4
4
  import { InputReaderImplementation } from './inputReader.js';
5
5
  import { ParserUnexpectedEndOfInputError } from './parserError.js';
6
6
 
@@ -13,7 +13,7 @@ const commonParserContextArguments = [
13
13
  ] as const;
14
14
 
15
15
  test('parserContext.read', async t => {
16
- const parserContext = new ParserContextImplementation(stringInputCompanion, new InputReaderImplementation(stringInputCompanion, (async function * () {
16
+ const parserContext = new ParserContextImplementation(stringParserInputCompanion, new InputReaderImplementation(stringParserInputCompanion, (async function * () {
17
17
  yield '';
18
18
  yield 'abc';
19
19
  yield 'def';
@@ -30,8 +30,31 @@ test('parserContext.read', async t => {
30
30
  });
31
31
  });
32
32
 
33
+ test('parserContext.readSequence', async t => {
34
+ const parserContext = new ParserContextImplementation(stringParserInputCompanion, new InputReaderImplementation(stringParserInputCompanion, (async function * () {
35
+ yield '';
36
+ yield 'abc';
37
+ yield 'def';
38
+ yield '';
39
+ yield 'gh';
40
+ })()), ...commonParserContextArguments);
41
+
42
+ t.is(await parserContext.readSequence(0, 0), '');
43
+ t.is(await parserContext.readSequence(0, 1), 'a');
44
+ t.is(await parserContext.readSequence(1, 4), 'cde');
45
+ t.is(await parserContext.readSequence(0, 1), 'f');
46
+ t.is(await parserContext.readSequence(0, 0), '');
47
+ t.is(await parserContext.readSequence(0, 1), 'g');
48
+ t.is(await parserContext.readSequence(1, 1), '');
49
+ t.is(await parserContext.readSequence(0, 0), '');
50
+
51
+ await t.throwsAsync(async () => parserContext.readSequence(0, 1), {
52
+ instanceOf: ParserUnexpectedEndOfInputError,
53
+ });
54
+ });
55
+
33
56
  test('parserContext.lookahead', async t => {
34
- const parserContext = new ParserContextImplementation(stringInputCompanion, new InputReaderImplementation(stringInputCompanion, (async function * () {
57
+ const parserContext = new ParserContextImplementation(stringParserInputCompanion, new InputReaderImplementation(stringParserInputCompanion, (async function * () {
35
58
  yield * 'abcdefgh';
36
59
  })()), ...commonParserContextArguments);
37
60
 
@@ -42,11 +65,15 @@ test('parserContext.lookahead', async t => {
42
65
  });
43
66
 
44
67
  t.is(await lookaheadContext3.peek(2), 'c');
68
+ t.is(await lookaheadContext3.peekSequence(2, 3), 'c');
45
69
  t.is(await lookaheadContext3.peek(3), undefined);
70
+ t.is(await lookaheadContext3.peekSequence(3, 4), undefined);
46
71
  t.is(await lookaheadContext3.read(0), 'a');
47
72
  t.is(await lookaheadContext3.read(0), 'b');
48
73
  t.is(await lookaheadContext3.read(0), 'c');
49
74
  t.is(await lookaheadContext3.peek(0), undefined);
75
+ t.is(await lookaheadContext3.peekSequence(0, 1), undefined);
76
+ t.is(await lookaheadContext3.peekSequence(0, 0), '');
50
77
 
51
78
  t.is(await parserContext.peek(0), 'a');
52
79
  t.is(await lookaheadContext1.peek(0), 'a');
@@ -63,10 +90,14 @@ test('parserContext.lookahead', async t => {
63
90
  t.is(lookaheadContext1.position, 1);
64
91
  t.is(lookaheadContext2.position, 0);
65
92
  t.is(lookaheadContext3.position, 3);
93
+
94
+ lookaheadContext3.skip(1);
95
+ t.is(await lookaheadContext3.peekSequence(0, 0), undefined);
96
+ t.is(lookaheadContext3.position, 4);
66
97
  });
67
98
 
68
99
  test('parserContext.unlookahead', async t => {
69
- const parserContext = new ParserContextImplementation(stringInputCompanion, new InputReaderImplementation(stringInputCompanion, (async function * () {
100
+ const parserContext = new ParserContextImplementation(stringParserInputCompanion, new InputReaderImplementation(stringParserInputCompanion, (async function * () {
70
101
  yield * 'abcdefgh';
71
102
  })()), ...commonParserContextArguments);
72
103
 
@@ -86,7 +117,7 @@ test('parserContext.unlookahead', async t => {
86
117
  });
87
118
 
88
119
  test('parserContext.unlookahead while peeking', async t => {
89
- const parserContext = new ParserContextImplementation(stringInputCompanion, new InputReaderImplementation(stringInputCompanion, (async function * () {
120
+ const parserContext = new ParserContextImplementation(stringParserInputCompanion, new InputReaderImplementation(stringParserInputCompanion, (async function * () {
90
121
  yield * 'abcdefgh';
91
122
  })()), ...commonParserContextArguments);
92
123
 
@@ -103,7 +134,7 @@ test('parserContext.unlookahead while peeking', async t => {
103
134
  });
104
135
 
105
136
  test('parserContext deep unlookahead normal order', async t => {
106
- const parserContext = new ParserContextImplementation(stringInputCompanion, new InputReaderImplementation(stringInputCompanion, (async function * () {
137
+ const parserContext = new ParserContextImplementation(stringParserInputCompanion, new InputReaderImplementation(stringParserInputCompanion, (async function * () {
107
138
  yield * 'abcdefgh';
108
139
  })()), ...commonParserContextArguments);
109
140
 
@@ -125,7 +156,7 @@ test('parserContext deep unlookahead normal order', async t => {
125
156
  });
126
157
 
127
158
  test('parserContext deep unlookahead weird order', async t => {
128
- const parserContext = new ParserContextImplementation(stringInputCompanion, new InputReaderImplementation(stringInputCompanion, (async function * () {
159
+ const parserContext = new ParserContextImplementation(stringParserInputCompanion, new InputReaderImplementation(stringParserInputCompanion, (async function * () {
129
160
  yield * 'abcdefgh';
130
161
  })()), ...commonParserContextArguments);
131
162