kezhuw commented on code in PR #2206: URL: https://github.com/apache/zookeeper/pull/2206#discussion_r1857737011
########## zookeeper-jute/src/main/java/org/apache/jute/compiler/JRecord.java: ########## @@ -767,4 +793,88 @@ 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 space) { + if (jField == null || jField.getTypeToken() == null || jField.getNextToken() == null) { + return ""; + } + + // get the comment before the line + List<String> comments = getComments(jField.getTypeToken().specialToken); + + // get the end-of-line comments of fields + // If the current field and the next field are on the same line, + // the leading field comment of the next field should be discarded + Token tmpToken = jField.getNextToken(); + while (tmpToken.specialToken != null) { + + if (jField.getTypeToken().beginLine == tmpToken.specialToken.beginLine) { + Token endLineComments = tmpToken.specialToken; + tmpToken.specialToken = null; + comments.addAll(getComments(endLineComments)); + break; + } + + tmpToken = tmpToken.specialToken; + } + + return formatComments(space, comments); + } + + public String getRecordComments() { + if (getRecordToken() == null || getRecordToken().specialToken == null) { + return ""; + } + + // get the comments before the class + return formatComments("", getComments(getRecordToken().specialToken)); + } + + private static String formatComments(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(); + } + + private static List<String> getComments(Token token) { Review Comment: 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. ########## zookeeper-jute/src/main/java/org/apache/jute/compiler/generated/rcc.jj: ########## @@ -111,17 +119,7 @@ SKIP : SPECIAL_TOKEN : { - "//" : WithinOneLineComment -} - -<WithinOneLineComment> SPECIAL_TOKEN : -{ - <("\n" | "\r" | "\r\n" )> : DEFAULT -} - -<WithinOneLineComment> MORE : -{ - <~[]> + <WithinOneLineComment: "//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")> Review Comment: Should be "ONE_LINE_COMMENT" ? It is as a "Token.kind" constant in generated in "RccConstants". ########## zookeeper-jute/src/main/java/org/apache/jute/compiler/JRecord.java: ########## @@ -767,4 +793,88 @@ 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 space) { Review Comment: s/space/indent/ ? -- 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