Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Kotlin core for java runtime #40

Merged
merged 32 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
acd7f2b
compiles!
ericvergnaud Feb 21, 2024
c8020bc
Make the core runtime usable from Tool
ericvergnaud Feb 22, 2024
27183fc
rename java runtime
ericvergnaud Feb 22, 2024
cdaa210
drop unused java runtime classes (we have them in Kotlin)
ericvergnaud Feb 22, 2024
81ff4d0
fix compile erros
ericvergnaud Feb 22, 2024
6add06f
simplify
ericvergnaud Feb 22, 2024
cca13da
fix compile errors
ericvergnaud Feb 22, 2024
91b0880
have tool compile using core runtime
ericvergnaud Feb 22, 2024
06e6a9a
compile runtime-test-suite
ericvergnaud Feb 24, 2024
cc7c514
fully compiles
ericvergnaud Feb 24, 2024
2d9fb6e
ensure all modules compile to the same JDK
ericvergnaud Feb 25, 2024
9c125e4
make sharedContextCache optional
ericvergnaud Feb 25, 2024
e8ae531
make interpreter optional to allow construction from Java
ericvergnaud Feb 25, 2024
2a702e7
restore nullables as explicitly required by tool-testsuite
ericvergnaud Feb 25, 2024
cfc98a2
Merge branch 'dev' into use-core-for-java-runtime
ericvergnaud Feb 25, 2024
06672c5
fix merge conflicts
ericvergnaud Feb 25, 2024
884e76a
fix file path issue
ericvergnaud Feb 26, 2024
1f6a704
more defensive coding
ericvergnaud Feb 26, 2024
18deed5
complete migration of #39
ericvergnaud Feb 26, 2024
19455bf
fix incorrect nullables
ericvergnaud Feb 26, 2024
69d4abd
fix file path issue
ericvergnaud Feb 26, 2024
235eeed
fix failing tests
ericvergnaud Feb 26, 2024
b353309
fix typo
ericvergnaud Feb 26, 2024
1e645ae
fix failing tests
ericvergnaud Feb 26, 2024
89fc760
use getters
ericvergnaud Feb 26, 2024
2a3267d
fix failing tests
ericvergnaud Feb 26, 2024
0694d2c
fix failed build
ericvergnaud Feb 27, 2024
051c37f
fix kotlin runtime tests
ericvergnaud Feb 27, 2024
de58513
Merge branch 'dev' into use-core-for-java-runtime
ericvergnaud Feb 27, 2024
4c7dc61
fix failing tests
ericvergnaud Feb 27, 2024
7f63597
fix corrupt merge
ericvergnaud Feb 27, 2024
0090baa
Merge branch 'dev' into use-core-for-java-runtime
ericvergnaud Feb 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 3 additions & 3 deletions antlr5-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>17</release>
<source>17</source>
<target>17</target>
<release>${maven.compiler.release}</release>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

import org.antlr.v5.Tool;
import org.antlr.v5.codegen.CodeGenerator;
import org.antlr.v5.runtime.misc.MultiMap;
import org.antlr.v5.runtime.misc.Utils;
import org.antlr.v5.runtime._unused.misc.MultiMap;
import org.antlr.v5.tool.Grammar;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
Expand Down Expand Up @@ -485,7 +484,7 @@ private static String getPackageName(String relativeFolderPath) {
// intentionally blank
}

return Utils.join(parts.iterator(), ".");
return String.join(".", parts);
}

