[antlr-interest] A Kinder, Gentler Java

Owen Densmore owen at backspaces.net
Sun Oct 29 13:38:21 PST 2006


I'd like to build a preprocessor to create a much cleaner, less  
verbose version of Java. I'm not sure if ANTLR can do this for me ..  
sorta a preprocessor from a "java--" to java. If it can, PLEASE send  
along a few pointers to me, a newbie!

Here's the story:

I've been following the JVM "agile" languages: Groovy, JRuby, Jython.  
Although I've very much enjoyed using them, I've also been impressed  
with the improvements in Java itself, especially with Java 1.5.

Although I like the agile languages, it's my impression that just  
simplifying Java itself would be good enough for me. These three  
sites capture the sort of thing I'd like to achieve:
10 Reasons We Need Java 3.0
   http://www.onjava.com/lpt/a/2524
Shorter Syntax for Common Operations
   http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6389769\
   Here's a better formatted version:
   http://tinyurl.com/hc2ea
James Gosling: on the Java Road -- Scripting flamewar
(This also refers to the Shorter Syntax article above)
   http://blogs.sun.com/jag/entry/scripting_flamewar
They all address verbosity and warts in Java fairly nicely.

I'm convinced that a preprocessor could handle these, translating  
simplifications of Java into standard Java. I mean, how hard could it  
be to introduce raw strings and multi-line strings, right? Maybe  
something like python/groovy strings would do the trick. So:
   r"\n\r" -> "\\n\\r" in Java and
"""
This is a multi line string
with newlines.
"""
   ->
"This is a multi line string\n"+"with newlines.\n"

Reducing the verbosity of declarations should fairly reasonable:
Java:
   Foo<T> foo = new Foo<T>(a,b,c);
Simplified to:
   Foo<T> foo = new(a,b,c);
or
   foo := Foo<T>(a,b,c);

The biggest simplification I'm thinking about would be for one-method  
interfaces generally used an anonymous inner classes. [Google for  
Functor to see great examples. Closures also capture the idea.]
So in Java, you define a method in a class:
   ApplyExec(List<T>, Exec<T>);
.. that uses a simple interface:
   interface Exec<T> {
     void exec(T t);
   }
It would be called like this in Java:
   ApplyExec(myTList, new Exec<T>(){
       void exec(T t) {
         t.foo = t.bar + 7;
       }
   })
I'd prefer a very simple:
   ApplyExec(myTList,{t|t.foo = t.bar + 7})
Note that this captures the feel of a dynamic call, but it really is  
not, its a simple anonymous inner class shorthand. This would need  
either a name convention or a bit more information or both to work  
without building a fairly sophisticated parser. But still, you get  
the idea.

So is this something we could do easily in ANTLR??  If so, how!?

Thanks



More information about the antlr-interest mailing list