THashMap<String,Type> variableTypes = new THashMap<String,Type>();
PrintStream fileOutput;
-
+
+ /**
+ * Only checks the commands for compilation errors but does not run them.
+ */
+ private boolean validateOnly;
+
public CommandSession(ModuleRepository moduleRepository, SCLReportingHandler handler) {
this.moduleRepository = moduleRepository;
this.defaultHandler = new PrintDecorator(
Function command = (Function)evaluator
.localEnvironment(localEnvironment)
.decorateExpression(true)
+ .validateOnly(validateOnly)
.eval();
return new CompiledCommand(command, evaluator.getType());
}
handler.printCommand(reader.extractString(expression.location));
command = compile(expression);
} catch (SCLExpressionCompilationException e) {
- CompilationError[] errors = ((SCLExpressionCompilationException)e).getErrors();
+ if(validateOnly)
+ throw e;
+ CompilationError[] errors = e.getErrors();
for(CompilationError error : errors) {
if(error.location != Locations.NO_LOCATION)
handler.printError(reader.locationUnderlining(error.location));
}
reader.forgetEverythingBefore(Locations.endOf(expression.location));
- Object resultValue = command.command.apply(variableValues);
- String resultString = toString(resultValue, command.type);
- if(!resultString.isEmpty())
- handler.print(resultString);
+ if(!validateOnly) {
+ Object resultValue = command.command.apply(variableValues);
+ String resultString = toString(resultValue, command.type);
+ if(!resultString.isEmpty())
+ handler.print(resultString);
+ }
} catch(Exception e) {
+ if(validateOnly)
+ throw e;
if(!(e instanceof CancelExecution)) {
if(e instanceof InterruptedException)
handler.printError("Execution interrupted.");
}
}
+ private CompilationError[] validate(Reader commandReader) {
+ CommandParser parser = new CommandParser(defaultHandler, new MemoReader(commandReader));
+ validateOnly = true;
+ try {
+ parser.parseCommands();
+ parser.finishBlock();
+ return CompilationError.EMPTY_ARRAY;
+ } catch(SCLExpressionCompilationException e) {
+ return e.getErrors();
+ } catch(SCLSyntaxErrorException e) {
+ return new CompilationError[] { new CompilationError(e.location, e.getMessage()) };
+ } catch(Exception e) {
+ return new CompilationError[] { new CompilationError(Locations.NO_LOCATION, e.getMessage()) };
+ } finally {
+ validateOnly = false;
+ }
+ }
+
public void execute(Reader commandReader, SCLReportingHandler handler) {
if(handler == null)
handler = defaultHandler;
execute(new StringReader(command), handler);
}
- public CompilationError[] validate(String command) {
- return CompilationError.EMPTY_ARRAY;
- /*try {
- compile(command);
- return CompilationError.EMPTY_ARRAY;
- } catch(SCLExpressionCompilationException e) {
- return e.getErrors();
- }*/
- }
-
private static final String THIS_CLASS_NAME = CommandSession.class.getName();
public static void formatException(
formatException(handler, e);
}
}
+
+ public static CompilationError[] validate(ModuleRepository moduleRepository,StringReader commandReader) {
+ CommandSession session = new CommandSession(moduleRepository, null);
+ return session.validate(commandReader);
+ }
+
+ public static CompilationError[] validate(ModuleRepository moduleRepository,String command) {
+ return validate(moduleRepository, new StringReader(command));
+ }
+
+ public CompilationError[] validate(String command) {
+ return validate(new StringReader(command));
+ }
+
}
import org.simantics.scl.compiler.elaboration.java.Builtins;
import org.simantics.scl.compiler.environment.Environment;
import org.simantics.scl.compiler.environment.LocalEnvironment;
+import org.simantics.scl.compiler.errors.CompilationError;
import org.simantics.scl.compiler.errors.ErrorLog;
import org.simantics.scl.compiler.internal.codegen.references.IVal;
import org.simantics.scl.compiler.internal.codegen.ssa.SSAModule;
private LocalStorage localStorage;
private boolean interpretIfPossible = true;
private ExpressionParseMode parseMode = ExpressionParseMode.EXPRESSION;
+ private boolean validateOnly;
public ExpressionEvaluator(RuntimeEnvironment runtimeEnvironment,
String expressionText) {
return this;
}
+ public ExpressionEvaluator validateOnly(boolean validateOnly) {
+ this.validateOnly = validateOnly;
+ return this;
+ }
+
/**
* Sets a local environment that can arbitrarily modify the resolving of the expression.
*/
return "store_" + name;
}
}
+
+ public CompilationError[] validate() {
+ try {
+ validateOnly = true;
+ eval();
+ return CompilationError.EMPTY_ARRAY;
+ } catch(SCLExpressionCompilationException e) {
+ return e.getErrors();
+ }
+ }
public Object eval() throws SCLExpressionCompilationException {
fillDefaults();
Types.functionE(type, Types.PROC, Types.UNIT)),
new EVar(variableName)
)));
+ if(validateOnly)
+ localStorage.store(variableName, null, type);
}
}
if(!(block.getStatements().getLast() instanceof GuardStatement))
if(localEnvironment != null)
expression = localEnvironment.postDecorateExpression(expression);
+
+ if(validateOnly)
+ return null;
Type type = expression.getType();
type = type.convertMetaVarsToVars();
package org.simantics.scl.compiler.tests;
+import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.simantics.scl.compiler.commands.CommandSession;
+import org.simantics.scl.compiler.errors.CompilationError;
+import org.simantics.scl.compiler.errors.CompilationErrorFormatter;
import org.simantics.scl.compiler.module.repository.ModuleRepository;
import org.simantics.scl.runtime.reporting.AbstractSCLReportingHandler;
import org.simantics.scl.runtime.reporting.SCLReportingHandler;
+
public class TestCommandSession {
ModuleRepository moduleRepository;
session.execute("iter (\\i -> print i) [1,2,3]");
session.execute("iter (\\i -> print i) [(),(),()]");
}
+
+ @Test
+ public void testValidation() {
+ {
+ CompilationError[] errors = CommandSession.validate(moduleRepository, "1+1");
+ Assert.assertEquals(0, errors.length);
+ }
+ {
+ CompilationError[] errors = CommandSession.validate(moduleRepository, "\"a\"+1");
+ Assert.assertEquals(1, errors.length);
+ }
+ {
+ CompilationError[] errors = CommandSession.validate(moduleRepository,
+ "a = 1\n" +
+ "b = 2\n" +
+ "a + b");
+ Assert.assertEquals(0, errors.length);
+ }
+ {
+ String source =
+ "a = 1\n" +
+ "b = 2.0\n" +
+ "a + b";
+ CompilationError[] errors = CommandSession.validate(moduleRepository, source);
+ //System.out.println(CompilationErrorFormatter.toString(source, errors));
+ Assert.assertEquals(1, errors.length);
+ }
+ }
}