Changeset 428


Ignore:
Timestamp:
Jun 15, 2010 12:19:00 AM (14 years ago)
Author:
Anya Helene Bagge
Message:
  • MagnoliaProjectManager for keeping track of all modules in a project; use this to load modules and obtain module trees
  • CompilationError and associated exceptions; can be added as markers to source code through the project manager
  • MagnoliaAdapter for accessing parts of a module tree
Location:
trunk/MagnoliaPlugin/src/org/magnolialang
Files:
9 added
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagnoliaPlugin/src/org/magnolialang/eclipse/commands/ViewFlattenedModule.java

    r419 r428  
    1414import org.eclipse.imp.model.ISourceProject;
    1515import org.eclipse.imp.parser.IParseController;
     16import org.eclipse.imp.pdb.facts.IConstructor;
    1617import org.eclipse.imp.pdb.facts.IList;
    1718import org.eclipse.imp.pdb.facts.IValue;
     
    2324import org.magnolialang.eclipse.rascal.RascalInterpreter;
    2425import org.magnolialang.env.MagnoliaEnvironment;
     26import org.magnolialang.errors.CompilationError;
    2527import org.magnolialang.load.ProjectURIResolver;
    2628import org.magnolialang.load.ModuleLoader;
     29import org.magnolialang.manager.MagnoliaProjectManager;
    2730public class ViewFlattenedModule extends AbstractHandler {
    2831        @SuppressWarnings("deprecation")
     
    3235                if (editorPart instanceof UniversalEditor) {
    3336                        IParseController parseCtrl = ((UniversalEditor) editorPart).getParseController();
    34                         URI uri = ProjectURIResolver.constructProjectURI(parseCtrl.getProject(), parseCtrl.getPath());
     37                        MagnoliaProjectManager manager = MagnoliaProjectManager.getProjectManager(parseCtrl.getProject());
     38                        try {
     39                                URI uri = ProjectURIResolver.constructProjectURI(parseCtrl.getProject(), parseCtrl.getPath());
     40                                IList mods;
     41                                IValue result = null;
    3542
    36                         ModuleLoader load = new ModuleLoader(parseCtrl.getProject());
    37                         IList mods;
    38                         IValue result = null;
    39                         try {
    40                                 MagnoliaEnvironment env = new MagnoliaEnvironment();
    41                                 IValue tree = load.loadModule(uri, env);
     43                                IConstructor tree = manager.getModuleTree(manager.getModuleName(uri));
     44                                MagnoliaEnvironment env = manager.getModuleEnv(manager.getModuleName(uri));
    4245                                mods = env.getImports();
    4346                               
    4447                                RascalInterpreter eval = RascalInterpreter.getInstance();
    4548                                IValue mEnv = eval.call("loadTrees", "import MagnoliaCompile;", mods);
    46                                 result = eval.call("flattenModule", "import MagnoliaCompile;", tree, mEnv);
    47                         } catch (Throwable e1) {
     49                                result = eval.call("flattenConcepts", "import MagnoliaCompile;", tree, mEnv);
     50                                manager.createModule(manager.getModuleName(uri)+"Flat", manager.getModuleName(uri), (IConstructor)result);
     51                        } catch(CompilationError ce) {
     52                                manager.reportError(ce);
     53                        }
     54                                catch (Throwable e1) {
     55                       
    4856                                e1.printStackTrace();
    4957                                throw new ImplementationError("Flattening failed:", e1);
    5058                        }
    51                        
    52                         //RascalInterpreter.getInstance().eval("import MagnoliaCompile;");
    53                         //String cmd = "flattenModule(|" + uri.toString() + "|, \"sophus-src\")";
    54                        
    55                         if(result != null && result.getType().isStringType()) {
    56                                 IPath path = parseCtrl.getPath();
    57                                 String resultString = ((IString)result).getValue();
    58                                 ISourceProject project = parseCtrl.getProject();
    59                                 String ext = path.getFileExtension();
    60                                 path = path.removeFileExtension();
    61                                 path = path.addFileExtension("flattened." + ext);
    62                                 IFile file = project.getRawProject().getFile(path);
    63                                 try {
    64                                         if(file.exists())
    65                                                 file.setContents(new StringBufferInputStream(resultString), false, true, null);
    66                                         else
    67                                                 file.create(new StringBufferInputStream(resultString), false, null);
    68                                 } catch (CoreException e) {
    69                                         e.printStackTrace();
    70                                 }
    71                         }
    72                         //Object astObject = parseCtrl.getCurrentAst();
    73                         //if(astObject instanceof IConstructor)
    74                                
    75 
    7659                }
    7760                return null;
  • trunk/MagnoliaPlugin/src/org/magnolialang/eclipse/editor/MagnoliaEditor.java

    r397 r428  
    2323        setAction(ITextEditorActionConstants.SHOW_INFORMATION, action);
    2424    }
     25   
     26    @Override public void dispose() {
     27        ((ParseController)getParseController()).dispose();
     28        super.dispose();
     29    }
    2530}
  • trunk/MagnoliaPlugin/src/org/magnolialang/eclipse/editor/ParseController.java

    r424 r428  
    2323import org.magnolialang.load.ModuleLoader;
    2424import org.magnolialang.load.ProjectURIResolver;
     25import org.magnolialang.manager.IProjectManager;
     26import org.magnolialang.manager.MagnoliaProjectManager;
    2527import org.rascalmpl.interpreter.env.ModuleEnvironment;
    2628import org.rascalmpl.interpreter.staticErrors.SyntaxError;
     
    3739        private IConstructor parseTree;
    3840        private IPath path;
     41        private MagnoliaProjectManager manager;
    3942        public static final String LANGUAGE_NAME = "Magnolia";
    4043
     
    8386
    8487        public void initialize(IPath filePath, ISourceProject project, IMessageHandler handler) {
     88                if(manager != null)
     89                        manager.removeEditedModule(this);
     90               
    8591                this.path = filePath;
    8692                this.handler = handler;
    8793                this.project = project;
     94                this.manager = MagnoliaProjectManager.getProjectManager(project);
     95
     96                manager.addEditedModule(this);
    8897        }
    8998
     
    131140        }
    132141
     142        public void dispose() {
     143                manager.removeEditedModule(this);       
     144        }
     145
    133146       
    134147}
  • trunk/MagnoliaPlugin/src/org/magnolialang/load/IBuildPath.java

    r426 r428  
    6262         */
    6363        public URI find(String moduleName);
     64
     65        /**
     66         * Construct a dotted module name based on a URI.
     67         *
     68         * @param uri
     69         */
     70        public Pair<URI, String> getModuleName(URI uri);
    6471}
  • trunk/MagnoliaPlugin/src/org/magnolialang/load/MagnoliaBuildPath.java

    r426 r428  
    88import java.util.Map;
    99
     10import org.eclipse.core.runtime.IPath;
     11import org.eclipse.core.runtime.Path;
     12import org.eclipse.imp.utils.Pair;
    1013import org.magnolialang.eclipse.MagnoliaPlugin;
    1114import org.magnolialang.uri.URIResolverRegistry;
     
    130133        }
    131134
     135        @Override
     136        public Pair<URI, String> getModuleName(URI uri) {
     137                uri = uri.normalize();
     138                for(URI path : srcPath) {
     139                        path = path.normalize();
     140                        if(path.getScheme().equals(uri.getScheme())) {
     141                                if((path.getHost() == null && uri.getHost() == null)
     142                                                || (path.getHost().equalsIgnoreCase(uri.getHost()))) {
     143                                        IPath p1 = new Path(path.getPath());
     144                                        IPath p2 = new Path(uri.getPath());
     145                                        if(p1.isPrefixOf(p2)) {
     146                                                p2 = p2.makeRelativeTo(p1);
     147                                                String moduleName = p2.removeFileExtension().toString().replace('/', '.');
     148                                                return new Pair<URI, String>(path, moduleName);
     149                                        }
     150                                }
     151                                       
     152                        }
     153                }
     154                return new Pair<URI, String>(null, null);
     155        }
     156
    132157}
  • trunk/MagnoliaPlugin/src/org/magnolialang/load/ModuleLoader.java

    r426 r428  
    11package org.magnolialang.load;
     2
     3import static org.magnolialang.parsetree.XaTreeAdapter.getArg;
     4import static org.magnolialang.parsetree.XaTreeAdapter.getArgs;
     5import static org.magnolialang.parsetree.XaTreeAdapter.isCons;
     6import static org.magnolialang.parsetree.XaTreeAdapter.yield;
    27
    38import java.io.ByteArrayOutputStream;
     
    510import java.io.InputStream;
    611import java.net.URI;
    7 import java.util.ArrayList;
    8 import java.util.List;
    9 import org.eclipse.imp.model.ISourceProject;
    1012
     13import org.eclipse.imp.pdb.facts.IConstructor;
     14import org.eclipse.imp.pdb.facts.ISourceLocation;
     15import org.eclipse.imp.pdb.facts.IValue;
     16import org.eclipse.imp.pdb.facts.IValueFactory;
    1117import org.magnolialang.eclipse.MagnoliaPlugin;
    12 import org.magnolialang.eclipse.editor.ImplementationError;
    1318import org.magnolialang.env.MagnoliaEnvironment;
     19import org.magnolialang.errors.ModuleLoadError;
     20import org.magnolialang.manager.MagnoliaProjectManager;
    1421import org.magnolialang.parsetree.TreeImploder;
    15 import org.magnolialang.uri.URIResolverRegistry;
    16 
    17 import static org.magnolialang.parsetree.XaTreeAdapter.*;
    1822import org.rascalmpl.interpreter.staticErrors.SyntaxError;
    1923import org.rascalmpl.values.ValueFactoryFactory;
     
    2226import org.rascalmpl.values.uptr.Factory;
    2327import org.rascalmpl.values.uptr.ParsetreeAdapter;
    24 import org.eclipse.core.runtime.Path;
    25 import org.eclipse.imp.pdb.facts.IConstructor;
    26 import org.eclipse.imp.pdb.facts.IList;
    27 import org.eclipse.imp.pdb.facts.IMap;
    28 import org.eclipse.imp.pdb.facts.ISourceLocation;
    29 import org.eclipse.imp.pdb.facts.IValue;
    30 import org.eclipse.imp.pdb.facts.IValueFactory;
    31 import org.eclipse.imp.pdb.facts.exceptions.FactTypeUseException;
    32 import org.eclipse.imp.pdb.facts.io.PBFReader;
    33 import static org.magnolialang.parsetree.XaTreeFactory.*;
    3428public class ModuleLoader{
    3529        //private List<ISdfSearchPathContributor> contributors = new ArrayList<ISdfSearchPathContributor>();
    3630        private ModuleParser parser;
    37         private final boolean saveParsedModules = (System.getProperty("rascal.options.saveBinaries") != null ? System.getProperty("rascal.options.saveBinaries").equals("true") : false);
    38         private ISourceProject project;
    39         private MagnoliaBuildPath buildPath;
     31        private MagnoliaProjectManager manager;
    4032       
    4133        public ModuleLoader(){
     
    4335        }
    4436       
    45         public ModuleLoader(ISourceProject project){
    46                 this(new ModuleParser(), project);
     37        public ModuleLoader(MagnoliaProjectManager manager){
     38                this(new ModuleParser(), manager);
    4739        }
    4840       
    49         public ModuleLoader(ModuleParser parser, ISourceProject project){
     41        public ModuleLoader(ModuleParser parser, MagnoliaProjectManager manager){
    5042                this.parser = parser;
    51                 this.project = project;
    52                 this.buildPath = new MagnoliaBuildPath(null);
    53                 if(project != null)
    54                         this.buildPath.addPath(ProjectURIResolver.constructProjectURI(project, new Path("")));
     43                this.manager = manager;
    5544        }
    5645
     
    5948        }
    6049       
    61 //      public void addSdfSearchPathContributor(ISdfSearchPathContributor contrib){
    62 //              contributors.add(0, contrib);
    63 //      }
    64        
    65        
    66 
    67 
    6850        private SyntaxError parseError(IConstructor tree, URI location){
    6951                SummaryAdapter summary = new SummaryAdapter(tree);
     
    10688        }
    10789
    108         public IConstructor parseModule(URI location, MagnoliaEnvironment env) throws IOException{
     90        public IConstructor parseModule(URI location) {
    10991                byte[] data;
    110                
     92
    11193                InputStream inputStream = null;
     94
    11295                try{
    113                         inputStream = MagnoliaPlugin.getResolverRegistry().getInputStream(location);
    114                         data = readModule(inputStream);
    115                 }finally{
    116                         if(inputStream != null){
    117                                 inputStream.close();
     96                        try{
     97                                inputStream = MagnoliaPlugin.getResolverRegistry().getInputStream(location);
     98                                data = readModule(inputStream);
    11899                        }
     100                        finally{
     101                                if(inputStream != null){
     102                                        inputStream.close();
     103                                }
     104                        }
     105                        IConstructor tree = parser.parseModule(location, data);
     106                        if(tree.getConstructorType() == Factory.ParseTree_Summary){
     107                                throw parseError(tree, location);
     108                        }
     109                        return tree;
    119110                }
    120                
    121                 IConstructor tree = parser.parseModule(location, data);
    122                
    123                 if(tree.getConstructorType() == Factory.ParseTree_Summary){
    124                         throw parseError(tree, location);
     111                catch(IOException e) {
     112                        throw new ModuleLoadError("Error loading " + location, e);
    125113                }
    126                
    127                 return tree;
    128114        }
    129115       
    130116        public IConstructor loadModule(URI location, MagnoliaEnvironment env) throws IOException {
    131                 IConstructor tree = ParsetreeAdapter.getTop(parseModule(location, null));
     117                IConstructor tree = ParsetreeAdapter.getTop(parseModule(location));
    132118                tree = (IConstructor) TreeImploder.implodeTree(tree);
    133119                return loadModule(tree, env);
     
    143129                                                        if(isCons(imp, "ImportAll", 1)) {
    144130                                                                String name = yield(getArg(imp, 0));
    145                                                                 URI uri = buildPath.find(name);
    146                                                                 loadModule(uri, env);
    147                                                                 System.out.println("import " + uri);
     131                                                                IConstructor iTree;
     132                                                                try {
     133                                                                        iTree = manager.getModuleTree(name);
     134                                                                }
     135                                                                catch(Exception e) {
     136                                                                        throw new ModuleLoadError("Error loading module " + name, e, imp);
     137                                                                }
     138                                                                if(iTree == null)
     139                                                                        throw new ModuleLoadError("Error loading module " + name, imp);                                                 
     140                                                                loadModule(iTree, env);
    148141                                                        }
    149142                                                }
  • trunk/MagnoliaPlugin/src/org/magnolialang/load/ProjectURIResolver.java

    r426 r428  
    5252        }
    5353
    54         private IFile resolve(URI uri) throws IOException, MalformedURLException {
     54        private static IFile resolve(URI uri) throws IOException, MalformedURLException {
     55                if(!uri.getScheme().equals("project"))
     56                        throw new IOException("Not a project URI:" + uri);
     57               
    5558                IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(uri.getHost());
    5659               
     
    6265        }
    6366       
     67        public static IFile getFile(URI uri) {
     68                try {
     69                        return resolve(uri);
     70                } catch (MalformedURLException e) {
     71                        return null;
     72                } catch (IOException e) {
     73                        return null;
     74                }
     75        }
    6476        public OutputStream getOutputStream(URI uri, boolean append) throws IOException {
    6577                return new FileOutputStream(resolve(uri).getRawLocation().toOSString(), append);
Note: See TracChangeset for help on using the changeset viewer.