Initial commit

This commit is contained in:
Geert Bevin 2023-04-01 01:07:12 -04:00
parent e6ff931205
commit 0c38aec70b
22 changed files with 691 additions and 0 deletions

55
.gitignore vendored Normal file
View file

@ -0,0 +1,55 @@
.gradle
.DS_Store
build
lib/bld/**
lib/compile/**
lib/runtime/**
lib/standalone/**
lib/test/**
!bld-wrapper.jar
!bld-wrapper.properties
# IDEA ignores
# User-specific
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# AWS User-specific
.idea/**/aws.xml
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# SonarLint plugin
.idea/sonarlint/
# Editor-based Rest Client
.idea/httpRequests

8
.idea/.gitignore generated vendored Normal file
View file

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

29
.idea/app.iml generated Normal file
View file

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/build/main" />
<output-test url="file://$MODULE_DIR$/build/test" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/antlr" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/src/main/resources/templates" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="RUNTIME">
<library>
<CLASSES>
<root url="file://$MODULE_DIR$/src/main/resources/templates" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" name="compile" level="project" />
<orderEntry type="library" scope="RUNTIME" name="runtime" level="project" />
<orderEntry type="library" scope="TEST" name="test" level="project" />
</component>
</module>

14
.idea/bld.iml generated Normal file
View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/build/bld" />
<output-test url="file://$MODULE_DIR$/build/bld" />
<exclude-output />
<content url="file://$MODULE_DIR$/src/bld">
<sourceFolder url="file://$MODULE_DIR$/src/bld/java" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="bld" level="project" />
</component>
</module>

17
.idea/libraries/bld.xml generated Normal file
View file

@ -0,0 +1,17 @@
<component name="libraryTable">
<library name="bld">
<CLASSES>
<root url="file://$PROJECT_DIR$/lib/bld" />
<root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.11.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.11-sources.jar!/" />
</SOURCES>
<excluded>
<root url="jar://$PROJECT_DIR$/lib/bld/bld-wrapper.jar!/" />
</excluded>
<jarDirectory url="file://$PROJECT_DIR$/lib/bld" recursive="false" />
<jarDirectory url="file://$PROJECT_DIR$/lib/bld" recursive="false" type="SOURCES" />
</library>
</component>

13
.idea/libraries/compile.xml generated Normal file
View file

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="compile">
<CLASSES>
<root url="file://$PROJECT_DIR$/lib/compile" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="file://$PROJECT_DIR$/lib/compile" />
</SOURCES>
<jarDirectory url="file://$PROJECT_DIR$/lib/compile" recursive="false" />
<jarDirectory url="file://$PROJECT_DIR$/lib/compile" recursive="false" type="SOURCES" />
</library>
</component>

13
.idea/libraries/runtime.xml generated Normal file
View file

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="runtime">
<CLASSES>
<root url="file://$PROJECT_DIR$/lib/runtime" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="file://$PROJECT_DIR$/lib/runtime" />
</SOURCES>
<jarDirectory url="file://$PROJECT_DIR$/lib/runtime" recursive="false" />
<jarDirectory url="file://$PROJECT_DIR$/lib/runtime" recursive="false" type="SOURCES" />
</library>
</component>

13
.idea/libraries/test.xml generated Normal file
View file

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="test">
<CLASSES>
<root url="file://$PROJECT_DIR$/lib/test" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="file://$PROJECT_DIR$/lib/test" />
</SOURCES>
<jarDirectory url="file://$PROJECT_DIR$/lib/test" recursive="false" />
<jarDirectory url="file://$PROJECT_DIR$/lib/test" recursive="false" type="SOURCES" />
</library>
</component>

9
.idea/modules.xml generated Normal file
View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/app.iml" filepath="$PROJECT_DIR$/.idea/app.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/bld.iml" filepath="$PROJECT_DIR$/.idea/bld.iml" />
</modules>
</component>
</project>

9
.idea/runConfigurations/Run Main.xml generated Normal file
View file

@ -0,0 +1,9 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Run Main" type="Application" factoryName="Application" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="rife.bld.extension.Antlr4Main" />
<module name="app" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>

13
.idea/runConfigurations/Run Tests.xml generated Normal file
View file

