X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Fcommands%2FCommandSession.java;fp=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Fcommands%2FCommandSession.java;h=1cf6d0de9c5f0786fe4df80072602e22670f514e;hp=82d615ca89f476e248615ffb158075d5b7fa5826;hb=bea2a8313dd7408716eda5d4e46bba53ef523d54;hpb=fede738f59eeae3156c0f2f1df4dd9e99f28b3b1 diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java index 82d615ca8..1cf6d0de9 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java @@ -76,7 +76,12 @@ public class CommandSession { THashMap variableTypes = new THashMap(); 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( @@ -320,6 +325,7 @@ public class CommandSession { Function command = (Function)evaluator .localEnvironment(localEnvironment) .decorateExpression(true) + .validateOnly(validateOnly) .eval(); return new CompiledCommand(command, evaluator.getType()); } @@ -361,7 +367,9 @@ public class CommandSession { 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)); @@ -371,11 +379,15 @@ public class CommandSession { } 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."); @@ -463,6 +475,24 @@ public class CommandSession { } } + 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; @@ -494,16 +524,6 @@ public class CommandSession { 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( @@ -632,4 +652,18 @@ public class CommandSession { 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)); + } + }