Antlr4 Java20 Grammar Query #4834
-
My use case is to count lines of Java code BUT only to include certain method types in the line counting. I intend to extend a base listener and implement the line counting function. But the listener methods are generated but never called when I parse some code. Separately, I've previously used an extended java base listener to calculate (for example) Cyclomatic complexity of my source code, but I am a grammar newbie. I am using the Java20 grammar: I've made a small change to the file from this: to this: NL : [\r\n]+ I can see a new names are now included in the generated
So, now I thought I could use this new Lexer rule in my grammar `// Paragraph 15.30 newLine As I expected, the base Listener
I can override these methods, but sadly they never get executed when I parse some code. I've executed My test file is simple and starts with `public class FastCarTest {
} The response I get from Any help to explain what I am doing wrong gratefully received. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You'll have to leave the grammar as is when parsing your input because changing the lexing rule for whitespace will break everything in the parser. Lexing occurs first and it is not guided at all by the parser. If you made this change, you would have a huge task of modifying the parser grammar to optionally recognize a newline between everything else. There is a better way. After parsing, what you will need is to first identify which methods you want to line count. You can do this with an Antlr Listener. You can then grab the text for the methods and just count newlines in the text. A Trash script can perform essentially what you want. Trash automates the generation of a standardized driver application for a grammar. You can then use the other Trash tools and Unix
|
Beta Was this translation helpful? Give feedback.
You'll have to leave the grammar as is when parsing your input because changing the lexing rule for whitespace will break everything in the parser. Lexing occurs first and it is not guided at all by the parser. If you made this change, you would have a huge task of modifying the parser grammar to optionally recognize a newline between everything else. There is a better way.
After parsing, what you will need is to first identify which methods you want to line count. You can do this with an Antlr Listener. You can then grab the text for the methods and just count newlines in the text.
A Trash script can perform essentially what you want. Trash automates the generation of a standardized driv…