0

What I want to realize: Treating comment by ANTLR4 in JavaScript

Now I'm coding interpret C script by antlr4js, and I want to take comment content. example:

/* this is my function */
int add(int a, int b);

then, I want get string "this is my function", and attach to add().

I googled and found that I can use hidden channel. Here is the page that I refer to :

This is Stuff: Tackling Comments in ANTLR Compiler

I can code collecting comment after lexing (describing at Collecting Hidden Tokens in above page), but I can't follow next section Merging Tokens With AST because I can't find out CommonTree class what base class.

problem point

The image to code.(in above page)

public class HiddenTokenAwareTree extends CommonTree {

  private List<Token> preceding = new LinkedList<Token>();
  private List<Token> orphans = new LinkedList<Token>();
  private List<Token> following = new LinkedList<Token>();

  // ... constructors, getters and setters follow

}

This is Java code, so when implement in javascript, code will be like this:

var CommonTree = require("antlr4/PATH/TO/COMMONTREE").CommonTree;

class HiddenTokenAwareTree{

}

I can't find out this PATH/TO/COMMONTREE.

my project setting

node: 6.14.6 $ npm list --depth=0 ├── antlr4@4.8.0 ├── webpack@4.44.1 └── webpack-cli@3.3.12 I installed antlr4 by $ npm i antlr4.

Thanks for any advices !

1 Answers1

1

The tutorial you're linking to is for ANTLR v3, ANTLR v4 doesn't have the ability to define a custom tree adapter.

What you could do is create a visitor or listener and retrieve hidden tokens at certain nodes in your parse tree.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • Thanks for your advice ! Now I googled again and found [this page](https://stackoverflow.com/questions/49752817/how-can-i-detect-whitespace-in-my-parse-tree-in-antlr4) then I can treat comment, thanks ! – french_cruller Sep 21 '20 at 00:58