Détail du package

@nrsk/sigma

norskeld1.1kMIT3.8.0

TypeScript parser combinator library for building fast and convenient parsers.

typescript, parser-combinators, parser, combinators

readme

𝝨 sigma

Build/Test Coverage NPM Supported Node Versions Bundlephobia Tree Shaking Semantic Release

TypeScript parser combinator library for building fast and convenient parsers.

Features

  • [x] Capable of parsing LL grammars using recursive descent with backtracking.
  • [x] Ergonomic API with excellent TypeScript support.
  • [x] Zero dependencies. Supports tree shaking.
  • [x] Performant enough to beat similar parser combinator libraries.

All-in-all, Sigma is easy to use and extend, reasonably fast and convenient, but a bit limited regarding what types of grammars it can parse.

Docs

You can find the documentation here. If you want to contribute, feel free to check out the source code.

Installation

Node

Just use your favorite package manager.

npm i @nrsk/sigma

Deno

You can import the library via Skypack (note the ?dts query parameter, this is to pull types):

import { ... } from 'https://cdn.skypack.dev/@nrsk/sigma?dts'
import { ... } from 'https://cdn.skypack.dev/@nrsk/sigma/parsers?dts'
import { ... } from 'https://cdn.skypack.dev/@nrsk/sigma/combinators?dts'

Example

Below is an example of parsing nested tuples like (1, 2, (3, 4)) into an AST.

<summary>Click to show the tuples example.</summary> ts import { choice, map, optional, sepBy, sequence, takeMid } from '@nrsk/sigma/combinators' import { defer, integer, run, string, whitespace } from '@nrsk/sigma/parsers' import type { Span } from '@nrsk/sigma' /* AST. */ interface NumberNode { type: 'number' span: Span value: number } interface ListNode { type: 'list' span: Span value: Array<NumberNode | ListNode> } /* Mapping functions to turn parsed string values into AST nodes. */ function toNumber(value: number, span: Span): NumberNode { return { type: 'number', span, value } } function toList(value: Array<NumberNode | ListNode>, span: Span): ListNode { return { type: 'list', span, value } } /* Parsers. */ const OpenParen = string('(') const CloseParen = string(')') const Space = optional(whitespace()) const Comma = sequence(Space, string(','), Space) const TupleNumber = defer<NumberNode>() const TupleList = defer<ListNode>() TupleNumber.with( map( integer(), toNumber ) ) TupleList.with( map( takeMid( OpenParen, sepBy(choice(TupleList, TupleNumber), Comma), CloseParen ), toList ) ) Then we simply run the root parser, feeding it with text: ts run(TupleList).with('(1, 2, (3, 4))') And in the end we get the following output with the AST, which can then be manipulated if needed: ts { isOk: true, span: [ 0, 14 ], pos: 14, value: { type: 'list', span: [ 0, 14 ], value: [ { type: 'number', span: [ 1, 2 ], value: 1 }, { type: 'number', span: [ 4, 5 ], value: 2 }, { type: 'list', span: [ 7, 13 ], value: [ { type: 'number', span: [ 8, 9 ], value: 3 }, { type: 'number', span: [ 11, 12 ], value: 4 } ] } ] } }

Development

Fork, clone, then instead of npm install run:

npm run install:all

Note

This will install dependencies for the package itself, and also for docs and benchmarks packages. This is due to limitations of the current repository setup and needed to avoid problems with eslint that runs on pre-commit hook.

This project follows the conventional commits spec and uses a slightly modified commitlint preset for automatic linting commits and generating changelog.

License

MIT.

changelog

3.7.0 (2023-08-22)

Features

3.6.5 (2023-08-17)

Bug Fixes

  • deferred parsers should throw if not initialized (b244cda)

Reverts

  • keeping core exports for now (8c2849d)

3.6.4 (2023-07-03)

