Skip to content

Commit

Permalink
ZOOKEEPER-4880: Generate comments from zookeeper.jute into code.
Browse files Browse the repository at this point in the history
  • Loading branch information
luozongle01 committed Oct 23, 2024
1 parent 837f86c commit 6365b88
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 34 deletions.
16 changes: 12 additions & 4 deletions zookeeper-jute/src/main/java/org/apache/jute/compiler/JField.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,27 @@

package org.apache.jute.compiler;

import org.apache.jute.compiler.generated.Token;

/**
*
*/
public class JField {
private JType mType;
private String mName;

/**
* field name token.
*/
private Token mNameToken;

/**
* Creates a new instance of JField.
*/
public JField(JType type, String name) {
public JField(JType type, Token token) {
mType = type;
mName = name;
mName = token.image;
mNameToken = token;
}

public String getSignature() {
Expand All @@ -42,7 +50,7 @@ public String genCppDecl() {
}

public String genCDecl() {
return mType.genCDecl(mName);
return mType.genCDecl(mNameToken);
}

public String genCsharpDecl() {
Expand All @@ -54,7 +62,7 @@ public String genCsharpConstructorParam(String fname) {
}

public String genJavaDecl() {
return mType.genJavaDecl(mName);
return mType.genJavaDecl(mNameToken);
}

public String genJavaConstructorParam(String fname) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import org.apache.jute.compiler.generated.Token;

/**
*
Expand All @@ -36,18 +38,20 @@ public class JRecord extends JCompType {
private String mName;
private String mModule;
private List<JField> mFields;
private Token mRecordToken;

/**
* Creates a new instance of JRecord.
*/
public JRecord(String name, ArrayList<JField> flist) {
public JRecord(String name, ArrayList<JField> flist, Token token) {
super("struct " + name.substring(name.lastIndexOf('.') + 1),
name.replaceAll("\\.", "::"), getCsharpFQName(name), name, "Record", name, getCsharpFQName("IRecord"));
mFQName = name;
int idx = name.lastIndexOf('.');
mName = name.substring(idx + 1);
mModule = name.substring(0, idx);
mFields = flist;
mRecordToken = token;
}

public String getName() {
Expand Down Expand Up @@ -208,6 +212,11 @@ public void genCCode(FileWriter h, FileWriter c) throws IOException {
}
}
String recName = getName();

String recordComments = getRecordComments();
if (recordComments != null && !recordComments.isEmpty()) {
h.write(recordComments);
}
h.write("struct " + recName + " {\n");
for (JField f : mFields) {
h.write(f.genCDecl());
Expand Down Expand Up @@ -436,6 +445,10 @@ 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(); ) {
Expand Down Expand Up @@ -767,4 +780,30 @@ public static String getCsharpFQName(String name) {
}
return fQName.toString();
}

protected String getRecordComments() {
if (mRecordToken == null || mRecordToken.specialToken == null) {
return null;
}

StringJoiner joiner = new StringJoiner("\n * ", "/**\n * ", "\n */\n");
Token tmpTkn = mRecordToken.specialToken;
while (tmpTkn.specialToken != null) {
tmpTkn = tmpTkn.specialToken;
}

while (tmpTkn != null) {
String image = tmpTkn.image.trim();
tmpTkn = tmpTkn.next;
if (image.startsWith("//")) {
image = image.replace("//", "").trim();
}
if (image.isEmpty()) {
continue;
}
joiner.add(image);
}

return joiner.toString();
}
}
42 changes: 42 additions & 0 deletions zookeeper-jute/src/main/java/org/apache/jute/compiler/JType.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

package org.apache.jute.compiler;


import org.apache.jute.compiler.generated.Rcc;
import org.apache.jute.compiler.generated.Token;

/**
* Abstract Base class for all types supported by Hadoop Record I/O.
*/
Expand Down Expand Up @@ -56,6 +60,10 @@ String genCDecl(String name) {
return " " + mCName + " " + name + ";\n";
}

String genCDecl(Token token) {
return getCComments(token) + genCDecl(token.image);
}

public String genCsharpDecl(String name) {
return " private " + mCsharpName + " " + name + ";\n";
}
Expand All @@ -64,6 +72,40 @@ String genJavaDecl(String fname) {
return " private " + mJavaName + " " + fname + ";\n";
}

String genJavaDecl(Token token) {
return getJavaComments(token) + genJavaDecl(token.image);
}

protected String getJavaComments(Token token) {
String comments = "";
if (token == null || token.beginLine != token.endLine) {
return comments;
}
Token specialToken = Rcc.getFieldSpecialToken(token.beginLine);

if (specialToken == null || specialToken.image == null || specialToken.image.isEmpty()) {
return comments;
}

comments = " /**\n * " + specialToken.image.trim() + "\n */\n";
return comments;
}

protected String getCComments(Token token) {
String comments = "";
if (token == null || token.beginLine != token.endLine) {
return comments;
}
Token specialToken = Rcc.getFieldSpecialToken(token.beginLine);

if (specialToken == null || specialToken.image == null || specialToken.image.isEmpty()) {
return comments;
}

comments = " /* " + specialToken.image.trim() + " */\n";
return comments;
}

String genJavaConstructorParam(String fname) {
return " " + mJavaName + " " + fname;
}
Expand Down
102 changes: 73 additions & 29 deletions zookeeper-jute/src/main/java/org/apache/jute/compiler/generated/rcc.jj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ import java.io.IOException;
@SuppressWarnings("unused")
public class Rcc {
private static Hashtable<String, JRecord> recTab = new Hashtable<String, JRecord>();

/**
* key: Line number value: End of line comments
* Used to obtain line ending comments
*/
private static Hashtable<Integer, Token> fieldSpecialTokenMap = new Hashtable<Integer, Token>();
private static String curDir = System.getProperty("user.dir");
private static String curFileName;
private static String curModuleName;
Expand Down Expand Up @@ -97,6 +103,24 @@ public class Rcc {
}
}
}

public static Token getFieldSpecialToken(int line) {
return fieldSpecialTokenMap.get(line);
}

public static void putFieldSpecialToken(Token fieldToken) {
if (fieldToken == null || fieldToken.specialToken == null) {
return;
}

Token tmp = fieldToken;
while ((tmp = tmp.specialToken) != null && tmp.image != null) {
if (tmp.beginLine == tmp.endLine) {
fieldSpecialTokenMap.putIfAbsent(tmp.beginLine, tmp);
}
}

}
}

PARSER_END(Rcc)
Expand Down Expand Up @@ -246,7 +270,10 @@ String ModuleName() :
}
{
t = <IDENT_TKN>
{ name += t.image; }
{
Rcc.putFieldSpecialToken(t);
name += t.image;
}
(
<DOT_TKN>
t = <IDENT_TKN>
Expand Down Expand Up @@ -274,9 +301,12 @@ JRecord Record() :
ArrayList<JField> flist = new ArrayList<JField>();
Token t;
JField f;
// Get the comments on the class token
Token recordTkn;
Token rbraceTkn;
}
{
<RECORD_TKN>
recordTkn = <RECORD_TKN>
t = <IDENT_TKN>
{ rname = t.image; }
<LBRACE_TKN>
Expand All @@ -285,11 +315,14 @@ JRecord Record() :
{ flist.add(f); }
<SEMICOLON_TKN>
)+
<RBRACE_TKN>

// Get the line ending comment of the last attribute
rbraceTkn = <RBRACE_TKN>
{
String fqn = curModuleName + "." + rname;
JRecord r = new JRecord(fqn, flist);
JRecord r = new JRecord(fqn, flist, recordTkn);
recTab.put(fqn, r);
Rcc.putFieldSpecialToken(rbraceTkn);
return r;
}
}
Expand All @@ -302,36 +335,34 @@ JField Field() :
{
jt = Type()
t = <IDENT_TKN>
{ return new JField(jt, t.image); }
{ return new JField(jt, t); }
}

JType Type() :
{
JType jt;
Token t;
Token t = null;
String rname;
}
{
jt = Map()
{ return jt; }
(jt = Map()
| jt = Vector()
{ return jt; }
| <BYTE_TKN>
{ return new JByte(); }
| <BOOLEAN_TKN>
{ return new JBoolean(); }
| <INT_TKN>
{ return new JInt(); }
| <LONG_TKN>
{ return new JLong(); }
| <FLOAT_TKN>
{ return new JFloat(); }
| <DOUBLE_TKN>
{ return new JDouble(); }
| <USTRING_TKN>
{ return new JString(); }
| <BUFFER_TKN>
{ return new JBuffer(); }
| t = <BYTE_TKN>
{ jt = new JByte(); }
| t = <BOOLEAN_TKN>
{ jt = new JBoolean(); }
| t = <INT_TKN>
{ jt = new JInt(); }
| t = <LONG_TKN>
{ jt = new JLong(); }
| t = <FLOAT_TKN>
{ jt = new JFloat(); }
| t = <DOUBLE_TKN>
{ jt = new JDouble(); }
| t = <USTRING_TKN>
{ jt = new JString(); }
| t = <BUFFER_TKN>
{ jt = new JBuffer(); }
| rname = ModuleName()
{
if (rname.indexOf('.', 0) < 0) {
Expand All @@ -343,32 +374,45 @@ JType Type() :
System.exit(1);
}
return r;
})

{
Rcc.putFieldSpecialToken(t);
return jt;
}
}

JMap Map() :
{
JType jt1;
JType jt2;
Token t;
}
{
<MAP_TKN>
t = <MAP_TKN>
<LT_TKN>
jt1 = Type()
<COMMA_TKN>
jt2 = Type()
<GT_TKN>
{ return new JMap(jt1, jt2); }
{
Rcc.putFieldSpecialToken(t);
return new JMap(jt1, jt2);
}
}

JVector Vector() :
{
JType jt;
Token t;
}
{
<VECTOR_TKN>
t = <VECTOR_TKN>
<LT_TKN>
jt = Type()
<GT_TKN>
{ return new JVector(jt); }
{
Rcc.putFieldSpecialToken(t);
return new JVector(jt);
}
}

0 comments on commit 6365b88

Please sign in to comment.