kezhuw commented on code in PR #2206: URL: https://github.com/apache/zookeeper/pull/2206#discussion_r1863085617
########## zookeeper-jute/src/main/java/org/apache/jute/compiler/JRecord.java: ########## @@ -767,4 +795,135 @@ public static String getCsharpFQName(String name) { } return fQName.toString(); } + + public String getJavaFieldComments(JField jField) { + return getFieldComments(jField, " "); + } + + public String getCFieldComments(JField jField) { + return getFieldComments(jField, " "); + } + + private String getFieldComments(JField jField, String indent) { + if (jField == null || jField.getTypeToken() == null || jField.getNextToken() == null) { + return ""; + } + + // get the comment before the line + Token beforeTheLineCommentToken = getCommentToken(jField.getPreviousToken(), jField.getTypeToken()); + List<String> comments = extractLeadingComments(beforeTheLineCommentToken, null); + + Token endOfLineCommentToken = getCommentToken(null, jField.getNextToken()); + if (endOfLineCommentToken != null && jField.getTypeToken().beginLine == endOfLineCommentToken.beginLine) { + + comments.addAll(extractLeadingComments(endOfLineCommentToken, endOfLineCommentToken.next)); + } + + return formatComments(indent, comments); + } + + private Token getCommentToken(Token previousToken, Token token) { + if (token == null || token.specialToken == null || belongsToThePreviousToken(previousToken, token.specialToken)) { + return null; + } + + Token tmpToken = token.specialToken; + while (tmpToken.specialToken != null) { + + if (belongsToThePreviousToken(previousToken, tmpToken.specialToken)) { Review Comment: ```java Token commentToken = token.specialToken; while (commentToken.specialToken != null) { commentToken = commentToken.specialToken; } // Skip end of line comment belong to previous token. if (previousToken != null && commentToken.beginLine == previousToken.endLine) { commentToken = commentToken.next; } ``` Is this sufficient ? ########## zookeeper-jute/src/main/java/org/apache/jute/compiler/JRecord.java: ########## @@ -767,4 +795,135 @@ public static String getCsharpFQName(String name) { } return fQName.toString(); } + + public String getJavaFieldComments(JField jField) { + return getFieldComments(jField, " "); + } + + public String getCFieldComments(JField jField) { + return getFieldComments(jField, " "); + } + + private String getFieldComments(JField jField, String indent) { + if (jField == null || jField.getTypeToken() == null || jField.getNextToken() == null) { + return ""; + } + + // get the comment before the line + Token beforeTheLineCommentToken = getCommentToken(jField.getPreviousToken(), jField.getTypeToken()); + List<String> comments = extractLeadingComments(beforeTheLineCommentToken, null); + + Token endOfLineCommentToken = getCommentToken(null, jField.getNextToken()); + if (endOfLineCommentToken != null && jField.getTypeToken().beginLine == endOfLineCommentToken.beginLine) { + + comments.addAll(extractLeadingComments(endOfLineCommentToken, endOfLineCommentToken.next)); + } + + return formatComments(indent, comments); + } + + private Token getCommentToken(Token previousToken, Token token) { + if (token == null || token.specialToken == null || belongsToThePreviousToken(previousToken, token.specialToken)) { + return null; + } + + Token tmpToken = token.specialToken; + while (tmpToken.specialToken != null) { + + if (belongsToThePreviousToken(previousToken, tmpToken.specialToken)) { + return tmpToken; + } + tmpToken = tmpToken.specialToken; + } + return tmpToken; + } + + /** + * Determine whether the current commentToken belongs to the previousToken. + * + * @return true: If the current commentToken should belong to the previousToken. + */ + private boolean belongsToThePreviousToken(Token previousToken, Token commentToken) { + if (previousToken == null || commentToken == null) { + return false; + } + + return previousToken.beginLine == commentToken.beginLine; + } + + public String getRecordComments() { + if (getRecordToken() == null || getRecordToken().specialToken == null) { + return ""; + } + + // get the comments before the class + Token beforeTheClassToken = getCommentToken(null, getRecordToken()); Review Comment: ```suggestion Token commentToken = getCommentToken(getRecordToken(), null); ``` I think `commentToken` is good enough. Also I suggest renaming `extractLeadingComments` to simple `extractComments`. ########## zookeeper-jute/src/main/java/org/apache/jute/compiler/generated/rcc.jj: ########## @@ -274,21 +250,33 @@ JRecord Record() : ArrayList<JField> flist = new ArrayList<JField>(); Token t; JField f; + // Get the comments on the class token + Token recordTkn; + Token typeTkn; + Token previousToken = null; } { - <RECORD_TKN> + recordTkn = <RECORD_TKN> t = <IDENT_TKN> { rname = t.image; } <LBRACE_TKN> ( + {typeTkn = getToken(1);} f = Field() { flist.add(f); } <SEMICOLON_TKN> + { + f.setTypeToken(typeTkn); + f.setPreviousToken(previousToken); + f.setNextToken(getToken(1)); + previousToken = typeTkn; Review Comment: Not a big deal. But I think the `previousToken` should be `<SEMICOLON_TKN>`. ########## zookeeper-jute/src/main/java/org/apache/jute/compiler/JRecord.java: ########## @@ -767,4 +795,135 @@ public static String getCsharpFQName(String name) { } return fQName.toString(); } + + public String getJavaFieldComments(JField jField) { + return getFieldComments(jField, " "); + } + + public String getCFieldComments(JField jField) { + return getFieldComments(jField, " "); + } + + private String getFieldComments(JField jField, String indent) { + if (jField == null || jField.getTypeToken() == null || jField.getNextToken() == null) { + return ""; + } + + // get the comment before the line + Token beforeTheLineCommentToken = getCommentToken(jField.getPreviousToken(), jField.getTypeToken()); + List<String> comments = extractLeadingComments(beforeTheLineCommentToken, null); + + Token endOfLineCommentToken = getCommentToken(null, jField.getNextToken()); + if (endOfLineCommentToken != null && jField.getTypeToken().beginLine == endOfLineCommentToken.beginLine) { + + comments.addAll(extractLeadingComments(endOfLineCommentToken, endOfLineCommentToken.next)); + } + + return formatComments(indent, comments); + } + + private Token getCommentToken(Token previousToken, Token token) { Review Comment: ```suggestion private Token getCommentToken(Token token, Token previousToken) { ``` I think `previousToken` is an assistant parameter and prabably don't deserve the main focus. ########## zookeeper-jute/src/main/java/org/apache/jute/compiler/JRecord.java: ########## @@ -767,4 +795,135 @@ public static String getCsharpFQName(String name) { } return fQName.toString(); } + + public String getJavaFieldComments(JField jField) { + return getFieldComments(jField, " "); + } + + public String getCFieldComments(JField jField) { + return getFieldComments(jField, " "); + } + + private String getFieldComments(JField jField, String indent) { + if (jField == null || jField.getTypeToken() == null || jField.getNextToken() == null) { + return ""; + } + + // get the comment before the line + Token beforeTheLineCommentToken = getCommentToken(jField.getPreviousToken(), jField.getTypeToken()); + List<String> comments = extractLeadingComments(beforeTheLineCommentToken, null); + + Token endOfLineCommentToken = getCommentToken(null, jField.getNextToken()); + if (endOfLineCommentToken != null && jField.getTypeToken().beginLine == endOfLineCommentToken.beginLine) { + + comments.addAll(extractLeadingComments(endOfLineCommentToken, endOfLineCommentToken.next)); + } + + return formatComments(indent, comments); + } + + private Token getCommentToken(Token previousToken, Token token) { + if (token == null || token.specialToken == null || belongsToThePreviousToken(previousToken, token.specialToken)) { + return null; + } + + Token tmpToken = token.specialToken; Review Comment: ```suggestion Token commentToken = token.specialToken; ``` I think `commentToken` is better than `tmpToken`. ########## zookeeper-jute/src/main/java/org/apache/jute/compiler/generated/rcc.jj: ########## @@ -274,21 +250,33 @@ JRecord Record() : ArrayList<JField> flist = new ArrayList<JField>(); Token t; JField f; + // Get the comments on the class token + Token recordTkn; + Token typeTkn; + Token previousToken = null; } { - <RECORD_TKN> + recordTkn = <RECORD_TKN> t = <IDENT_TKN> { rname = t.image; } <LBRACE_TKN> Review Comment: ```suggestion previousToken = <LBRACE_TKN> ``` ########## zookeeper-jute/src/main/java/org/apache/jute/compiler/JRecord.java: ########## @@ -436,10 +455,19 @@ public void genJavaCode(File outputDirectory) throws IOException { jj.write("import org.apache.jute.*;\n"); jj.write("import org.apache.jute.Record; // JDK14 needs explicit import due to clash with java.lang.Record\n"); jj.write("import org.apache.yetus.audience.InterfaceAudience;\n"); + String recordComments = getRecordComments(); + if (recordComments != null && !recordComments.isEmpty()) { + jj.write(recordComments); + } jj.write("@InterfaceAudience.Public\n"); jj.write("public class " + getName() + " implements Record {\n"); for (Iterator<JField> i = mFields.iterator(); i.hasNext(); ) { JField jf = i.next(); + Review Comment: ```suggestion ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@zookeeper.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org