{"library":"prsc","title":"prsc: Tiny Parser Combinators","description":"prsc is a compact parser combinators library for both JavaScript and TypeScript, heavily influenced by the Rust parsing library `nom`. It enables developers to construct complex parsers from simpler ones for string inputs, offering primitive parsers like `token` and combinators such as `map`, `filter`, `then`, and `star`. The library ships with ES6 modules (`.mjs`), UMD bundles (`.js`), and comprehensive TypeScript typings, facilitating its use across various environments, including Node.js and browsers. The current stable version is 4.0.0. Releases are somewhat frequent, with minor features and bug fixes rolled out between major versions, indicating active maintenance and continuous improvement in performance and usability for writing fast, reliable parsers.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install prsc"],"cli":null},"imports":["import type { Parser } from 'prsc';","import { ok, error } from 'prsc';","import { map, then, star, recognize, token, plus, preceded, optional } from 'prsc';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { ok, error, map, recognize, plus, then, token, star, preceded, optional } from 'prsc';\n\n// Create a primitive parser that accepts a single digit\nconst digit = (input, offset) => {\n\tif (/^[0-9]$/.test(input[offset])) {\n\t\treturn ok(offset + 1, input[offset]);\n\t}\n\treturn error(offset, ['digit']);\n};\n\n// Use that to accept a string of one or more digits\nconst digits = plus(digit);\n\n// Then use recognize to get the matching string and use map to parse that into a number\nconst number = map(recognize(digits), (str) => parseInt(str, 10));\n\n// Multiplication: term = number * term\n// Recursive definition requires indirection for `factor`\nconst termIndirect = (input, offset) => term(input, offset);\nconst factor = then(\n\tnumber,\n\toptional(preceded(token('*'), termIndirect)),\n\t(num, optFactor) => (optFactor ? num * optFactor : num)\n);\n\n// Addition: expression = factor + expression\nconst expressionIndirect = (input, offset) => expression(input, offset);\nconst expression = then(\n\tfactor,\n\tstar(preceded(token('+'), expressionIndirect)),\n\t(firstFactor, additionalFactors) =>\n\t\tadditionalFactors.reduce((sum, f) => sum + f, firstFactor)\n);\n\n// Parsing some input\nconst result1 = expression('2*3+4*5', 0);\nconsole.log(result1);\n// Expected: { success: true, offset: 7, value: 26 }\n\nconst result2 = expression('10+20*2', 0);\nconsole.log(result2);\n// Expected: { success: true, offset: 7, value: 50 } (10 + (20 * 2))\n","lang":"typescript","description":"This example demonstrates building a parser for a simple arithmetic language supporting addition and multiplication, showcasing primitive parsers, combinators like `map`, `then`, `star`, and handling recursive grammar rules.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}