Skip to content

Commit

Permalink
clean in initial configuration interfaces; no more clones of System.c…
Browse files Browse the repository at this point in the history
…onsole calls but IRascalMonitor knows how to build the right monitors and Evaluator knows how to link the monitor and the stdout streams if necessary
  • Loading branch information
jurgenvinju committed Apr 4, 2024
1 parent 3756142 commit 5f592ce
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 49 deletions.
29 changes: 29 additions & 0 deletions src/org/rascalmpl/debug/IRascalMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@
*******************************************************************************/
package org.rascalmpl.debug;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.function.Function;
import java.util.function.Supplier;

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

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

public interface IRascalMonitor {
/**
Expand Down Expand Up @@ -93,4 +101,25 @@ default void jobStep(String name, String message) {
* Inform (about a warning
*/
public void warning(String message, ISourceLocation src);

/**
* Convenience method will produce a monitor with ANSI progress bars if possible,
* and otherwise default to a dumn terminal console progress logger.
* @return
*/
@SuppressWarnings("unchecked")
public static <T extends IRascalMonitor> T buildConsoleMonitor(InputStream in, OutputStream out) {
return (T) ((System.console() != null)
? new TerminalProgressBarMonitor(out, in, TerminalFactory.get())
: new ConsoleRascalMonitor(new PrintStream(out))
);
}

/**
* Convenience method will produce a monitor that eats up all events without logging
* or reporting
*/
public default IRascalMonitor buildNullMonitor() {
return new NullRascalMonitor();
}
}
2 changes: 0 additions & 2 deletions src/org/rascalmpl/ideservices/BasicIDEServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.rascalmpl.debug.IRascalMonitor;
import org.rascalmpl.repl.TerminalProgressBarMonitor;
import org.rascalmpl.uri.URIResolverRegistry;
import org.rascalmpl.uri.URIUtil;

