kezhuw commented on code in PR #2206:
URL: https://github.com/apache/zookeeper/pull/2206#discussion_r1861950607


##########
zookeeper-jute/src/main/java/org/apache/jute/compiler/generated/rcc.jj:
##########
@@ -274,22 +257,38 @@ JRecord Record() :
     ArrayList<JField> flist = new ArrayList<JField>();
     Token t;
     JField f;
+    // Get the comments on the class token
+    Token recordTkn;
+    Token rbraceTkn;
+    Token typeTkn;
 }
 {
-    <RECORD_TKN>
+    recordTkn = <RECORD_TKN>
     t = <IDENT_TKN>
     { rname = t.image; }
     <LBRACE_TKN>
     (
+        {typeTkn = getToken(1);}
         f = Field()
-        { flist.add(f); }
+        {
+            flist.add(f);
+        }
         <SEMICOLON_TKN>
+        {
+            f.setTypeToken(typeTkn);
+            prevFieldSetNextTkn(typeTkn);
+            prevField = f;

Review Comment:
   `prevField` is not used outside `Record`, it should be a local also.



##########
zookeeper-jute/src/main/java/org/apache/jute/compiler/generated/rcc.jj:
##########
@@ -274,22 +257,38 @@ JRecord Record() :
     ArrayList<JField> flist = new ArrayList<JField>();
     Token t;
     JField f;
+    // Get the comments on the class token
+    Token recordTkn;
+    Token rbraceTkn;
+    Token typeTkn;
 }
 {
-    <RECORD_TKN>
+    recordTkn = <RECORD_TKN>
     t = <IDENT_TKN>
     { rname = t.image; }
     <LBRACE_TKN>
     (
+        {typeTkn = getToken(1);}
         f = Field()
-        { flist.add(f); }
+        {
+            flist.add(f);
+        }
         <SEMICOLON_TKN>
+        {
+            f.setTypeToken(typeTkn);
+            prevFieldSetNextTkn(typeTkn);
+            prevField = f;

Review Comment:
   How about replace `nextToken` with `semicolonToken` ? I think it is suitable:
   1. Semicolon is the end of field declaration.
   2. `semicolonToken.next` is the next token. We can extract possible end of 
line comments from there.
   
   This way we don't need `prevField` anymore.



##########
zookeeper-jute/src/main/java/org/apache/jute/compiler/JRecord.java:
##########
@@ -767,4 +795,127 @@ 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
+        List<String> comments = 
extractLeadingComments(getCommentToken(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 commentToken = 
getCommentToken(jField.getNextToken().specialToken);
+        if (commentToken != null && jField.getTypeToken().beginLine == 
commentToken.beginLine) {
+
+            if (jField.getNextToken().specialToken == commentToken) {
+                jField.getNextToken().specialToken = null;
+            }
+
+            if (commentToken.next != null) {
+                commentToken.next.specialToken = null;
+                commentToken.next = null;
+            }
+            comments.addAll(extractLeadingComments(commentToken));
+        }
+
+        return formatComments(indent, comments);
+    }
+
+    private Token getCommentToken(Token token) {

Review Comment:
   How about accept normal token as input ? I saw pattern 
`getCommentToken(xzyToken.specialToken)`.



##########
zookeeper-jute/src/main/java/org/apache/jute/compiler/JRecord.java:
##########
@@ -767,4 +795,127 @@ 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
+        List<String> comments = 
extractLeadingComments(getCommentToken(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 commentToken = 
getCommentToken(jField.getNextToken().specialToken);
+        if (commentToken != null && jField.getTypeToken().beginLine == 
commentToken.beginLine) {
+
+            if (jField.getNextToken().specialToken == commentToken) {
+                jField.getNextToken().specialToken = null;
+            }
+
+            if (commentToken.next != null) {
+                commentToken.next.specialToken = null;
+                commentToken.next = null;

Review Comment:
   Also `extractLeadingComments` could take a `sentinelToken` to limit end-line 
comment traversal without changing `Token.next` field.



##########
zookeeper-jute/src/main/java/org/apache/jute/compiler/JRecord.java:
##########
@@ -767,4 +795,127 @@ 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
+        List<String> comments = 
extractLeadingComments(getCommentToken(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 commentToken = 
getCommentToken(jField.getNextToken().specialToken);
+        if (commentToken != null && jField.getTypeToken().beginLine == 
commentToken.beginLine) {
+
+            if (jField.getNextToken().specialToken == commentToken) {
+                jField.getNextToken().specialToken = null;
+            }
+
+            if (commentToken.next != null) {
+                commentToken.next.specialToken = null;
+                commentToken.next = null;

Review Comment:
   I am thinking whether we should have a `previousToken`. This way 
`getCommentToken` can skip end-line comment for previous entry without 
modifying things in code generation. I expect code generation is reproducible.



-- 
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

Reply via email to