Skip to main content
summaryrefslogtreecommitdiffstats
blob: 779cd2ccb51194df25c24456d53151b5b9b631af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Copyright (c) 2008 Arno Haase.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html

Contributors:
    Arno Haase - initial API and implementation
 */
package org.eclipse.xtend.backend.common;

import java.util.ArrayList;
import java.util.List;


/**
 * 
 * @author Arno Haase (http://www.haase-consulting.com)
 */
public abstract class ExpressionBase {
    private final SourcePos _sourcePos;
    private final List<ExecutionListener> _listeners = new ArrayList<ExecutionListener> ();

    public ExpressionBase (SourcePos sourcePos) {
        _sourcePos = sourcePos;
    }

    public SourcePos getPos () {
        return _sourcePos;
    }
    
    public void registerExecutionListener (ExecutionListener l) {
        _listeners.add (l);
    }
    
    private void firePreEvent (ExecutionContext ctx) {
        for (ExecutionListener l: _listeners)
            l.preExecute (ctx);
    }

    private void firePostEvent (Object result, ExecutionContext ctx) {
        for (int i=_listeners.size()-1; i >= 0; i--)
            _listeners.get(i).postExecute (result, ctx);
    }
    
    public final Object evaluate (ExecutionContext ctx) {
        try {
            firePreEvent (ctx);
            final Object result = evaluateInternal (ctx);
            firePostEvent (result, ctx);
            return result;
        }
        catch (ExecutionException exc) {
            exc.addStackTraceElement (_sourcePos, ctx.getLocalVarContext().getLocalVars());
            throw exc;
        }
        catch (Exception exc) {
            throw new ExecutionException (exc, _sourcePos, ctx.getLocalVarContext().getLocalVars());
        }
    }
    
    protected abstract Object evaluateInternal (ExecutionContext ctx);
}

Back to the top