Skip to content

Commit 5a585ff

Browse files
committed
first pass at fjava
1 parent d86ca6f commit 5a585ff

File tree

23 files changed

+3349
-23
lines changed

23 files changed

+3349
-23
lines changed

drjava/build.xml

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,28 @@
151151
<property name="version-source" value="${src-working-dir}/Version.orig" />
152152
<property name="version-target" value="${src-working-dir}/Version.java" />
153153

154+
<property name="lib" location="lib"/>
155+
156+
157+
<path id="classpath">
158+
<fileset dir="${lib}">
159+
<include name="*.jar"/>
160+
</fileset>
161+
</path>
162+
163+
<!-- === Generate Lexer/Parser === -->
164+
<target name="generate-lexer" description="Generate ANTLR lexer and parser">
165+
<java classname="org.antlr.v4.Tool" fork="true" classpathref="classpath">
166+
<arg value="-package" />
167+
<arg value="edu.rice.cs.drjava.model.compiler.fjpreprocessor.grammar" />
168+
<arg line="-no-listener -no-visitor"/>
169+
<arg value="${src-working-dir}/model/compiler/fjpreprocessor/grammar/JavaLexer.g4"/>
170+
<!-- Optional: JavaParser.g4 -->
171+
<!-- <arg value="${src}/grammar/JavaParser.g4"/> -->
172+
</java>
173+
</target>
174+
175+
154176
<target name="generate-source" depends="resolve-development-value, resolve-version-tag"
155177
description="Generate the CodeStatus and Version source files">
156178
<filter token="DEVELOPMENT" value="${development-value}" />
@@ -179,7 +201,7 @@
179201
******************* -->
180202

181203
<!-- The following target assumes that javac resolves to a Java 8 compiler -->
182-
<target name="compile" depends="generate-source, do-compile, copy-resources, unjar-libs"
204+
<target name="compile" depends="generate-lexer, generate-source, do-compile, copy-resources, unjar-libs"
183205
description="Compile all source files (after generating the source)">
184206
</target>
185207

@@ -1002,4 +1024,3 @@
10021024
<available file="${generate-dir}" property="already-generated" />
10031025
</target>
10041026
</project>
1005-
1.98 MB
Binary file not shown.

drjava/src/edu/rice/cs/drjava/config/OptionConstants.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ public interface OptionConstants {
9696

9797
/** The old extension for an advanced language level source file */
9898
public static final String OLD_DJ2_FILE_EXTENSION = ".dj2";
99+
100+
/** The extension for a functional java source file */
101+
public static final String FJAVA_FILE_EXTENSION = ".fjava";
99102

100103
/* Constants for language levels */
101104
public static final int FULL_JAVA = 0;
@@ -110,7 +113,8 @@ public interface OptionConstants {
110113
OLD_DJ0_FILE_EXTENSION, // = .dj0
111114
OLD_DJ1_FILE_EXTENSION, // = .dj1
112115
OLD_DJ2_FILE_EXTENSION, // = .dj2
113-
DJ_FILE_EXTENSION }; // = .dj
116+
DJ_FILE_EXTENSION, // = .dj
117+
FJAVA_FILE_EXTENSION }; // = .fjava
114118

115119
// /** The configuration XML file that DrJava looks for inside a .djapp file */
116120
// public static final String EXTPROCESS_FILE_NAME_INSIDE_JAR = "process" + EXTPROCESS_FILE_EXTENSION;

drjava/src/edu/rice/cs/drjava/model/DrJavaFileUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public static Set<String> getSourceFileExtensions() {
4848
extensions.add(OptionConstants.OLD_DJ0_FILE_EXTENSION);
4949
extensions.add(OptionConstants.OLD_DJ1_FILE_EXTENSION);
5050
extensions.add(OptionConstants.OLD_DJ2_FILE_EXTENSION);
51+
extensions.add(OptionConstants.FJAVA_FILE_EXTENSION);
5152
return extensions;
5253
}
5354

