use Tatoo's compiler as front end of javac

Rémi Forax forax at univ-mlv.fr
Mon Dec 8 15:22:27 PST 2008


Jonathan Gibbons a écrit :
> Rémi Forax wrote:
>> Hi all,
>> I've written a blog entry that show how to use Tatoo
>> to generate a parser usable as front end of javac.
>> http://weblogs.java.net/blog/forax/archive/2008/12/using_tatoo_as.html
>>
>> cheers,
>> Rémi
>> I've patched the compiler grammar's javac (which is already a patch 
>> of javac) a litte bit to be able to specify a parser factory on the 
>> command line. 
>
> How big is the patch to compiler grammar's javac?  
Not that big. It removes dependency to ANTLR and use reflection
to get the ParserFactory.
I have reuse the same name for the option but perhaps "parserFactory" is 
a better
name than "parser".
> Would it be worth contributing the patch to the main OpenJDK javac -- 
> i.e. the work to specify a parser factory on the command line.
see the patch below :)

By the way, ParserFactory should be abstract and should have an 
implementation
containing Javac specifics.
Currently, the factory instantiates a Keyword and a Scanner.Factory that 
are specific to
the current hand written scanner.
>
> -- Jon
>
Rémi

diff --git 
a/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java 
b/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java
--- a/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java
+++ b/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java
@@ -26,6 +26,7 @@ package com.sun.tools.javac.main;
 package com.sun.tools.javac.main;
 
 import java.io.*;
+import java.lang.reflect.InvocationTargetException;
 import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.LinkedHashMap;
@@ -45,7 +46,6 @@ import com.sun.source.util.TaskEvent;
 import com.sun.source.util.TaskEvent;
 import com.sun.source.util.TaskListener;
 
-import com.sun.tools.javac.antlr.AntlrParserFactory;
 import com.sun.tools.javac.util.*;
 import com.sun.tools.javac.code.*;
 import com.sun.tools.javac.tree.*;
@@ -387,11 +387,28 @@ public class JavaCompiler implements Cla
             : null;
 
         String parser = options.get("parser");
-        //useAntlrParser = (parser != null && 
parser.equalsIgnoreCase("antlr"));
-        useAntlrParser = (parser == null || 
parser.equalsIgnoreCase("antlr"));
-
-        if (useAntlrParser)
-            AntlrParserFactory.preRegister(context);
+        if (parser != null) {
+            try {
+                Class<?> parserClass = Class.forName(parser);
+                parserClass.getMethod("preRegister", Context.class).
+                    invoke(null, context);
+            } catch(ClassNotFoundException e) {
+                throw new AssertionError(e);
+            } catch(IllegalAccessException e) {
+                throw new AssertionError(e);
+            } catch(NoSuchMethodException e) {
+                throw new AssertionError(e);
+            } catch(InvocationTargetException e) {
+                Throwable cause=e.getCause();
+                if (cause instanceof RuntimeException) {
+                    throw (RuntimeException)cause;
+                }
+                if (cause instanceof Error) {
+                    throw (Error)cause;
+                }
+                throw new AssertionError(cause);
+            }
+        }
 
         parserFactory = ParserFactory.instance(context);
     }




More information about the compiler-grammar-dev mailing list