From 6160aa6101fead4b23f71c9f92d5c4b3174b9ddf Mon Sep 17 00:00:00 2001 From: Justin Thomas Date: Tue, 12 Mar 2019 06:51:56 -0700 Subject: [PATCH] Added visitor example. --- doc/javascript-target.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/doc/javascript-target.md b/doc/javascript-target.md index 2d9973be6..845fcba59 100644 --- a/doc/javascript-target.md +++ b/doc/javascript-target.md @@ -117,6 +117,44 @@ This program will work. But it won't be useful unless you do one of the followin (please note that production code is target specific, so you can't have multi target grammars that include production code) +## How do I create and run a visitor? +```javascript +// test.js +var antlr4 = require('antlr4'); +var MyGrammarLexer = require('./QueryLexer').QueryLexer; +var MyGrammarParser = require('./QueryParser').QueryParser; +var MyGrammarListener = require('./QueryListener').QueryListener; + + +var input = "field = 123 AND items in (1,2,3)" +var chars = new antlr4.InputStream(input); +var lexer = new MyGrammarLexer(chars); +var tokens = new antlr4.CommonTokenStream(lexer); +var parser = new MyGrammarParser(tokens); +parser.buildParseTrees = true; +var tree = parser.query(); + +class Visitor { + visitChildren(ctx) { + if (!ctx) { + return; + } + + if (ctx.children) { + return ctx.children.map(child => { + if (child.children && child.children.length != 0) { + return child.accept(this); + } else { + return child.getText(); + } + }); + } + } +} + +tree.accept(new Visitor()); +```` + ## How do I create and run a custom listener? Let's suppose your MyGrammar grammar comprises 2 rules: "key" and "value". The antlr4 tool will have generated the following listener: