use Tatoo's compiler as front end of javac

Jonathan Gibbons Jonathan.Gibbons at Sun.COM
Tue Dec 16 15:45:49 PST 2008


Rémi,

Thanks for the suggested patch. I'm thinking it might be worth
investigating this one step further, and see if we can leverage the
JDK ServiceLoader mechanism to come up with a more general
mechanism that would apply to any object that might be found via
the javac Context.

I can see 3 possibilities:

Early: as soon as the context is created, populate it with any
objects found via the service loader.

Later: after the command line options have been read, populate
the context with any objects found via the service loader. This
will allow the ability to select objects via hidden command line
options.

Latest: on an instance by instance basis, check the service loader
before creating each default object for the context. This is the
most expensive option, since it requires checking the service
loader on an object by object basis; the previous two options
would just require one access to the service loader, for any/all
objects to put in the context.

We could also go for a radically different dependency injection
framework, but I'd prefer to go for a small/lightweight change,
and not any major refactoring at this time.

-- Jon




Rémi Forax wrote:
> 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