ANTLR3JavaScriptTarget

The ANTLR 3 JavaScript target is available for the first time in the ANTLR 3.1 release.  Being new, this target is in beta state. This means that most parts are working (and passing a large suite of functional tests), but bugs and problems are to be expected and documentation is pretty poor.

Both the runtime module and the code generation templates should now be feature complete and in sync with the Java target, except for the features listed below.

Please send bug reports, feedback, and patches to me or the antlr-interest mailing list. 

This page was modeled on the Python target's page: thanks Benjamin Niemann.

Release History

IMPORTANT: Unfortunately, the JavaScript target is broken in the latest 3.1 and 3.2 releases. If you'd like to use the JavaScript target, you'll need to download a recent source snapshot from Fisheye or Github and build from source.

3.1 - JavaScript target added.

3.2 - Unfortunately, ANTLR 3.2 shipped with some serious bugs in the JavaScript target that makes it mostly unusable. These have been fixed in the trunk, so it is suggested that you build ANTLR from source if you'd like to use the JavaScript target. An up-to-date build of the JavaScript runtime is attached to this page for convenience.

Requirements

The JavaScript target has been tested on IE 7/8, FF 2/3, Safari 3, and Rhino.  We plan to support all A Grade Browsers, Rhino, and SpiderMonkey eventually.  The JavaScript runtime library will need to be loaded before you can make use of a compiled grammar.  In addition, if you're using Rhino and would like to use the ANTLRFileStream class, you'll need to include the antlr3-cli script.

Usage

Selecting JavaScript Output

Just add language=JavaScript; to the options section of your grammar:

grammar T;
options {
    language=JavaScript;
    [other options]
}

...  

For a grammar T.g ANTLR3 will then create the files TLexer.js and TParser.js which contain the classes TLexer and TParser (or just one of those, if you have a pure lexer/parser). For tree parsers, ANTLR3 creates T.js containing the class T.

Using the Generated Classes

To use a grammar T.g:

<script type="text/javascript" src="lib/antlr3-all-min.js"></script>
<script type="text/javascript" src="TLexer.js"></script>
<script type="text/javascript" src="TParser.js"></script>

<script type="text/javascript">
var input = "...what you want to feed into the parser...",
     cstream = new org.antlr.runtime.ANTLRStringStream(input),
     lexer = new TLexer(cstream),
     tstream = new org.antlr.runtime.CommonTokenStream(lexer),
     parser = new TParser(tstream);
parser.entry_rule();
</script>

If you want to access the tokens in your code, they are stored as static properties of the lexer and parser:

doSomethingWith(TLexer.IDENTIFIER);
doSomethingWith(TParser.IDENTIFIER);

Using Tree Parsers

For grammars T.g (parser and lexer) and TWalker.g (the tree parser):

<script type="text/javascript" src="lib/antlr3-all-min.js"></script>
<script type="text/javascript" src="TLexer.js"></script>
<script type="text/javascript" src="TParser.js"></script>
<script type="text/javascript" src="TWalker.js"></script>

<script type="text/javascript">
var input = "...what you want to feed into the parser...",
     cstream = new org.antlr.runtime.ANTLRStringStream(input),
     lexer = new TLexer(cstream),
     tstream = new org.antlr.runtime.CommonTokenStream(lexer),
     parser = new TParser(tstream),
     r = parser.entry_rule();

var nodes = new org.antlr.runtime.tree.CommonTreeNodeStream(r.getTree());
nodes.setTokenStream(tstream);
var walker = new TWalker(nodes);
walker.tree_entry_rule();
</script>

API Documentation

Heh . . . we're working on this (using JSDoc).  For now, your best bet is to use the Java target API documentation, which should be almost entirely compatible with the JavaScript target at both the class and method level.

Examples

There are a wealth of examples available in antlr-examples repository.  You also might want to look at the functional tests.

Unsupported Features

  •  -debug option is not available
  • haven't integrated with ANTLRWorks yet
  • StringTemplate output ("output=template;") not available