Skip to content

Commit

Permalink
Merge branch 'main' into json-lazy-parser-unparser
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenvinju committed Jun 27, 2024
2 parents 149e546 + 78f67d0 commit 7b320a2
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 106 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.rascalmpl</groupId>
<artifactId>rascal</artifactId>
<version>0.40.3-SNAPSHOT</version>
<version>0.40.3-RC2-SNAPSHOT</version>
<packaging>jar</packaging>

<scm>
Expand Down Expand Up @@ -32,7 +32,7 @@
<exec.mainClass>org.rascalmpl.shell.RascalShell</exec.mainClass>
<rascal.test.memory>2</rascal.test.memory>
<maven.compiler.release>11</maven.compiler.release>
<rascal-maven.version>0.27.1-BOOT3</rascal-maven.version>
<rascal-maven.version>0.27.3</rascal-maven.version>
</properties>


Expand Down
23 changes: 16 additions & 7 deletions src/org/rascalmpl/debug/IRascalMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
import java.util.function.Function;
import java.util.function.Supplier;

import org.rascalmpl.interpreter.ConsoleRascalMonitor;
import org.rascalmpl.interpreter.BatchProgressMonitor;
import org.rascalmpl.interpreter.NullRascalMonitor;
import org.rascalmpl.repl.TerminalProgressBarMonitor;

import io.usethesource.vallang.ISourceLocation;
import jline.Terminal;
import jline.TerminalFactory;

