Skip to content

Commit

Permalink
better handling of System.console
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenvinju committed Apr 4, 2024
1 parent 5f592ce commit 1c6fad5
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/org/rascalmpl/checker/StaticChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class StaticChecker {
public StaticChecker(OutputStream stderr, OutputStream stdout) {
GlobalEnvironment heap = new GlobalEnvironment();
ModuleEnvironment root = heap.addModule(new ModuleEnvironment("$staticchecker$", heap));
eval = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, stderr, stdout, root, heap);
eval = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, stderr, stdout, root, heap, IRascalMonitor.buildConsoleMonitor(System.in, System.out));

Check warning on line 48 in src/org/rascalmpl/checker/StaticChecker.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/checker/StaticChecker.java#L48

Added line #L48 was not covered by tests
eval.addRascalSearchPathContributor(StandardLibraryContributor.getInstance());
checkerEnabled = false;
initialized = false;
Expand Down
7 changes: 3 additions & 4 deletions src/org/rascalmpl/debug/IRascalMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,11 @@ default void jobStep(String name, String message) {
* 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)
public static IRascalMonitor buildConsoleMonitor(InputStream in, OutputStream out) {
return System.console() != null
? new TerminalProgressBarMonitor(out, in, TerminalFactory.get())

Check warning on line 112 in src/org/rascalmpl/debug/IRascalMonitor.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/debug/IRascalMonitor.java#L112

Added line #L112 was not covered by tests
: new ConsoleRascalMonitor(new PrintStream(out))
);
;
}

/**
Expand Down
17 changes: 11 additions & 6 deletions src/org/rascalmpl/interpreter/Evaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,19 @@ public void decCallNesting() {
*/
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());
}

/**
* If your monitor should wrap stdout (like TerminalProgressBarMonitor) then you can use this constructor.
*/
public <M extends OutputStream & IRascalMonitor> Evaluator(IValueFactory f, InputStream input, OutputStream stderr, M monitor, ModuleEnvironment scope, GlobalEnvironment heap) {
this(f, input, stderr, monitor, scope, heap, new ArrayList<ClassLoader>(Collections.singleton(Evaluator.class.getClassLoader())), new RascalSearchPath());
setMonitor(monitor);
}

Check warning on line 246 in src/org/rascalmpl/interpreter/Evaluator.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/Evaluator.java#L244-L246

Added lines #L244 - L246 were not covered by tests

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());
public Evaluator(IValueFactory f, InputStream input, OutputStream stderr, OutputStream stdout, ModuleEnvironment scope, GlobalEnvironment heap, IRascalMonitor monitor) {
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 vf, InputStream input, OutputStream stderr, OutputStream stdout, ModuleEnvironment scope, GlobalEnvironment heap, List<ClassLoader> classLoaders, RascalSearchPath rascalPathResolver) {
Expand Down Expand Up @@ -320,10 +328,7 @@ private Evaluator(Evaluator source, ModuleEnvironment scope) {
* @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);
}
this(vf, System.in, System.err, System.out, IRascalMonitor.buildConsoleMonitor(System.in, System.out), root, heap);
}