@ -0,0 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Run Tests" type="JUnit" factoryName="JUnit">
<module name="app" />
<option name="PACKAGE_NAME" value="moog" />
<option name="MAIN_CLASS_NAME" value="rife.bld.extension" />
<option name="METHOD_NAME" value="" />
<option name="TEST_OBJECT" value="directory" />
<dir value="$PROJECT_DIR$/src/test/java" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>

6
.idea/vcs.xml generated Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

24
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,24 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Run Main",
"request": "launch",
"mainClass": "rife.bld.extension.Antlr4Main"
},
{
"type": "java",
"name": "Run Tests",
"request": "launch",
"mainClass": "org.junit.platform.console.ConsoleLauncher",
"args": [
"--details=verbose",
"--scan-classpath",
"--disable-banner",
"--disable-ansi-colors",
"--exclude-engine=junit-platform-suite",
"--exclude-engine=junit-vintage"]
}
]
}

15
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,15 @@
{
"java.project.sourcePaths": [
"src/main/java",
"src/main/resources",
"src/test/java",
"src/bld/java"
],
"java.configuration.updateBuildConfiguration": "automatic",
"java.project.referencedLibraries": [
"${HOME}/.rife2/dist/rife2-1.5.11.jar",
"lib/compile/*.jar",
"lib/runtime/*.jar",
"lib/test/*.jar"
]
}

2
bld Executable file
View file

@ -0,0 +1,2 @@
#!/usr/bin/env sh
java -jar "$(dirname "$0")/lib/bld/bld-wrapper.jar" "$0" --build rife.bld.extension.Antlr4Build "$@"

4
bld.bat Normal file
View file

@ -0,0 +1,4 @@
@echo off
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
java -jar "%DIRNAME%/lib/bld/bld-wrapper.jar" "%0" --build rife.bld.extension.Antlr4Build %*

BIN
lib/bld/bld-wrapper.jar Normal file

Binary file not shown.

View file

@ -0,0 +1,5 @@
#Fri Mar 31 21:16:42 EDT 2023
bld.extension=
bld.repositories=https\://repo1.maven.org/maven2/
rife2.downloadLocation=
rife2.version=1.5.11

View file

@ -0,0 +1,45 @@
/*
* Copyright 2001-2023 Geert Bevin (gbevin[remove] at uwyn dot com)
* Licensed under the Apache License, Version 2.0 (the "License")
*/
package rife.bld.extension;
import rife.bld.Project;
import rife.bld.publish.PublishInfo;
import java.io.File;
import java.util.List;
import static rife.bld.dependencies.Repository.*;
import static rife.bld.dependencies.Scope.*;
public class Antlr4Build extends Project {
public Antlr4Build() {
pkg = "rife.bld.extension";
name = "Antlr4";
version = version(0,9,0);
javadocOptions.add("-Xdoclint:-missing");
publishRepository = MAVEN_LOCAL;
publishInfo = new PublishInfo().groupId("com.uwyn.rife2").artifactId("bld-antlr4");
downloadSources = true;
repositories = List.of(MAVEN_CENTRAL, SONATYPE_SNAPSHOTS);
scope(compile)
.include(dependency("com.uwyn.rife2", "rife2", version(1,5,11)))
.include(dependency("org.antlr", "antlr4", version(4,12,0)));
scope(test)
.include(dependency("org.junit.jupiter", "junit-jupiter", version(5,9,2)))
.include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1,9,2)));
}
@Override
public List<String> testClasspath() {
var classpath = super.testClasspath();
classpath.add(new File(srcTestDirectory(), "antlr").toString());
return classpath;
}
public static void main(String[] args) {
new Antlr4Build().start(args);
}
}

View file

