Added decorators for output text
This commit is contained in:
parent
ce1547d71a
commit
b3ae5c9122
|
@ -1,6 +1,7 @@
|
|||
package environment;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.function.Function;
|
||||
|
||||
import error.ErrorManager;
|
||||
|
||||
|
@ -16,6 +17,12 @@ public class RuntimeEnvironment {
|
|||
private InputStream input;
|
||||
private PrintStream output;
|
||||
private PrintStream errorOutput;
|
||||
|
||||
private Function<String, String> outputDecorator;
|
||||
private Function<String, String> valueOutputDecorator;
|
||||
private Function<String, String> warningOutputDecorator;
|
||||
private Function<String, String> errorOutputDecorator;
|
||||
private Function<String, String> criticalOutputDecorator;
|
||||
private Runnable terminationFunction;
|
||||
private Runnable errorTerminationFunction;
|
||||
private ErrorManager errorManager;
|
||||
|
@ -50,6 +57,26 @@ public class RuntimeEnvironment {
|
|||
this.errorManager = errorManager;
|
||||
}
|
||||
|
||||
public void setOutputDecorator(Function<String, String> outputDecorator) {
|
||||
this.outputDecorator = outputDecorator;
|
||||
}
|
||||
|
||||
public void setValueOutputDecorator(Function<String, String> valueOutputDecorator) {
|
||||
this.valueOutputDecorator = valueOutputDecorator;
|
||||
}
|
||||
|
||||
public void setWarningOutputDecorator(Function<String, String> warningOutputDecorator) {
|
||||
this.warningOutputDecorator = warningOutputDecorator;
|
||||
}
|
||||
|
||||
public void setErrorOutputDecorator(Function<String, String> errorOutputDecorator) {
|
||||
this.errorOutputDecorator = errorOutputDecorator;
|
||||
}
|
||||
|
||||
public void setCriticalOutputDecorator(Function<String, String> criticalOutputDecorator) {
|
||||
this.criticalOutputDecorator = criticalOutputDecorator;
|
||||
}
|
||||
|
||||
public String getInputName() {
|
||||
return inputName;
|
||||
}
|
||||
|
@ -78,4 +105,24 @@ public class RuntimeEnvironment {
|
|||
return errorManager;
|
||||
}
|
||||
|
||||
public String decorateOutput(String output) {
|
||||
return outputDecorator.apply(output);
|
||||
}
|
||||
|
||||
public String decorateValueOutput(String valueOutput) {
|
||||
return valueOutputDecorator.apply(valueOutput);
|
||||
}
|
||||
|
||||
public String decorateWarningOutput(String warningOutput) {
|
||||
return warningOutputDecorator.apply(warningOutput);
|
||||
}
|
||||
|
||||
public String decorateErrorOutput(String errorOutput) {
|
||||
return errorOutputDecorator.apply(errorOutput);
|
||||
}
|
||||
|
||||
public String decorateCriticalOutput(String criticalOutput) {
|
||||
return criticalOutputDecorator.apply(criticalOutput);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -12,11 +12,6 @@ import environment.RuntimeEnvironment;
|
|||
*/
|
||||
public class ErrorManager {
|
||||
|
||||
private static final String ANSI_RESET = "\u001B[0m";
|
||||
private static final String ANSI_RED = "\u001B[31m";
|
||||
private static final String ANSI_PURPLE = "\u001B[35m";
|
||||
private static final String ANSI_YELLOW = "\u001B[33m";
|
||||
|
||||
private RuntimeEnvironment environment;
|
||||
|
||||
public ErrorManager() {
|
||||
|
@ -44,10 +39,10 @@ public class ErrorManager {
|
|||
|
||||
private String formatMessage(LispException lispException) {
|
||||
Severity severity = lispException.getSeverity();
|
||||
String color = severity.getMessageColor();
|
||||
String prefix = severity.toDisplayString();
|
||||
String message = MessageFormat.format("{0}: {1}", prefix, lispException.getMessage());
|
||||
|
||||
return MessageFormat.format("{0}{1}: {2}{3}", color, prefix, lispException.getMessage(), ANSI_RESET);
|
||||
return severity.decorate(message, environment);
|
||||
}
|
||||
|
||||
private boolean isCritical(LispException lispException) {
|
||||
|
@ -57,8 +52,8 @@ public class ErrorManager {
|
|||
public static enum Severity {
|
||||
WARNING {
|
||||
|
||||
public String getMessageColor() {
|
||||
return ANSI_YELLOW;
|
||||
public String decorate(String warningOutput, RuntimeEnvironment environment) {
|
||||
return environment.decorateWarningOutput(warningOutput);
|
||||
}
|
||||
|
||||
public String toDisplayString() {
|
||||
|
@ -67,8 +62,8 @@ public class ErrorManager {
|
|||
},
|
||||
ERROR {
|
||||
|
||||
public String getMessageColor() {
|
||||
return ANSI_RED;
|
||||
public String decorate(String errorOutput, RuntimeEnvironment environment) {
|
||||
return environment.decorateErrorOutput(errorOutput);
|
||||
}
|
||||
|
||||
public String toDisplayString() {
|
||||
|
@ -77,8 +72,8 @@ public class ErrorManager {
|
|||
},
|
||||
CRITICAL {
|
||||
|
||||
public String getMessageColor() {
|
||||
return ANSI_PURPLE;
|
||||
public String decorate(String criticalOutput, RuntimeEnvironment environment) {
|
||||
return environment.decorateCriticalOutput(criticalOutput);
|
||||
}
|
||||
|
||||
public String toDisplayString() {
|
||||
|
@ -86,7 +81,7 @@ public class ErrorManager {
|
|||
}
|
||||
};
|
||||
|
||||
public abstract String getMessageColor();
|
||||
public abstract String decorate(String output, RuntimeEnvironment environment);
|
||||
|
||||
public abstract String toDisplayString();
|
||||
}
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package interpreter;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
|
||||
import environment.RuntimeEnvironment;
|
||||
import error.*;
|
||||
import function.builtin.EVAL;
|
||||
|
@ -10,9 +8,6 @@ import sexpression.SExpression;
|
|||
|
||||
public class LispInterpreter {
|
||||
|
||||
public static final String ANSI_RESET = "\u001B[0m";
|
||||
public static final String ANSI_GREEN = "\u001B[32m";
|
||||
|
||||
protected RuntimeEnvironment environment;
|
||||
protected ErrorManager errorManager;
|
||||
private LispParser parser;
|
||||
|
@ -47,7 +42,7 @@ public class LispInterpreter {
|
|||
|
||||
private void printValueOfNextSExpressionWithException() {
|
||||
SExpression sExpression = parser.getNextSExpression();
|
||||
String result = MessageFormat.format("{0}{1}{2}", ANSI_GREEN, EVAL.eval(sExpression), ANSI_RESET);
|
||||
String result = environment.decorateValueOutput(String.valueOf(EVAL.eval(sExpression)));
|
||||
|
||||
erasePrompt();
|
||||
environment.getOutput().println(result);
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package interpreter;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.function.Function;
|
||||
|
||||
public interface LispInterpreterBuilder {
|
||||
|
||||
|
@ -18,6 +19,16 @@ public interface LispInterpreterBuilder {
|
|||
|
||||
void useFile(String fileName);
|
||||
|
||||
void setOutputDecorator(Function<String, String> outputDecorator);
|
||||
|
||||
void setValueOutputDecorator(Function<String, String> valueOutputDecorator);
|
||||
|
||||
void setWarningOutputDecorator(Function<String, String> warningOutputDecorator);
|
||||
|
||||
void setErrorOutputDecorator(Function<String, String> errorOutputDecorator);
|
||||
|
||||
void setCriticalOutputDecorator(Function<String, String> criticalOutputDecorator);
|
||||
|
||||
LispInterpreter build();
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package interpreter;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.function.Function;
|
||||
|
||||
import environment.RuntimeEnvironment;
|
||||
import error.*;
|
||||
|
@ -19,6 +20,11 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder {
|
|||
private PrintStream errorOutputStream;
|
||||
private Runnable terminationFunction;
|
||||
private Runnable errorTerminationFunction;
|
||||
private Function<String, String> outputDecorator;
|
||||
private Function<String, String> valueOutputDecorator;
|
||||
private Function<String, String> warningOutputDecorator;
|
||||
private Function<String, String> errorOutputDecorator;
|
||||
private Function<String, String> criticalOutputDecorator;
|
||||
private RuntimeEnvironment environment;
|
||||
private boolean isInteractive;
|
||||
private boolean isBuilt;
|
||||
|
@ -28,6 +34,11 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder {
|
|||
this.inputName = "";
|
||||
this.isInteractive = true;
|
||||
this.isBuilt = false;
|
||||
this.outputDecorator = s -> s;
|
||||
this.valueOutputDecorator = s -> s;
|
||||
this.warningOutputDecorator = s -> s;
|
||||
this.errorOutputDecorator = s -> s;
|
||||
this.criticalOutputDecorator = s -> s;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -67,6 +78,33 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder {
|
|||
this.isInteractive = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOutputDecorator(Function<String, String> decorator) {
|
||||
this.outputDecorator = decorator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValueOutputDecorator(Function<String, String> decorator) {
|
||||
this.valueOutputDecorator = decorator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setWarningOutputDecorator(Function<String, String> decorator) {
|
||||
this.warningOutputDecorator = decorator;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setErrorOutputDecorator(Function<String, String> decorator) {
|
||||
this.errorOutputDecorator = decorator;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCriticalOutputDecorator(Function<String, String> decorator) {
|
||||
this.criticalOutputDecorator = decorator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LispInterpreter build() {
|
||||
if (!isBuilt)
|
||||
|
@ -91,6 +129,11 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder {
|
|||
environment.setTerminationFunction(terminationFunction);
|
||||
environment.setErrorTerminationFunction(errorTerminationFunction);
|
||||
environment.setErrorManager(errorManager);
|
||||
environment.setOutputDecorator(outputDecorator);
|
||||
environment.setValueOutputDecorator(valueOutputDecorator);
|
||||
environment.setWarningOutputDecorator(warningOutputDecorator);
|
||||
environment.setErrorOutputDecorator(errorOutputDecorator);
|
||||
environment.setCriticalOutputDecorator(criticalOutputDecorator);
|
||||
configureInput(errorManager);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +1,17 @@
|
|||
package main;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import interpreter.*;
|
||||
|
||||
public class LispMain {
|
||||
|
||||
private static final String ANSI_RESET = "\u001B[0m";
|
||||
private static final String ANSI_RED = "\u001B[31m";
|
||||
private static final String ANSI_GREEN = "\u001B[32m";
|
||||
private static final String ANSI_YELLOW = "\u001B[33m";
|
||||
private static final String ANSI_PURPLE = "\u001B[35m";
|
||||
|
||||
private LispMain() {}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@ -13,11 +21,15 @@ public class LispMain {
|
|||
|
||||
private static LispInterpreter buildInterpreter(String[] args) {
|
||||
LispInterpreterBuilder builder = LispInterpreterBuilderImpl.getInstance();
|
||||
configureInput(args, builder);
|
||||
builder.setOutput(System.out);
|
||||
builder.setErrorOutput(System.err);
|
||||
builder.setTerminationFunction(() -> System.exit(0));
|
||||
builder.setErrorTerminationFunction(() -> System.exit(1));
|
||||
configureInput(args, builder);
|
||||
builder.setValueOutputDecorator(makeColorDecorator(ANSI_GREEN));
|
||||
builder.setWarningOutputDecorator(makeColorDecorator(ANSI_YELLOW));
|
||||
builder.setErrorOutputDecorator(makeColorDecorator(ANSI_RED));
|
||||
builder.setCriticalOutputDecorator(makeColorDecorator(ANSI_PURPLE));
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
@ -31,4 +43,14 @@ public class LispMain {
|
|||
}
|
||||
}
|
||||
|
||||
private static Function<String, String> makeColorDecorator(String color) {
|
||||
return new Function<String, String>() {
|
||||
|
||||
@Override
|
||||
public String apply(String s) {
|
||||
return color + s + ANSI_RESET;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -29,6 +29,9 @@ public class ErrorManagerTester {
|
|||
environment.setErrorTerminationFunction(() -> indicatorSet.add(TERMINATED));
|
||||
environment.setErrorOutput(new PrintStream(errorOutputStream));
|
||||
environment.setOutput(new PrintStream(outputStream));
|
||||
environment.setWarningOutputDecorator(s -> s);
|
||||
environment.setErrorOutputDecorator(s -> s);
|
||||
environment.setCriticalOutputDecorator(s -> s);
|
||||
|
||||
return new ErrorManager();
|
||||
}
|
||||
|
|
|
@ -45,6 +45,9 @@ public class LOADTester {
|
|||
environment.setOutput(new PrintStream(outputStream));
|
||||
environment.setErrorOutput(new PrintStream(errorOutputStream));
|
||||
environment.setErrorManager(new ErrorManager());
|
||||
environment.setOutputDecorator(s -> s);
|
||||
environment.setWarningOutputDecorator(s -> s);
|
||||
environment.setErrorOutputDecorator(s -> s);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -31,6 +31,7 @@ public class DEFUNTester {
|
|||
|
||||
environment.setOutput(new PrintStream(outputStream));
|
||||
environment.setErrorManager(new ErrorManager());
|
||||
environment.setWarningOutputDecorator(s -> s);
|
||||
|
||||
FunctionTable.reset();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue