Skip to content

bablr

bablr is the eponymous package of the BABLR ecosystem. It is used to run BABLR parsers.

If you wish to use parsers from the terminal, this is instead supported by the @bablr/cli package which wraps this one.

Usage

import { printSource, printTree } from '@bablr/agast-helpers/tree';
import { treeParse } from 'bablr';

class language { /* see below */ }
let matcher = m`<Sequence />`;
let source = '1,2,3';

let document = treeParse(language, matcher, source);

// This property is guaranteed
printSource(document) === source; // 1,2,3

printTree(document); // see below

The result of printTree(document) is this CSTML text:

<NaturalSequence>
  values[]: <*NaturalNumber '1' />
  #separator: <* ',' />
  values[]: <*NaturalNumber '2' />
  #separator: <* ',' />
  values[]: <*NaturalNumber '3' />
</>

And finally here is the definition of language:

import { m } from '@bablr/helpers/grammar';

class language {
  static {
    freezeClass(this);
  }

  *NaturalSequence() {
    let sep = true;
    while (sep && (yield eatMatch(m`values[]: <*NaturalNumber /\d/ />`))) {
      yield eatMatch(m`#: <* /[ \t]+/ />`);
      sep = yield eatMatch(m`#separator: <* ',' />`);
      yield eatMatch(m`#: <* /[ \t]+/ />`);
    }
  }

  *NaturalNumber() {
    yield eat(m`/\d+/`);
  }
}