Coder Social home page Coder Social logo

anko / partser Goto Github PK

View Code? Open in Web Editor NEW
7.0 7.0 0.0 561 KB

combinatory parsing library with hot-swappable parts and nested environments

License: ISC License

JavaScript 100.00%
hot-swap javascript modular parser-library programmable self-modifying-code

partser's People

Contributors

ahupp avatar anko avatar dependabot[bot] avatar hughfdjackson avatar jneen avatar jwmerrill avatar laughinghan avatar michaelficarra avatar pbevin avatar raynos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

partser's Issues

Could this be extended to pass a compilation environment between parsers?

Currently, parsers in partser only take 2 arguments: the input string and character offset to start from.

What if they also took a third argument containing arbitrary data? Then partser could be used to build compilers that pass compilation state around. Specifically for eslisp (my primary motivation here) that could enable user-defined reader macros to be implemented by moving the rest of the compiler (macro expansion, code generation, etc) into partser.map calls.

A sketch of that:

var p = require('.')

var space = p.regex(/\s/)

var sepBy = function (thing, separator) {
  var unit = p.map(
      p.seq(thing, p.times(separator, 0, Infinity)),
      function (results) { return results[0] })
  return p.times(unit, 0, Infinity)
}

var atom = p.regex(/[a-zA-Z0-9_-]+/)

var expression = p.eof

var list = p.map(
    p.seq(p.string('('), sepBy(atom, space), p.string(')')),
    function (results, environment) {
      // Expects the `environment` argument to be an object containing
      // functions for compiling AST, and registering new macros.  Also
      // contains a reference to the partsers making up the parser, so they can
      // be modified at run-time.

      var listContents = results[1]
      // Compile stuff here, call macros based on `listContents`, et cetera.
      // You know, classic central compiler "god switch statement".
      return listContents.map(environment.compile)
    })

var environment = {
  // Put whatever you want here.  For example: compiler functions, a table to
  // store macros, or a reference to the parser itself.
  compile: function (name) {
    return {
      type: 'Literal',
      name: name
    }
  }
}
var result = list('(a b c)', 0, environment) // Note the third argument.

console.log(result.value)

Output would be:

[ { type: 'Literal', name: 'a' },
  { type: 'Literal', name: 'b' },
  { type: 'Literal', name: 'c' } ]

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.