Changeset 428
- Timestamp:
- Jun 15, 2010 12:19:00 AM (14 years ago)
- 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 14 14 import org.eclipse.imp.model.ISourceProject; 15 15 import org.eclipse.imp.parser.IParseController; 16 import org.eclipse.imp.pdb.facts.IConstructor; 16 17 import org.eclipse.imp.pdb.facts.IList; 17 18 import org.eclipse.imp.pdb.facts.IValue; … … 23 24 import org.magnolialang.eclipse.rascal.RascalInterpreter; 24 25 import org.magnolialang.env.MagnoliaEnvironment; 26 import org.magnolialang.errors.CompilationError; 25 27 import org.magnolialang.load.ProjectURIResolver; 26 28 import org.magnolialang.load.ModuleLoader; 29 import org.magnolialang.manager.MagnoliaProjectManager; 27 30 public class ViewFlattenedModule extends AbstractHandler { 28 31 @SuppressWarnings("deprecation") … … 32 35 if (editorPart instanceof UniversalEditor) { 33 36 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; 35 42 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)); 42 45 mods = env.getImports(); 43 46 44 47 RascalInterpreter eval = RascalInterpreter.getInstance(); 45 48 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 48 56 e1.printStackTrace(); 49 57 throw new ImplementationError("Flattening failed:", e1); 50 58 } 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 else67 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 76 59 } 77 60 return null; -
trunk/MagnoliaPlugin/src/org/magnolialang/eclipse/editor/MagnoliaEditor.java
r397 r428 23 23 setAction(ITextEditorActionConstants.SHOW_INFORMATION, action); 24 24 } 25 26 @Override public void dispose() { 27 ((ParseController)getParseController()).dispose(); 28 super.dispose(); 29 } 25 30 } -
trunk/MagnoliaPlugin/src/org/magnolialang/eclipse/editor/ParseController.java
r424 r428 23 23 import org.magnolialang.load.ModuleLoader; 24 24 import org.magnolialang.load.ProjectURIResolver; 25 import org.magnolialang.manager.IProjectManager; 26 import org.magnolialang.manager.MagnoliaProjectManager; 25 27 import org.rascalmpl.interpreter.env.ModuleEnvironment; 26 28 import org.rascalmpl.interpreter.staticErrors.SyntaxError; … … 37 39 private IConstructor parseTree; 38 40 private IPath path; 41 private MagnoliaProjectManager manager; 39 42 public static final String LANGUAGE_NAME = "Magnolia"; 40 43 … … 83 86 84 87 public void initialize(IPath filePath, ISourceProject project, IMessageHandler handler) { 88 if(manager != null) 89 manager.removeEditedModule(this); 90 85 91 this.path = filePath; 86 92 this.handler = handler; 87 93 this.project = project; 94 this.manager = MagnoliaProjectManager.getProjectManager(project); 95 96 manager.addEditedModule(this); 88 97 } 89 98 … … 131 140 } 132 141 142 public void dispose() { 143 manager.removeEditedModule(this); 144 } 145 133 146 134 147 } -
trunk/MagnoliaPlugin/src/org/magnolialang/load/IBuildPath.java
r426 r428 62 62 */ 63 63 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); 64 71 } -
trunk/MagnoliaPlugin/src/org/magnolialang/load/MagnoliaBuildPath.java
r426 r428 8 8 import java.util.Map; 9 9 10 import org.eclipse.core.runtime.IPath; 11 import org.eclipse.core.runtime.Path; 12 import org.eclipse.imp.utils.Pair; 10 13 import org.magnolialang.eclipse.MagnoliaPlugin; 11 14 import org.magnolialang.uri.URIResolverRegistry; … … 130 133 } 131 134 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 132 157 } -
trunk/MagnoliaPlugin/src/org/magnolialang/load/ModuleLoader.java
r426 r428 1 1 package org.magnolialang.load; 2 3 import static org.magnolialang.parsetree.XaTreeAdapter.getArg; 4 import static org.magnolialang.parsetree.XaTreeAdapter.getArgs; 5 import static org.magnolialang.parsetree.XaTreeAdapter.isCons; 6 import static org.magnolialang.parsetree.XaTreeAdapter.yield; 2 7 3 8 import java.io.ByteArrayOutputStream; … … 5 10 import java.io.InputStream; 6 11 import java.net.URI; 7 import java.util.ArrayList;8 import java.util.List;9 import org.eclipse.imp.model.ISourceProject;10 12 13 import org.eclipse.imp.pdb.facts.IConstructor; 14 import org.eclipse.imp.pdb.facts.ISourceLocation; 15 import org.eclipse.imp.pdb.facts.IValue; 16 import org.eclipse.imp.pdb.facts.IValueFactory; 11 17 import org.magnolialang.eclipse.MagnoliaPlugin; 12 import org.magnolialang.eclipse.editor.ImplementationError;13 18 import org.magnolialang.env.MagnoliaEnvironment; 19 import org.magnolialang.errors.ModuleLoadError; 20 import org.magnolialang.manager.MagnoliaProjectManager; 14 21 import org.magnolialang.parsetree.TreeImploder; 15 import org.magnolialang.uri.URIResolverRegistry;16 17 import static org.magnolialang.parsetree.XaTreeAdapter.*;18 22 import org.rascalmpl.interpreter.staticErrors.SyntaxError; 19 23 import org.rascalmpl.values.ValueFactoryFactory; … … 22 26 import org.rascalmpl.values.uptr.Factory; 23 27 import 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.*;34 28 public class ModuleLoader{ 35 29 //private List<ISdfSearchPathContributor> contributors = new ArrayList<ISdfSearchPathContributor>(); 36 30 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; 40 32 41 33 public ModuleLoader(){ … … 43 35 } 44 36 45 public ModuleLoader( ISourceProject project){46 this(new ModuleParser(), project);37 public ModuleLoader(MagnoliaProjectManager manager){ 38 this(new ModuleParser(), manager); 47 39 } 48 40 49 public ModuleLoader(ModuleParser parser, ISourceProject project){41 public ModuleLoader(ModuleParser parser, MagnoliaProjectManager manager){ 50 42 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; 55 44 } 56 45 … … 59 48 } 60 49 61 // public void addSdfSearchPathContributor(ISdfSearchPathContributor contrib){62 // contributors.add(0, contrib);63 // }64 65 66 67 68 50 private SyntaxError parseError(IConstructor tree, URI location){ 69 51 SummaryAdapter summary = new SummaryAdapter(tree); … … 106 88 } 107 89 108 public IConstructor parseModule(URI location , MagnoliaEnvironment env) throws IOException{90 public IConstructor parseModule(URI location) { 109 91 byte[] data; 110 92 111 93 InputStream inputStream = null; 94 112 95 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); 118 99 } 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; 119 110 } 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); 125 113 } 126 127 return tree;128 114 } 129 115 130 116 public IConstructor loadModule(URI location, MagnoliaEnvironment env) throws IOException { 131 IConstructor tree = ParsetreeAdapter.getTop(parseModule(location , null));117 IConstructor tree = ParsetreeAdapter.getTop(parseModule(location)); 132 118 tree = (IConstructor) TreeImploder.implodeTree(tree); 133 119 return loadModule(tree, env); … … 143 129 if(isCons(imp, "ImportAll", 1)) { 144 130 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); 148 141 } 149 142 } -
trunk/MagnoliaPlugin/src/org/magnolialang/load/ProjectURIResolver.java
r426 r428 52 52 } 53 53 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 55 58 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(uri.getHost()); 56 59 … … 62 65 } 63 66 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 } 64 76 public OutputStream getOutputStream(URI uri, boolean append) throws IOException { 65 77 return new FileOutputStream(resolve(uri).getRawLocation().toOSString(), append);
Note:
See TracChangeset
for help on using the changeset viewer.