forked from jasder/antlr
Merge pull request #2198 from kasbah/fix-js-examples
fix indentation in javascript target examples
This commit is contained in:
commit
0d6aebbb8e
|
@ -180,6 +180,7 @@ YYYY/MM/DD, github id, Full name, email
|
|||
2017/12/03, oranoran, Oran Epelbaum, oran / epelbaum me
|
||||
2017/12/20, kbsletten, Kyle Sletten, kbsletten@gmail.com
|
||||
2017/12/27, jkmar, Jakub Marciniszyn, marciniszyn.jk@gmail.com
|
||||
2018/01/06, kasbah, Kaspar Emanuel, kaspar@monostable.co.uk
|
||||
2018/02/08, razfriman, Raz Friman, raz@razfriman.com
|
||||
2018/02/11, io7m, Mark Raynsford, code@io7m.com
|
||||
2018/05/15, johnvanderholt, jan dillingh johnvanderholte@gmail.com
|
||||
|
|
|
@ -95,11 +95,16 @@ 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:
|
||||
|
||||
```javascript
|
||||
var antlr4 = require('antlr4');
|
||||
var MyGrammarLexer = require('./MyGrammarLexer').MyGrammarLexer;
|
||||
var MyGrammarParser = require('./MyGrammarParser').MyGrammarParser;
|
||||
var MyGrammarListener = require('./MyGrammarListener').MyGrammarListener;
|
||||
|
||||
var input = "your text to parse here"
|
||||
var chars = new antlr4.InputStream(input);
|
||||
var lexer = new MyGrammarLexer.MyGrammarLexer(chars);
|
||||
var lexer = new MyGrammarLexer(chars);
|
||||
var tokens = new antlr4.CommonTokenStream(lexer);
|
||||
var parser = new MyGrammarParser.MyGrammarParser(tokens);
|
||||
var parser = new MyGrammarParser(tokens);
|
||||
parser.buildParseTrees = true;
|
||||
var tree = parser.MyStartRule();
|
||||
```
|
||||
|
@ -130,28 +135,28 @@ Let's suppose your MyGrammar grammar comprises 2 rules: "key" and "value". The a
|
|||
In order to provide custom behavior, you might want to create the following class:
|
||||
|
||||
```javascript
|
||||
KeyPrinter = function() {
|
||||
MyGrammarListener.call(this); // inherit default listener
|
||||
return this;
|
||||
};
|
||||
var KeyPrinter = function() {
|
||||
MyGrammarListener.call(this); // inherit default listener
|
||||
return this;
|
||||
};
|
||||
|
||||
// inherit default listener
|
||||
// continue inheriting default listener
|
||||
KeyPrinter.prototype = Object.create(MyGrammarListener.prototype);
|
||||
KeyPrinter.prototype.constructor = KeyPrinter;
|
||||
|
||||
// override default listener behavior
|
||||
KeyPrinter.prototype.exitKey = function(ctx) {
|
||||
console.log("Oh, a key!");
|
||||
};
|
||||
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:
|
||||
|
||||
```javascript
|
||||
...
|
||||
tree = parser.StartRule() - only repeated here for reference
|
||||
var printer = new KeyPrinter();
|
||||
antlr4.tree.ParseTreeWalker.DEFAULT.walk(printer, tree);
|
||||
...
|
||||
tree = parser.StartRule() // only repeated here for reference
|
||||
var printer = new KeyPrinter();
|
||||
antlr4.tree.ParseTreeWalker.DEFAULT.walk(printer, tree);
|
||||
```
|
||||
|
||||
## What about TypeScript?
|
||||
|
|
Loading…
Reference in New Issue