Skip to content
r47onfirePublic

About

simple PEG-like recoverable parsing library

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Nebula

A grammar-driven packrat parser engine with left-recursion support. Grammars are plain JSON -- no code generation, no separate grammar file format -- and parsing produces a concrete syntax tree (CST) whose leaves, concatenated, reproduce the input exactly.

The parsing algorithm is a modified PEG packrat parser that supports left recursion, based on "Packrat Parsers Can Support Left Recursion" by Warth, Douglass, and Millstein (PEPM '08).

Nebula is primarily designed to be used in Backolon, but it is a general-purpose parsing library and knows nothing about any particular language.

Install

bun add github:@r47onfire/nebula
# or
npm install github:@r47onfire/nebula

Example

import {
    defineGrammar,
    lit,
    param,
    parseToCST,
    regex,
    rule,
    seq,
} from "@r47onfire/nebula";

const grammar = defineGrammar({
    main: { def: rule("wrap", lit("("), lit(")")) },
    // a parameterized rule: `wrap` is called with two combinator arguments
    wrap: {
        params: ["open", "close"],
        def: seq(param("open"), rule("word"), param("close")),
    },
    word: { def: regex(/[a-z]+/) },
});

const cst = parseToCST(
    "(abc)",
    new URL("filename"),
    0,
    "main",
    grammar,
    "exprs",
);

parseToCST never throws for bad input: syntax errors come back as error nodes in the CST (with an errorExpected field pointing to the grammar rule or combinator that should have matched), so a caller can report every error it can recover from, not just the first.

Repository layout

  • Source files go in src/
  • Test files go in test/
  • Build scripts and helpers go in scripts/
  • Prose documentation guides go in guides/
  • Build artifacts go in dist/ - gitignore'd
  • Website build files go in docs/ - gitignore'd

Development

bun run build   # bundle src/index.ts into dist/
bun test        # run the test suite
bun run dev     # serve docs/ on http://localhost:8000 and rebuild on change

License

AGPL-3.0-only

About

simple PEG-like recoverable parsing library

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages