Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/himanoa/parser-combinator
https://github.com/himanoa/parser-combinator
Last synced: 20 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/himanoa/parser-combinator
- Owner: himanoa
- License: mit
- Created: 2022-09-28T18:27:29.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-10-16T14:50:34.000Z (about 2 years ago)
- Last Synced: 2024-12-08T21:41:52.148Z (25 days ago)
- Language: TypeScript
- Size: 52.7 KB
- Stars: 3
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# @himanoa/parser-combinator
parser-combinator library for typescript
## Install
### npm
`npm i @himanoa/parser-combinator`
## Example
```typescript
import { choice, createParser, map, str } from "@himanoa/parser-combinator";const booleanSymbol = choice([
map(str("true"), () => true),
map(str("false"), () => false),
]);const parser = createParser(booleanSymbol);
const trueResult = parser("true");
console.log(trueResult.kind === "success" ? trueResult.value : "failed parse");
console.log(trueResult.kind === "success" ? trueResult.value : "failed parse");const falseResult = parser("false");
console.log(
falseResult.kind === "success" ? falseResult.value : "failed parse",
);
console.log(
falseResult.kind === "success" ? falseResult.value : "failed parse",
);const failedResult = parser("asdfasdf");
console.log(
failedResult.kind === "success" ? failedResult.value : "failed parse",
);
console.log(
failedResult.kind === "success" ? failedResult.value : "failed parse",
);
```## API
- anyChar: Parse any token
- eof: Parse eof token
- char: Parse `c` token
- choice: Parse any one matching token from multiple parsers
- count: Parse parser from zero up to count times
- and: Parse all parsers
- many: Parse parser from zero or more tiems returning a Array with the values
from p
- many1: Parse parser from one or more tiems returning a Array with the values
from p
- str: Parse many char token
- countMinMax: Parse parser from min to max times
- map: convert `value` when parse success
- mapErr: convert `expected` when parse failed
- surround: Parse open followed by parser followed by close
- not: Reverse parser result
- skip: Crush parse results with never
- satisfy: Parses a token and succeeds depending on the result of predicate
- optional: Parse a token and return a or null. Return null when failed parse.