Skip to content

Commit

Permalink
Merge pull request antlr#15 from Strumenta/merging
Browse files Browse the repository at this point in the history
Merging antlr#13
  • Loading branch information
ftomassetti authored May 10, 2019
2 parents fd006ee + e7bf49e commit 3e94f41
Show file tree
Hide file tree
Showing 92 changed files with 2,461 additions and 2,158 deletions.
2 changes: 1 addition & 1 deletion antlr-kotlin-examples-jvm/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.3.20'
ext.kotlin_version = '1.3.31'
ext.projectVersion = '0.0.3'
ext.antlrKotlinVersion = projectVersion

Expand Down
2 changes: 1 addition & 1 deletion antlr-kotlin-examples-jvm/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
org.gradle.jvmargs=-Xmx8192m
kotlinVersion=1.3.20
kotlinVersion=1.3.31
projectGroup=com.strumenta.antlr-kotlin
projectVersion=0.0.3
antlrVersion=4.7.1
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,21 @@
package com.strumenta.antlrkotlin.gradleplugin;

import com.strumenta.antlrkotlin.gradleplugin.internal.AntlrSourceVirtualDirectoryImpl;
import org.gradle.api.*;
import com.strumenta.antlrkotlin.gradleplugin.internal.SourceDirectorySetFactory;
import org.gradle.api.GradleException;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.DependencySet;
import org.gradle.api.internal.file.SourceDirectorySetFactory;
import org.gradle.api.internal.plugins.DslObject;
import org.gradle.api.internal.tasks.DefaultSourceSet;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.tasks.SourceSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;

import static org.gradle.api.plugins.JavaPlugin.COMPILE_CONFIGURATION_NAME;

Expand All @@ -47,8 +45,8 @@ public class AntlrKotlinPlugin implements Plugin<Project> {
private final SourceDirectorySetFactory sourceDirectorySetFactory;

@Inject
public AntlrKotlinPlugin(SourceDirectorySetFactory sourceDirectorySetFactory) {
this.sourceDirectorySetFactory = sourceDirectorySetFactory;
public AntlrKotlinPlugin(final ObjectFactory objectFactory) {
this.sourceDirectorySetFactory = name -> objectFactory.sourceDirectorySet(name, name);
}

public void apply(final Project project) {
Expand All @@ -72,7 +70,7 @@ public void apply(final Project project) {

dependencies.add(project.getDependencies().create("org.antlr:antlr4:4.7.1"));
dependencies.add(project.getDependencies().create("com.strumenta.antlr-kotlin:antlr-kotlin-target:"
+ antlrKotlinVersion));
+ antlrKotlinVersion));
});

