@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
@@ -0,0 +1,36 @@
1
+ import { getParserName, setParserName, type Parser } from './parser.js';
2
+ import { ParserParsingFailedError } from './parserError.js';
3
+
4
+ export const createNegativeLookaheadParser = <Sequence>(
5
+ childParser: Parser<unknown, Sequence>,
6
+ ): Parser<void, Sequence> => {
7
+ const negativeLookaheadParser: Parser<void, Sequence> = async parserContext => {
8
+ const childParserContext = parserContext.lookahead();
9
+
10
+ let childParserSuccess: boolean;
11
+
12
+ try {
13
+ await childParser(childParserContext);
14
+
15
+ childParserSuccess = true;
16
+ } catch (error) {
17
+ if (!(error instanceof ParserParsingFailedError)) {
18
+ throw error;
19
+ }
20
+
21
+ childParserSuccess = false;
22
+ } finally {
23
+ childParserContext.dispose();
24
+ }
25
+
26
+ parserContext.invariant(
27
+ !childParserSuccess,
28
+ 'Negative lookahead assertion failed for child parser %s.',
29
+ getParserName(childParser),
30
+ );
31
+ };
32
+
33
+ setParserName(negativeLookaheadParser, `(?!${getParserName(childParser)})`);
34
+
35
+ return negativeLookaheadParser;
36
+ };
@@ -0,0 +1,20 @@
1
+ import test from 'ava';
2
+ import { createNonEmptyArrayParser } from './nonEmptyArrayParser.js';
3
+ import { type Parser, runParser } from './parser.js';
4
+ import { stringParserInputCompanion } from './parserInputCompanion.js';
5
+ import { createExactElementParser } from './exactElementParser.js';
6
+
7
+ test('empty array does not match', async t => {
8
+ const parser: Parser<string[], string> = createNonEmptyArrayParser(createExactElementParser('0'));
9
+
10
+ await t.throwsAsync(() => runParser(parser, '', stringParserInputCompanion), {
11
+ message: /Expected .* to match at least once/,
12
+ });
13
+ });
14
+
15
+ test('non-empty array matches', async t => {
16
+ const parser: Parser<string[], string> = createNonEmptyArrayParser(createExactElementParser('0'));
17
+ const result = await runParser(parser, '0', stringParserInputCompanion);
18
+
19
+ t.deepEqual(result, ['0']);
20
+ });
@@ -0,0 +1,44 @@
1
+ import { getParserName, type Parser, setParserName } from './parser.js';
2
+ import { ParserParsingFailedError } from './parserError.js';
3
+
4
+ export const createNonEmptyArrayParser = <ElementOutput, Sequence>(
5
+ elementParser: Parser<ElementOutput, Sequence>,
6
+ ): Parser<ElementOutput[], Sequence> => {
7
+ const nonEmptyArrayParser: Parser<ElementOutput[], Sequence> = async parserContext => {
8
+ const elements: ElementOutput[] = [];
9
+
10
+ while (true) {
11
+ const elementParserContext = parserContext.lookahead();
12
+ const initialPosition = elementParserContext.position;
13
+ try {
14
+ const element = await elementParser(elementParserContext);
15
+ if (elementParserContext.position === initialPosition) {
16
+ break;
17
+ }
18
+
19
+ elements.push(element);
20
+ elementParserContext.unlookahead();
21
+ } catch (error) {
22
+ if (error instanceof ParserParsingFailedError) {
23
+ break;
24
+ }
25
+
26
+ throw error;
27
+ } finally {
28
+ elementParserContext.dispose();
29
+ }
30
+ }
31
+
32
+ parserContext.invariant(
33
+ elements.length > 0,
34
+ 'Expected elementParser (%s) to match at least once',
35
+ getParserName(elementParser, 'anonymousNonEmptyArrayChild'),
36
+ );
37
+
38
+ return elements;
39
+ };
40
+
41
+ setParserName(nonEmptyArrayParser, getParserName(elementParser, 'anonymousNonEmptyArrayChild') + '+');
42
+
43
+ return nonEmptyArrayParser;
44
+ };
@@ -1,11 +1,12 @@
1
- import { getParserName, Parser, setParserName } from "./parser.js";
2
- import { ParserParsingFailedError } from "./parserError.js";
1
+ import { getParserName, type Parser, setParserName } from './parser.js';
2
+ import { ParserParsingFailedError } from './parserError.js';
3
3
 
