https://github.com/luggage66/sprache-js
A simple parser combinator with usable error messages.
https://github.com/luggage66/sprache-js
parser
Last synced: 4 months ago
JSON representation
A simple parser combinator with usable error messages.
- Host: GitHub
- URL: https://github.com/luggage66/sprache-js
- Owner: luggage66
- License: mit
- Created: 2017-09-19T03:25:49.000Z (almost 8 years ago)
- Default Branch: develop
- Last Pushed: 2023-11-30T21:04:36.000Z (over 1 year ago)
- Last Synced: 2025-03-17T03:11:36.008Z (4 months ago)
- Topics: parser
- Language: TypeScript
- Homepage:
- Size: 1.56 MB
- Stars: 12
- Watchers: 1
- Forks: 2
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Sprache.js
sprache.js is a TypeScript port of [Sprache](https://github.com/sprache/Sprache), a simple parser for C#
[]()
[](https://codecov.io/gh/luggage66/Sprache-js)```sh
npm install sprache --save
```## Usage
Unlike most parser-building frameworks, you use Sprache from your program code, and don't need to set up any build-time code generation tasks.
A simple parser might parse a sequence of characters:
```js
import { Parse } from 'sprache';// Parse any number of capital 'A's in a row
var parseA = Parse.char('A').atLeastOnce();
```Sprache provides a number of built-in functions that can make bigger parsers from smaller ones, often callable via generators:
```js
import { Parse } from 'sprache';const identifier = Parse.query(function*() {
const leading = yield Parse.whiteSpace.many();
const first = yield Parse.letter.once();
const rest = yield Parse.letterOrDigit.many();
const trailing = yield Parse.whiteSpace.many();return Parse.return([first].concat(rest).join(''));
});var id = identifier.parse(" abc123 ");
Assert.isEqual("abc123", id);
```## More Examples
More examples are available in [src/examples/](https://github.com/luggage66/Sprache-js/tree/master/src/examples)
## Building / Running examples
Requirements:
* NodeJS
* yarn```sh
winget install OpenJS.NodeJS
winget install Yarn.Yarn
``````sh
yarn install
yarn run build
yarn run test
```To run as example
```sh
yarn run build && node dist/examples/sql
```Is VSCode, just run task "npm: install", then F5 to run.