@@ -70,6 +71,7 @@ public static String getSuggestedFileExtension() {
7071
public static boolean isSourceFile(String fileName) {
7172
return fileName != null &&
7273
( fileName.endsWith(OptionConstants.JAVA_FILE_EXTENSION)
74+
|| fileName.endsWith(OptionConstants.FJAVA_FILE_EXTENSION)
7375
|| fileName.endsWith(OptionConstants.DJ_FILE_EXTENSION)
7476
|| fileName.endsWith(OptionConstants.OLD_DJ0_FILE_EXTENSION)
7577
|| fileName.endsWith(OptionConstants.OLD_DJ1_FILE_EXTENSION)
@@ -95,6 +97,7 @@ public static boolean isSourceFile(File f) {
9597
*/
9698
public static boolean isLLFile(String fileName) {
9799
return fileName.endsWith(OptionConstants.DJ_FILE_EXTENSION)
100+
|| fileName.endsWith(OptionConstants.FJAVA_FILE_EXTENSION)
98101
|| fileName.endsWith(OptionConstants.OLD_DJ0_FILE_EXTENSION)
99102
|| fileName.endsWith(OptionConstants.OLD_DJ1_FILE_EXTENSION)
100103
|| fileName.endsWith(OptionConstants.OLD_DJ2_FILE_EXTENSION);
@@ -183,6 +186,10 @@ public static String getJavaForLLFile(String fileName) {
183186
return fileName.substring(0, fileName.lastIndexOf(OptionConstants.DJ_FILE_EXTENSION))
184187
+ OptionConstants.JAVA_FILE_EXTENSION;
185188
}
189+
else if (fileName.endsWith(OptionConstants.FJAVA_FILE_EXTENSION)) {
190+
return fileName.substring(0, fileName.lastIndexOf(OptionConstants.FJAVA_FILE_EXTENSION))
191+
+ OptionConstants.JAVA_FILE_EXTENSION;
192+
}
186193
else if (fileName.endsWith(OptionConstants.OLD_DJ0_FILE_EXTENSION)) {
187194
return fileName.substring(0, fileName.lastIndexOf(OptionConstants.OLD_DJ0_FILE_EXTENSION))
188195
+ OptionConstants.JAVA_FILE_EXTENSION;
@@ -195,6 +202,7 @@ else if (fileName.endsWith(OptionConstants.OLD_DJ2_FILE_EXTENSION)) {
195202
return fileName.substring(0, fileName.lastIndexOf(OptionConstants.OLD_DJ2_FILE_EXTENSION))
196203
+ OptionConstants.JAVA_FILE_EXTENSION;
197204
}
205+
198206
else return fileName;
199207
}
200208

drjava/src/edu/rice/cs/drjava/model/compiler/DefaultCompilerModel.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import edu.rice.cs.drjava.model.OpenDefinitionsDocument;
4545
import edu.rice.cs.drjava.model.DrJavaFileUtils;
4646
import edu.rice.cs.drjava.model.definitions.InvalidPackageException;
47+
import edu.rice.cs.drjava.model.compiler.fjpreprocessor.Preprocessor;
4748

4849
import edu.rice.cs.util.FileOps;
4950
import edu.rice.cs.util.Log;
@@ -57,6 +58,8 @@
5758
import edu.rice.cs.plt.io.IOUtil;
5859
import edu.rice.cs.plt.iter.IterUtil;
5960
import edu.rice.cs.plt.collect.CollectUtil;
61+
62+
6063
// import edu.rice.cs.plt.tuple.Pair;
6164
// TODO: use the preceding pair class instead of javalanglevels.Pair; must change javalanglevels code as well
6265

@@ -549,29 +552,18 @@ public boolean accept(File dir, String name) {
549552
}
550553
}
551554

552-
/* Perform language levels conversion, creating corresponding .java files. */
553-
LanguageLevelConverter llc = new LanguageLevelConverter();
554-
Options llOpts; /* Options passed as arguments to LLConverter */
555-
if (bootClassPath == null) { llOpts = new Options(getActiveCompiler().version(), classPath); }
556-
else { llOpts = new Options(getActiveCompiler().version(), classPath, bootClassPath); }
557555

558-
// NOTE: the workaround "_testFileSort(files)" instead of simply "files") may no longer be necessary.
559556

560-
/* Perform the LL conversion incorporating the following workaround: Forward references can generate spurious
561-
* conversion errors in some cases. This problem can be mitigated by compiling JUnit test files (with names
562-
* containing the substring "Test") last.
563-
*/
564-
Map<File,Set<String>> sourceToTopLevelClassMap = new HashMap<File,Set<String>>();
565-
Pair<LinkedList<JExprParseException>, LinkedList<Pair<String, JExpressionIF>>> llErrors =
566-
llc.convert(_testFileSort(files).toArray(new File[0]), llOpts, sourceToTopLevelClassMap);
567-
568-
/* Add any errors encountered in conversion to the compilation error log. */
569-
errors.addAll(_parseExceptions2CompilerErrors(llErrors.getFirst()));
570-
errors.addAll(_visitorErrors2CompilerErrors(llErrors.getSecond()));
557+
/* Perform language levels conversion, creating corresponding .java files. */
558+
try {
559+
Preprocessor.preprocessList(files);
560+
}
561+
catch (Throwable t) {
562+
errors.add(new DJError("Language Level Preprocessor failed: " + t.getMessage(), false));
563+
}
571564

572-
// Since we (optionally) delete all class files in LL directories, we don't need the code
573-
// to smart-delete class files anymore.
574-
// smartDeleteClassFiles(sourceToTopLevelClassMap);
565+
566+
575567
}
576568

577569
if (containsLanguageLevels) { return new LinkedList<File>(javaFileSet); }
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package edu.rice.cs.drjava.model.compiler.fjpreprocessor;
2+
3+
import java.util.ArrayList;
4+
5+
import org.antlr.v4.runtime.*;
6+
7+
import edu.rice.cs.drjava.model.compiler.fjpreprocessor.grammar.JavaLexer;
8+
9+
public class BufferedLexer {
10+
11+
JavaLexer lexer;
12+
ArrayList<Token> buffer;
13+
14+
public BufferedLexer(CharStream input) {
15+
this.lexer = new JavaLexer(input);
16+
this.buffer = new ArrayList<>();
17+
}
18+
19+
public Token nextTokenUnbuffered() {
20+
Token token = lexer.nextToken();
21+
return token;
22+
}
23+
24+
public Token nextTokenUnbufferedSkipWS() {
25+
Token token = lexer.nextToken();
26+
while (token.getType() == JavaLexer.WS) {
27+
token = lexer.nextToken();
28+
}
29+
return token;
30+
}
31+
32+
public Token nextToken() {
33+
34+
if (buffer.isEmpty()) {
35+
Token token = lexer.nextToken();
36+
return token;
37+
} else {
38+
Token token = buffer.remove(0);
39+
40+
return token;
41+
}
42+
}
43+
44+
public Token nextTokenSkipWS() {
45+
Token token = nextToken();
46+
while (token.getType() == JavaLexer.WS) {
47+
token = nextToken();
48+
}
49+
return token;
50+
}
51+
52+
public Token[] peekN(int n, boolean skipWS) {
53+
while (buffer.size() < n) {
54+
Token token = skipWS ? nextTokenUnbufferedSkipWS()
55+
: nextTokenUnbuffered();
56+
if (token.getType() == Token.EOF) {
57+
break;
58+
}
59+
buffer.add(token);
60+
}
61+
int newn = Math.min(n, buffer.size());
62+
Token[] tokens = new Token[newn];
63+
for (int i = 0; i < newn; i++) {
64+
tokens[i] = buffer.get(i);
65+
}
66+
return tokens;
67+
}
68+
69+
}

0 commit comments

Comments
 (0)