4
4
  export const createOptionalParser = <Output, Sequence>(
5
5
  childParser: Parser<Output, Sequence>,
6
6
  ): Parser<undefined | Output, Sequence> => {
7
7
  const optionalParser: Parser<undefined | Output, Sequence> = async parserContext => {
8
8
  const childParserContext = parserContext.lookahead();
9
+
9
10
  try {
10
11
  const value = await childParser(childParserContext);
11
12
  childParserContext.unlookahead();
@@ -24,4 +25,4 @@ export const createOptionalParser = <Output, Sequence>(
24
25
  setParserName(optionalParser, getParserName(childParser, 'anonymousOptionalChild') + '?');
25
26
 
26
27
  return optionalParser;
27
- }
28
+ };
@@ -1,34 +1,44 @@
1
1
  import test from 'ava';
2
+ import invariant from 'invariant';
2
3
  import { createUnionParser } from './unionParser.js';
3
- import { Parser, runParser } from './parser.js';
4
- import { stringInputCompanion, uint8ArrayInputCompanion } from './inputCompanion.js';
5
- import { ParserParsingInvariantError, ParserParsingJoinAllError, ParserParsingJoinDeepestError, ParserParsingJoinError, ParserParsingJoinFurthestError, ParserParsingJoinNoneError } from './parserError.js';
4
+ import { type Parser, runParser, runParserWithRemainingInput } from './parser.js';
5
+ import { stringParserInputCompanion, uint8ArrayParserInputCompanion } from './parserInputCompanion.js';
6
+ import {
7
+ ParserError,
8
+ ParserParsingInvariantError,
9
+ ParserParsingJoinAllError,
10
+ ParserParsingJoinDeepestError,
11
+ ParserParsingJoinError,
12
+ ParserParsingJoinFurthestError,
13
+ ParserParsingJoinNoneError,
14
+ ParserUnexpectedRemainingInputError,
15
+ } from './parserError.js';
6
16
  import { createTupleParser } from './tupleParser.js';
7
17
  import { promiseCompose } from './promiseCompose.js';
8
18
  import { createDisjunctionParser } from './disjunctionParser.js';
9
- import invariant from 'invariant';
10
- import { createExactSequenceParser } from './exactSequenceParser.js';
19
+ import { createExactSequenceNaiveParser } from './exactSequenceParser.js';
11
20
  import { createArrayParser } from './arrayParser.js';
12
21
  import { createElementParser } from './elementParser.js';
22
+ import { toAsyncIterable } from './toAsyncIterable.js';
13
23
 
14
24
  const aUnionParser = createUnionParser<string, string>([
15
- createExactSequenceParser('1'),
16
- createExactSequenceParser('aa'),
17
- createExactSequenceParser('AAA'),
25
+ createExactSequenceNaiveParser('1'),
26
+ createExactSequenceNaiveParser('aa'),
27
+ createExactSequenceNaiveParser('AAA'),
18
28
  ]);
19
29
 
20
30
  const abDisjunctionParser = createDisjunctionParser<string, string>([
21
31
  aUnionParser,
22
- createExactSequenceParser('2'),
23
- createExactSequenceParser('bb'),
24
- createExactSequenceParser('BBB'),
32
+ createExactSequenceNaiveParser('2'),
33
+ createExactSequenceNaiveParser('bb'),
34
+ createExactSequenceNaiveParser('BBB'),
25
35
  ]);
26
36
 
27
37
  const abcUnionParser = createUnionParser<string, string>([
28
38
  abDisjunctionParser,
29
- createExactSequenceParser('final_3'),
30
- createExactSequenceParser('final_cc'),
31
- createExactSequenceParser('final_CCC'),
39
+ createExactSequenceNaiveParser('final_3'),
40
+ createExactSequenceNaiveParser('final_cc'),
41
+ createExactSequenceNaiveParser('final_CCC'),
32
42
  ]);
33
43
 
34
44
  const sampleParser = promiseCompose(
@@ -44,6 +54,16 @@ async function * asyncIteratorFromString(string: string) {
44
54
  yield string;
45
55
  }
46
56
 
57
+ async function stringFromAsyncIterable(asyncIterable: AsyncIterable<string>) {
58
+ let string = '';
59
+
60
+ for await (const chunk of asyncIterable) {
61
+ string += chunk;
62
+ }
63
+
64
+ return string;
65
+ }
66
+
47
67
  function sortChildErrors(error: ParserParsingJoinNoneError) {
48
68
  error.childErrors.sort((a, b) => a.message.localeCompare(b.message));
49
69
 
@@ -55,14 +75,14 @@ function sortChildErrors(error: ParserParsingJoinNoneError) {
55
75
  }
56
76
 
57
77
  function removeStackLocations(errorStack: string) {
58
- return errorStack.replace(/((at [^\n]+)[\s\n]+)+(at [^\n]+)/g, 'at [LOCATIONS]');
78
+ return errorStack.replaceAll(/((at [^\n]+)[\s\n]+)+(at [^\n]+)/g, 'at [LOCATIONS]');
59
79
  }
60
80
 
61
81
  test('errorJoinMode: none', async t => {
62
- const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'), stringInputCompanion, {
82
+ const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'), stringParserInputCompanion, {
63
83
  errorJoinMode: 'none',
64
84
  }), {
65
- instanceOf: ParserParsingJoinNoneError
85
+ instanceOf: ParserParsingJoinNoneError,
66
86
  });
67
87
 
68
88
  t.is(error.position, 12);
@@ -72,10 +92,10 @@ test('errorJoinMode: none', async t => {
72
92
  });
73
93
 
74
94
  test('errorJoinMode: all', async t => {
75
- const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'), stringInputCompanion, {
95
+ const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'), stringParserInputCompanion, {
76
96
  errorJoinMode: 'all',
77
97
  }), {
78
- instanceOf: ParserParsingJoinAllError
98
+ instanceOf: ParserParsingJoinAllError,
79
99
  });
80
100
 
81
101
  sortChildErrors(error);
@@ -107,7 +127,7 @@ test('errorJoinMode: all', async t => {
107
127
  });
108
128
 
109
129
  test('errorJoinMode: deepest', async t => {
110
- const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'), stringInputCompanion, {
130
+ const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'), stringParserInputCompanion, {
111
131
  errorJoinMode: 'deepest',
112
132
  }), {
113
133
  instanceOf: ParserParsingJoinDeepestError,
@@ -142,7 +162,7 @@ test('errorJoinMode: deepest', async t => {
142
162
  });
143
163
 
144
164
  test('errorJoinMode: furthest', async t => {
145
- const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'), stringInputCompanion, {
165
+ const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'), stringParserInputCompanion, {
146
166
  errorJoinMode: 'furthest',
147
167
  }), {
148
168
  instanceOf: ParserParsingJoinFurthestError,
@@ -162,15 +182,116 @@ test('errorJoinMode: furthest', async t => {
162
182
  t.is(error1.position, 12);
163
183
  });
164
184
 
165
- test('throws on inputCompanion type mismatch', async t => {
185
+ test('throws on parserInputCompanion type mismatch', async t => {
166
186
  const anythingParser: Parser<any, any> = createArrayParser(createElementParser());
167
187
 
168
- await runParser(anythingParser, asyncIteratorFromString('anything'), stringInputCompanion);
169
- await runParser(anythingParser, 'anything', stringInputCompanion);
170
- await runParser(anythingParser, Buffer.from('anything'), uint8ArrayInputCompanion);
171
- await runParser(anythingParser, new Uint8Array([ 1, 2, 3 ]), uint8ArrayInputCompanion);
188
+ await runParser(anythingParser, asyncIteratorFromString('anything'), stringParserInputCompanion);
189
+ await runParser(anythingParser, 'anything', stringParserInputCompanion);
190
+ await runParser(anythingParser, Buffer.from('anything'), uint8ArrayParserInputCompanion);
191
+ await runParser(anythingParser, new Uint8Array([ 1, 2, 3 ]), uint8ArrayParserInputCompanion);
172
192
 
173
- await t.throwsAsync(runParser(anythingParser, asyncIteratorFromString('anything'), uint8ArrayInputCompanion), {
193
+ await t.throwsAsync(runParser(anythingParser, asyncIteratorFromString('anything'), uint8ArrayParserInputCompanion), {
174
194
  message: /input companion/,
175
195
  });
176
196
  });
197
+
198
+ test('thrown error has input reader state', async t => {
199
+ const error = await t.throwsAsync(
200
+ runParser(
201
+ createTupleParser([
202
+ createExactSequenceNaiveParser('foo'),
203
+ createExactSequenceNaiveParser('bar'),
204
+ ]),
205
+ (async function * () {
206
+ yield 'foo';
207
+ yield 'qux';
208
+ yield 'bar';
209
+ })(),
210
+ stringParserInputCompanion,
211
+ ),
212
+ {
213
+ instanceOf: ParserError,
214
+ },
215
+ );
216
+
217
+ t.is(error.position, 4);
218
+
219
+ invariant(error.inputReaderSate, 'error.inputReaderSate');
220
+
221
+ const {
222
+ consumedBufferedSequences,
223
+ unconsumedBufferedSequences,
224
+ unbufferedSequences,
225
+ position,
226
+ ...inputReaderStateRest
227
+ } = error.inputReaderSate;
228
+
229
+ t.is(position, 4);
230
+ t.deepEqual(inputReaderStateRest, {});
231
+
232
+ const unbufferedSequencesArray = [];
233
+
234
+ if (unbufferedSequences) {
235
+ for await (const sequence of toAsyncIterable(unbufferedSequences)) {
236
+ unbufferedSequencesArray.push(sequence);
237
+ }
238
+ }
239
+
240
+ t.deepEqual(consumedBufferedSequences, [ 'q' ]);
241
+ t.deepEqual(unconsumedBufferedSequences, [ 'ux' ]);
242
+ t.deepEqual(unbufferedSequencesArray, [ 'bar' ]);
243
+ });
244
+
245
+ test('runParser throws with remaining input', async t => {
246
+ const parser: Parser<string, string> = createExactSequenceNaiveParser('foo');
247
+ const error = await t.throwsAsync(() => runParser(parser, 'foobar', stringParserInputCompanion), {
248
+ instanceOf: ParserUnexpectedRemainingInputError,
249
+ message: /remaining input/,
250
+ });
251
+
252
+ t.is(error.position, 3);
253
+
254
+ const {
255
+ position,
256
+ consumedBufferedSequences,
257
+ unconsumedBufferedSequences,
258
+ unbufferedSequences,
259
+ ...inputReaderStateRest
260
+ } = error.inputReaderSate!;
261
+
262
+ t.deepEqual(inputReaderStateRest, {});
263
+ t.is(position, 3);
264
+ t.deepEqual(consumedBufferedSequences, [ 'foo' ]);
265
+ t.deepEqual(unconsumedBufferedSequences, [ 'bar' ]);
266
+ t.truthy(unbufferedSequences);
267
+ });
268
+
269
+ test('runParser does not throw without remaining input', async t => {
270
+ const parser: Parser<string, string> = createExactSequenceNaiveParser('foo');
271
+ const output = await runParser(parser, 'foo', stringParserInputCompanion);
272
+
273
+ t.deepEqual(output, 'foo');
274
+ });
275
+
276
+ test('runParserWithRemainingInput with remaining input', async t => {
277
+ const parser: Parser<string, string> = createExactSequenceNaiveParser('foo');
278
+ const {
279
+ output,
280
+ remainingInput,
281
+ position,
282
+ ...resultRest
283
+ } = await runParserWithRemainingInput(parser, 'foobar', stringParserInputCompanion);
284
+
285
+ t.deepEqual(resultRest, {});
286
+ t.deepEqual(output, 'foo');
287
+ t.deepEqual(await stringFromAsyncIterable(remainingInput!), 'bar');
288
+ t.is(position, 3);
289
+ });
290
+
291
+ test('runParserWithRemainingInput without remaining input', async t => {
292
+ const parser: Parser<string, string> = createExactSequenceNaiveParser('foo');
293
+ const { output, remainingInput } = await runParserWithRemainingInput(parser, 'foo', stringParserInputCompanion);
294
+
295
+ t.deepEqual(output, 'foo');
296
+ t.is(remainingInput, undefined);
297
+ });
@@ -8,32 +8,32 @@ Generated by [AVA](https://avajs.dev).
8
8
 
9
9
  > Snapshot 1
10
10
 
11
- `ParserParsingJoinNoneError: (position: 12) No union child parser succeeded.␊
11
+ `ParserParsingJoinNoneError: No union child parser succeeded.␊
12
12
  at [LOCATIONS]`
13
13
 
14
14
  ## errorJoinMode: all
15
15
 
16
16
  > Snapshot 1
17
17
 
18
- `ParserParsingJoinAllError: (position: 12) No union child parser succeeded.␊
18
+ `ParserParsingJoinAllError: No union child parser succeeded.␊
19
19
  Child error stacks, indented:␊
20
- ParserParsingInvariantError: (position: 10) Expected "3", got "C", at [LOCATIONS]␊
21
- ParserParsingInvariantError: (position: 10) Expected "c", got "C", at [LOCATIONS]␊
22
- ParserParsingJoinAllError: (position: 3) No disjunction child parser succeeded.␊
20
+ ParserParsingInvariantError: Expected "3", got "C", at [LOCATIONS]␊
21
+ ParserParsingInvariantError: Expected "c", got "C", at [LOCATIONS]␊
22
+ ParserParsingJoinAllError: No disjunction child parser succeeded.␊
23
23
  Child error stacks, indented:␊
24
- ParserParsingJoinAllError: (position: 4) No union child parser succeeded.␊
24
+ ParserParsingJoinAllError: No union child parser succeeded.␊
25
25
  Child error stacks, indented:␊
26
- ParserParsingInvariantError: (position: 4) Expected "1", got "f", at [LOCATIONS]␊
27
- ParserParsingInvariantError: (position: 4) Expected "a", got "f", at [LOCATIONS]␊
28
- ParserParsingInvariantError: (position: 4) Expected "A", got "f", at [LOCATIONS]␊
26
+ ParserParsingInvariantError: Expected "1", got "f", at [LOCATIONS]␊
27
+ ParserParsingInvariantError: Expected "a", got "f", at [LOCATIONS]␊
28
+ ParserParsingInvariantError: Expected "A", got "f", at [LOCATIONS]␊
29
29
  End of child error stacks␊
30
30
  at [LOCATIONS]␊
31
- ParserParsingInvariantError: (position: 4) Expected "2", got "f", at [LOCATIONS]␊
32
- ParserParsingInvariantError: (position: 4) Expected "b", got "f", at [LOCATIONS]␊
33
- ParserParsingInvariantError: (position: 4) Expected "B", got "f", at [LOCATIONS]␊
31
+ ParserParsingInvariantError: Expected "2", got "f", at [LOCATIONS]␊
32
+ ParserParsingInvariantError: Expected "b", got "f", at [LOCATIONS]␊
33
+ ParserParsingInvariantError: Expected "B", got "f", at [LOCATIONS]␊
34
34
  End of child error stacks␊
35
35
  at [LOCATIONS]␊
36
- ParserParsingInvariantError: (position: 12) Expected "C", got "!", at [LOCATIONS]␊
36
+ ParserParsingInvariantError: Expected "C", got "!", at [LOCATIONS]␊
37
37
  End of child error stacks␊
38
38
  at [LOCATIONS]`
39
39
 
@@ -41,15 +41,15 @@ Generated by [AVA](https://avajs.dev).
41
41
 
42
42
  > Snapshot 1
43
43
 
44
- `ParserParsingJoinDeepestError: (position: 12) No union child parser succeeded.␊
44
+ `ParserParsingJoinDeepestError: No union child parser succeeded.␊
45
45
  Deepest child error stacks, indented:␊
46
- ParserParsingJoinDeepestError: (position: 3) No disjunction child parser succeeded.␊
46
+ ParserParsingJoinDeepestError: No disjunction child parser succeeded.␊
47
47
  Deepest child error stacks, indented:␊
48
- ParserParsingJoinDeepestError: (position: 4) No union child parser succeeded.␊
48
+ ParserParsingJoinDeepestError: No union child parser succeeded.␊
49
49
  Deepest child error stacks, indented:␊
50
- ParserParsingInvariantError: (position: 4) Expected "1", got "f", at [LOCATIONS]␊
51
- ParserParsingInvariantError: (position: 4) Expected "a", got "f", at [LOCATIONS]␊
52
- ParserParsingInvariantError: (position: 4) Expected "A", got "f", at [LOCATIONS]␊
50
+ ParserParsingInvariantError: Expected "1", got "f", at [LOCATIONS]␊
51
+ ParserParsingInvariantError: Expected "a", got "f", at [LOCATIONS]␊
52
+ ParserParsingInvariantError: Expected "A", got "f", at [LOCATIONS]␊
53
53
  End of deepest child error stacks␊
54
54
  at [LOCATIONS]␊
55
55
  End of deepest child error stacks␊
@@ -61,8 +61,8 @@ Generated by [AVA](https://avajs.dev).
61
61
 
62
62
  > Snapshot 1
63
63
 
64
- `ParserParsingJoinFurthestError: (position: 12) No union child parser succeeded.␊
64
+ `ParserParsingJoinFurthestError: No union child parser succeeded.␊
65
65
  Furthest child error stacks, indented:␊
66
- ParserParsingInvariantError: (position: 12) Expected "C", got "!", at [LOCATIONS]␊
66
+ ParserParsingInvariantError: Expected "C", got "!", at [LOCATIONS]␊
67
67
  End of furthest child error stacks␊
68
68
  at [LOCATIONS]`
Binary file