metalanguage and language set

java2ngr

Existing java code is often the best starting point for creating new java-generating ngrease languages. The first step in extracting languages is to convert the java source into an ngrease element, from which it is easier to extract new languages than from raw strings and files.

In this tutorial we will write a java source directory and transform it to an ngrease element. We will first do it "manually" as a normal transformation, but since this is a frequently needed feature, there is a command-line tool for it, and we will redo the transformation with it in the end.

Create a java package

~/work $ mkdir java2ngr && cd java2ngr
~/work/java2ngr $ mkdir -p src/hello
~/work/java2ngr $ $EDITOR src/hello/Hello.java
package hello;

public class Hello {

public static void main(String[] args) {
System.out.println("Hello World!");
}

}

Convert java to ngr

~/work/java2ngr $ $EDITOR hello-java2ngr.ngr
# ngrease 1

$ (
context-chain {
$:default-context
$:evaluate:$:reference:resource:/net/sf/ngrease/languages/dir/tojavasrcmodule/context.ngr
$:evaluate:$:reference:resource:/net/sf/ngrease/java2ngr/context.ngr
}
):transform {to:java-source-module from:
$:include-as-file:resource:/src
}

We refer to the directory as a resource so we'll have to modify the search path before evaluating.

~/work/java2ngr $ export NGREASEPATH=.
~/work/java2ngr $ ngrease -f hello-java2ngr.ngr
Don't know how to output result: java-source-module { name:src package { name:hello class { public name:Hello method { public static returns:void name:main parameter { type:String[] name:args } body:method-call { field-ref { variable-ref:System out } println string-constant:'Hello World!' } } } } }

Pretty-print the ngr as source for output

We don't have use for the ngr element so we build a string out of it.

~/work/java2ngr $ $EDITOR hello-java2ngr.ngr
# ngrease 1

$ (
string:$ (
context-chain {
$:default-context
$:evaluate:$:reference:resource:/net/sf/ngrease/languages/dir/tojavasrcmodule/context.ngr
$:evaluate:$:reference:resource:/net/sf/ngrease/java2ngr/context.ngr
}
):transform {to:java-source-module from:
):pretty-print:$:transform {to:java-source-module from:
$:include-as-file:resource:/src
}
~/work/java2ngr $ ngrease -f hello-java2ngr.ngr
java-source-module { name:src package { name:hello class { public name:Hello method { public static returns:void name:main parameter { type:String[] name:args } body:method-call { field-ref { variable-ref:System out } println string-constant:'Hello World!' } } } } }

Verify transformation back to java source

Now we can save the ngr representation and use it for generating java.

~/work/java2ngr $ ngrease -f hello-java2ngr.ngr > hello-java.ngr
~/work/java2ngr $ $EDITOR hello-java-as-java-srcdir-shar.ngr
# ngrease 1

$ (
context-chain {
$:default-context
$:evaluate:$:reference:resource:/net/sf/ngrease/languages/java/context.ngr
$:evaluate:$:reference:resource:/net/sf/ngrease/languages/shar/context.ngr
}
):with {
parent-dir:"."
$:transform {to {java-source-directory directory sh-source} from:
$:reference:resource:/hello-java.ngr
}
}
~/work/java2ngr $ ngrease -f hello-java-as-java-srcdir-shar.ngr
mkdir ./src mkdir ./src/hello cat > ./src/hello/Hello.java <<\TODO_UNIQUE_EOF_STRING package hello; public class Hello { public static void main(String[] args) { System.out.println("Hello World!"); } } TODO_UNIQUE_EOF_STRING
(Output asserted)

Using the java2ngr command-line tool

Now we'll redo the transformation using the java2ngr command-line tool.

~/work/java2ngr $ java2ngr --srcdir src | head -n 2
java-source-module { name:src
(Output asserted)
~/work/java2ngr $ cd ..