This is a simple quickstart project for the CQL java libraries. It demonstrates how to generate and build Java lexers, parsers, listeners, and visitors using gradle and the CQL ANTLR4 grammar. It also contains the start of a simple proof of concept visitor class for processing CQL files.
This project uses the Gradle build system. A gradle wrapper, which automatically downloads and uses an instance of gradle, is provided for convenience. To build the project, simply execute the following command:
./gradlew build
This will generate the ANTLR4 artifacts to src/generated
, build the CQL code library, build the
example classes, and execute the example unit tests.
To clean up the artifacts:
./gradlew clean
You can generate an IDE project for IntelliJ IDEA:
./gradlew idea
This is preferred over having IDEA load the gradle file directly, because it (a) generates the CQL
libraries, and (b) generates the configuration for the IDEA ANTLR plugin. To open the project in
IntelliJ IDEA, launch IDEA and open the java-quickstart.ipr
file.
See further below for installing and using the ANTLR plugin for IDEA.
You can execute the sample code using the gradlew
command or a script generated by gradle.
To execute the sample code using gradlew
, you must execute the run
command:
./gradlew run
To execute the sample code using a script generated by gradle, first generate the scripts:
./gradlew installDist
Then execute the generated script (optionally passing in an argument). The following example executes the sample cql code with the path to a CQL file as an argument:
./build/install/java-quickstart/bin/java-quickstart ./src/test/resources/ChlamydiaScreening_CQM.cql
The following steps detail how to install the ANTLR plugin for IntelliJ IDEA. This allows you to more easily edit and test the grammar, even supporting real-time parse trees of CQL files. These instructions apply to the Mac OS X version, but installation on Windows should be similar.
- Download and install the IntelliJ IDEA Community Edition if you don't have it already
- Open IntelliJ and open the "Preferences" pane (IntelliJ IDEA --> Preferences)
- Choose "Plugins" from the "IDE Settings" section
- Click the "Browse Repositories..." button
- Use the search box to find the "ANTLR v4 grammar plugin"
- Click the "Install Plugin" button
- After installation, restart IDEA
NOTE: There is currently a minor incompatibility between the ANTLR plugin for IDEA and the ANTLR4
libraries used by the gradle project. The plugin uses a 4.4.x version of ANTLR, but the gradle
project uses 4.3. As a result, if you regenerate the ANTLR lexer/parser classes using the plugin
(for example, by editing the g4 file from the IDE), the generated classes will not run using the
ANTLR 4.3 runtime libraries. If this happens, re-run gradle build
to fix it.
The most helpful feature of the ANTLR plugin for IDEA is the ability to test CQL phrases and see dynamic parse trees for them. To do this:
- Open the cql.g4 file (located in
src/main/resources
) - Right-click the
logic
grammar rule in the file - Choose "Test Rule logic"
- Enter a phrase in the preview window to see if it is valid and generate a parse tree
Note: You can test other grammar rules by changing the rule you select in step 2.
Copyright 2014 The MITRE Corporation
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.