# prsc

> Tiny parser combinators library

Latest version **4.0.0** (published 2022-06-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install prsc
pnpm add prsc
yarn add prsc
bun add prsc
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2022-06-23 |
| First published | 2019-11-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 102.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Stef Busking |
| Maintainers | bwrrp |
| Keywords | parser, combinators |

## Links

- npm: https://www.npmjs.com/package/prsc
- Repository: https://github.com/bwrrp/prsc.js
- Homepage: https://github.com/bwrrp/prsc.js#readme
- Issues: https://github.com/bwrrp/prsc.js/issues
- npm.io page: https://npm.io/package/prsc

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 4.0.0 (latest) — 2022-06-23
- 3.3.0 — 2022-06-21
- 3.2.0 — 2022-06-20
- 3.1.1 — 2022-06-17
- 3.1.0 — 2022-03-31
- 3.0.2 — 2022-03-29
- 3.0.1 — 2022-03-29
- 3.0.0 — 2022-03-28
- 2.1.0 — 2022-03-25
- 2.0.1 — 2021-02-07
- 2.0.0 — 2021-02-07
- 1.1.0 — 2019-12-20
- 1.0.0 — 2019-11-08
- 0.1.1 — 2019-11-01
- 0.1.0 — 2019-11-01

## README

# prsc

[![NPM version](https://badge.fury.io/js/prsc.svg)](https://badge.fury.io/js/prsc)
[![CI](https://github.com/bwrrp/prsc.js/workflows/CI/badge.svg)](https://github.com/bwrrp/prsc.js/actions?query=workflow%3ACI)

Tiny parser combinators library for JavaScript and TypeScript. Heavily
inspired by [nom](https://github.com/Geal/nom).

## Installation

The prsc library can be installed using npm or yarn:

```bat
npm install --save prsc
```

or

```bat
yarn add prsc
```

The package includes both a UMD bundle (`dist/prsc.js`), compatible with
Node.js, and an ES6 module (`dist/prsc.mjs`). TypeScript typings are included
(`dist/prsc.d.ts`) and should work automatically in most cases.

## Usage

This library exports [a number of functions](./docs/prsc.md) that make it
easy to build parsers for input represented as a string. Start from primitive
parsers such as `token` or your own functions matching the `Parser` type,
transform results using `map` and `filter` and combine them using the other
functions such as `then` and `star`.

### Example

The following parser accepts a simple arithmetic language consisting of the
`+` and `*` operators:

```javascript
// Create a primitive parser that accepts a single digit
const digit = (input, offset) => {
	if (/^[0-9]$/.test(input[offset])) {
		return ok(offset + 1);
	}
	return error(offset, ['digit']);
};

// Use that to accept a string of one or more digits
const digits = plus(digit);

// Then use recognize to get the matching string and use map to parse that into a number
const number = map(recognize(digits), (str) => parseInt(str, 10));

// Multiplication
const term = then(
	number,
	star(preceded(token('*'), number)),
	// Multiply everything together
	(num, factors) => factors.reduce((product, factor) => product * factor, num)
);

// Addition
const expression = then(term, star(preceded(token('+'), term)), (num, terms) =>
	terms.reduce((sum, term) => sum + term, num)
);

// Parsing some input
console.log(expression('2*3+4*5', 0));
// > { success: true, offset: 7, value: 26 }
```

### Tips

Use functions to provide a layer of indirection for recursive definitions:

```javascript
const term = then(number, optional(preceded(token('*'), termIndirect)), ...);
function termIndirect(input, offset) {
	return term(input, offset);
}
```

Use the typings when working in TypeScript for strongly-typed parsers:

```typescript
const digit: Parser<string> = ...;
const digits = plus(digit); // Parser<string[]>
const number = map(
	digits,
	strs => parseInt(strs.join(''), 10)
); // Parser<number>
```

---
_Source: https://npm.io/package/prsc · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
