anko / partser Goto Github PK
View Code? Open in Web Editor NEWcombinatory parsing library with hot-swappable parts and nested environments
License: ISC License
combinatory parsing library with hot-swappable parts and nested environments
License: ISC License
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' } ]
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.