The JavaScript blog.


libraries node browser modules parsers lexers

Make Parsers and DSLs with Jacob

Posted on .

Recently there have been several exciting projects that provide support for upcoming ES6 and ES7 features. One way of implementing parsers like this is with a context-free grammar. A popular way to do this is with Jison, but Gabriele Cannata sent in a new library called Jacob (GitHub: Canna71/Jacob, License: ISC, npm: jacob).

Jacob has a lexer and a parser. In that sense it's inspired by both Flex and Bison. If you want to learn about Flex and Bison, I found an article about writing a toy compiler with Flex, Bison, and LLVM. Jacob's lexer supports regular expressions and lexer actions. The parser supports simple LR parsing, Look-Ahead LR parsing, and canonical LR parsing. The grammar is specified with Extended Backus–Naur Form, which means you define a formal language based on how symbols can be combined into legal sequences.

This means you can define your language or DSL, validate code written with it, and produce abstract syntax trees that can be used to write compilers.

I've written very basic grammar files with Jison before, and I've found that it takes practice to get the hang of it. The best place to get started is with real examples, so have a look at the Jacob examples folder. Also, the JSON definition on the json.org site will help you to understand how something you're familiar with maps to a formal grammar.

If you've ever wondered how people write things like transpilers in a formal and structured way, then it's worth checking out Jison and Jacob.


typescript as3 parsers

From AS3 to TypeScript

Posted on .

If you've got existing ActionScript assets but want to migrate them to another language, what do you do? One possibility is to convert AS3 to TypeScript. They share similar language features, but are different enough that the process isn't trivial.

There's a project called AS3toTypeScript by Richard Davey that runs through lots of simple replacements, like converting Boolean to bool. It's written in PHP and can be used with a web interface.

In From AS3 to TypeScript by Jesse Freeman, the author converts a game to TypeScript, which seems like a typical task for an ActionScript developer. He points out that TypeScript works well because of the Visual Studio support, so it makes sense if you're a Microsoft-based developer.

François de Campredon recently sent me as3-to-typescript (License: Apache, npm: as3-to-typescript). This is a Node-based project that includes the AS3Parser from Adobe. That means the conversion process actually parses ActionScript and attempts to output syntactically correct TypeScript.

The tests are fairly minimal given the goals of the project, but they do show what the tool currently supports. I'm not particularly for or against ActionScript or TypeScript, but as3-to-typescript is a very interesting and useful combination of technologies that might help you find new life for old game code.