| 1 |
# JavaScript |
| 2 |
|
| 3 |
## Which browsers are supported? |
| 4 |
|
| 5 |
In theory, all browsers supporting ECMAScript 5.1. |
| 6 |
|
| 7 |
In practice, this target has been extensively tested against: |
| 8 |
|
| 9 |
* Firefox 34.0.5 |
| 10 |
* Safari 8.0.2 |
| 11 |
* Chrome 39.0.2171 |
| 12 |
* Explorer 11.0.3 |
| 13 |
|
| 14 |
The above tests were conducted using Selenium. No issue was found, so you should find that the runtime works pretty much against any recent JavaScript engine. |
| 15 |
|
| 16 |
## Is NodeJS supported? |
| 17 |
|
| 18 |
The runtime has also been extensively tested against Node.js 14 LTS. No issue was found. |
| 19 |
NodeJS together with a packaging tool is now the preferred development path, developers are encouraged to follow it. |
| 20 |
|
| 21 |
## What about modules? |
| 22 |
|
| 23 |
Starting with version 8.1, Antlr4 JavaScript runtime follows esm semantics (see https://tc39.es/ecma262/#sec-modules for details) |
| 24 |
Generated lexers, parsers, listeners and visitors also follow this new standard. |
| 25 |
If you have used previous versions of the runtime, you will need to migrate and make your parser a module. |
| 26 |
|
| 27 |
## How to create a JavaScript lexer or parser? |
| 28 |
|
| 29 |
This is pretty much the same as creating a Java lexer or parser, except you need to specify the language target, for example: |
| 30 |
|
| 31 |
```bash |
| 32 |
$ antlr4 -Dlanguage=JavaScript MyGrammar.g4 |
| 33 |
``` |
| 34 |
|
| 35 |
For a full list of antlr4 tool options, please visit the [tool documentation page](tool-options.md). |
| 36 |
|
| 37 |
## Where can I get the runtime? |
| 38 |
|
| 39 |
Once you've generated the lexer and/or parser code, you need to download the runtime. |
| 40 |
|
| 41 |
The JavaScript runtime is [available from npm](https://www.npmjs.com/package/antlr4). |
| 42 |
|
| 43 |
If you can't use npm, the JavaScript runtime is also available from the ANTLR web site [download section](http://www.antlr.org/download/index.html). The runtime is provided in the form of source code, so no additional installation is required. |
| 44 |
|
| 45 |
We will not document here how to refer to the runtime from your project, since this would differ a lot depending on your project type and IDE. |
| 46 |
|
| 47 |
## How do I get the runtime in my browser? |
| 48 |
|
| 49 |
The runtime is quite big and is currently maintained in the form of around 50 scripts, which follow the same structure as the runtimes for other targets (Java, C#, Python...). |
| 50 |
|
| 51 |
This structure is key in keeping code maintainable and consistent across targets. |
| 52 |
|
| 53 |
However, it would be a bit of a problem when it comes to get it into a browser. Nobody wants to write 50 times: |
| 54 |
|
| 55 |
``` |
| 56 |
<script src='lib/myscript.js'> |
| 57 |
``` |
| 58 |
|
| 59 |
To avoid having doing this, the preferred approach is to bundle antlr4 with your parser code, using webpack. |
| 60 |
|
| 61 |
You can get [information on webpack here](https://webpack.github.io). |
| 62 |
|
| 63 |
The steps to create your parsing code are the following: |
| 64 |
- generate your lexer, parser, listener and visitor using the antlr tool |
| 65 |
- write your parse tree handling code by providing your custom listener or visitor, and associated code, using 'require' to load antlr. |
| 66 |
- create an index.js file with the entry point to your parsing code (or several if required). |
| 67 |
- test your parsing logic thoroughly using node.js |
| 68 |
|
| 69 |
You are now ready to bundle your parsing code as follows: |
| 70 |
- following webpack specs, create a webpack.config file |
| 71 |
- For Webpack version 5, |
| 72 |
- in the `webpack.config` file, exclude node.js only modules using: `resolve: { fallback: { fs: false } }` |
| 73 |
- For older versions of Webpack, |
| 74 |
- in the `webpack.config` file, exclude node.js only modules using: `node: { module: "empty", net: "empty", fs: "empty" }` |
| 75 |
- from the cmd line, navigate to the directory containing webpack.config and type: webpack |
| 76 |
|
| 77 |
This will produce a single js file containing all your parsing code. Easy to include in your web pages! |
| 78 |
|
| 79 |
## How do I run the generated lexer and/or parser? |
| 80 |
|
| 81 |
Let's suppose that your grammar is named, as above, "MyGrammar". Let's suppose this parser comprises a rule named "StartRule". The tool will have generated for you the following files: |
| 82 |
|
| 83 |
* MyGrammarLexer.js |
| 84 |
* MyGrammarParser.js |
| 85 |
* MyGrammarListener.js (if you have not activated the -no-listener option) |
| 86 |
* MyGrammarVisitor.js (if you have activated the -visitor option) |
| 87 |
|
| 88 |
(Developers used to Java/C# ANTLR will notice that there is no base listener or visitor generated, this is because JavaScript having no support for interfaces, the generated listener and visitor are fully fledged classes) |
| 89 |
|
| 90 |
Now a fully functioning script might look like the following: |
| 91 |
|
| 92 |
```javascript |
| 93 |
import antlr4 from 'antlr4'; |
| 94 |
import MyGrammarLexer from './MyGrammarLexer.js'; |
| 95 |
import MyGrammarParser from './MyGrammarParser.js'; |
| 96 |
import MyGrammarListener from './MyGrammarListener.js'; |
| 97 |
|
| 98 |
const input = "your text to parse here" |
| 99 |
const chars = new antlr4.InputStream(input); |
| 100 |
const lexer = new MyGrammarLexer(chars); |
| 101 |
const tokens = new antlr4.CommonTokenStream(lexer); |
| 102 |
const parser = new MyGrammarParser(tokens); |
| 103 |
parser.buildParseTrees = true; |
| 104 |
const tree = parser.MyStartRule(); |
| 105 |
``` |
| 106 |
|
| 107 |
This program will work. But it won't be useful unless you do one of the following: |
| 108 |
|
| 109 |
* you visit the parse tree using a custom listener |
| 110 |
* you visit the parse tree using a custom visitor |
| 111 |
* your grammar comprises production code (like AntLR3) |
| 112 |
|
| 113 |
(please note that production code is target specific, so you can't have multi target grammars that include production code) |
| 114 |
|
| 115 |
## How do I create and run a visitor? |
| 116 |
```javascript |
| 117 |
// test.js |
| 118 |
import antlr4 from 'antlr4'; |
| 119 |
import MyGrammarLexer from './QueryLexer.js'; |
| 120 |
import MyGrammarParser from './QueryParser.js'; |
| 121 |
import MyGrammarListener from './QueryListener.js'; |
| 122 |
|
| 123 |
|
| 124 |
const input = "field = 123 AND items in (1,2,3)" |
| 125 |
const chars = new antlr4.InputStream(input); |
| 126 |
const lexer = new MyGrammarLexer(chars); |
| 127 |
const tokens = new antlr4.CommonTokenStream(lexer); |
| 128 |
const parser = new MyGrammarParser(tokens); |
| 129 |
parser.buildParseTrees = true; |
| 130 |
const tree = parser.query(); |
| 131 |
|
| 132 |
class Visitor { |
| 133 |
visitChildren(ctx) { |
| 134 |
if (!ctx) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
if (ctx.children) { |
| 139 |
return ctx.children.map(child => { |
| 140 |
if (child.children && child.children.length != 0) { |
| 141 |
return child.accept(this); |
| 142 |
} else { |
| 143 |
return child.getText(); |
| 144 |
} |
| 145 |
}); |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
tree.accept(new Visitor()); |
| 151 |
```` |
| 152 |
|
| 153 |
## How do I create and run a custom listener? |
| 154 |
|
| 155 |
Let's suppose your MyGrammar grammar comprises 2 rules: "key" and "value". The antlr4 tool will have generated the following listener: |
| 156 |
|
| 157 |
```javascript |
| 158 |
class MyGrammarListener extends ParseTreeListener { |
| 159 |
|
| 160 |
constructor() { |
| 161 |
super(); |
| 162 |
} |
| 163 |
|
| 164 |
enterKey(ctx) {} |
| 165 |
exitKey(ctx) {} |
| 166 |
enterValue(ctx) {} |
| 167 |
exitValue(ctx) {} |
| 168 |
} |
| 169 |
``` |
| 170 |
|
| 171 |
In order to provide custom behavior, you might want to create the following class: |
| 172 |
|
| 173 |
```javascript |
| 174 |
class KeyPrinter extends MyGrammarListener { |
| 175 |
|
| 176 |
// override default listener behavior |
| 177 |
exitKey(ctx) { |
| 178 |
console.log("Oh, a key!"); |
| 179 |
} |
| 180 |
} |
| 181 |
``` |
| 182 |
|
| 183 |
In order to execute this listener, you would simply add the following lines to the above code: |
| 184 |
|
| 185 |
```javascript |
| 186 |
... |
| 187 |
tree = parser.StartRule() // only repeated here for reference |
| 188 |
const printer = new KeyPrinter(); |
| 189 |
antlr4.tree.ParseTreeWalker.DEFAULT.walk(printer, tree); |
| 190 |
``` |
| 191 |
|
| 192 |
## What about TypeScript? |
| 193 |
|
| 194 |
We currently do not have a TypeScript target, but Sam Harwell is [working on a port](https://github.com/tunnelvisionlabs/antlr4ts). [Here](https://github.com/ChuckJonas/extract-interface-ts) is Section 4.3 of [ANTLR 4 book](http://a.co/5jUJYmh) converted to typescript. |
| 195 |
|
| 196 |
## How do I integrate my parser with ACE editor? |
| 197 |
|
| 198 |
This specific task is described in this [dedicated page](ace-javascript-target.md). |
| 199 |
|
| 200 |
## How can I learn more about ANTLR? |
| 201 |
|
| 202 |
|
| 203 |
Further information can be found from "The definitive ANTLR 4 reference" book. |
| 204 |
|
| 205 |
The JavaScript implementation of ANTLR is as close as possible to the Java one, so you shouldn't find it difficult to adapt the book's examples to JavaScript. |