Skip to content

Commit

Permalink
Generate a Java token kind enum with a mapping to the integer kinds.
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Jan 7, 2018
1 parent a64a944 commit 73fcc27
Show file tree
Hide file tree
Showing 5 changed files with 419 additions and 70 deletions.
Expand Up @@ -32,9 +32,9 @@ public final void generate() throws Exception {
after();
}

protected Pair<CompilationUnit, ClassOrInterfaceDeclaration> parseNode(BaseNodeMetaModel nodeMetaModel) throws IOException {
protected Pair<CompilationUnit, ClassOrInterfaceDeclaration> parseNode(BaseNodeMetaModel nodeMetaModel) {
CompilationUnit nodeCu = sourceRoot.parse(nodeMetaModel.getPackageName(), nodeMetaModel.getTypeName() + ".java");
ClassOrInterfaceDeclaration nodeCoid = nodeCu.getClassByName(nodeMetaModel.getTypeName()).orElseThrow(() -> new IOException("Can't find class"));
ClassOrInterfaceDeclaration nodeCoid = nodeCu.getClassByName(nodeMetaModel.getTypeName()).orElseThrow(() -> new AssertionError("Can't find class"));
return new Pair<>(nodeCu, nodeCoid);
}

Expand Down
Expand Up @@ -2,6 +2,7 @@

import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.generator.core.node.*;
import com.github.javaparser.generator.core.other.TokenKindGenerator;
import com.github.javaparser.generator.core.visitor.*;
import com.github.javaparser.utils.Log;
import com.github.javaparser.utils.SourceRoot;
Expand All @@ -11,28 +12,37 @@

/**
* Generates all generated visitors in the javaparser-core module.
* Suggested usage is by running the run_core_generators.sh script.
* You may want to run_metamodel_generator.sh before that.
*/
public class CoreGenerator {
private static final ParserConfiguration parserConfiguration = new ParserConfiguration()
// .setStoreTokens(false)
// .setAttributeComments(false)
// .setLexicalPreservationEnabled(true)
;

public static void main(String[] args) throws Exception {
if (args.length != 1) {
throw new RuntimeException("Need 1 parameter: the JavaParser source checkout root directory.");
}
Log.setAdapter(new Log.StandardOutStandardErrorAdapter());
final Path root = Paths.get(args[0], "..", "javaparser-core", "src", "main", "java");
final SourceRoot sourceRoot = new SourceRoot(root)
final SourceRoot sourceRoot = new SourceRoot(root, parserConfiguration)
// .setPrinter(LexicalPreservingPrinter::print)
.setParserConfiguration(new ParserConfiguration()
// .setStoreTokens(false)
// .setAttributeComments(false)
// .setLexicalPreservationEnabled(true)
);
;

final Path generatedJavaCcRoot = Paths.get(args[0], "..", "javaparser-core", "target", "generated-sources", "javacc");
final SourceRoot generatedJavaCcSourceRoot = new SourceRoot(generatedJavaCcRoot, parserConfiguration)
// .setPrinter(LexicalPreservingPrinter::print)
;

new CoreGenerator().run(sourceRoot);
new CoreGenerator().run(sourceRoot, generatedJavaCcSourceRoot);

sourceRoot.saveAll();
}

private void run(SourceRoot sourceRoot) throws Exception {
private void run(SourceRoot sourceRoot, SourceRoot generatedJavaCcSourceRoot) throws Exception {
new TypeCastingGenerator(sourceRoot).generate();
new GenericListVisitorAdapterGenerator(sourceRoot).generate();
new GenericVisitorAdapterGenerator(sourceRoot).generate();
Expand All @@ -58,5 +68,6 @@ private void run(SourceRoot sourceRoot) throws Exception {
new MainConstructorGenerator(sourceRoot).generate();
new FinalGenerator(sourceRoot).generate();
new AcceptGenerator(sourceRoot).generate();
new TokenKindGenerator(sourceRoot, generatedJavaCcSourceRoot).generate();
}
}
@@ -0,0 +1,71 @@
package com.github.javaparser.generator.core.other;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.BodyDeclaration;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.EnumConstantDeclaration;
import com.github.javaparser.ast.body.EnumDeclaration;
import com.github.javaparser.ast.expr.IntegerLiteralExpr;
import com.github.javaparser.ast.stmt.ReturnStmt;
import com.github.javaparser.ast.stmt.SwitchEntryStmt;
import com.github.javaparser.ast.stmt.SwitchStmt;
import com.github.javaparser.generator.Generator;
import com.github.javaparser.utils.Log;
import com.github.javaparser.utils.SourceRoot;

/**
* Generates the TokenKind enum from {@link com.github.javaparser.GeneratedJavaParserConstants}
*/
public class TokenKindGenerator extends Generator {
private final SourceRoot generatedJavaCcSourceRoot;

public TokenKindGenerator(SourceRoot sourceRoot, SourceRoot generatedJavaCcSourceRoot) {
super(sourceRoot);
this.generatedJavaCcSourceRoot = generatedJavaCcSourceRoot;
}

@Override
public void generate() {
Log.info("Running %s", getClass().getSimpleName());

final CompilationUnit javaTokenCu = sourceRoot.parse("com.github.javaparser", "JavaToken.java");
final ClassOrInterfaceDeclaration javaToken = javaTokenCu.getClassByName("JavaToken").orElseThrow(() -> new AssertionError("Can't find class in java file."));
final EnumDeclaration kindEnum = javaToken.findFirst(EnumDeclaration.class, e -> e.getNameAsString().equals("Kind")).orElseThrow(() -> new AssertionError("Can't find class in java file."));

kindEnum.getEntries().clear();
annotateGenerated(kindEnum);

final SwitchStmt valueOfSwitch = kindEnum.findFirst(SwitchStmt.class).orElseThrow(() -> new AssertionError("Can't find valueOf switch."));
valueOfSwitch.findAll(SwitchEntryStmt.class).stream().filter(e -> e.getLabel().isPresent()).forEach(Node::remove);

final CompilationUnit constantsCu = generatedJavaCcSourceRoot.parse("com.github.javaparser", "GeneratedJavaParserConstants.java");
final ClassOrInterfaceDeclaration constants = constantsCu.getInterfaceByName("GeneratedJavaParserConstants").orElseThrow(() -> new AssertionError("Can't find class in java file."));
for (BodyDeclaration<?> member : constants.getMembers()) {
member.toFieldDeclaration()
.filter(field -> {
String javadoc = field.getJavadocComment().get().getContent();
return javadoc.contains("RegularExpression Id") || javadoc.contains("End of File");
})
.map(field -> field.getVariable(0))
.ifPresent(var -> {
final String name = var.getNameAsString();
final IntegerLiteralExpr kind = var.getInitializer().get().asIntegerLiteralExpr();
generateEnumEntry(kindEnum, name, kind);
generateValueOfEntry(valueOfSwitch, name, kind);
});
}
}

private void generateValueOfEntry(SwitchStmt valueOfSwitch, String name, IntegerLiteralExpr kind) {
final SwitchEntryStmt entry = new SwitchEntryStmt(kind, new NodeList<>(new ReturnStmt(name)));
valueOfSwitch.getEntries().addFirst(entry);
}

private void generateEnumEntry(EnumDeclaration kindEnum, String name, IntegerLiteralExpr kind) {
final EnumConstantDeclaration enumEntry = new EnumConstantDeclaration(name);
enumEntry.getArguments().add(kind);
kindEnum.addEntry(enumEntry);
}
}

0 comments on commit 73fcc27

Please sign in to comment.