Expand Down
23 changes: 23 additions & 0 deletions src/org/rascalmpl/interpreter/Evaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@ public void decCallNesting() {

private static final Object dummy = new Object();

/**
* Promotes the monitor to the outputstream automatically if so required.
*/
public Evaluator(IValueFactory f, InputStream input, OutputStream stderr, OutputStream stdout, IRascalMonitor monitor, ModuleEnvironment scope, GlobalEnvironment heap) {
this(f, input, stderr, monitor instanceof OutputStream ? (OutputStream) monitor : stdout, scope, heap, new ArrayList<ClassLoader>(Collections.singleton(Evaluator.class.getClassLoader())), new RascalSearchPath());
setMonitor(monitor);
}

public Evaluator(IValueFactory f, InputStream input, OutputStream stderr, OutputStream stdout, ModuleEnvironment scope, GlobalEnvironment heap) {
this(f, input, stderr, stdout, scope, heap, new ArrayList<ClassLoader>(Collections.singleton(Evaluator.class.getClassLoader())), new RascalSearchPath());
}
Expand Down Expand Up @@ -304,6 +312,20 @@ private Evaluator(Evaluator source, ModuleEnvironment scope) {
setEventTrigger(AbstractInterpreterEventTrigger.newNullEventTrigger());
}

/**
* This constructor detects if we are running a terminal or not and changes streams and monitors
* accordingly
* @param valueFactory
* @param root
* @param heap2
*/
public Evaluator(IValueFactory vf, ModuleEnvironment root, GlobalEnvironment heap) {
this(vf, System.in, System.err, IRascalMonitor.buildConsoleMonitor(System.in, System.out), root, heap);
if (defStdout instanceof TerminalProgressBarMonitor) {
setMonitor((TerminalProgressBarMonitor) defStdout);
}
}

public void resetJavaBridge() {
this.javaBridge = new JavaBridge(classLoaders, vf, config);
}
Expand All @@ -317,6 +339,7 @@ public IRascalMonitor setMonitor(IRascalMonitor monitor) {
interrupt = false;
IRascalMonitor old = this.monitor;
this.monitor = monitor;

return old;
}

Expand Down
2 changes: 0 additions & 2 deletions src/org/rascalmpl/repl/TerminalProgressBarMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.time.Duration;
import java.time.Instant;
import java.util.LinkedList;
import java.util.List;

import org.fusesource.jansi.Ansi;
import org.rascalmpl.debug.IRascalMonitor;
import io.usethesource.vallang.ISourceLocation;
import jline.Terminal;
Expand Down
1 change: 0 additions & 1 deletion src/org/rascalmpl/shell/REPLRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.net.URISyntaxException;
import java.util.Map;

import org.rascalmpl.ideservices.BasicIDEServices;
import org.rascalmpl.ideservices.IDEServices;
import org.rascalmpl.interpreter.Evaluator;
import org.rascalmpl.repl.BaseREPL;
Expand Down
10 changes: 2 additions & 8 deletions src/org/rascalmpl/shell/RascalShell.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
import org.rascalmpl.debug.IRascalMonitor;
import org.rascalmpl.ideservices.BasicIDEServices;
import org.rascalmpl.ideservices.IDEServices;
import org.rascalmpl.interpreter.ConsoleRascalMonitor;
import org.rascalmpl.interpreter.utils.RascalManifest;
import org.rascalmpl.repl.TerminalProgressBarMonitor;

import jline.Terminal;
import jline.TerminalFactory;
import jline.TerminalSupport;
Expand Down Expand Up @@ -68,13 +65,10 @@ public static void main(String[] args) throws IOException {
term = new EclipseTerminalConnection(term, Integer.parseInt(sneakyRepl));
}

IRascalMonitor monitor
= System.console() != null
? new TerminalProgressBarMonitor(System.out, System.in, term)
: new ConsoleRascalMonitor();
IRascalMonitor monitor = IRascalMonitor.buildConsoleMonitor(System.in, System.out);

IDEServices services = new BasicIDEServices(new PrintWriter(System.err), monitor);
runner = new REPLRunner(System.in, System.err, System.console() != null ? (OutputStream) monitor : System.out, term, services);
runner = new REPLRunner(System.in, System.err, monitor instanceof OutputStream ? (OutputStream) monitor : System.out, term, services);
}
runner.run(args);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import jline.TerminalFactory;

import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.Runner;
Expand All @@ -40,7 +38,6 @@
import org.rascalmpl.interpreter.load.StandardLibraryContributor;
import org.rascalmpl.interpreter.result.AbstractFunction;
import org.rascalmpl.interpreter.utils.RascalManifest;
import org.rascalmpl.repl.TerminalProgressBarMonitor;
import org.rascalmpl.uri.URIUtil;
import org.rascalmpl.values.ValueFactoryFactory;

Expand All @@ -56,9 +53,7 @@
*
*/
public class RascalJUnitParallelRecursiveTestRunner extends Runner {
private final static IRascalMonitor monitor = System.console() != null
? new TerminalProgressBarMonitor(System.out, System.in, TerminalFactory.get())
: new NullRascalMonitor();
private final static IRascalMonitor monitor = IRascalMonitor.buildConsoleMonitor(System.in, System.out);

private final int numberOfWorkers;
private final Semaphore importsCompleted = new Semaphore(0);
Expand Down Expand Up @@ -306,9 +301,7 @@ private void initializeEvaluator() {
heap = new GlobalEnvironment();
root = heap.addModule(new ModuleEnvironment("___junit_test___", heap));

var outStream = System.console() != null ? (TerminalProgressBarMonitor) monitor : System.out;
evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, System.err, outStream, root, heap);
evaluator.setMonitor(monitor);
evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), root, heap);
evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance());
evaluator.getConfiguration().setErrors(true);