project.getConfigurations().getByName(COMPILE_CONFIGURATION_NAME).extendsFrom(antlrConfiguration);
Expand All @@ -92,7 +90,7 @@ public void apply(final Project project) {
= new AntlrSourceVirtualDirectoryImpl(((DefaultSourceSet) sourceSet).getDisplayName(), sourceDirectorySetFactory);
new DslObject(sourceSet).getConvention().getPlugins().put(
AntlrSourceVirtualDirectory.NAME, antlrDirectoryDelegate);
final String srcDir = "src/"+ sourceSet.getName() +"/antlr";
final String srcDir = "src/" + sourceSet.getName() + "/antlr";
antlrDirectoryDelegate.getAntlr().srcDir(srcDir);
sourceSet.getAllSource().source(antlrDirectoryDelegate.getAntlr());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

package com.strumenta.antlrkotlin.gradleplugin;

import org.gradle.api.Action;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileTree;
import org.gradle.api.file.SourceDirectorySet;
import com.strumenta.antlrkotlin.gradleplugin.internal.AntlrResult;
import com.strumenta.antlrkotlin.gradleplugin.internal.AntlrSourceGenerationException;
import com.strumenta.antlrkotlin.gradleplugin.internal.AntlrSpec;
import com.strumenta.antlrkotlin.gradleplugin.internal.AntlrSpecFactory;
import com.strumenta.antlrkotlin.gradleplugin.internal.AntlrWorkerManager;
import org.gradle.api.Action;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileTree;
import org.gradle.api.file.SourceDirectorySet;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input;
Expand Down Expand Up @@ -194,17 +194,17 @@ public void execute(IncrementalTaskInputs inputs) {
final Set<File> sourceFiles = getSource().getFiles();
final AtomicBoolean cleanRebuild = new AtomicBoolean();
inputs.outOfDate(
new Action<InputFileDetails>() {
public void execute(InputFileDetails details) {
File input = details.getFile();
if (sourceFiles.contains(input)) {
grammarFiles.add(input);
} else {
// classpath change?
cleanRebuild.set(true);
new Action<InputFileDetails>() {
public void execute(InputFileDetails details) {
File input = details.getFile();
if (sourceFiles.contains(input)) {
grammarFiles.add(input);
} else {
// classpath change?
cleanRebuild.set(true);
}
}
}
}
);
inputs.removed(new Action<InputFileDetails>() {
@Override
Expand All @@ -230,20 +230,20 @@ public void execute(InputFileDetails details) {

private void evaluate(AntlrResult result) {
int errorCount = result.getErrorCount();
if(errorCount < 0) {
if (errorCount < 0) {
throw new AntlrSourceGenerationException("There were errors during grammar generation", result.getException());
} else if (errorCount == 1) {
throw new AntlrSourceGenerationException("There was 1 error during grammar generation", result.getException());
} else if (errorCount > 1) {
throw new AntlrSourceGenerationException("There were "
+ errorCount
+ " errors during grammar generation", result.getException());
+ errorCount
+ " errors during grammar generation", result.getException());
}
}

/**
* Sets the source for this task. Delegates to {@link #setSource(Object)}.
*
* <p>
* If the source is of type {@link SourceDirectorySet}, then the relative path of each source grammar files
* is used to determine the relative output path of the generated source
* If the source is not of type {@link SourceDirectorySet}, then the generated source files end up
Expand All @@ -259,7 +259,7 @@ public void setSource(FileTree source) {

/**
* Sets the source for this task. Delegates to {@link SourceTask#setSource(Object)}.
*
* <p>
* If the source is of type {@link SourceDirectorySet}, then the relative path of each source grammar files
* is used to determine the relative output path of the generated source
* If the source is not of type {@link SourceDirectorySet}, then the generated source files end up
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import org.gradle.api.GradleException;
import org.gradle.internal.UncheckedException;
import org.gradle.internal.os.OperatingSystem;
import org.gradle.internal.reflect.*;
import org.gradle.internal.reflect.JavaMethod;
import org.gradle.internal.reflect.NoSuchPropertyException;
import org.gradle.internal.reflect.PropertyMutator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -62,7 +64,7 @@ static Object loadTool(String className, String[] args) throws ClassNotFoundExce
Constructor<?> constructor = toolClass.getConstructor(String[].class);
return constructor.newInstance(new Object[]{args});
}
} catch(ClassNotFoundException cnf){
} catch (ClassNotFoundException cnf) {
LOGGER.error("Tool not loaded " + cnf.getMessage());
throw cnf;
} catch (InvocationTargetException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class AntlrResult implements Serializable {
public AntlrResult(int errorCount) {
this(errorCount, null);
}

public AntlrResult(int errorCount, Exception exception) {
this.errorCount = errorCount;
this.exception = exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import groovy.lang.Closure;
import org.gradle.api.Action;
import org.gradle.api.file.SourceDirectorySet;
import org.gradle.api.internal.file.SourceDirectorySetFactory;
import org.gradle.api.plugins.antlr.AntlrSourceVirtualDirectory;
import org.gradle.util.ConfigureUtil;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.strumenta.antlrkotlin.gradleplugin.internal;

import org.gradle.api.file.SourceDirectorySet;
import com.strumenta.antlrkotlin.gradleplugin.AntlrKotlinTask;
import org.gradle.api.file.SourceDirectorySet;

import java.io.File;
import java.util.LinkedList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ private AntlrWorker createWorkerProcess(File workingDir, WorkerProcessFactory wo
} else {
LOGGER.debug("Setting no antlr classpath");
}
builder.sharedPackages(new String[] {"antlr", "org.antlr"});
builder.sharedPackages(new String[]{"antlr", "org.antlr"});
JavaExecHandleBuilder javaCommand = builder.getJavaCommand();
javaCommand.setWorkingDir(workingDir);
javaCommand.setMaxHeapSize(spec.getMaxHeapSize());
javaCommand.systemProperty("ANTLR_DO_NOT_EXIT", "true");
javaCommand.redirectErrorStream();
return builder.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.strumenta.antlrkotlin.gradleplugin.internal;

import org.gradle.api.file.SourceDirectorySet;

public interface SourceDirectorySetFactory {
SourceDirectorySet create(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

package org.antlr.v4.kotlinruntime

import org.antlr.v4.kotlinruntime.tree.ErrorNode

/**
* The interface for defining strategies to deal with syntax errors encountered
* during a parse by ANTLR-generated parsers. We distinguish between three
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
*/
package org.antlr.v4.kotlinruntime

import org.antlr.v4.kotlinruntime.misc.Utils

///**
// * This is an [ANTLRInputStream] that is loaded from a file all at once
// * when you construct the object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
*/
package org.antlr.v4.kotlinruntime

import com.strumenta.kotlinmultiplatform.*
import com.strumenta.kotlinmultiplatform.Math
import com.strumenta.kotlinmultiplatform.assert
import com.strumenta.kotlinmultiplatform.convertToString
import com.strumenta.kotlinmultiplatform.toCharArray
import org.antlr.v4.kotlinruntime.misc.Interval

/**
Expand All @@ -31,7 +34,7 @@ open class ANTLRInputStream : CharStream {

/** What is name or source of this char stream? */
var name: String? = null
//
//
override val sourceName: String
get() = if (name == null || name!!.isEmpty()) {
IntStream.UNKNOWN_SOURCE_NAME
Expand Down Expand Up @@ -108,7 +111,8 @@ open class ANTLRInputStream : CharStream {
fun reset() {
p = 0
}
//

//
override fun consume() {
if (p >= n) {
assert(LA(1) == IntStream.EOF)
Expand Down Expand Up @@ -162,7 +166,8 @@ open class ANTLRInputStream : CharStream {
override fun mark(): Int {
return -1
}
//

//
override fun release(marker: Int) {}

/** consume() ahead until p==index; can't just set p=index as we must
Expand All @@ -186,7 +191,7 @@ open class ANTLRInputStream : CharStream {
var stop = interval.b
if (stop >= n) stop = n - 1
val count = stop - start + 1
return if (start >= n) "" else data!!.copyOfRange(start, start+count).convertToString()
return if (start >= n) "" else data!!.copyOfRange(start, start + count).convertToString()
// System.err.println("data: "+Arrays.toString(data)+", n="+n+
// ", start="+start+
// ", stop="+stop);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ open class DefaultErrorStrategy : ANTLRErrorStrategy {
else
//TODO
input = e.startToken.text!!
//input = tokens!!.getText(e.startToken, e.offendingToken)!!
//input = tokens!!.getText(e.startToken, e.offendingToken)!!
} else {
input = "<unknown input>"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package org.antlr.v4.kotlinruntime

import com.strumenta.kotlinmultiplatform.BitSet
import org.antlr.v4.kotlinruntime.atn.ATNConfig
import org.antlr.v4.kotlinruntime.atn.ATNConfigSet
import org.antlr.v4.kotlinruntime.dfa.DFA
import org.antlr.v4.kotlinruntime.misc.Interval
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ private fun formatMessage(predicate: String?, message: String?): String {
* prediction.
*/
class FailedPredicateException constructor(recognizer: Parser,
val predicate: String? = null,
message: String? = null) : RecognitionException(recognizer, recognizer.readInputStream()!!, recognizer.context!!, formatMessage(predicate, message)) {
val predicate: String? = null,
message: String? = null) : RecognitionException(recognizer, recognizer.readInputStream()!!, recognizer.context!!, formatMessage(predicate, message)) {
var ruleIndex: Int = -1
var predIndex: Int = -1

Expand All @@ -41,5 +41,4 @@ class FailedPredicateException constructor(recognizer: Parser,
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ abstract class Lexer : Recognizer<Int, LexerATNSimulator>, TokenSource {
this.inputStream = input as CharStream
this._tokenFactorySourcePair = Pair<TokenSource, CharStream>(this, readInputStream())
}
override fun readInputStream() : CharStream? {

override fun readInputStream(): CharStream? {
return this.inputStream as CharStream?
}

Expand Down Expand Up @@ -268,8 +269,6 @@ abstract class Lexer : Recognizer<Int, LexerATNSimulator>, TokenSource {
}




/** By default does not support multiple emits per nextToken invocation
* for efficiency reasons. Subclass and override this method, nextToken,
* and getToken (to push tokens into a list and pull from that list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import org.antlr.v4.kotlinruntime.dfa.DFA
class LexerInterpreter(override val grammarFileName: String, override val vocabulary: Vocabulary, ruleNames: Collection<String>, channelNames: Collection<String>, modeNames: Collection<String>, override val atn: ATN, input: CharStream) : Lexer(input) {



@Deprecated("")
@get:Deprecated("")
override val tokenNames: Array<String?>?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ constructor(
* {@inheritDoc}
*/
override// have to calculate the result from the line/column of the previous
// token, along with the text of the token.
// only reach this if tokens is empty, meaning EOF occurs at the first
// position in the input
// token, along with the text of the token.
// only reach this if tokens is empty, meaning EOF occurs at the first
// position in the input
val charPositionInLine: Int
get() {
if (i < tokens!!.size) {
Expand All @@ -102,10 +102,10 @@ constructor(
* {@inheritDoc}
*/
override// have to calculate the result from the line/column of the previous
// token, along with the text of the token.
// if no text is available, assume the token did not contain any newline characters.
// only reach this if tokens is empty, meaning EOF occurs at the first
// position in the input
// token, along with the text of the token.
// if no text is available, assume the token did not contain any newline characters.
// only reach this if tokens is empty, meaning EOF occurs at the first
// position in the input
val line: Int
get() {
if (i < tokens!!.size) {
Expand Down
Loading

0 comments on commit 3e94f41

Please sign in to comment.