shake, with fries

by Zach Carter

Posts tagged commonjs

Dec27

Orderly.js: Orderly to JSONSchema Compiler

javascript commonjs jison orderly jsonschema json compiler parser | comments

Orderly is a textual format for describing JSON. Orderly can be compiled into JSONSchema. It is designed to be easy to read and write.

I decided to give Jison a real-world spin by creating an Orderly to JSONSchema compiler for JavaScript. After about three days, interspersed with lots of eating and family vists, I have a working implementation, which I call Orderly.js.

Usage

var orderly = require("orderly");

var orderlySource = "array {};";

var jsonSchemaSource = orderly.compile(orderlySource);

var jsonSchemaObject = orderly.parse(orderlySource);

print(JSON.stringify(jsonSchemaObject));

An API for building parsers

I must say, Jison made this really simple. I don't consider myself a speedy coder in the slightest, probably the opposite, but I was able to crank this out in a (holiday) weekend. Jison takes care of the hard parts of parsing, all you need is a working grammar* and the appropriate hooks. I'll be writing more on Jison shortly.

*This can be non-trivial, though if the language you are parsing already specifies a grammar, as was the case with Orderly, you're set!

Dec23

Stay parsey, my friends.

javascript commonjs parser lexer compilers jison | comments

I hinted on twitter that I was implementing some kind of compiler in JavaScript. It was a project I began to help study for a compilers course I was taking, which also gave me an opportunity to earn extra credit. Though, really, I just thought it was a friggen cool thing to implement. I ended up focusing on the compiler front-end to create a parser generator, which I call Jison.

Anyway, the semester ended, I received the extra credit, and pushed the source to github. I'm still working on the initial documentation to include in a more formal announcement this week.

The demo page that was built to showcase some of the features to my professor is also available. It turned out to be a very useful tool for this particular niche in its own right. The page can help you visually debug CFGs, so it is helpful regardless of the parsing tool the grammar is intended for, or would be helpful for simply learning about CFGs and parsing algorithms.

I'll post a follow up with documentation and more examples soon. For now, check out the JSON parser.

Stay parsey, my friends.

Oct07

Another Way to Run Narwhal

javascript narwhal mozilla xulrunner xpcshell commonjs | comments

What's Narwhal? Ahem:

Narwhal is a cross-platform, multi-interpreter, general purpose JavaScript platform. It aims to provide a solid foundation for building JavaScript applications, primarily outside the web browser. Narwhal includes a package manager, module system, and standard library for multiple JavaScript interpreters. - narwhaljs.org

It basically enables you to do things in JavaScript you might do in other scripting languages, like Python or Ruby. It's one implementation of the CommonJS specification. If you're in to JavaScript, you should get acquainted with it.

The default engine uses Rhino, but there's also a XULRunner engine, allowing you to use Narwhal from within Firefox extensions and other XUL applications. Really exciting stuff!

I wanted a quick way test Narwhal/XPCOM modules, so I made an engine using xpcshell to do just that. It shares most of its libs with the xulrunner engine.

To set it up:

git clone git://github.com/tlrobinson/narwhal.git narwhal
cd narwhal/engines
git clone git://github.com/Gozala/narwhal-xulrunner.git xulrunner
git clone git://github.com/zaach/narwhal-xpcshell.git xpcshell
cd xpcshell
bin/install
cd ../..
NARWHAL_PLATFORM=xpcshell bin/narwhal
js> 

It should work on Linux and OS X. Windows support is certainly possible. I'll have a hack at it one day.

You can learn more about what can be done with Narwhal on their site.