Search in sources :

Example 1 with IEvaluationResult

use of org.eclipse.xtext.xbase.interpreter.IEvaluationResult in project smarthome by eclipse.

the class ScriptImpl method execute.

@Override
public Object execute(final IEvaluationContext evaluationContext) throws ScriptExecutionException {
    if (xExpression != null) {
        Resource resource = xExpression.eResource();
        IExpressionInterpreter interpreter = null;
        if (resource instanceof XtextResource) {
            IResourceServiceProvider provider = ((XtextResource) resource).getResourceServiceProvider();
            interpreter = provider.get(IExpressionInterpreter.class);
        }
        if (interpreter == null) {
            throw new ScriptExecutionException("Script interpreter couldn't be obtain");
        }
        try {
            IEvaluationResult result = interpreter.evaluate(xExpression, evaluationContext, CancelIndicator.NullImpl);
            if (result == null) {
                // i.e. NEVER ;-)
                return null;
            }
            if (result.getException() != null) {
                throw new ScriptExecutionException(result.getException().getMessage(), result.getException());
            }
            return result.getResult();
        } catch (Throwable e) {
            if (e instanceof ScriptExecutionException) {
                throw (ScriptExecutionException) e;
            } else {
                throw new ScriptExecutionException("An error occurred during the script execution: " + e.getMessage(), e);
            }
        }
    } else {
        throw new ScriptExecutionException("Script does not contain any expression");
    }
}
Also used : IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IExpressionInterpreter(org.eclipse.xtext.xbase.interpreter.IExpressionInterpreter) ScriptExecutionException(org.eclipse.smarthome.model.script.engine.ScriptExecutionException) IEvaluationResult(org.eclipse.xtext.xbase.interpreter.IEvaluationResult) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource)

Example 2 with IEvaluationResult

use of org.eclipse.xtext.xbase.interpreter.IEvaluationResult in project xtext-eclipse by eclipse.

the class ReplAutoEdit method computeResultText.

protected String computeResultText(final IDocument document, final DocumentCommand command, XtextResource resource) throws BadLocationException {
    if (resource.getContents().isEmpty())
        return null;
    if (!(resource.getContents().get(0) instanceof Model))
        return null;
    Model model = (Model) resource.getContents().get(0);
    if (model.getBlock() == null)
        return null;
    EList<XExpression> expressions = model.getBlock().getExpressions();
    if (expressions.isEmpty())
        return null;
    XExpression expression = expressions.get(expressions.size() - 1);
    if (expression == null) {
        return null;
    }
    ICompositeNode node = NodeModelUtils.getNode(expression);
    if (node == null || document.getLineOfOffset(command.offset) + 1 != node.getEndLine())
        return null;
    List<Issue> issues = validator.validate(resource, CheckMode.NORMAL_AND_FAST, CancelIndicator.NullImpl);
    if (issues.stream().anyMatch(i -> i.getSeverity() == Severity.ERROR)) {
        return null;
    }
    XbaseInterpreter xbaseInterpreter = getConfiguredInterpreter(resource);
    IEvaluationResult result = xbaseInterpreter.evaluate(model.getBlock(), new DefaultEvaluationContext(), new CancelIndicator() {

        private long stopAt = System.currentTimeMillis() + 2000;

        @Override
        public boolean isCanceled() {
            return System.currentTimeMillis() > stopAt;
        }
    });
    if (result == null)
        return null;
    String value = "" + result.getResult();
    if (result.getException() != null)
        value = "threw " + result.getException().getClass().getSimpleName();
    LightweightTypeReference expressionType = typeResolver.resolveTypes(expression).getActualType(expression);
    if (expressionType != null) {
        String newText = command.text + "// " + value + " (" + expressionType.getSimpleName() + ")" + command.text;
        return newText;
    }
    return command.text + "// " + value;
}
Also used : Issue(org.eclipse.xtext.validation.Issue) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) IEvaluationResult(org.eclipse.xtext.xbase.interpreter.IEvaluationResult) DefaultEvaluationContext(org.eclipse.xtext.xbase.interpreter.impl.DefaultEvaluationContext) XbaseInterpreter(org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter) Model(org.eclipse.xtext.purexbase.pureXbase.Model) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XExpression(org.eclipse.xtext.xbase.XExpression) CancelIndicator(org.eclipse.xtext.util.CancelIndicator)

Aggregations

IEvaluationResult (org.eclipse.xtext.xbase.interpreter.IEvaluationResult)2 Resource (org.eclipse.emf.ecore.resource.Resource)1 ScriptExecutionException (org.eclipse.smarthome.model.script.engine.ScriptExecutionException)1 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)1 Model (org.eclipse.xtext.purexbase.pureXbase.Model)1 IResourceServiceProvider (org.eclipse.xtext.resource.IResourceServiceProvider)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 CancelIndicator (org.eclipse.xtext.util.CancelIndicator)1 Issue (org.eclipse.xtext.validation.Issue)1 XExpression (org.eclipse.xtext.xbase.XExpression)1 IExpressionInterpreter (org.eclipse.xtext.xbase.interpreter.IExpressionInterpreter)1 DefaultEvaluationContext (org.eclipse.xtext.xbase.interpreter.impl.DefaultEvaluationContext)1 XbaseInterpreter (org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter)1 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)1