public Set<String> getIncludesPatterns() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static byte[] checksum(File file) throws IOException {
* directory and reflect the input organization of the grammar files.
*
* @param sourceDirectory The source directory {@link File} object
* @param grammarFileName The full path to the input grammar file
* @param grammarFile The input grammar file
*
* @return The path to the grammar file relative to the source directory
*/
Expand Down
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.outputTimestamp>1693865452</project.build.outputTimestamp>
<antlr.testinprocess>true</antlr.testinprocess>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<kotlin.version>2.0.0-Beta4</kotlin.version>
<maven.compiler.release>17</maven.compiler.release>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<kotlin.version>2.0.0-Beta4</kotlin.version>
<junit.version>5.9.0</junit.version>
</properties>

Expand Down
10 changes: 5 additions & 5 deletions runtime-testsuite/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr5-runtime</artifactId>
<artifactId>antlr5-java-runtime</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
Expand Down Expand Up @@ -72,7 +72,7 @@
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
Expand Down Expand Up @@ -189,9 +189,9 @@
</execution>
</executions>
<configuration>
<release>17</release>
<source>17</source>
<target>17</target>
<release>${maven.compiler.release}</release>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ ModMemberEquals(n,m,v) ::= <%this.<n> % <m> == <v>%>

ModMemberNotEquals(n,m,v) ::= <%this.<n> % <m> != <v>%>

DumpDFA() ::= "this.dumpDFA(outStream);"
DumpDFA() ::= "this.dumpDFA(new IPrintStream() { public void print(String value) { outStream.print(value); } public void printLine(String value) { outStream.println(value); } });"

Pass() ::= ""

Expand All @@ -66,23 +66,23 @@ TextEquals(a) ::= <%this.getText().equals("<a>")%>

PlusText(a) ::= <%"<a>" + this.getText()%>

InputText() ::= "this._input.getText()"
InputText() ::= "this.get_input().getText()"

LTEquals(i, v) ::= <%this._input.LT(<i>).getText().equals(<v>)%>
LTEquals(i, v) ::= <%this.get_input().LT(<i>).getText().equals(<v>)%>

LANotEquals(i, v) ::= <%this._input.LA(<i>)!=<v>%>
LANotEquals(i, v) ::= <%this.get_input().LA(<i>)!=<v>%>

TokenStartColumnEquals(i) ::= <%this._tokenStartCharPositionInLine==<i>%>
TokenStartColumnEquals(i) ::= <%this.get_tokenStartCharPositionInLine()==<i>%>

ImportListener(X) ::= ""

GetExpectedTokenNames() ::= "this.getExpectedTokens().toString(this.tokenNames)"
GetExpectedTokenNames() ::= "this.getExpectedTokens().toString(this.getVocabulary())"

ImportRuleInvocationStack() ::= ""

RuleInvocationStack() ::= "getRuleInvocationStack()"
RuleInvocationStack() ::= "getRuleInvocationStack(getContext())"

LL_EXACT_AMBIG_DETECTION() ::= <<_interp.setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);>>
LL_EXACT_AMBIG_DETECTION() ::= <<getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);>>

ParserToken(parser, token) ::= <%<parser>.<token>%>

Expand All @@ -106,16 +106,16 @@ PositionAdjustingLexer() ::= <<