public interface IRascalMonitor {
Expand Down Expand Up @@ -153,18 +154,26 @@ default void jobStep(String name, String message) {
public void warning(String message, ISourceLocation src);

/**
* Convenience method will produce a monitor with ANSI progress bars if possible,
* Convenience method will produce a monitor with ANSI progress bars if not in batch mode,
* and otherwise default to a dumn terminal console progress logger.
* @return
*/
public static IRascalMonitor buildConsoleMonitor(InputStream in, OutputStream out) {
return buildConsoleMonitor(in, out, System.console() != null);
return buildConsoleMonitor(in, out, inBatchMode());
}

public static IRascalMonitor buildConsoleMonitor(InputStream in, OutputStream out, boolean ansiEnabled) {
return ansiEnabled
? new TerminalProgressBarMonitor(out, in, TerminalFactory.get())
: new ConsoleRascalMonitor(new PrintStream(out))
public static boolean inBatchMode() {
return "true".equals(System.getenv("CI"))
|| System.getProperty("rascal.monitor.batch") != null
;
}

public static IRascalMonitor buildConsoleMonitor(InputStream in, OutputStream out, boolean batchMode) {
Terminal terminal = TerminalFactory.get();

return !batchMode && terminal.isAnsiSupported()
? new TerminalProgressBarMonitor(out, in, terminal)
: new BatchProgressMonitor(new PrintStream(out))
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,31 @@

import io.usethesource.vallang.ISourceLocation;

public class ConsoleRascalMonitor implements IRascalMonitor {
public class BatchProgressMonitor implements IRascalMonitor {
PrintWriter out;

public ConsoleRascalMonitor() {
public BatchProgressMonitor() {
this.out = new PrintWriter(System.err);
}

public ConsoleRascalMonitor(PrintStream out) {
public BatchProgressMonitor(PrintStream out) {
this.out = new PrintWriter(out);
}

@Override
public int jobEnd(String name, boolean succeeded) {
out.println("\tJob done: " + name);
return 0;
}

@Override
public void jobStep(String name, String msg, int inc) {
out.println(name);
out.flush();
// not printing intermediate steps to keep the logs clean.
}

@Override
public void jobStart(String name, int workShare, int totalWork) {
out.println(name);
out.println("Job started: " + name);
out.flush();
}

Expand Down
41 changes: 35 additions & 6 deletions src/org/rascalmpl/interpreter/Evaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public void decCallNesting() {
/**
* True if we're doing profiling
*/
private static boolean doProfiling = false;
private boolean doProfiling = false;

/**
* Track if we already started a profiler, to avoid starting duplicates after a callback to an eval function.
Expand Down Expand Up @@ -888,7 +888,7 @@ public Result<IValue> eval(Statement stat) {
__setInterrupt(false);
try {
Profiler profiler = null;
if (Evaluator.doProfiling && !profilerRunning) {
if (doProfiling && !profilerRunning) {
profiler = new Profiler(this);
profiler.start();
profilerRunning = true;
Expand Down Expand Up @@ -1092,7 +1092,7 @@ public Result<IValue> eval(IRascalMonitor monitor, Command command) {
private Result<IValue> eval(Commands commands) {
__setInterrupt(false);
Profiler profiler = null;
if (Evaluator.doProfiling && !profilerRunning) {
if (doProfiling && !profilerRunning) {
profiler = new Profiler(this);
profiler.start();
profilerRunning = true;
Expand All @@ -1115,7 +1115,7 @@ private Result<IValue> eval(Commands commands) {
private Result<IValue> eval(Command command) {
__setInterrupt(false);
Profiler profiler = null;
if (Evaluator.doProfiling && !profilerRunning) {
if (doProfiling && !profilerRunning) {
profiler = new Profiler(this);
profiler.start();
profilerRunning = true;
Expand Down Expand Up @@ -1166,6 +1166,13 @@ public Result<IValue> eval(IRascalMonitor monitor, Declaration declaration) {
}
}

/**
* This starts a comprehensive batch of imports with its
* own monitoring job. The bar will grow as new modules
* are discovered to be recursively imported or extended.
* @param monitor
* @param string
*/
public void doImport(IRascalMonitor monitor, String... string) {
IRascalMonitor old = setMonitor(monitor);
interrupt = false;
Expand All @@ -1184,6 +1191,28 @@ public void doImport(IRascalMonitor monitor, String... string) {
}
}

/**
* This is if a LOADING_JOB_CONSTANT-named job is already running, and
* we need to execute the next few imports. It assumed at least names.length
* work is on the todo list for the monitor. The todo work will grow
* as recursively imported or extended modules are discovered.
*/
public void doNextImport(String jobName, String... names) {
IRascalMonitor old = setMonitor(monitor);
interrupt = false;
try {
ISourceLocation uri = URIUtil.rootLocation("import");
for (String module : names) {
monitor.jobStep(jobName, "Starting on " + module);
org.rascalmpl.semantics.dynamic.Import.importModule(module, uri, this);
}
}
finally {
setMonitor(old);
setCurrentAST(null);
}
}

public Set<String> reloadModules(IRascalMonitor monitor, Set<String> names, ISourceLocation errorLocation) {
Set<String> reloaded = new HashSet<>();
reloadModules(monitor, names, errorLocation, true, reloaded);
Expand Down Expand Up @@ -1440,7 +1469,7 @@ public ITree parseModuleAndFragments(IRascalMonitor monitor, String jobName, cha

@Override
public void updateProperties() {
Evaluator.doProfiling = config.getProfilingProperty();
doProfiling = config.getProfilingProperty();

setCallTracing(config.getTracingProperty());
}
Expand Down Expand Up @@ -1573,7 +1602,7 @@ public void revertToDefaultWriters() {

public Result<IValue> call(IRascalMonitor monitor, ICallableValue fun, Type[] argTypes, IValue[] argValues, Map<String, IValue> keyArgValues) {
Profiler profiler = null;
if (Evaluator.doProfiling && !profilerRunning) {
if (doProfiling && !profilerRunning) {
profiler = new Profiler(this);
profiler.start();
profilerRunning = true;
Expand Down
43 changes: 8 additions & 35 deletions src/org/rascalmpl/library/Location.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ This conversion supports generic Unix path syntax, including:
loc locFromUnixPath(str path) = parseUnixPath(path);

@synopsis{Check that two locations refer to the same file.}
bool isSameFile(loc l, loc r) = l.top[fragment=""] == r.top[fragment=""];
@javaClass{org.rascalmpl.library.Prelude}
java bool isSameFile(loc l, loc r);

@synopsis{Compare two location values lexicographically.}
@description{
Expand Down Expand Up @@ -100,22 +101,8 @@ Strict containment between two locations `inner` and `outer` holds when
- both.
}

bool isStrictlyContainedIn(loc inner, loc outer){
if(inner == outer){
return false;
}
if(isSameFile(inner, outer)){
if(inner.offset?){
if(outer.offset?){
return inner.offset == outer.offset && inner.offset + inner.length < outer.offset + outer.length
|| inner.offset > outer.offset && inner.offset + inner.length <= outer.offset + outer.length;
} else {
return inner.offset > 0;
}
}
}
return false;
}
@javaClass{org.rascalmpl.library.Prelude}
java bool isStrictlyContainedIn(loc inner, loc outer);


@synopsis{Is a location textually contained in another location?}
Expand All @@ -127,20 +114,8 @@ Containment between two locations `inner` and `outer` holds when
- `inner` is strictly contained in `outer`.
}

bool isContainedIn(loc inner, loc outer){
if(isSameFile(inner, outer)){
if(inner.offset?){
if(outer.offset?){
return (inner.offset >= outer.offset && inner.offset + inner.length <= outer.offset + outer.length);
} else {
return true;
}
} else {
return !outer.offset?;
}
}
return false;
}
@javaClass{org.rascalmpl.library.Prelude}
java bool isContainedIn(loc inner, loc outer);


@synopsis{Begins a location's text before (but may overlap with) another location's text?}
Expand Down Expand Up @@ -184,10 +159,8 @@ bool isImmediatelyAfter(loc l, loc r)


@synopsis{Refer two locations to text that overlaps?}
bool isOverlapping(loc l, loc r)
= isSameFile(l, r) && ( (l.offset <= r.offset && l.offset + l.length > r.offset)
|| (r.offset <= l.offset && r.offset + r.length > l.offset)
);
@javaClass{org.rascalmpl.library.Prelude}
java bool isOverlapping(loc l, loc r);


@synopsis{Compute a location that textually covers the text of a list of locations.}
Expand Down
89 changes: 89 additions & 0 deletions src/org/rascalmpl/library/Prelude.java
Original file line number Diff line number Diff line change
Expand Up @@ -3972,5 +3972,94 @@ public void unwatch(ISourceLocation src, IBool recursive, IFunction callback) {
}


// this fact that this is just a java function helps the jitter with inline it
private static boolean isSameFilePure(ISourceLocation a, ISourceLocation b) {
a = a.top();
b = b.top();
if (!a.hasFragment() && !b.hasFragment()) {
// fast path: use equals of ISourceLocations
return a.equals(b);
}
// fallback, just compare everything except the fragment
return a.getScheme().equals(b.getScheme())
&& a.getAuthority().equals(b.getAuthority())
&& a.getPath().equals(b.getPath())
&& a.getQuery().equals(b.getQuery())
;
}

public IBool isSameFile(ISourceLocation a, ISourceLocation b) {
return values.bool(isSameFilePure(a, b));
}

public IBool isStrictlyContainedIn(ISourceLocation inner, ISourceLocation outer) {
if (!isSameFilePure(inner, outer)) {
return values.bool(false);
}
// original code would also do full equality, but we don't need that due to the logic in the outer &
// inner offset compares
if (inner.hasOffsetLength()) {
if (outer.hasOffsetLength()) {
int innerStart = inner.getOffset();
int innerEnd = innerStart + inner.getLength();
int outerStart = outer.getOffset();
int outerEnd = outerStart + outer.getLength();

return values.bool(
(innerStart == outerStart && innerEnd < outerEnd)
|| (innerStart > outerStart && innerEnd <= outerEnd)
);
}
else {
return values.bool(inner.getLength() > 0);
}
}
return values.bool(false);
}

public IBool isContainedIn(ISourceLocation inner, ISourceLocation outer) {
if (!isSameFilePure(inner, outer)) {
return values.bool(false);
}
if (inner.hasOffsetLength()) {
if (outer.hasOffsetLength()) {
int innerStart = inner.getOffset();
int innerEnd = innerStart + inner.getLength();
int outerStart = outer.getOffset();
int outerEnd = outerStart + outer.getLength();

return values.bool(
outerStart <= innerStart && innerEnd <= outerEnd
);
}
return values.bool(true);
}

return values.bool(!outer.hasOffsetLength());
}

public IBool isOverlapping(ISourceLocation first, ISourceLocation second) {
if (!isSameFilePure(first, second)) {
return values.bool(false);
}
if (first.hasOffsetLength()) {
if (second.hasOffsetLength()) {
int firstStart = first.getOffset();
int firstEnd = firstStart + first.getLength();
int secondStart = second.getOffset();
int secondEnd = secondStart + second.getLength();

return values.bool(
(firstStart <= secondStart && secondStart <= firstEnd)
|| (secondStart <= firstStart && firstStart <= secondEnd)
);

}
}
return values.bool(true);

}


}

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ str getParserMethodName(conditional(Symbol s, _)) = getParserMethodName(s);
default str getParserMethodName(Symbol s) = value2id(s);

public str newGenerate(str package, str name, Grammar gr) {
return job("Generating <name>", str (void (str m, int w) worked) {
return job("Generating parser; <for (st <- gr.rules, st is sort || st is lex) {><type(st,())> <}>"[..-1], str (void (str m, int w) worked) {
int uniqueItem = 1; // -1 and -2 are reserved by the SGTDBF implementation
int newItem() { uniqueItem += 1; return uniqueItem; };

Expand Down
Loading

0 comments on commit 7b320a2

Please sign in to comment.