-
Notifications
You must be signed in to change notification settings - Fork 7.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ZOOKEEPER-4880: Generate comments from zookeeper.jute into code. #2206
base: master
Are you sure you want to change the base?
Conversation
f24cad3
to
6365b88
Compare
@kezhuw Could you help me take a look? Thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @luozongle01, thank you for your contribution!
Given the fact that jute and all targeting languages support both // and multi-line comments, I suggest we output whatever comments captured in origin sharp. With help of beginLine
, beginColumn
, endLine
and endColumn
of Token
, I think we can reproduce comments in generated code with indentation adjusted.
while (tmpTkn != null) { | ||
String image = tmpTkn.image.trim(); | ||
tmpTkn = tmpTkn.next; | ||
if (image.startsWith("//")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The capture does not shaped well for directly usage. I tasted the following capture, and it captured a complete line comment. This way we can output whatever it captured without post work.
SPECIAL_TOKEN :
{
<SINGLE_LINE_COMMENT: "//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")>
}
See also https://javacc.github.io/javacc/tutorials/token-manager.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much for your comment. I will make some further modifications.😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The capture does not shaped well for directly usage. I tasted the following capture, and it captured a complete line comment. This way we can output whatever it captured without post work.
SPECIAL_TOKEN : { <SINGLE_LINE_COMMENT: "//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")> }
See also https://javacc.github.io/javacc/tutorials/token-manager.html
It seems like there's no need to change this now, because I get all the comments,then I use and distinguished them by line numbe. 😂😂
52d4194
to
0631b4f
Compare
Hi, @kezhuw, I made some modifications, thank you for your review last time. I feel much better now than last time. Currently, multi-line comments and single-line comments are supported. I put Here is my testing process.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@luozongle01 Thank you for your work!
It might be late, I am sorry for this, but I think we probably should align on how comments from jute should be formatted in generated sources ? I see two approaches.
- Keep it in origin format.
- Transfer comments from one format to another.
I saw this pr is going on the second approach. I am good to unify the generated comment format. But I think it could be challenge, as we should escape between two formats which could make generated comments strange/inaccurate.
// /**
// * Some dead comments.
// */
//
// Things changed. We are going ..
Currently, the generated comments blindly strip some special strings. I don't think it is a good thing.
I suggest we keep it simple and don't touch comment content, this is also a safe path. If we want javadoc style comments, I think it is ok to convert them manually.
String[] commentArray = commentMulti.split("\n"); | ||
for (String comment : commentArray) { | ||
|
||
comment = comment.replaceAll("//|/\\*\\*|/\\*|\\*\\*/|\\*/", "") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do think people will want to maintain this kind of code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/do/don't/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do think people will want to maintain this kind of code.
I'll amend that and I won't touch the comments so they're not needed here.
comment = comment.replaceAll("//|/\\*\\*|/\\*|\\*\\*/|\\*/", "") | ||
.replaceAll("^[* ]+", "").trim(); | ||
|
||
if (!comment.isEmpty()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The generated comments has no empty line which is not good.
@kezhuw Thank you very much for your review. I really didn't consider that 😂😂. I will keep the original format output. |
I prefer to move them above the code. |
Thanks, I thought so too, I'll modify the code. |
0631b4f
to
ae35908
Compare
Hi @kezhuw , I made some changes, could you take a look at it for me? I won't touch the annotation content now.
private static String getComments(String space, List<String> commentList) {
if (commentList == null || commentList.isEmpty()) {
return "";
}
String comments = String.join("", commentList);
StringBuilder formatBuilder = new StringBuilder();
for (String s : comments.split("\r?\n")) {
formatBuilder.append("\n")
.append(space)
.append(s.replaceAll("^[ \t]+", ""));
}
return formatBuilder.append("\n").toString();
} |
Hi @kezhuw, These days, I have researched how JavaParser and Protobuf handle comments again. The Comment#content method of Javaparser can retrieve the content inside the comment, which will remove the first layer The creatCommentFromToken method is the process of handling content Then I also tested the process of protobuf handling comments. protobuf will format Will convert multi line comments nested within single line comments into So I think I just need to handle the following single line comments and a few situations
For internal comments, I only need to convert So can I make further modifications this way or are there any issues I haven't thought of? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution! I left some inline comments regarding the code.
Regarding comment style, I would say zookeeper-jute
is used only by ZooKeeper and is not adopted by other projects as IDL. It is enough for us to go here.
} | ||
|
||
public void addJField(JField jField) { | ||
jFieldTreeSet.add(jField); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extends RangeInfo
is aggressive. addCommentRange(JXyz jXyz, RangeInfo rangeInfo)
should be sufficient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I will modify it
return getCommentsList(jField, jFieldTreeSet, fieldCommentsTokenMap); | ||
} | ||
|
||
private static List<String> getCommentsList(RangeInfo rangeInfo, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I expect this should be much clear. Given that we support only comments in three places:
- Comments before
class
for record types. - Comments before fields.
- Line ending comments for fields.
For the first, we could extract comments from Token::specialToken
. For the second, similar to the first, but we have to drop comments(could be in either //
or /* */
form) belong to previous field. For the third, additional lookup is required, also drop comments belong to next field.
5b46847
to
74e3326
Compare
74e3326
to
a33fc24
Compare
Hi @kezhuw, Thank you for your review last time. The code feels much more concise now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me in direction. I left some inline comments.
<WithinOneLineComment> MORE : | ||
{ | ||
<~[]> | ||
<WithinOneLineComment: "//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be "ONE_LINE_COMMENT" ? It is as a "Token.kind" constant in generated in "RccConstants".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be "ONE_LINE_COMMENT" ? It is as a "Token.kind" constant in generated in "RccConstants".
Yes, I'll make some modifications here.
return getFieldComments(jField, " "); | ||
} | ||
|
||
private String getFieldComments(JField jField, String space) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/space/indent/ ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/space/indent/ ?
This is because org.apache.jute.compiler.JType#genCDecl
also contains spaces, so I also used spaces. 😂
return formatBuilder.append("\n").toString(); | ||
} | ||
|
||
private static List<String> getComments(Token token) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, all leading spaces in mult-line comment are dropped in generation. Can we reconstruct these for multi-line comment ?
Or how about <MULTI_LINE_COMMENT: "/*" (~["*"])* "*" (~["*","/"] (~["*"])* "*" | "*")* "/">
? (https://stackoverflow.com/questions/34550579/multiline-comments-in-javacc)
I find no hole despite there is one comment say that "not correct with respect to the language definition".
I think it is easy to reconstruct original format if multi-line comment is captured into one token.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all leading spaces in mult-line comment are dropped in generation
/**
** Something.
**/
For example, above comment will be formatted as following.
/**
** Something.
**/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or how about <MULTI_LINE_COMMENT: "/" (~[""])* "" (~["","/"] (~[""]) "" | "")* "/"> ? (https://stackoverflow.com/questions/34550579/multiline-comments-in-javacc)
I think it is easy to reconstruct original format if multi-line comment is captured into one token.
I am not sure the LOOKAHEAD
could help here. I tried but did not succeed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, then I'll research and see if I can capture multi line comments as one token.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, all leading spaces in mult-line comment are dropped in generation. Can we reconstruct these for multi-line comment ?
Or how about
<MULTI_LINE_COMMENT: "/*" (~["*"])* "*" (~["*","/"] (~["*"])* "*" | "*")* "/">
? (https://stackoverflow.com/questions/34550579/multiline-comments-in-javacc)I find no hole despite there is one comment say that "not correct with respect to the language definition".
I think it is easy to reconstruct original format if multi-line comment is captured into one token.
This way, multiple lines of comments can be obtained into one token.
MORE :
{
<ENTER_JAVADOC_COMMENT: "/**" ~["/"]> { input_stream.backup(1); } : IN_JAVADOC_COMMENT
|
<ENTER_MULTILINE_COMMENT: "/*"> : IN_MULTI_LINE_COMMENT
}
<IN_JAVADOC_COMMENT>
SPECIAL_TOKEN :
{
<JAVADOC_COMMENT: "*/" > : DEFAULT
}
<IN_MULTI_LINE_COMMENT>
SPECIAL_TOKEN :
{
<MULTI_LINE_COMMENT: "*/" > : DEFAULT
}
<IN_JAVADOC_COMMENT, IN_MULTI_LINE_COMMENT>
MORE :
{
<COMMENT_CONTENT: ~[] >
}
But there is still a small issue, it seems that there is no way to remove the space at the beginning of each middle line, and there is no space before the first line of multi line comments. 😂😂
Generate comments from zookeeper.jute into code: https://issues.apache.org/jira/browse/ZOOKEEPER-4880
Brief Description
Support generating end-of-line comments and class comments in
zookeeper.jute
into code.How to test this change
zookeeper-jute/target/generated-sources/java/org/apache/zookeeper
andzookeeper-client/zookeeper-client-c/generated
directories before and after the modification, there are no other differences except the comments.2. When the test class has multiple lines of comments, the comments can be generated normally.
3. Testing vector type properties can generate comments normally.
4. Testing class type properties can generate comments normally
5. Testing comments that do not meet expectations will not generate comments and will not affect the generation of normal code.