Code Monkey home page Code Monkey logo

bread-n-butter's People

Contributors

dependabot[bot] avatar minamorl avatar mrsekut avatar seanchas116 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

bread-n-butter's Issues

API Proposal: parser.flatten

It's very easy to create parsers which generates recursively nested values. When writing a lexer, we'd expect the output to be a flat (1D) array of tokens (or bnb.ParseNodes). So I find myself constantly mapping the parse result with a flatten function:

type Node = bnb.ParseNode<string, string>;
type NodeOrArrayOfNode = Node | NodeOrArrayOfNode[];

function flatten(nodes: NodeOrArrayOfNode) {
  const result: Node[] = [];
  function addNode(node: NodeOrArrayOfNode) {
    if (Array.isArray(node)) {
      for (const innerNode of node) {
        addNode(innerNode);
      }
    } else {
      result.push(node);
    }
  }

  addNode(nodes);
  return result;
}

// usage:
const parser = bnb
  .text('a')
  .node('a')
  .and(bnb.text('b').node('b'))
  .or(bnb.text('c').node('c'))  // => [bnb.ParseNode<"a", "a">, bnb.ParseNode<"b", "b">] | bnb.ParseNode<"c", "c">
  .map(flatten);  // => bnb.ParseNode<string, string>[]

It would be great if this mechanism is provided as a method of bnb.Parser.

This also helps make sense of the inferred type information, otherwise it would quickly collapse into a pile of gibberish (for instance, in the example above, for such a simple parser, the type yielded before the map call is already not quite readable).

Missing notFollowedBy

As you requested, I open a new issue for the functions which where missing for my project. The most important ones were then, skip and notFollowedBy, anyway I saw that the former where already on the TODO list. I think that lookahead is the negative function of notFollowedBy, so it probably is a good idea to add it too, even if I would name it followedBy.

If you want, I have a little bit of available time and can make PRs to add these functions.

Status of project?

Hi there; hoping none of this comes across with an entitled tone, I'm just looking for some signal about the status of this project. I'm building a side-project and looking around for promising typescript combinator libraries and I stumbled across this while looking at jneen/parsimmon#230. Is this library still being worked on?

Non discarding API

When implementing a lexer, we always want to keep all the tokens, even for the trivial ones (i.e. whitespaces and separators), in order to create a high-fidelity AST which can be mapped back to the original source. This renders some of the functions less useful, e.g. bnb.sepBy which discards the separators, and bnb.wrap which discards the wrappers. Especially bnb.sepBy because it's cumbersome to imitate its behavior with other constructs.

One thought is to add an optional nondiscarding parameter to these functions, when specified as true, will keep these was-to-be-discarded tokens in the return value.

Suggestion: `merge` shouldn't be a method

The following method:

merge<A, B>(a: ActionResult<A>, b: ActionResult<B>): ActionResult<B>

at

merge<A, B>(a: ActionResult<A>, b: ActionResult<B>): ActionResult<B> {

should be considered as a free function instead.

Reason:

  1. It doesn't depend on this.
  2. It might be interpreted as merging of contexts, which it doesn't seem to be.
  3. From a semantic point of view, it has nothing to do with a context directly.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.