Commit e066fc89 authored by Fred Eisele's avatar Fred Eisele
Browse files

generates parser from antlr and compiles

parent fbc259b9
......@@ -6,7 +6,7 @@ doc/aqlmanual/aqlmanual.out
doc/aqlmanual/aqlmanual.pdf
doc/aqlmanual/aqlmanual.synctex.gz
doc/aqlmanual/aqlmanual.toc
/target/
build/
buildSrc/build/
.classpath
......
import org.babeloff.ProjectVersion
import org.babeloff.ReleaseVersionTask
import org.babeloff.SnapshotVersionTask
import org.babeloff.plugins.antlr.Antlr4Task
// apply from: 'gradle/foo.gradle'
apply plugin: 'java-library'
apply plugin: 'maven'
apply plugin: 'antlr'
apply plugin: 'idea'
apply plugin: 'eclipse'
......@@ -70,23 +73,20 @@ task release(dependsOn: backupReleaseDistribution) {
configurations {
all { }
antlr { resolutionStrategy { failOnVersionConflict() } }
}
// https://github.com/babeloff/boot-antlr4/blob/master/src/babeloff/boot_antlr4.clj
//
// (deftask build
// [s show bool "show the arguments"]
// (comp
// (antlr/generate
// :grammar "AqlLexerRules.g4"
// :package "catdata.aql.grammar")
// (antlr/generate
// :grammar "AqlParser.g4"
// :package "catdata.aql.grammar"
// :visitor true
// :listener true)
// (javac)))
}
task antlrGen (type: Antlr4Task) {
maxHeapSize = "64m"
sourceDir = file("${project.rootDir}/src/main/antlr")
lexerGrammar = file("${project.rootDir}/src/main/antlr/catdata/aql/grammar/AqlLexerRules.g4")
parserGrammar = file("${project.rootDir}/src/main/antlr/catdata/aql/grammar/AqlParser.g4")
module = "catdata.aql.grammar"
targetDir = file("${project.buildDir}/generated-src/antlr/main/catdata/aql/grammar")
visitor = false
listener = true
}
compileJava.dependsOn antlrGen
wrapper {
gradleVersion = '4.10'
......@@ -97,20 +97,14 @@ wrapper {
sourceSets {
main {
java {
srcDirs = ['src', 'build/generated-src/antlr']
}
antlr {
// srcDirs = ['src/main/antlr','src/main/antlr_imports']
srcDirs = ['src/main/java','build/generated-src/antlr']
}
}
test {
java {
srcDirs = ['test']
srcDirs = ['test/java']
}
}
}
idea {
......@@ -121,24 +115,10 @@ idea {
}
}
generateGrammarSource {
// generateGrammarSource = "AqlParser"
maxHeapSize = "64m"
// includes = ["${project.rootDir}/src/main/antlr4/imports"]
outputDirectory = file("${project.buildDir}/generated-src/antlr/main")
source = "${sourceSets.main.antlr}/"
arguments << '-lib' << 'src/main/antlr_imports/'
arguments << '-visitor'
arguments << '-listener'
arguments << '-long-messages'
arguments << '-package' << 'catdata.aql.grammar'
}
repositories {
maven { url "https://nexus.isis.vanderbilt.edu/repository/maven-releases" }
maven { url "https://nexus.isis.vanderbilt.edu/repository/maven-snapshots" }
maven { url "http://repo.maven.apache.org/maven2" }
mavenCentral()
}
dependencies {
......@@ -160,6 +140,20 @@ dependencies {
compile group: 'com.fifesoft', name: 'rsyntaxtextarea', version:'2.6.1'
compile group: 'es.nitaur.markdown', name: 'txtmark', version:'0.16'
compile group: 'org.antlr', name: 'antlr4-runtime', version:'4.7.1'
antlr group: 'org.antlr', name: 'antlr4', version: '4.7.1'
testCompile group: 'junit', name: 'junit', version:'4.12'
// antlr group: 'org.antlr', name: 'antlr4', version: '4.7.1'
testCompileOnly(
'junit:junit:4.12'
)
testImplementation(
'org.junit.jupiter:junit-jupiter-api:5.3.1'
)
testRuntimeOnly(
'org.junit.jupiter:junit-jupiter-engine:5.3.1',
'org.junit.vintage:junit-vintage-engine:5.3.1'
)
}
test {
useJUnitPlatform()
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.antlr', name: 'antlr4', version:'4.7.1'
}
package org.babeloff.plugins.antlr;
public class Antlr4Plugin {
}
package org.babeloff.plugins.antlr
import org.gradle.api.GradleException
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputDirectory;
import java.nio.file.Files;
import org.antlr.v4.Tool;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;
class Antlr4Task extends DefaultTask {
/**
* g grammar FILE str "grammar file"
* l target LIB_DIR str "specify location of grammars, tokens files"
* e encoding CODE str "specify grammar file encoding; e.g., euc-jp"
* f message-format STYLE str "specify output STYLE for messages in antlr, gnu, vs2005"
* p package NAME str "specify a package/namespace for the generated code"
* D override OPT [str] "<option>=value set/override a grammar-level option"
* s show bool "show the constructed properties"
* a atn bool "generate rule augmented transition network diagrams"
* v long-messages bool "show exception details when available for errors and warnings"
* _ listener bool "generate parse tree listener"
* _ visitor bool "generate parse tree visitor"
* d depend bool "generate file dependencies"
* w warn-error bool "treat warnings as errors"
* _ save-lexer bool "extract lexer from combined grammar"
* _ debug-st bool "launch StringTemplate visualizer on generated code"
* _ debug-st-wait bool "wait for STViz to close before continuing"
* _ force-atn bool "use the ATN simulator for all predictions"
* _ log bool "dump lots of logging info to antlr-timestamp.log"]
*/
@Input @Optional String maxHeapSize;
@InputFile String lexerGrammar;
@InputFile String parserGrammar;
@InputDirectory File sourceDir;
@OutputDirectory File targetDir;
@Input @Optional String encoding;
@Input @Optional String msgFormat;
@Input @Optional String module;
@Input @Optional String override;
@Input @Optional Boolean show;
@Input @Optional Boolean atn;
@Input @Optional Boolean longMsg;
@Input @Optional Boolean listener;
@Input @Optional Boolean visitor;
@Input @Optional Boolean depend;
@Input @Optional Boolean warnError;
@Input @Optional Boolean saveLexer;
@Input @Optional Boolean debugStrTempl;
@Input @Optional Boolean debugStrTemplWait;
@Input @Optional Boolean forceAtn;
@Input @Optional Boolean log;
Antlr4Task() {
description = 'An antlr4 grammar generates a java parser'
group = 'org.babeloff'
}
@TaskAction
void start() {
ArrayList<String> args = new ArrayList<>();
args.push('-o');
args.push(targetDir.canonicalFile.toString())
logger.quiet 'target dir: ' + targetDir.canonicalFile
args.push('-lib');
args.push(sourceDir.canonicalFile.toString())
logger.quiet 'source dir: ' + sourceDir.canonicalFile
args.push('-package')
args.push(module)
def lexerArgs = args.clone();
lexerArgs.push(lexerGrammar);
String[] lexerArgsArray = lexerArgs.toArray(new String[lexerArgs.size()]);
logger.quiet 'lexer: ' + lexerArgsArray;
try {
Tool lexerTool = new Tool(lexerArgsArray);
lexerTool.processGrammarsOnCommandLine();
}
catch (Exception ex) {
throw new GradleException(ex.message)
}
// make the token file available to antlr so
// the same tokens get used by the parser as by the lexer.
def parserArgs = args.clone();
if (visitor) ( parserArgs.push('-visitor'))
if (listener) ( parserArgs.push('-listener'))
parserArgs.push(parserGrammar);
String[] parserArgsArray = parserArgs.toArray(new String[parserArgs.size()]);
logger.quiet 'parsing: ' + parserArgsArray;
try {
Tool parserTool = new Tool(parserArgsArray);
parserTool.processGrammarsOnCommandLine();
}
catch (Exception ex) {
throw new GradleException(ex.message)
}
}
}
......@@ -204,8 +204,8 @@ EVAL_SQL_PERSISTENT_INDICIES : 'eval_sql_persistent_indices' ;
COPRODUCT_ALLOW_ENTITY : 'coproduct_allow_entity_collisions_unsafe' ;
COPRODUCT_ALLOW_TYPE : 'coproduct_allow_type_collisions_unsafe' ;
QUERY_REMOVE_REDUNDANCY : 'query_remove_redundancy' ;
TRUE : 'true' ;
FALSE : 'false' ;
TRUE : True ;
FALSE : False ;
AUTO : 'auto' ;
FAIL : 'fail' ;
......
......@@ -111,7 +111,7 @@ fragment DecDigit
// Literals
fragment BoolLiteral
: TRUE | FALSE
: True | False
;
......@@ -300,3 +300,7 @@ fragment Pound
fragment Tilde
: '~'
;
fragment True : 'true' ;
fragment False : 'false' ;
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment