Understanding AST and visitToken() function #12907
Replies: 2 comments 5 replies
-
kind of yes, you need to read https://checkstyle.org/writingchecks.html#Understanding_the_visitor_pattern |
Beta Was this translation helpful? Give feedback.
-
No. Your code only goes from sibling to sibling like a production line or 1 dimensional array. It has no depth to it. If you are familar with linked lists, then you need to think of Checkstyle's AST as a 2 dimensional linked list. If your not familar with linked lists, then I would suggest looking it up. We are virtually the same as a linked list. Your code is missing usages of If you want to simulate what CS does for "walking" through the nodes, one by one, then look at checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java Lines 403 to 414 in 6a36be8 Example:
Technically no. Also, you are not required to use the |
Beta Was this translation helpful? Give feedback.
-
Here is a conversation that I had with chatGPT:
I was trying to understand what is the purpose of AST here:
An arbitrary source code that I have doubt about (kindly read below):
Question 1.1
Is the above java code a smaller representation of what happens in checkstyle codebase when working on source codes? Is my line of thought correct below.
When a user runs the checkstyle application in the terminal using the command
java -jar "checkstyle-10.7.0-all.jar" -c config.xml test.java
, the source code is taken fromtest.java
in the format of a largeString
. Since, parsing on just the source code is not beneficial as making checks, formatting etc would be difficult, we convert theString
(that istest.java
) into an AST usingDetailAST ast = DetailASTUtil.parse(source);
whereast
holds the whole AST oftest.java
.Since, working with the AST is much more simpler and beneficial (as it gives more control over the code), we use the AST to make the checks etc and report violation.
This is where
visitToken()
comes into the picture.visitToken(DefaultAST)
takes theast
(which is the AST oftest.java
) and helps perform all kinds of operations like going to thenextSibling
, going to theparentNode
, going to the leaf nodes etc.This automatically requires for
visitToken()
to be present in every file of the source code of checkstyle that is involved in making checks on the file sent by the user which istest.java
.Information that chatGPT gave me:
Beta Was this translation helpful? Give feedback.
All reactions