Skip to main content
summaryrefslogtreecommitdiffstats
blob: e8d9e488f3abedcf73d75c69d802ed0e2408936d (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*******************************************************************************
 * Copyright (c) 2005, 2006 committers of openArchitectureWare and others.
 * 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:
 *     committers of openArchitectureWare - initial API and implementation
 *******************************************************************************/
package org.eclipse.xtend;

import java.io.StringReader;
import java.util.Set;

import org.eclipse.internal.xtend.xtend.ast.Extension;
import org.eclipse.internal.xtend.xtend.ast.ExtensionFile;
import org.eclipse.internal.xtend.xtend.parser.ParseFacade;
import org.eclipse.xtend.expression.AnalysationIssue;
import org.eclipse.xtend.expression.ExecutionContext;
import org.eclipse.xtend.expression.ExecutionContextImpl;
import org.eclipse.xtend.expression.Resource;
import org.eclipse.xtend.expression.TypeSystemImpl;
import org.eclipse.xtend.typesystem.MetaModel;
import org.eclipse.xtend.typesystem.Type;

public class XtendFacade {

    private ExecutionContext ctx;

    private XtendFacade(final ExecutionContext ctx) {
        this.ctx = ctx;
    }
    
    public final XtendFacade cloneWithExtensions(String extensionCode) {
        return new XtendFacade( ctx.cloneWithResource(parse(extensionCode)));
    }
    
    public final void registerMetaModel(final MetaModel mm) {
    	if (ctx instanceof ExecutionContextImpl) {
    		((ExecutionContextImpl) ctx).registerMetaModel(mm);
    	} else {
    		throw new IllegalStateException("Couldn't register Metamodel - ExecutionContextImpl expected.");
    	}
    }
    
    private ExtensionFile parse(final String extFile) {
    	return ParseFacade.file(new StringReader(extFile), "nofile");
    }

    public final static XtendFacade create(final String... extFile) {
        return create(new ExecutionContextImpl(new TypeSystemImpl()),extFile);
    }
    
    public final static XtendFacade create(ExecutionContext ctx,final String... extFile) {
    	ctx = ctx.cloneWithResource(new Resource() {
    		
    		public String getFullyQualifiedName() {
    			return null;
    		}
    		
    		public void setFullyQualifiedName(final String fqn) {
    			
    		}
    		
    		public String[] getImportedNamespaces() {
    			return null;
    		}
    		
    		public String[] getImportedExtensions() {
    			return extFile;
    		}
    	});
    	return new XtendFacade(ctx);
    }

    public Object call(final String ext, Object... params) {
        if (params==null)
            params = new Object[] {null};
        final Extension extension = ctx.getExtension(ext, params);
        if (extension == null)
            throw new IllegalArgumentException("Couldn't find extension " + ext);
        return extension.evaluate(params, ctx);
    }

    public Type analyze(final String string, Object[] objects, final Set<AnalysationIssue> issues) {
        if (objects == null) {
            objects = new Object[0];
        }
        final Extension extension = ctx.getExtension(string, objects);
        final Type[] params = new Type[objects.length];
        for (int i = 0; i < params.length; i++) {
            params[i] = ctx.getType(objects[i]);
        }
        return extension.getReturnType(params, ctx, issues);
    }
}

Back to the top