]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java
Print AssertionError to SCLReportingHandler
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / commands / CommandSession.java
index 82d615ca89f476e248615ffb158075d5b7fa5826..9ff2ebabb0b3492c27814e0d0dd013b552b50044 100644 (file)
@@ -11,7 +11,6 @@ import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -43,6 +42,7 @@ import org.simantics.scl.compiler.module.ImportDeclaration;
 import org.simantics.scl.compiler.module.repository.ImportFailure;
 import org.simantics.scl.compiler.module.repository.ImportFailureException;
 import org.simantics.scl.compiler.module.repository.ModuleRepository;
+import org.simantics.scl.compiler.module.repository.UpdateListener;
 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
 import org.simantics.scl.compiler.top.ExpressionEvaluator;
 import org.simantics.scl.compiler.top.LocalStorage;
@@ -76,7 +76,13 @@ public class CommandSession {
     THashMap<String,Type> variableTypes = new THashMap<String,Type>();
     
     PrintStream fileOutput;
-
+    private UpdateListener dependenciesListener;
+    
+    /**
+     * 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(
@@ -103,10 +109,13 @@ public class CommandSession {
         
         runtimeEnvironment = null;
         try {
+            if(dependenciesListener != null)
+                dependenciesListener.stopListening();
             try {
                 runtimeEnvironment = moduleRepository.createRuntimeEnvironment(
                         environmentSpecification,
-                        getClass().getClassLoader());
+                        getClass().getClassLoader(),
+                        dependenciesListener);
             } catch(ImportFailureException e) {
                 THashSet<String> failedModules = new THashSet<String>();
                 for(ImportFailure failure : e.failures) {
@@ -124,7 +133,7 @@ public class CommandSession {
                 try {
                     runtimeEnvironment = moduleRepository.createRuntimeEnvironment(
                             environmentSpecification,
-                            getClass().getClassLoader());
+                            getClass().getClassLoader()); // no listener here, because should listen also failed modules
                 } catch (ImportFailureException e1) {
                     for(ImportFailure failure : e1.failures)
                         defaultHandler.printError(failure.toString());
@@ -320,6 +329,7 @@ public class CommandSession {
         Function command = (Function)evaluator
             .localEnvironment(localEnvironment)
             .decorateExpression(true)
+            .validateOnly(validateOnly)
             .eval();
         return new CompiledCommand(command, evaluator.getType());
     }
@@ -361,7 +371,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 +383,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.");
@@ -411,10 +427,9 @@ public class CommandSession {
         void finishBlock() {
             if(currentBlock != null) {
                 checkInterrupted();
-                LinkedList<Statement> statements = currentBlock.getStatements();
                 currentBlock.location = Locations.combine(
-                        statements.getFirst().location,
-                        statements.getLast().location);
+                        currentBlock.getFirst().location,
+                        currentBlock.getLast().location);
                 execute(reader, currentBlock, handler);
                 currentBlock = null;
             }
@@ -463,6 +478,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;
@@ -478,7 +511,7 @@ public class CommandSession {
             if(e.location != Locations.NO_LOCATION)
                 handler.printError(parser.reader.locationUnderlining(e.location));
             handler.printError(e.getMessage());
-        } catch(Exception e) {
+        } catch (Exception | AssertionError e) {
             if(e instanceof InterruptedException)
                 handler.printError("Execution interrupted.");
             else
@@ -494,16 +527,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 +655,21 @@ 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));
+    }
+
+    public void setDependenciesListener(UpdateListener dependenciesListener) {
+        this.dependenciesListener = dependenciesListener;
+    }
 }