Skip to content

Commit

Permalink
Merge pull request #62 from fujaba/fix/fmethod-import
Browse files Browse the repository at this point in the history
Improve import(...) syntax
  • Loading branch information
Clashsoft authored Sep 2, 2020
2 parents d713fca + 31d365a commit 39eaa39
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 deletions.
6 changes: 4 additions & 2 deletions src/main/antlr/FulibClass.g4
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,22 @@ typeParamList: LANGLE (typeParam (COMMA typeParam)*)? RANGLE;
typeParam: annotation* IDENTIFIER (EXTENDS annotatedType (AMP annotatedType)*)?;
typeArg: annotation* (QMARK (EXTENDS annotatedType | SUPER annotatedType)? | type);

type: (primitiveType | referenceType) arraySuffix*;
type: (primitiveType | referenceType | importType) arraySuffix*;
arraySuffix: annotation* LBRACKET RBRACKET;
annotatedType: annotation* type;
annotatedTypeList: annotatedType (COMMA annotatedType)*;

primitiveType: VOID | BOOLEAN | BYTE | SHORT | CHAR | INT | LONG | FLOAT | DOUBLE;
referenceType: referenceTypePart (DOT annotation* referenceTypePart)*;
referenceTypePart: IDENTIFIER typeArgList?;
importTypeName: IMPORT LPAREN qualifiedName RPAREN;
importType: importTypeName typeArgList?;
typeArgList: LANGLE (typeArg (COMMA typeArg)*)? RANGLE;

// --------------- Misc. ---------------

modifier: PUBLIC | PROTECTED | PRIVATE | ABSTRACT | STATIC | FINAL | TRANSIENT | VOLATILE | SYNCHRONIZED | NATIVE | STRICTFP | DEFAULT;
annotation: AT qualifiedName balancedParens?;
annotation: AT (qualifiedName | importTypeName) balancedParens?;

expr: (balancedBraces
| balancedParens
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/org/fulib/parser/FragmentMapBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ private static void writeType(TypeContext typeCtx, StringBuilder builder)
{
writeType(typeCtx.primitiveType(), builder);
}
else if (typeCtx.importType() != null)
{
writeType(typeCtx.importType(), builder);
}
else
{
writeType(typeCtx.referenceType(), builder);
Expand Down Expand Up @@ -327,11 +331,22 @@ private static void writeType(ReferenceTypeContext referenceTypeCtx, StringBuild
}
}

private static void writeType(ImportTypeContext importTypeCtx, StringBuilder builder)
{
// import(org.example.Foo) ends up as only Foo in the code, so the signature should also use the simple name
final List<TerminalNode> identifiers = importTypeCtx.importTypeName().qualifiedName().IDENTIFIER();
builder.append(identifiers.get(identifiers.size() - 1).getText());
writeTypeArgs(importTypeCtx.typeArgList(), builder);
}

private static void writeType(ReferenceTypePartContext referenceTypePartCtx, StringBuilder builder)
{
builder.append(referenceTypePartCtx.IDENTIFIER().getText());
writeTypeArgs(referenceTypePartCtx.typeArgList(), builder);
}

final TypeArgListContext typeArgListCtx = referenceTypePartCtx.typeArgList();
private static void writeTypeArgs(TypeArgListContext typeArgListCtx, StringBuilder builder)
{
if (typeArgListCtx == null)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public abstract class AbstractGenerator4ClassFile
// =============== Constants ===============

private static final Pattern SIGNATURE_PATTERN = Pattern.compile("^\\s*(\\w+)\\s*:\\s*(.*)\\s*$");
private static final Pattern IMPORT_PATTERN = Pattern.compile("import\\(((?:\\w+\\.)*(\\w+))\\)");
private static final Pattern IMPORT_PATTERN = Pattern.compile("import\\(((?:static\\s+)?(?:\\w+\\.)*(\\w+))\\)");

// =============== Fields ===============

Expand Down
17 changes: 4 additions & 13 deletions src/main/java/org/fulib/util/Generator4ClassFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,19 +240,10 @@ private void generateMethod(FileFragmentMap fragmentMap, FMethod method)
body = body.substring(0, body.length() - 1);
}

final String signature = method.getSignature();
if (method.getModified())
{
fragmentMap.remove(signature);
}
else
{
final STGroup group = this.getSTGroup("org/fulib/templates/method.stg");
final ST method1 = group.getInstanceOf("method");
method1.add("method", method);
method1.add("body", body);
fragmentMap.add(signature, method1.render(), METHOD_NEWLINES);
}
final STGroup group = this.getSTGroup("org/fulib/templates/method.stg");
final String finalBody = body;
this.generateFromSignatures(fragmentMap, group, "methodSignatures", method.getModified(),
st -> st.add("method", method).add("body", finalBody));
}

// --------------- Additional Fragments ---------------
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/org/fulib/templates/method.stg
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
methodSignatures(method, body) ::= <<
method: <method.signature>
>>

method(method, body) ::= <<
<if(method.annotations)><method.annotations>
<endif>
Expand Down
8 changes: 2 additions & 6 deletions src/test/java/org/fulib/generator/FMethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,8 @@ public void testFMethods() throws Exception
returnCode = Tools.javac(outFolder, model.getPackageSrcFolder());
assertThat("compiler return code: ", returnCode, is(0));

party.withImports("import org.junit.jupiter.api.Test;");
party.withImports("import static org.hamcrest.CoreMatchers.*;");
party.withImports("import static org.hamcrest.MatcherAssert.assertThat;");

mm.haveMethod(party, "" + "@Test\n" + "public void testQuestion()",
"" + " assertThat(theAnswer(21), equalTo(42));\n");
mm.haveMethod(party, "@import(org.junit.jupiter.api.Test) public void testQuestion()",
"import(static org.hamcrest.MatcherAssert.assertThat)(theAnswer(21), import(static org.hamcrest.CoreMatchers.equalTo)(42));");

Fulib.generator().generate(model);

Expand Down

0 comments on commit 39eaa39

Please sign in to comment.