public void resetJavaBridge() {
Expand Down
4 changes: 3 additions & 1 deletion src/org/rascalmpl/interpreter/JavaToRascal.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.ArrayList;
import java.util.Hashtable;

import org.rascalmpl.debug.IRascalMonitor;
import org.rascalmpl.interpreter.env.Environment;
import org.rascalmpl.interpreter.env.GlobalEnvironment;
import org.rascalmpl.interpreter.env.ModuleEnvironment;
Expand Down Expand Up @@ -63,7 +64,8 @@ public Evaluator getEvaluator() {

public JavaToRascal(InputStream input, OutputStream stdout, OutputStream stderr) {
this.evaluator = new Evaluator(vf, input, stderr, stdout,
new ModuleEnvironment(ModuleEnvironment.SHELL_MODULE, heap), heap);
new ModuleEnvironment(ModuleEnvironment.SHELL_MODULE, heap), heap,
IRascalMonitor.buildConsoleMonitor(input, stdout));

Check warning on line 68 in src/org/rascalmpl/interpreter/JavaToRascal.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/interpreter/JavaToRascal.java#L68

Added line #L68 was not covered by tests
}

public JavaToRascal(Evaluator evaluator) {
Expand Down
5 changes: 3 additions & 2 deletions src/org/rascalmpl/library/util/Eval.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.HashMap;
import java.util.Map;

import org.rascalmpl.debug.IRascalMonitor;
import org.rascalmpl.exceptions.RuntimeExceptionFactory;
import org.rascalmpl.exceptions.Throw;
import org.rascalmpl.interpreter.Evaluator;
Expand Down Expand Up @@ -65,15 +66,15 @@ public class Eval {



public Eval(IValueFactory values, OutputStream out, OutputStream err, InputStream in, ClassLoader loader) {
public Eval(IValueFactory values, OutputStream out, OutputStream err, InputStream in, ClassLoader loader, IRascalMonitor monitor) {
super();
this.values = values;
this.tr = new TypeReifier(values);
this.duration = values.integer(1000*100); // default duration for eval

GlobalEnvironment heap = new GlobalEnvironment();
ModuleEnvironment root = new ModuleEnvironment("$eval$", heap);
this.eval = new Evaluator(values, in, err, out, root, heap);
this.eval = new Evaluator(values, in, err, out, root, heap, monitor);

Check warning on line 77 in src/org/rascalmpl/library/util/Eval.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/util/Eval.java#L77

Added line #L77 was not covered by tests

// TODO: this is to fix the course Question compiler with a workaround.
// it would be better to parameterize eval with a PathConfig.
Expand Down
9 changes: 5 additions & 4 deletions src/org/rascalmpl/library/util/Reflective.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import java.io.Writer;
import java.net.URISyntaxException;

import org.rascalmpl.debug.IRascalMonitor;
import org.rascalmpl.exceptions.RuntimeExceptionFactory;
import org.rascalmpl.interpreter.ConsoleRascalMonitor;
import org.rascalmpl.interpreter.Evaluator;
import org.rascalmpl.interpreter.IEvaluator;
import org.rascalmpl.interpreter.env.GlobalEnvironment;
Expand Down Expand Up @@ -69,10 +69,12 @@

public class Reflective {
protected final IValueFactory values;
private final IRascalMonitor monitor;

public Reflective(IValueFactory values){
public Reflective(IValueFactory values, IRascalMonitor monitor) {
super();
this.values = values;
this.monitor = monitor;
}

public IString getRascalVersion() {
Expand Down Expand Up @@ -101,9 +103,8 @@ IEvaluator<?> getDefaultEvaluator(OutputStream stdout, OutputStream stderr) {
GlobalEnvironment heap = new GlobalEnvironment();
ModuleEnvironment root = heap.addModule(new ModuleEnvironment(ModuleEnvironment.SHELL_MODULE, heap));
IValueFactory vf = ValueFactoryFactory.getValueFactory();
Evaluator evaluator = new Evaluator(vf, System.in, stderr, stdout, root, heap);
Evaluator evaluator = new Evaluator(vf, System.in, stderr, stdout, root, heap, monitor);

Check warning on line 106 in src/org/rascalmpl/library/util/Reflective.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/util/Reflective.java#L106

Added line #L106 was not covered by tests
evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance());
evaluator.setMonitor(new ConsoleRascalMonitor());
return evaluator;
}

Expand Down
2 changes: 1 addition & 1 deletion src/org/rascalmpl/parser/ParserGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class ParserGenerator {
public ParserGenerator(IRascalMonitor monitor, OutputStream out, List<ClassLoader> loaders, IValueFactory factory, Configuration config) {
GlobalEnvironment heap = new GlobalEnvironment();
ModuleEnvironment scope = new ModuleEnvironment("$parsergenerator$", heap);
this.evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, out, out, scope,heap);
this.evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, out, out, scope, heap, monitor);
this.evaluator.getConfiguration().setRascalJavaClassPathProperty(config.getRascalJavaClassPathProperty());
this.evaluator.getConfiguration().setGeneratorProfiling(config.getGeneratorProfilingProperty());
evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance());
Expand Down
9 changes: 3 additions & 6 deletions src/org/rascalmpl/shell/ShellEvaluatorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.io.OutputStream;
import java.net.URISyntaxException;

import org.rascalmpl.interpreter.ConsoleRascalMonitor;
import org.rascalmpl.debug.IRascalMonitor;
import org.rascalmpl.interpreter.Evaluator;
import org.rascalmpl.interpreter.env.GlobalEnvironment;
import org.rascalmpl.interpreter.env.ModuleEnvironment;
Expand All @@ -31,10 +31,9 @@ public static Evaluator getDefaultEvaluator(InputStream input, OutputStream stdo
GlobalEnvironment heap = new GlobalEnvironment();
ModuleEnvironment root = heap.addModule(new ModuleEnvironment(ModuleEnvironment.SHELL_MODULE, heap));
IValueFactory vf = ValueFactoryFactory.getValueFactory();
Evaluator evaluator = new Evaluator(vf, input, stderr, stdout, root, heap);
Evaluator evaluator = new Evaluator(vf, input, stderr, stdout, root, heap, IRascalMonitor.buildConsoleMonitor(input, stdout));

Check warning on line 34 in src/org/rascalmpl/shell/ShellEvaluatorFactory.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/shell/ShellEvaluatorFactory.java#L34

Added line #L34 was not covered by tests
evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance());

evaluator.setMonitor(new ConsoleRascalMonitor());
URIResolverRegistry reg = URIResolverRegistry.getInstance();

if (!reg.getRegisteredInputSchemes().contains("project") && !reg.getRegisteredLogicalSchemes().contains("project")) {
Expand All @@ -51,11 +50,9 @@ public static Evaluator getDefaultEvaluatorForLocation(File fileOrFolderInProjec
GlobalEnvironment heap = new GlobalEnvironment();
ModuleEnvironment root = heap.addModule(new ModuleEnvironment(ModuleEnvironment.SHELL_MODULE, heap));
IValueFactory vf = ValueFactoryFactory.getValueFactory();
Evaluator evaluator = new Evaluator(vf, input, stderr, stdout, root, heap);
Evaluator evaluator = new Evaluator(vf, input, stderr, stdout, root, heap, IRascalMonitor.buildConsoleMonitor(input, stdout));

Check warning on line 53 in src/org/rascalmpl/shell/ShellEvaluatorFactory.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/shell/ShellEvaluatorFactory.java#L53

Added line #L53 was not covered by tests
evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance());

evaluator.setMonitor(new ConsoleRascalMonitor());

ISourceLocation rootFolder = inferProjectRoot(fileOrFolderInProject);
if (rootFolder != null) {
configureProjectEvaluator(evaluator, rootFolder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ public class ModuleTester extends Thread {
private GlobalEnvironment heap;
private ModuleEnvironment root;

private PrintWriter stderr = new PrintWriter(System.err);
private PrintWriter stdout = new PrintWriter(System.out);
private PrintWriter stderr;
private PrintWriter stdout;
private Evaluator evaluator;
private final List<Description> testModules = new ArrayList<>();

Expand Down Expand Up @@ -300,8 +300,11 @@ public Class<? extends Annotation> annotationType() {
private void initializeEvaluator() {
heap = new GlobalEnvironment();
root = heap.addModule(new ModuleEnvironment("___junit_test___", heap));

evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, System.err, System.out, monitor, root, heap);
stdout = new PrintWriter(evaluator.getStdOut());
stderr = new PrintWriter(evaluator.getStdErr());

evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), root, heap);
evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance());
evaluator.getConfiguration().setErrors(true);

Expand Down
3 changes: 2 additions & 1 deletion test/org/rascalmpl/MatchFingerprintTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.IOException;
import java.io.StringReader;

import org.rascalmpl.debug.IRascalMonitor;
import org.rascalmpl.interpreter.Evaluator;
import org.rascalmpl.interpreter.env.GlobalEnvironment;
import org.rascalmpl.interpreter.env.ModuleEnvironment;
Expand Down Expand Up @@ -46,7 +47,7 @@
public class MatchFingerprintTest extends TestCase {
private final GlobalEnvironment heap = new GlobalEnvironment();
private final ModuleEnvironment root = new ModuleEnvironment("root", heap);
private final Evaluator eval = new Evaluator(IRascalValueFactory.getInstance(), System.in, System.err, System.out, root, heap);
private final Evaluator eval = new Evaluator(IRascalValueFactory.getInstance(), System.in, System.err, System.out, root, heap, IRascalMonitor.buildConsoleMonitor(System.in, System.out));
private final RascalFunctionValueFactory VF = eval.getFunctionValueFactory();
private final TypeFactory TF = TypeFactory.getInstance();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ private static Evaluator freshEvaluator() {
var heap = new GlobalEnvironment();
var root = heap.addModule(new ModuleEnvironment("___test___", heap));

var evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, System.err, System.out, root, heap);
evaluator.setMonitor(monitor);
var evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, System.err, System.out, root, heap, monitor);

evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance());
evaluator.setTestResultListener(new ITestResultListener() {
@Override
Expand Down

0 comments on commit 1c6fad5

Please sign in to comment.