@ -0,0 +1,337 @@
/*
* Copyright 2001-2023 Geert Bevin (gbevin[remove] at uwyn dot com)
* Licensed under the Apache License, Version 2.0 (the "License")
*/
package rife.bld.extension;
import org.antlr.v4.Tool;
import rife.bld.operations.AbstractOperation;
import java.io.File;
import java.util.*;
/**
* Generates Java sources from ANTLR grammars.
*
* @author Geert Bevin (gbevin[remove] at uwyn dot com)
* @since 1.0
*/
public class Antlr4Operation extends AbstractOperation<Antlr4Operation> {
private final List<String> arguments_ = new ArrayList<>();
private final List<File> sourceDirectories_ = new ArrayList<>();
private final List<File> sourceFiles_ = new ArrayList<>();
private File libDirectory_ = null;
private File outputDirectory_ = null;
private Boolean listener_ = null;
private Boolean visitor_ = null;
/**
* Performs the antlr operation.
*
* @since 1.0
*/
public void execute()
throws Exception {
var sources = new ArrayList<String>();
for (var dir : sourceDirectories_) {
if (dir.exists() && dir.isDirectory()) {
var files = dir.listFiles((dir1, name) -> name.endsWith(".g") || name.endsWith(".g4"));
if (files != null) {
for (var file : files) {
sources.add(file.getAbsolutePath());
}
}
}
}
for (var file : sourceFiles_) {
if (file.exists()) {
sources.add(file.getAbsolutePath());
}
}
if (sources.isEmpty()) {
throw new IllegalArgumentException("ERROR: no ANTLR grammars found");
}
var arguments = new ArrayList<>(arguments_);
if (libDirectory_ != null) {
arguments.add("-lib");
arguments.add(libDirectory_.getAbsolutePath());
}
if (outputDirectory_ != null) {
arguments.add("-o");
arguments.add(outputDirectory_.getAbsolutePath());
}
if (listener_ != null) {
if (listener_) {
arguments.add("-listener");
} else {
arguments.add("-no-listener");
}
}
if (visitor_ != null) {
if (visitor_) {
arguments.add("-visitor");
} else {
arguments.add("-no-visitor");
}
}
arguments.addAll(sources);
var argument_array = new String[arguments.size()];
arguments.toArray(argument_array);
new Tool(argument_array).processGrammarsOnCommandLine();
}
/**
* Provides additional arguments for the antlr operation.
* <p>
* The following additional arguments are supported:
* <table>
* <tr><td><code>-atn</code></td><td>generate rule augmented transition network diagrams</td></tr>
* <tr><td><code>-D&lt;option&gt;=value</code></td><td>set/override a grammar-level option</td></tr>
* <tr><td><code>-Werror</code></td><td>treat warnings as errors"</td></tr>
* <tr><td><code>-XdbgST</code></td><td>launch StringTemplate visualizer on generated code</td></tr>
* <tr><td><code>-XdbgSTWait</code></td><td>wait for STViz to close before continuing</td></tr>
* <tr><td><code>-Xforce-atn</code></td><td>use the ATN simulator for all predictions</td></tr>
* <tr><td><code>-Xlog</code></td><td>dump lots of logging info to antlr-timestamp.log</td></tr>
* <tr><td><code>-Xexact-output-dir</code></td><td>all output goes into -o dir regardless of paths/package</td></tr>
* </table>
*
* @param arguments the additional arguments
* @return this operation instance
* @since 1.0
*/
public Antlr4Operation arguments(String... arguments) {
arguments_.addAll(Arrays.asList(arguments));
return this;
}
/**
* Provides additional arguments for the antlr operation.
* <p>
* See {@link #arguments(String...)} for details.
*
* @param arguments the additional arguments
* @return this operation instance
* @since 1.0
*/
public Antlr4Operation arguments(List<String> arguments) {
arguments_.addAll(arguments);
return this;
}
/**
* Provides the source directories that will be used for the antlr operation.
*
* @param directories the source directories
* @return this operation instance
* @since 1.0
*/
public Antlr4Operation sourceDirectories(List<File> directories) {
sourceDirectories_.addAll(directories);
return this;
}
/**
* Provides the source files that will be used for the antlr operation.
*
* @param files the source files
* @return this operation instance
* @since 1.0
*/
public Antlr4Operation sourceFiles(List<File> files) {
sourceFiles_.addAll(files);
return this;
}
/**
* Provides the output directory where all output is generated.
*
* @param directory the output directory
* @return this operation instance
* @since 1.0
*/
public Antlr4Operation outputDirectory(File directory) {
outputDirectory_ = directory;
return this;
}
/**
* Provides the location of grammars and tokens files.
*
* @param directory the lib directory
* @return this operation instance
* @since 1.0
*/
public Antlr4Operation libDirectory(File directory) {
libDirectory_ = directory;
return this;
}
/**
* Provides grammar file encoding; e.g., euc-jp.
*
* @param encoding the encoding
* @return this operation instance
* @since 1.0
*/
public Antlr4Operation grammarEncoding(String encoding) {
arguments("-encoding", encoding);
return this;
}
/**
* Specify output style for messages in antlr, gnu, vs2005.
*
* @param format the output styke
* @return this operation instance
* @since 1.0
*/
public Antlr4Operation msgFormat(String format) {
arguments("-message-format", format);
return this;
}
/**
* Show exception details when available for errors and warnings.
*
* @return this operation instance
* @since 1.0
*/
public Antlr4Operation longMessages() {
arguments("-long-messages");
return this;
}
/**
* Generate parse tree listener (default).
*
* @return this operation instance
* @since 1.0
*/
public Antlr4Operation listener() {
listener_ = true;
return this;
}
/**
* Don't generate parse tree listener.
*
* @return this operation instance
* @since 1.0
*/
public Antlr4Operation noListener() {
listener_ = false;
return this;
}
/**
* Generate parse tree visitor.
*
* @return this operation instance
* @since 1.0
*/
public Antlr4Operation visitor() {
visitor_ = true;
return this;
}
/**
* Don't generate parse tree visitor (default).
*
* @return this operation instance
* @since 1.0
*/
public Antlr4Operation noVisitor() {
visitor_ = false;
return this;
}
/**
* Specify a package/namespace for the generated code
*
* @param pkg the package name
* @return this operation instance
* @since 1.0
*/
public Antlr4Operation pkg(String pkg) {
arguments("-package", pkg);
return this;
}
/**
* Generate file dependencies.
*
* @return this operation instance
* @since 1.0
*/
public Antlr4Operation depend() {
arguments("-depend");
return this;
}
/**
* Retrieves the list of arguments that will be used for the
* antlr operation.
* <p>
* This is a modifiable list that can be retrieved and changed.
*
* @return the arguments
* @since 1.0
*/
public List<String> arguments() {
return arguments_;
}
/**
* Retrieves the list of source directories that will be used for the
* antlr operation.
* <p>
* This is a modifiable list that can be retrieved and changed.
*
* @return the source directories
* @since 1.0
*/
public List<File> sourceDirectories() {
return sourceDirectories_;
}
/**
* Retrieves the list of source files that will be used for the
* antlr operation.
* <p>
* This is a modifiable list that can be retrieved and changed.
*
* @return the source files
* @since 1.0
*/
public List<File> sourceFiles() {
return sourceFiles_;
}
/**
* Retrieves the output directory where all output is generated.
*
* @return the output directory; or
* * {@code null} if the directory wasn't specified
* @since 1.0
*/
public File outputDirectory() {
return outputDirectory_;
}
/**
* Retrieves the location of grammars and tokens files.
*
* @return the lib directory; or
* {@code null} if the directory wasn't specified
* @since 1.0
*/
public File libDirectory() {
return libDirectory_;
}
}

