Skip to content
Snippets Groups Projects
Commit 8cb83dcd authored by René Schöne's avatar René Schöne
Browse files

Add option to exclude generated elements.

parent 257d1286
No related branches found
No related tags found
No related merge requests found
Pipeline #8742 passed
...@@ -78,11 +78,13 @@ public class JsonBuilder { ...@@ -78,11 +78,13 @@ public class JsonBuilder {
private static final String[] MEMBER_KINDS = { "constr", "attr", "rel", "field", "method" }; private static final String[] MEMBER_KINDS = { "constr", "attr", "rel", "field", "method" };
private final File rootDir; private final File rootDir;
private final boolean excludeGenerated;
private java.util.List<String> ragRoot = new java.util.LinkedList<>(); private java.util.List<String> ragRoot = new java.util.LinkedList<>();
public Map<String, File> sourceFiles = new HashMap<>(); public Map<String, File> sourceFiles = new HashMap<>();
public JsonBuilder(File rootDir) { public JsonBuilder(File rootDir, boolean excludeGenerated) {
this.rootDir = rootDir; this.rootDir = rootDir;
this.excludeGenerated = excludeGenerated;
ragRoot = RelativePath.buildPathList(rootDir); ragRoot = RelativePath.buildPathList(rootDir);
typenames.add("packages"); // Reserve packages as JSON filename. typenames.add("packages"); // Reserve packages as JSON filename.
} }
...@@ -111,6 +113,9 @@ public class JsonBuilder { ...@@ -111,6 +113,9 @@ public class JsonBuilder {
JsonArray array = new JsonArray(); JsonArray array = new JsonArray();
do { do {
ParameterDeclaration param = iter.next(); ParameterDeclaration param = iter.next();
if (matchesGenerated(param.name())) {
continue;
}
JsonObject pobj = new JsonObject(); JsonObject pobj = new JsonObject();
pobj.add("t", typeRef(param.type())); pobj.add("t", typeRef(param.type()));
pobj.add("n", param.name()); pobj.add("n", param.name());
...@@ -135,7 +140,7 @@ public class JsonBuilder { ...@@ -135,7 +140,7 @@ public class JsonBuilder {
JsonObject doc = method.jsonDocObject(); JsonObject doc = method.jsonDocObject();
// System.out.println("" + method.type() + ": " + method + ", kind=" + method.objectKind()); // System.out.println("" + method.type() + ": " + method + ", kind=" + method.objectKind());
// System.out.println("`-> doc=" + doc + " doc.get(relation)=" + (doc != null ? doc.get("relation") : "/")); // System.out.println("`-> doc=" + doc + " doc.get(relation)=" + (doc != null ? doc.get("relation") : "/"));
if (shouldDocument(method, doc)) { if (shouldDocument(method, doc) && !matchesGenerated(method.name())) {
JsonObject obj = new JsonObject(); JsonObject obj = new JsonObject();
obj.add("name", Json.of(method.name())); obj.add("name", Json.of(method.name()));
JsonArray modifiers = method.getModifiers().toJson(); JsonArray modifiers = method.getModifiers().toJson();
...@@ -220,6 +225,13 @@ public class JsonBuilder { ...@@ -220,6 +225,13 @@ public class JsonBuilder {
&& !typeDecl.isTypeVariable() && typeDecl.compilationUnit().fromSource(); && !typeDecl.isTypeVariable() && typeDecl.compilationUnit().fromSource();
} }
/**
* Check if name contains "_internal_" or "_impl_" and if it should be excluded (based on constructor argument)
*/
private boolean matchesGenerated(String name) {
return this.excludeGenerated && (name.contains("_internal_") || name.contains("_impl_") || name.contains("$"));
}
// TODO: inner class names. // TODO: inner class names.
/** /**
...@@ -255,6 +267,9 @@ public class JsonBuilder { ...@@ -255,6 +267,9 @@ public class JsonBuilder {
JsonObject doc = field.jsonDocObject(); JsonObject doc = field.jsonDocObject();
if (shouldDocument(field, doc)) { if (shouldDocument(field, doc)) {
for (Declarator declarator : field.getDeclaratorList()) { for (Declarator declarator : field.getDeclaratorList()) {
if (!matchesGenerated(declarator.name())) {
continue;
}
JsonObject obj = new JsonObject(); JsonObject obj = new JsonObject();
obj.add("name", Json.of(declarator.name())); obj.add("name", Json.of(declarator.name()));
obj.add("type", typeRef(declarator.type())); obj.add("type", typeRef(declarator.type()));
...@@ -404,13 +419,29 @@ public class JsonBuilder { ...@@ -404,13 +419,29 @@ public class JsonBuilder {
} }
doc.set("astdecl", decl); doc.set("astdecl", decl);
JsonArray array = decl.get("c").array(); JsonArray array = decl.get("c").array();
// objects to keep are non-generated parameters
List<JsonObject> objectsToKeep = new ArrayList<>();
boolean removeSomeObjects = false;
for (JsonValue c : array) { for (JsonValue c : array) {
JsonObject comp = c.object(); JsonObject comp = c.object();
if (matchesGenerated(comp.get("n").stringValue(""))) {
removeSomeObjects = true;
} else {
objectsToKeep.add(comp);
}
if (!comp.get("e").stringValue("").isEmpty()) { if (!comp.get("e").stringValue("").isEmpty()) {
comp.set("e", comp.set("e",
typeRef(safeLookupType(type, stripGenericPart(comp.get("e").stringValue(""))))); typeRef(safeLookupType(type, stripGenericPart(comp.get("e").stringValue("")))));
} }
} }
// recreate the array with only those object to keep
if (removeSomeObjects) {
array = new JsonArray();
for (JsonObject comp : objectsToKeep) {
array.add(comp);
}
decl.set("c", array);
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} catch (JsonParser.SyntaxError syntaxError) { } catch (JsonParser.SyntaxError syntaxError) {
...@@ -439,6 +470,10 @@ public class JsonBuilder { ...@@ -439,6 +470,10 @@ public class JsonBuilder {
return obj; return obj;
} }
private void excludeGeneratedFromProduction(JsonObject decl) {
}
private TypeDecl safeLookupType(TypeDecl parent, String name) { private TypeDecl safeLookupType(TypeDecl parent, String name) {
SimpleSet<TypeDecl> types = parent.lookupType(name); SimpleSet<TypeDecl> types = parent.lookupType(name);
if (types.isEmpty()) { if (types.isEmpty()) {
......
...@@ -187,6 +187,7 @@ public class RagDocBuilder extends Frontend { ...@@ -187,6 +187,7 @@ public class RagDocBuilder extends Frontend {
@Override protected void initOptions() { @Override protected void initOptions() {
super.initOptions(); super.initOptions();
program.options().addKeyValueOption("-ragroot"); program.options().addKeyValueOption("-ragroot");
program.options().addKeyOption("-excludeGenerated");
} }
@Override protected int processArgs(String[] args) { @Override protected int processArgs(String[] args) {
...@@ -206,7 +207,8 @@ public class RagDocBuilder extends Frontend { ...@@ -206,7 +207,8 @@ public class RagDocBuilder extends Frontend {
} }
} }
System.out.println("Using ragroot directory: " + ragRoot.getAbsolutePath()); System.out.println("Using ragroot directory: " + ragRoot.getAbsolutePath());
jsonBuilder = new JsonBuilder(ragRoot); boolean excludeGenerated = program.options().hasOption("-excludeGenerated");
jsonBuilder = new JsonBuilder(ragRoot, excludeGenerated);
ASTNode.jsonBuilder = jsonBuilder; ASTNode.jsonBuilder = jsonBuilder;
} }
return result; return result;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment