org.eclipse.xtext.xbase.interpreter.IExpressionInterpreter Java Examples

The following examples show how to use org.eclipse.xtext.xbase.interpreter.IExpressionInterpreter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ScriptImpl.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
@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) {
                // this can only happen on an InterpreterCancelledException,
                // 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");
    }
}
 
Example #2
Source File: SarlScriptExecutor.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public Object execute(int lineno, String code) throws Exception {
	final List<String> issues = new ArrayList<>();
	final Collection<Resource> resources = new ArrayList<>();

	final CompiledFile outFile = compile(lineno,
			"package x.x.x;\n" //$NON-NLS-1$
			+ "class ____Fake_Class____ {\nstatic var __fake_attr__ : Object = {\n" //$NON-NLS-1$
			+ code + ";\n};\n}", //$NON-NLS-1$
			issues, (it) -> {
				resources.add(it);
			});
	try {
		assertNoIssue(lineno, issues);
		if (resources.isEmpty()) {
			throw new NoXtextResourceException(lineno);
		}
		for (Resource resource : resources) {
			SarlScript script = (SarlScript) resource.getContents().get(0);
			SarlClass clazz = (SarlClass) script.getXtendTypes().get(0);
			SarlField field = (SarlField) clazz.getMembers().get(0);
			XExpression xexpression = field.getInitialValue();
			final IExpressionInterpreter interpreter = this.interpreterProvider.get();
			if (interpreter instanceof SarlExpressionInterpreter && this.classLoaderBuilder != null) {
				final SarlExpressionInterpreter exprEvaluator = (SarlExpressionInterpreter) interpreter;
				exprEvaluator.expandClassLoader(this.classLoaderBuilder);
			}
			IEvaluationResult result = interpreter.evaluate(xexpression);
			if (result.getException() == null) {
				return result.getResult();
			}
			throw new RuntimeException(result.getException());
		}
	} finally {
		if (outFile != null && outFile.getRootFolder() != null) {
			FileSystem.delete(outFile.getRootFolder());
		}
	}
	return null;
}
 
Example #3
Source File: ScriptImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@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) {
                // this can only happen on an InterpreterCancelledException,
                // 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");
    }
}
 
Example #4
Source File: DefaultXbaseRuntimeModule.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends IExpressionInterpreter> bindIExpressionInterpreter() {
	return XbaseInterpreter.class;
}
 
Example #5
Source File: ClosureInvocationHandler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public ClosureInvocationHandler(XClosure closure, IEvaluationContext context, IExpressionInterpreter interpreter, CancelIndicator indicator) {
	this.closure = closure;
	this.context = context;
	this.interpreter = interpreter;
	this.indicator = indicator;
}
 
Example #6
Source File: SarlScriptExecutor.java    From sarl with Apache License 2.0 4 votes vote down vote up
/** Change the injector.
 *
 * @param injector the new injector.
 */
@Inject
public void setInjector(Injector injector) {
	this.compilerProvider = injector.getProvider(SarlBatchCompiler.class);
	this.interpreterProvider = injector.getProvider(IExpressionInterpreter.class);
}
 
Example #7
Source File: AbstractSARLRuntimeModule.java    From sarl with Apache License 2.0 4 votes vote down vote up
public Class<? extends IExpressionInterpreter> bindIExpressionInterpreter() {
	return SarlExpressionInterpreter.class;
}