Bug Fixes

  • package: remove postinstall script, relax engine constraints (#81) (52dc1e4), closes #80

3.6.3 (2023-06-04)

Bug Fixes

  • combinators/sepBy: do not mutate position on no match (#79) (baf8c16), closes #77

3.6.2 (2023-03-14)

Bug Fixes

  • package: re-order exports fields for proper resolution in some setups (b7cb115)

3.6.1 (2023-02-21)

Bug Fixes

  • types: fix inference for some parsers, add type testing (#75) (80cccf0), closes #48

3.6.0 (2023-01-17)

Bug Fixes

  • combinators/error: return correct pos (c11002c)

Features

3.5.0 (2023-01-16)

Features

  • combinators/attempt: add attempt combinator (7133746)

3.4.0 (2023-01-16)

Bug Fixes

  • parsers/string: correctly determine pos without getting out of bounds (9927128)

Features

  • combinators/lookahead: add lookahead combinator (497c7a8)

3.3.0 (2023-01-15)

Features

  • parsers/tryRun: add tryRun parser (#62) (a34c0f9)

3.2.0 (2023-01-15)

Features

  • parsers/regexp: add injection of global flag if it's not present (#61) (90f86d3)

3.1.4 (2023-01-14)

Bug Fixes

  • types: fix toUnion signature to correctly infer type (4dca5e0)

3.1.3 (2023-01-05)

Bug Fixes

  • parsers/noneOf: halt at reaching eoi (06779bd)
  • parsers/oneOf: halt at reaching eoi (b20f2a2)

3.1.2 (2023-01-05)

Bug Fixes

  • combinators/many: fix an edge case when combined with noneOf (4630bf4)

3.1.1 (2022-10-25)

Bug Fixes

  • vitest: fix coverage collection (91a1f9b)

3.1.0 (2022-10-08)

Bug Fixes

Features

  • build: finalize tsup setup with multiple options for build (d9a54a8)
  • build: introduce tsup, simplify build, make package a module (e803455)
  • chore: cleanup packages and files (6cd7146)
  • vitest: add vitest, aliasing, cleanup uvu deps (a4883a3)

3.0.0 (2022-10-02)

Code Refactoring

  • change the output type from to number for number parsers (24406ab)

BREAKING CHANGES

  • The output type of number parsers has changed from string to number, i.e. these parsers now have the type of Parser<number> instead of Parser<string>.

2.10.0 (2022-07-29)

Features

2.9.0 (2022-07-12)

Features

  • tsdoc: add tsdocs for parsers (c78d57a)
  • tsdoc: add tsdocs to combinators (5220e26)
  • tsdoc: final touches (3233440)

2.8.0 (2022-07-12)

Features

  • combinators/*Until: add takeUntil and skipUntil combinators (003f82d)

2.7.0 (2022-07-04)

Features

  • parsers/noneOf: add noneOf parser (d6bc81c)

2.6.0 (2022-07-04)

Features

  • parsers/oneOf: add oneOf parser (6d6c2a6)

2.5.0 (2022-07-04)

Features

  • parsers/any: add any parser (bfb9620)

2.4.0 (2022-07-02)

Features

  • combinators/sepBy1: add sepBy1 combinator (52b7ddf)

2.3.1 (2022-06-01)

Bug Fixes

  • combinators/many1: fix many1 behavior (169bf2e)

2.3.0 (2022-06-01)

Features

  • combinators/when: add context-aware when combinator (1537b51)

2.2.0 (2022-05-31)

Features

  • bench: add latest sigma for benchmarks (f48e132)

2.1.3 (2022-05-31)

Bug Fixes

  • combinators/choice: get rid of non-null assertion and refactor a bit (2f0a9f9)
  • combinators/sepBy: remove unreachable return (7b3a9d0)
  • parsers/float: do not return input (bd92d3d)

2.1.2 (2022-05-31)

Bug Fixes

  • combinators/sepBy: fix behavior & improve perf (f709a30)

2.1.1 (2022-05-14)

Bug Fixes

2.1.0 (2022-05-14)

Features

  • combinators/many1: add many1 combinator (5342135)

Performance Improvements

  • no-release: refactor benchmarks & add another bench (11d42b4)

2.0.2 (2022-05-14)

Bug Fixes

  • change error back to expected (d1ba6c3)

2.0.1 (2022-05-13)

Performance Improvements

  • improve performance by simplifying values passed around (505dad8)
  • no-release: add benchmarks (fd39bb1)

2.0.0 (2022-01-23)

Code Refactoring

  • remove lazy parser (4340d7d)
  • restructure & rename combinators/parsers (f6a6956)

BREAKING CHANGES

  • Lifted contents of the internal directory since the library itself gets bundled anyway.
  • Removed all aliases for parsers and combinators.
  • Removed whitespaceOptional parser. Its functionality can be replicated with optional(whitespace()).
  • Removed lazy parser. defer is superior to it at almost every aspect.
  • Renamed the following parsers:
    • newline -> eol
    • integer -> int
    • integerUnsigned -> uint
    • uniString -> ustring
  • Renamed the following combinators:
    • list -> sepBy

1.4.2 (2022-01-12)

Bug Fixes

  • combinators/choice: get rid of manual overloads (ace89ea)
  • combinators/sequence: get rid of manual overloads (d81a7b6)

1.4.1 (2022-01-02)

Bug Fixes

  • mark package as side-effect-free (e2f7327)

1.4.0 (2021-12-02)

Features

  • combinators/take: add takeSides combinator (7bd2eb1)

1.3.0 (2021-11-28)

Bug Fixes

  • parsers/float: fix regexps, options & tests (78fa9af)
  • parsers/integer: fix regexp (c689144)
  • parsers/integer: fix regexps, options & tests (2421d6d)

Features

  • parsers/eof: add eof parser (d88a3f2)
  • parsers/float: add float parser (f993009)
  • parsers/integer: add integer parser (f206d49)
  • parsers/letter: add letter and letters parsers (2540b17)
  • parsers/newline: add newline parser (ae8d6ec)
  • parsers/rest: add rest parser (1c4c745)
  • parsers/whitespace: add whitespace & whitespaceOptional parsers (821680a)

1.2.0 (2021-11-17)

Features

1.1.1 (2021-11-15)

Docs

  • readme: add installation and example sections (e80cae3)

1.1.0 (2021-11-15)

Features

1.0.0 (2021-11-12)

Features