@Override
public Token nextToken() {
if (!(_interp instanceof PositionAdjustingLexerATNSimulator)) {
_interp = new PositionAdjustingLexerATNSimulator(this, _ATN, _decisionToDFA, _sharedContextCache);
if (!(getInterpreter() instanceof PositionAdjustingLexerATNSimulator)) {
setInterpreter(new PositionAdjustingLexerATNSimulator(this, _ATN, _decisionToDFA, _sharedContextCache));
}

return super.nextToken();
}

@Override
public Token emit() {
switch (_type) {
switch (getType()) {
case TOKENS:
handleAcceptPositionForKeyword("tokens");
break;
Expand All @@ -138,30 +138,27 @@ private boolean handleAcceptPositionForIdentifier() {
identifierLength++;
}

if (getInputStream().index() > _tokenStartCharIndex + identifierLength) {
if (getInputStream().index() > get_tokenStartCharIndex() + identifierLength) {
int offset = identifierLength - 1;
getInterpreter().resetAcceptPosition(getInputStream(), _tokenStartCharIndex + offset, _tokenStartLine, _tokenStartCharPositionInLine + offset);
PositionAdjustingLexerATNSimulator simulator = (PositionAdjustingLexerATNSimulator)getInterpreter();
simulator.resetAcceptPosition(getInputStream(), get_tokenStartCharIndex() + offset, get_tokenStartLine(), get_tokenStartCharPositionInLine() + offset);
return true;
}

return false;
}

private boolean handleAcceptPositionForKeyword(String keyword) {
if (getInputStream().index() > _tokenStartCharIndex + keyword.length()) {
if (getInputStream().index() > get_tokenStartCharIndex() + keyword.length()) {
int offset = keyword.length() - 1;
getInterpreter().resetAcceptPosition(getInputStream(), _tokenStartCharIndex + offset, _tokenStartLine, _tokenStartCharPositionInLine + offset);
PositionAdjustingLexerATNSimulator simulator = (PositionAdjustingLexerATNSimulator)getInterpreter();
simulator.resetAcceptPosition(getInputStream(), get_tokenStartCharIndex() + offset, get_tokenStartLine(), get_tokenStartCharPositionInLine() + offset);
return true;
}

return false;
}

@Override
public PositionAdjustingLexerATNSimulator getInterpreter() {
return (PositionAdjustingLexerATNSimulator)super.getInterpreter();
}

private static boolean isIdentifierChar(char c) {
return Character.isLetterOrDigit(c) || c == '_';
}
Expand All @@ -177,8 +174,8 @@ protected static class PositionAdjustingLexerATNSimulator extends LexerATNSimula

protected void resetAcceptPosition(CharStream input, int index, int line, int charPositionInLine) {
input.seek(index);
this.line = line;
this.charPositionInLine = charPositionInLine;
this.setLine(line);
this.setCharPositionInLine(charPositionInLine);
consume(input);
}

Expand Down Expand Up @@ -233,10 +230,10 @@ RuleGetterListener(X) ::= <<
public class LeafListener extends TBaseListener {
public void exitA(TParser.AContext ctx) {
if (ctx.getChildCount()==2) {
outStream.printf("%s %s %s\n",ctx.b(0).start.getText(),
ctx.b(1).start.getText(),ctx.b().get(0).start.getText());
outStream.printf("%s %s %s\n",ctx.b(0).getStart().getText(),
ctx.b(1).getStart().getText(),ctx.b().get(0).getStart().getText());
} else
outStream.println(ctx.b(0).start.getText());
outStream.println(ctx.b(0).getStart().getText());
}
}
}
Expand All @@ -248,8 +245,8 @@ LRListener(X) ::= <<
public class LeafListener extends TBaseListener {
public void exitE(TParser.EContext ctx) {
if (ctx.getChildCount()==3) {
outStream.printf("%s %s %s\n",ctx.e(0).start.getText(),
ctx.e(1).start.getText(), ctx.e().get(0).start.getText());
outStream.printf("%s %s %s\n",ctx.e(0).getStart().getText(),
ctx.e(1).getStart().getText(), ctx.e().get(0).getStart().getText());
} else
outStream.println(ctx.INT().getSymbol().getText());
}
Expand All @@ -261,7 +258,7 @@ LRWithLabelsListener(X) ::= <<
@parser::members {
public class LeafListener extends TBaseListener {
public void exitCall(TParser.CallContext ctx) {
outStream.printf("%s %s\n",ctx.e().start.getText(),ctx.eList());
outStream.printf("%s %s\n",ctx.e().getStart().getText(),ctx.eList());
}
public void exitInt(TParser.IntContext ctx) {
outStream.println(ctx.INT().getSymbol().getText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ ImportRuleInvocationStack() ::= ""

RuleInvocationStack() ::= "getRuleInvocationStack()"

LL_EXACT_AMBIG_DETECTION() ::= <<interpreter.predictionMode = PredictionMode.LL_EXACT_AMBIG_DETECTION>>
LL_EXACT_AMBIG_DETECTION() ::= <<interpreter!!.predictionMode = PredictionMode.LL_EXACT_AMBIG_DETECTION>>

ParserToken(parser, token) ::= <%<parser>.Tokens.<token>%>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
package org.antlr.v5.test.runtime;

import org.antlr.v5.Tool;
import org.antlr.v5.runtime.misc.Utils;
import org.antlr.v5.runtime.core.misc.Utils;
import org.antlr.v5.tool.ANTLRMessage;
import org.antlr.v5.tool.ANTLRToolListener;
import org.antlr.v5.tool.ToolMessage;
import org.stringtemplate.v4.ST;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ErrorQueue implements ANTLRToolListener {
public final Tool tool;
Expand Down Expand Up @@ -63,7 +64,7 @@ public String toString() {

public String toString(boolean rendered) {
if (!rendered) {
return Utils.join(all.iterator(), "\n");
return all.stream().map(Object::toString).collect(Collectors.joining("\n"));
}

if (tool == null) {
Expand Down
33 changes: 10 additions & 23 deletions runtime-testsuite/test/org/antlr/v5/test/runtime/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,27 @@

package org.antlr.v5.test.runtime;

import org.antlr.v5.runtime.misc.Utils;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.DosFileAttributes;

import static org.antlr.v5.test.runtime.RuntimeTestUtils.FileSeparator;

public class FileUtils {
public static void writeFile(String dir, String fileName, String content) {
try {
Utils.writeFile(dir + FileSeparator + fileName, content, "UTF-8");
}
catch (IOException ioe) {
System.err.println("can't write file");
ioe.printStackTrace(System.err);
}
}

public static String readFile(String dir, String fileName) {
public static void writeFile(String fileName, String content, String encoding) {
try {
return String.copyValueOf(Utils.readFile(dir+"/"+fileName, "UTF-8"));
}
catch (IOException ioe) {
System.err.println("can't read file");
File f = new File(fileName);
FileOutputStream fos = new FileOutputStream(f);
try(OutputStreamWriter osw = encoding != null ? new OutputStreamWriter(fos, encoding) : new OutputStreamWriter(fos)) {
osw.write(content);
}
} catch (IOException ioe) {
System.err.println("can't write file");
ioe.printStackTrace(System.err);
}
return null;
}

public static void replaceInFile(Path sourcePath, String target, String replacement) throws IOException {
Expand Down Expand Up @@ -72,7 +59,7 @@ public static void deleteDirectory(File f) throws IOException {
throw new IOException("Failed to delete file: " + f);
}

public static boolean isLink(Path path) throws IOException {
public static boolean isLink(Path path) {
try {
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
return attrs.isSymbolicLink() || (attrs instanceof DosFileAttributes && attrs.isOther());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -27,7 +29,8 @@ public static GeneratedState generate(RunOptions runOptions, String language, St
if (runOptions.slaveGrammars != null) {
for (String grammar : runOptions.slaveGrammars) {
GrammarFile slaveGrammarFile = parseGrammarFile(grammar);
writeFile(workingDirectory, slaveGrammarFile.grammarName + ".g4", slaveGrammarFile.content);
Path fullPath = Paths.get(workingDirectory, slaveGrammarFile.grammarName + ".g4");
writeFile(fullPath.toString() , slaveGrammarFile.content, null);
}
}

Expand All @@ -42,7 +45,8 @@ public static GeneratedState generate(RunOptions runOptions, String language, St
final List<String> options = new ArrayList<>();

for (GrammarFile grammarFile : grammarFiles) {
writeFile(workingDirectory, grammarFile.grammarName + ".g4", grammarFile.content);
Path fullPath = Paths.get(workingDirectory, grammarFile.grammarName + ".g4");
writeFile(fullPath.toString(), grammarFile.content, null);

if (grammarFile == mainFile) continue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public static String getRuntimePath(String language) {
}

public State run(RunOptions runOptions) {

GeneratedState generatedState = Generator.generate(runOptions, getLanguage(), getTempDirPath(), null);

if (generatedState.containsErrors() || runOptions.endStage == Stage.Generate) {
Expand All @@ -126,20 +127,28 @@ public State run(RunOptions runOptions) {
return new CompiledState(generatedState, new Exception(getTitleName() + " ANTLR runtime is not initialized"));
}

writeRecognizerFile(runOptions, generatedState);
try {
writeRecognizerFile(runOptions, generatedState);
} catch (IOException e) {
return new CompiledState(generatedState, e);
}

CompiledState compiledState = compile(runOptions, generatedState);

if (compiledState.containsErrors() || runOptions.endStage == Stage.Compile) {
return compiledState;
}

writeInputFile(runOptions);
try {
writeInputFile(runOptions);
} catch (IOException e) {
return new CompiledState(generatedState, e);
}

return execute(runOptions, compiledState);
}

protected void writeRecognizerFile(RunOptions runOptions, GeneratedState generatedState) {
protected void writeRecognizerFile(RunOptions runOptions, GeneratedState generatedState) throws IOException {
String text = RuntimeTestUtils.getTextFromResource("org/antlr/v5/test/runtime/helpers/" + getTestFileWithExt() + ".stg");
ST outputFileST = new ST(text);
outputFileST.add("grammarName", generatedState.getMainGrammarFile().grammarName);
Expand Down Expand Up @@ -206,7 +215,7 @@ protected CompiledState compile(RunOptions runOptions, GeneratedState generatedS
return new CompiledState(generatedState, null);
}

protected void writeInputFile(RunOptions runOptions) {
protected void writeInputFile(RunOptions runOptions) throws IOException {
writeFile(getTempDirPath(), InputFileName, runOptions.input);
}

Expand Down
Loading
Loading