Expand Down
18 changes: 2 additions & 16 deletions src/org/rascalmpl/test/infrastructure/RascalJUnitTestRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.junit.runner.Runner;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;
import org.rascalmpl.debug.IRascalMonitor;
import org.rascalmpl.interpreter.Evaluator;
import org.rascalmpl.interpreter.ITestResultListener;
import org.rascalmpl.interpreter.NullRascalMonitor;
Expand All @@ -37,7 +36,6 @@
import org.rascalmpl.interpreter.utils.RascalManifest;
import org.rascalmpl.library.util.PathConfig;
import org.rascalmpl.library.util.PathConfig.RascalConfigMode;
import org.rascalmpl.repl.TerminalProgressBarMonitor;
import org.rascalmpl.shell.ShellEvaluatorFactory;
import org.rascalmpl.uri.URIResolverRegistry;
import org.rascalmpl.uri.URIUtil;
Expand All @@ -48,8 +46,6 @@

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

public class RascalJUnitTestRunner extends Runner {
private static Evaluator evaluator;
Expand All @@ -65,18 +61,8 @@ public class RascalJUnitTestRunner extends Runner {
try {
heap = new GlobalEnvironment();
root = heap.addModule(new ModuleEnvironment("___junit_test___", heap));

Terminal tm = TerminalFactory.get();
IRascalMonitor monitor = System.console() != null
? new TerminalProgressBarMonitor(System.out, System.in, tm)
: new NullRascalMonitor();

var outStream = System.console() != null
? (TerminalProgressBarMonitor) monitor
: System.out;

evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, System.err, outStream, root, heap);
evaluator.setMonitor(System.console() != null ? monitor : new NullRascalMonitor());
evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), root, heap);

evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance());
evaluator.getConfiguration().setErrors(true);
}
Expand Down
16 changes: 5 additions & 11 deletions src/org/rascalmpl/test/infrastructure/TestFramework.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,23 @@
import org.junit.After;
import org.rascalmpl.debug.IRascalMonitor;
import org.rascalmpl.interpreter.Evaluator;
import org.rascalmpl.interpreter.NullRascalMonitor;
import org.rascalmpl.interpreter.env.GlobalEnvironment;
import org.rascalmpl.interpreter.env.ModuleEnvironment;
import org.rascalmpl.interpreter.load.StandardLibraryContributor;
import org.rascalmpl.interpreter.result.Result;
import org.rascalmpl.interpreter.staticErrors.StaticError;
import org.rascalmpl.repl.TerminalProgressBarMonitor;
import org.rascalmpl.uri.URIResolverRegistry;
import org.rascalmpl.uri.URIUtil;
import io.usethesource.vallang.IBool;
import io.usethesource.vallang.ISourceLocation;
import io.usethesource.vallang.IValue;
import io.usethesource.vallang.exceptions.FactTypeUseException;
import io.usethesource.vallang.type.TypeFactory;
import jline.TerminalFactory;

import org.rascalmpl.values.ValueFactoryFactory;


public class TestFramework {
private final static IRascalMonitor monitor = System.console() != null
? new TerminalProgressBarMonitor(System.out, System.in, TerminalFactory.get())
: new NullRascalMonitor();
private final static IRascalMonitor monitor = IRascalMonitor.buildConsoleMonitor(System.in, System.out);

private final static Evaluator evaluator;
private final static GlobalEnvironment heap;
Expand All @@ -65,10 +59,10 @@ public class TestFramework {
heap = new GlobalEnvironment();
root = heap.addModule(new ModuleEnvironment("___test___", heap));

stderr = new PrintWriter(System.err, true);
stdout = System.console() != null ? new PrintWriter((TerminalProgressBarMonitor) monitor) : new PrintWriter(System.out, true);
evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, System.err, System.console() != null ? (TerminalProgressBarMonitor) monitor : System.out , root, heap);
evaluator.setMonitor(monitor);
evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, System.err, System.out, monitor, root, heap);

stdout = evaluator.getOutPrinter();
stderr = evaluator.getErrorPrinter();

evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance());
RascalJUnitTestRunner.configureProjectEvaluator(evaluator, RascalJUnitTestRunner.inferProjectRoot(TestFramework.class));
Expand Down

0 comments on commit 5f592ce

Please sign in to comment.