Merge pull request #3077 from antlr/fix-javascript-intervalset

Fix javascript intervalset, fix #3075
This commit is contained in:
ericvergnaud 2021-02-13 11:52:23 +08:00 committed by GitHub
commit 9668ce7dbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 6046 additions and 15 deletions

View File

@ -14,11 +14,14 @@ import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Locale;
import java.util.logging.Logger;
import static org.junit.Assert.assertEquals;
@SuppressWarnings("ResultOfMethodCallIgnored")
public abstract class BaseRuntimeTestSupport implements RuntimeTestSupport {
// -J-Dorg.antlr.v4.test.BaseTest.level=FINE
@ -102,8 +105,7 @@ public abstract class BaseRuntimeTestSupport implements RuntimeTestSupport {
String prop = System.getProperty(propName);
if(prop!=null && prop.length()>0) {
tempTestDir = new File(prop);
}
else {
} else {
String dirName = getClass().getSimpleName() + "-" + Thread.currentThread().getName() + "-" + System.currentTimeMillis();
tempTestDir = new File(System.getProperty("java.io.tmpdir"), dirName);
}
@ -149,26 +151,36 @@ public abstract class BaseRuntimeTestSupport implements RuntimeTestSupport {
public static void eraseFilesInDir(File dir) {
String[] files = dir.list();
for(int i = 0; files!=null && i < files.length; i++) {
File file = new File(dir,files[i]);
if(file.isDirectory())
eraseDirectory(file);
else
file.delete();
try {
eraseFile(dir, files[i]);
} catch(IOException e) {
logger.info(e.getMessage());
}
}
}
private static void eraseFile(File dir, String name) throws IOException {
File file = new File(dir,name);
if(Files.isSymbolicLink(file.toPath()))
Files.delete(file.toPath());
else if(file.isDirectory())
eraseDirectory(file);
else
file.delete();
}
private static String detectedOS;
public static String getOS() {
if (detectedOS == null) {
String os = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH);
if ((os.indexOf("mac") >= 0) || (os.indexOf("darwin") >= 0)) {
if (os.contains("mac") || os.contains("darwin")) {
detectedOS = "mac";
}
else if (os.indexOf("win") >= 0) {
else if (os.contains("win")) {
detectedOS = "windows";
}
else if (os.indexOf("nux") >= 0) {
else if (os.contains("nux")) {
detectedOS = "linux";
}
else {

View File

@ -20,9 +20,10 @@
"@babel/core": "^7.12.9",
"@babel/preset-env": "^7.12.7",
"babel-loader": "^8.2.1",
"ini": "1.3.6",
"jest": "^26.6.3",
"webpack": "^4.44.2",
"webpack-cli": "^3.3.12",
"ini": "1.3.6"
"webpack-cli": "^3.3.12"
},
"scripts": {
"build": "webpack"

View File

@ -94,7 +94,7 @@ class IntervalSet {
reduce(k) {
// only need to reduce if k is not the last
if (k < this.intervalslength - 1) {
if (k < this.intervals.length - 1) {
const l = this.intervals[k];
const r = this.intervals[k + 1];
// if r contained in l
@ -103,7 +103,7 @@ class IntervalSet {
this.reduce(k);
} else if (l.stop >= r.start) {
this.intervals[k] = new Interval(l.start, r.stop);
this.intervals = this.intervals.splice(k + 1, 1);
this.intervals.splice(k + 1, 1);
}
}
}

View File

@ -0,0 +1,29 @@
import antlr4 from "../antlr4/index.js";
it("merges interval sets properly", () => {
const s1 = new antlr4.IntervalSet();
s1.addOne(10);
expect(s1.toString()).toEqual("10");
const s2 = new antlr4.IntervalSet();
s2.addOne(12);
expect(s2.toString()).toEqual("12");
const merged = new antlr4.IntervalSet();
merged.addSet(s1);
expect(merged.toString()).toEqual("10");
merged.addSet(s2);
expect(merged.toString()).toEqual("{10, 12}");
let s3 = new antlr4.IntervalSet();
s3.addOne(10);
merged.addSet(s3);
expect(merged.toString()).toEqual("{10, 12}");
s3 = new antlr4.IntervalSet();
s3.addOne(11);
merged.addSet(s3);
expect(merged.toString()).toEqual("10..12");
s3 = new antlr4.IntervalSet();
s3.addOne(12);
merged.addSet(s3);
expect(merged.toString()).toEqual("10..12");
});

View File

@ -25,4 +25,4 @@ module.exports = {
}
}]
}
}
};

5989
runtime/JavaScript/yarn.lock Normal file

File diff suppressed because it is too large Load Diff