Thursday, December 31, 2015

Configuring JJTree in Android Studio

First, follow the steps described in Configuring JavaCC in Android Studio.
Next, modify the module's build.gradle file as shown below:

android {
    // ...
    sourceSets {
        main {
            java.srcDirs = ['src/main/java',
                            file(project.buildDir.absolutePath + '/generated/jjtree'),
                            file(project.buildDir.absolutePath + '/generated/javacc')]
        }
    }
    applicationVariants.all { variant ->
        variant.javaCompile.dependsOn.add(compileJavacc)
    }
    // ...
}

compileJavacc {
    inputDirectory = file(project.buildDir.absolutePath + '/generated/jjtree')
    dependsOn.add(compileJjtree)
}

Move you *.jj file from src/main/javacc, which is JavaCC default directory, to src/main/jjtree, and change its extension to *.jjt.  When you build your project now, everything should continue to work, including all JavaCC unit tests.  In addition, you'll find new JJTree files under build/generated/jjtree.

You can also setup a task that cleans generated JavaCC and JJTree files, and make compileJavacc task depend on it:

task cleanJjdirs(type: Delete) {
    delete file(project.buildDir.absolutePath + '/generated/jjtree'),
            file(project.buildDir.absolutePath + '/generated/javacc')
}

Tuesday, December 22, 2015

Fixing Semantic LOOKAHEAD sample

Semantic LOOKAHEAD sample incorrectly states that the BC() choice must be made only when the next token is a "c", and the token following that is not a "c".  It actually should say that the BC() choice must be made only when the next token and the following tokens are “c”. To make the sample work the production should look like this:

void BC() :
{}
{
  "b"
  [ LOOKAHEAD( { getToken(1).kind == C && getToken(2).kind == C } )
    <C:"c">
  ]
}

The semantic lookahead is not really required here; the production can be simplified by using syntactic lookahead:

void BC() :
{}
{
  "b" [ LOOKAHEAD( "c" "c" ) "c" ]
}

Sunday, December 20, 2015

Configuring JavaCC in Android Studio

Apply JavaCC plugin for Gradle.  The top-level build.gradle should look something like this:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0-alpha3'
        classpath 'ca.coglinc:javacc-gradle-plugin:2.3.1'  
    }
}

Apply the plug-in in the module's build.gradle file:

apply plugin: 'com.android.application'
apply plugin: 'ca.coglinc.javacc'

Make your Java compilation tasks depend on compileJavacc task and add Java files generated by Javacc to your source path:

android {
    // ...
    sourceSets {
        main {
            java.srcDir compileJavacc.outputDirectory
        }
    }
    applicationVariants.all { variant ->
        variant.javaCompile.dependsOn compileJavacc
    }
    // ...
}

When integrating into an Android library, replace applicationVariants with libraryVariants.

Finally, install JavaCC IntelliJ plug-in to enable syntax highlighting.