View file

@ -0,0 +1,8 @@
grammar ArrayInit;
init : '{' value (',' value)* '}' ;
value : init
| INT
;
INT : [0-9]+ ;
WS : [ \t\r\n]+ -> skip ;

View file

@ -0,0 +1,52 @@
/*
* Copyright 2001-2023 Geert Bevin (gbevin[remove] at uwyn dot com)
* Licensed under the Apache License, Version 2.0 (the "License")
*/
package rife.bld.extension;
import org.junit.jupiter.api.Test;
import rife.resources.ResourceFinder;
import rife.resources.ResourceFinderClasspath;
import rife.tools.FileUtils;
import java.io.File;
import java.nio.file.Files;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class Antlr4Test {
@Test
void testInstantiation() {
var operation = new Antlr4Operation();
assertTrue(operation.arguments().isEmpty());
assertTrue(operation.sourceDirectories().isEmpty());
assertTrue(operation.sourceFiles().isEmpty());
assertNull(operation.libDirectory());
assertNull(operation.outputDirectory());
}
@Test
void testGenerateGrammar()
throws Exception {
var tmp = Files.createTempDirectory("test").toFile();
try {
assertEquals("", FileUtils.generateDirectoryListing(tmp));
new Antlr4Operation()
.sourceFiles(List.of(new File(ResourceFinderClasspath.instance().getResource("ArrayInit.g4").toURI())))
.outputDirectory(tmp)
.execute();
assertEquals("""
/ArrayInit.interp
/ArrayInit.tokens
/ArrayInitBaseListener.java
/ArrayInitLexer.interp
/ArrayInitLexer.java
/ArrayInitLexer.tokens
/ArrayInitListener.java
/ArrayInitParser.java""", FileUtils.generateDirectoryListing(tmp));
} finally {
FileUtils.deleteDirectory(tmp);
}
}
}