forked from jasder/antlr
Update the docs
This commit is contained in:
parent
7143fed700
commit
11ede0fc77
|
@ -11,11 +11,18 @@ In practice, this target has been extensively tested against:
|
||||||
* Chrome 39.0.2171
|
* Chrome 39.0.2171
|
||||||
* Explorer 11.0.3
|
* Explorer 11.0.3
|
||||||
|
|
||||||
The tests were conducted using Selenium. No issue was found, so you should find that the runtime works pretty much against any recent JavaScript engine.
|
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.
|
||||||
|
|
||||||
## Is NodeJS supported?
|
## Is NodeJS supported?
|
||||||
|
|
||||||
The runtime has also been extensively tested against Node.js 10 LTS. No issue was found.
|
The runtime has also been extensively tested against Node.js 14 LTS. No issue was found.
|
||||||
|
NodeJS together with a packaging tool is now the preferred development path, developers are encouraged to follow it.
|
||||||
|
|
||||||
|
## What about modules?
|
||||||
|
|
||||||
|
Starting with version 8.1, Antlr4 JavaScript runtime follows esm semantics (see https://tc39.es/ecma262/#sec-modules for details)
|
||||||
|
Generated lexers, parsers, listeners and visitors also follow this new standard.
|
||||||
|
If you have used previous versions of the runtime, you will need to migrate and make your parser a module.
|
||||||
|
|
||||||
## How to create a JavaScript lexer or parser?
|
## How to create a JavaScript lexer or parser?
|
||||||
|
|
||||||
|
@ -80,18 +87,18 @@ Let's suppose that your grammar is named, as above, "MyGrammar". Let's suppose t
|
||||||
Now a fully functioning script might look like the following:
|
Now a fully functioning script might look like the following:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var antlr4 = require('antlr4');
|
import antlr4 from 'antlr4';
|
||||||
var MyGrammarLexer = require('./MyGrammarLexer').MyGrammarLexer;
|
import MyGrammarLexer from './MyGrammarLexer.js');
|
||||||
var MyGrammarParser = require('./MyGrammarParser').MyGrammarParser;
|
import MyGrammarParser from './MyGrammarParser.js';
|
||||||
var MyGrammarListener = require('./MyGrammarListener').MyGrammarListener;
|
import MyGrammarListener from './MyGrammarListener.js';
|
||||||
|
|
||||||
var input = "your text to parse here"
|
const input = "your text to parse here"
|
||||||
var chars = new antlr4.InputStream(input);
|
const chars = new antlr4.InputStream(input);
|
||||||
var lexer = new MyGrammarLexer(chars);
|
const lexer = new MyGrammarLexer(chars);
|
||||||
var tokens = new antlr4.CommonTokenStream(lexer);
|
const tokens = new antlr4.CommonTokenStream(lexer);
|
||||||
var parser = new MyGrammarParser(tokens);
|
const parser = new MyGrammarParser(tokens);
|
||||||
parser.buildParseTrees = true;
|
parser.buildParseTrees = true;
|
||||||
var tree = parser.MyStartRule();
|
const tree = parser.MyStartRule();
|
||||||
```
|
```
|
||||||
|
|
||||||
This program will work. But it won't be useful unless you do one of the following:
|
This program will work. But it won't be useful unless you do one of the following:
|
||||||
|
@ -105,19 +112,19 @@ This program will work. But it won't be useful unless you do one of the followin
|
||||||
## How do I create and run a visitor?
|
## How do I create and run a visitor?
|
||||||
```javascript
|
```javascript
|
||||||
// test.js
|
// test.js
|
||||||
var antlr4 = require('antlr4');
|
import antlr4 from 'antlr4';
|
||||||
var MyGrammarLexer = require('./QueryLexer').QueryLexer;
|
import MyGrammarLexer from './QueryLexer.js';
|
||||||
var MyGrammarParser = require('./QueryParser').QueryParser;
|
import MyGrammarParser from './QueryParser.js';
|
||||||
var MyGrammarListener = require('./QueryListener').QueryListener;
|
import MyGrammarListener from './QueryListener.js';
|
||||||
|
|
||||||
|
|
||||||
var input = "field = 123 AND items in (1,2,3)"
|
const input = "field = 123 AND items in (1,2,3)"
|
||||||
var chars = new antlr4.InputStream(input);
|
const chars = new antlr4.InputStream(input);
|
||||||
var lexer = new MyGrammarLexer(chars);
|
const lexer = new MyGrammarLexer(chars);
|
||||||
var tokens = new antlr4.CommonTokenStream(lexer);
|
const tokens = new antlr4.CommonTokenStream(lexer);
|
||||||
var parser = new MyGrammarParser(tokens);
|
const parser = new MyGrammarParser(tokens);
|
||||||
parser.buildParseTrees = true;
|
parser.buildParseTrees = true;
|
||||||
var tree = parser.query();
|
const tree = parser.query();
|
||||||
|
|
||||||
class Visitor {
|
class Visitor {
|
||||||
visitChildren(ctx) {
|
visitChildren(ctx) {
|
||||||
|
@ -145,40 +152,37 @@ tree.accept(new Visitor());
|
||||||
Let's suppose your MyGrammar grammar comprises 2 rules: "key" and "value". The antlr4 tool will have generated the following listener:
|
Let's suppose your MyGrammar grammar comprises 2 rules: "key" and "value". The antlr4 tool will have generated the following listener:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
MyGrammarListener = function(ParseTreeListener) {
|
class MyGrammarListener extends ParseTreeListener {
|
||||||
// some code here
|
|
||||||
}
|
constructor() {
|
||||||
// some code here
|
super();
|
||||||
MyGrammarListener.prototype.enterKey = function(ctx) {};
|
}
|
||||||
MyGrammarListener.prototype.exitKey = function(ctx) {};
|
|
||||||
MyGrammarListener.prototype.enterValue = function(ctx) {};
|
enterKey(ctx) {}
|
||||||
MyGrammarListener.prototype.exitValue = function(ctx) {};
|
exitKey(ctx) {}
|
||||||
|
enterValue(ctx) {}
|
||||||
|
exitValue(ctx) {}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
In order to provide custom behavior, you might want to create the following class:
|
In order to provide custom behavior, you might want to create the following class:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var KeyPrinter = function() {
|
class KeyPrinter extends MyGrammarListener {
|
||||||
MyGrammarListener.call(this); // inherit default listener
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
|
|
||||||
// continue inheriting default listener
|
// override default listener behavior
|
||||||
KeyPrinter.prototype = Object.create(MyGrammarListener.prototype);
|
exitKey(ctx) {
|
||||||
KeyPrinter.prototype.constructor = KeyPrinter;
|
console.log("Oh, a key!");
|
||||||
|
}
|
||||||
// override default listener behavior
|
}
|
||||||
KeyPrinter.prototype.exitKey = function(ctx) {
|
|
||||||
console.log("Oh, a key!");
|
|
||||||
};
|
|
||||||
```
|
```
|
||||||
|
|
||||||
In order to execute this listener, you would simply add the following lines to the above code:
|
In order to execute this listener, you would simply add the following lines to the above code:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
...
|
...
|
||||||
tree = parser.StartRule() // only repeated here for reference
|
tree = parser.StartRule() // only repeated here for reference
|
||||||
var printer = new KeyPrinter();
|
const printer = new KeyPrinter();
|
||||||
antlr4.tree.ParseTreeWalker.DEFAULT.walk(printer, tree);
|
antlr4.tree.ParseTreeWalker.DEFAULT.walk(printer, tree);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue