Changeset 419
- Timestamp:
- Jun 11, 2010 9:39:28 AM (14 years ago)
- Location:
- trunk/MagnoliaPlugin/src/org/magnolialang
- Files:
-
- 2 added
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagnoliaPlugin/src/org/magnolialang/eclipse/commands/ViewFlattenedModule.java
r410 r419 1 1 package org.magnolialang.eclipse.commands; 2 2 3 import java.io.IOException; 3 4 import java.io.StringBufferInputStream; 4 5 import java.net.URI; … … 13 14 import org.eclipse.imp.model.ISourceProject; 14 15 import org.eclipse.imp.parser.IParseController; 16 import org.eclipse.imp.pdb.facts.IList; 15 17 import org.eclipse.imp.pdb.facts.IValue; 16 18 import org.eclipse.imp.pdb.facts.IString; … … 18 20 import org.eclipse.ui.IEditorPart; 19 21 import org.eclipse.ui.handlers.HandlerUtil; 22 import org.magnolialang.eclipse.editor.ImplementationError; 20 23 import org.magnolialang.eclipse.rascal.RascalInterpreter; 24 import org.magnolialang.env.MagnoliaEnvironment; 21 25 import org.magnolialang.load.ProjectURIResolver; 22 26 import org.magnolialang.load.ModuleLoader; 23 27 public class ViewFlattenedModule extends AbstractHandler { 24 28 @SuppressWarnings("deprecation") … … 30 34 URI uri = ProjectURIResolver.constructProjectURI(parseCtrl.getProject(), parseCtrl.getPath()); 31 35 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); 42 mods = env.getImports(); 43 44 RascalInterpreter eval = RascalInterpreter.getInstance(); 45 IValue mEnv = eval.call("loadTrees", "import MagnoliaCompile;", mods); 46 result = eval.call("flattenModule", "import MagnoliaCompile;", tree, mEnv); 47 } catch (Throwable e1) { 48 e1.printStackTrace(); 49 throw new ImplementationError("Flattening failed:", e1); 50 } 51 32 52 //RascalInterpreter.getInstance().eval("import MagnoliaCompile;"); 33 String cmd = "flattenModule(|" + uri.toString() + "|, \"sophus-src\")"; 34 IValue result = RascalInterpreter.getInstance().eval(cmd, "import MagnoliaCompile;"); 53 //String cmd = "flattenModule(|" + uri.toString() + "|, \"sophus-src\")"; 35 54 36 55 if(result != null && result.getType().isStringType()) { -
trunk/MagnoliaPlugin/src/org/magnolialang/eclipse/editor/ParseController.java
r405 r419 19 19 import org.eclipse.jface.text.IRegion; 20 20 import org.magnolialang.eclipse.MagnoliaPlugin; 21 import org.magnolialang.env.MagnoliaEnvironment; 21 22 import org.magnolialang.load.ModuleLoader; 22 23 import org.magnolialang.load.ProjectURIResolver; … … 89 90 90 91 URI uri = ProjectURIResolver.constructProjectURI(project, path); 91 IConstructor result = loader.parseModule(uri, input, new M oduleEnvironment("***editor***"));92 IConstructor result = loader.parseModule(uri, input, new MagnoliaEnvironment()); 92 93 93 94 if (result.getConstructorType() == Factory.ParseTree_Summary) { -
trunk/MagnoliaPlugin/src/org/magnolialang/eclipse/rascal/RascalInterpreter.java
r414 r419 66 66 }); 67 67 68 if(!prelude.equals("")) 69 eval(prelude, eval); 70 68 if(!prelude.equals("")) { 69 String[] cmds = prelude.split(";"); 70 for(String cmd : cmds) 71 eval(cmd + ";", eval); 72 } 71 73 evals.put(prelude, eval); 72 74 return eval; -
trunk/MagnoliaPlugin/src/org/magnolialang/load/ModuleLoader.java
r397 r419 7 7 import java.util.ArrayList; 8 8 import java.util.List; 9 import org.eclipse.imp.model.ISourceProject; 9 10 10 11 import org.magnolialang.eclipse.editor.ImplementationError; 11 import org.eclipse.imp.pdb.facts.IConstructor; 12 import org.eclipse.imp.pdb.facts.ISourceLocation; 13 import org.eclipse.imp.pdb.facts.IValueFactory; 14 import org.eclipse.imp.pdb.facts.exceptions.FactTypeUseException; 15 import org.eclipse.imp.pdb.facts.io.PBFReader; 16 import org.rascalmpl.ast.ASTFactory; 17 import org.rascalmpl.ast.AbstractAST; 18 import org.rascalmpl.ast.Module; 19 import org.rascalmpl.interpreter.env.ModuleEnvironment; 20 import org.rascalmpl.interpreter.staticErrors.ModuleLoadError; 12 import org.magnolialang.env.MagnoliaEnvironment; 13 import org.magnolialang.parsetree.TreeImploder; 14 import static org.magnolialang.parsetree.XaTreeAdapter.*; 21 15 import org.rascalmpl.interpreter.staticErrors.SyntaxError; 22 import org.rascalmpl.interpreter.utils.Names;23 import org.rascalmpl.parser.ASTBuilder;24 16 import org.rascalmpl.uri.URIResolverRegistry; 25 17 import org.rascalmpl.values.ValueFactoryFactory; … … 27 19 import org.rascalmpl.values.errors.SummaryAdapter; 28 20 import org.rascalmpl.values.uptr.Factory; 29 21 import org.rascalmpl.values.uptr.ParsetreeAdapter; 22 import org.eclipse.core.runtime.Path; 23 import org.eclipse.imp.pdb.facts.IConstructor; 24 import org.eclipse.imp.pdb.facts.IList; 25 import org.eclipse.imp.pdb.facts.IMap; 26 import org.eclipse.imp.pdb.facts.ISourceLocation; 27 import org.eclipse.imp.pdb.facts.IValue; 28 import org.eclipse.imp.pdb.facts.IValueFactory; 29 import org.eclipse.imp.pdb.facts.exceptions.FactTypeUseException; 30 import org.eclipse.imp.pdb.facts.io.PBFReader; 31 import static org.magnolialang.parsetree.XaTreeFactory.*; 30 32 public class ModuleLoader{ 31 33 //private List<ISdfSearchPathContributor> contributors = new ArrayList<ISdfSearchPathContributor>(); 32 34 private ModuleParser parser; 33 35 private final boolean saveParsedModules = (System.getProperty("rascal.options.saveBinaries") != null ? System.getProperty("rascal.options.saveBinaries").equals("true") : false); 36 private ISourceProject project; 34 37 35 38 public ModuleLoader(){ 36 this(new ModuleParser() );39 this(new ModuleParser(), null); 37 40 } 38 41 39 public ModuleLoader(ModuleParser parser){ 42 public ModuleLoader(ISourceProject project){ 43 this(new ModuleParser(), project); 44 } 45 46 public ModuleLoader(ModuleParser parser, ISourceProject project){ 40 47 this.parser = parser; 48 this.project = project; 41 49 } 42 50 … … 80 88 81 89 82 public IConstructor parseModule(URI location, String moduleString, M oduleEnvironment env) throws IOException{90 public IConstructor parseModule(URI location, String moduleString, MagnoliaEnvironment env) throws IOException{ 83 91 byte[] data = moduleString.getBytes(); 84 92 85 IConstructor tree = parser.parseModule(location, data , env);93 IConstructor tree = parser.parseModule(location, data); 86 94 87 95 if(tree.getConstructorType() == Factory.ParseTree_Summary){ … … 92 100 } 93 101 94 public IConstructor parseModule(URI location, M oduleEnvironment env) throws IOException{102 public IConstructor parseModule(URI location, MagnoliaEnvironment env) throws IOException{ 95 103 byte[] data; 96 104 … … 105 113 } 106 114 107 IConstructor tree = parser.parseModule(location, data , env);115 IConstructor tree = parser.parseModule(location, data); 108 116 109 117 if(tree.getConstructorType() == Factory.ParseTree_Summary){ … … 113 121 return tree; 114 122 } 115 123 124 public IConstructor loadModule(URI location, MagnoliaEnvironment env) throws IOException { 125 IConstructor tree = ParsetreeAdapter.getTop(parseModule(location, null)); 126 tree = (IConstructor) TreeImploder.implodeTree(tree); 127 return loadModule(tree, env); 128 } 129 public IConstructor loadModule(IConstructor tree, MagnoliaEnvironment env) throws IOException { 130 if(isCons(tree, "MagnoliaTree", 2)) { 131 IValue head = getArg(tree, 0); 132 if(isCons(head, "ModuleHead", 2)) { 133 env.addImport(yield(getArg(head, 0)), tree); 134 for(IValue clause : getArgs(getArg(head, 1))) { 135 if(isCons(clause, "Imports", 1)) { 136 for(IValue imp : getArgs(getArg(clause, 0))) { 137 if(isCons(imp, "ImportAll", 1)) { 138 String name = yield(getArg(imp, 0)); 139 URI uri = ProjectURIResolver.constructProjectURI(project, new Path(name).addFileExtension("mg")); 140 loadModule(uri, env); 141 System.out.println("import " + name); 142 } 143 } 144 } 145 } 146 } 147 } 148 return tree; 149 } 116 150 public void setParser(ModuleParser parser) { 117 151 this.parser = parser; -
trunk/MagnoliaPlugin/src/org/magnolialang/load/ModuleParser.java
r369 r419 99 99 } 100 100 101 public IConstructor parseModule(URI location, InputStream source , ModuleEnvironment env) throws IOException {101 public IConstructor parseModule(URI location, InputStream source) throws IOException { 102 102 try { 103 103 return parseFromStream(getTable(), location, source); … … 107 107 } 108 108 109 public IConstructor parseModule(URI location, byte[] data , ModuleEnvironment env) throws IOException {109 public IConstructor parseModule(URI location, byte[] data) throws IOException { 110 110 try { 111 111 return parseFromData(getTable(), location, data); -
trunk/MagnoliaPlugin/src/org/magnolialang/parsetree/TreeImplodeVisitor.java
r414 r419 16 16 import org.rascalmpl.values.ValueFactoryFactory; 17 17 import org.rascalmpl.values.uptr.Factory; 18 import org.rascalmpl.values.uptr.ParsetreeAdapter; 18 19 import org.rascalmpl.values.uptr.ProductionAdapter; 19 20 import org.rascalmpl.values.uptr.SymbolAdapter; … … 66 67 } 67 68 } 68 69 if(cons == null) 70 cons = ProductionAdapter.getConstructorName(prod); 71 69 72 IConstructor result = null; 70 73 IList concrete = null; … … 76 79 concrete = null; // vf.list(child(0)); 77 80 } 81 else if(sort.equals("<START>")) { 82 ITuple t = visitChildren(TreeAdapter.getArgs(tree)); 83 concrete = (IList) t.get(1); 84 cons = cons != null ? cons : sort; 85 result = (IConstructor) ((IList) t.get(0)).get(0); 86 concrete = ((IList) result.getAnnotation("concrete")).insert(concrete.get(0)).append(concrete.get(2)); 87 } 78 88 else if(SymbolAdapter.isCf(rhs)) { 79 89 // the nested symbol within the cf(...) … … 113 123 ITuple t = visitChildren(TreeAdapter.getArgs(tree)); 114 124 concrete = (IList) t.get(1); 115 result = cons(cons == null ? cons : ProductionAdapter.getConstructorName(prod),125 result = cons(cons != null ? cons : sort, 116 126 sort, 117 127 (IList) t.get(0)); … … 126 136 result = result.setAnnotation("concrete", concrete); 127 137 } 138 else 139 return result; 128 140 129 141 return result; -
trunk/MagnoliaPlugin/src/org/magnolialang/parsetree/XaTreeAdapter.java
r417 r419 90 90 && ((IConstructor)tree).getConstructorType() == Type_Cons; 91 91 } 92 93 public static boolean isCons(IValue tree, String name) { 94 return tree instanceof IConstructor 95 && ((IConstructor)tree).getConstructorType() == Type_Cons 96 && ((IString)((IConstructor)tree).get("name")).getValue().equals(name); 97 } 98 99 public static boolean isCons(IValue tree, String name, int arity) { 100 return tree instanceof IConstructor 101 && ((IConstructor)tree).getConstructorType() == Type_Cons 102 && ((IString)((IConstructor)tree).get("name")).getValue().equals(name) 103 && ((IList)((IConstructor)tree).get("args")).length() == arity; 104 } 92 105 93 106 public static boolean isSeq(IValue tree) { … … 101 114 } 102 115 116 public static boolean isLeaf(IValue tree, String chars) { 117 return tree instanceof IConstructor 118 && ((IConstructor)tree).getConstructorType() == Type_Leaf 119 && ((IString)((IConstructor)tree).get("strVal")).getValue().equals(chars); 120 } 121 103 122 public static boolean isVar(IValue tree) { 104 123 return tree instanceof IConstructor … … 106 125 } 107 126 127 public static boolean isVar(IValue tree, String name) { 128 return tree instanceof IConstructor 129 && ((IConstructor)tree).getConstructorType() == Type_TermVar 130 && ((IString)((IConstructor)tree).get("name")).getValue().equals(name); 131 } 132 133 public static IList getArgs(IValue tree) { 134 assert isSeq(tree) || isCons(tree); 135 return (IList) ((IConstructor)tree).get("args"); 136 } 137 138 public static IValue getArg(IValue tree, int arg) { 139 assert isSeq(tree) || isCons(tree); 140 return ((IList) ((IConstructor)tree).get("args")).get(arg); 141 } 142 143 public static String getString(IValue tree) { 144 if(tree instanceof IString) 145 return ((IString)tree).getValue(); 146 else 147 return null; 148 } 108 149 public static boolean isGround(IValue tree) { 109 150 try { … … 144 185 return lw.done(); 145 186 } 187 188 public static String yield(IValue tree) { 189 try { 190 return tree.accept(new NullVisitor<String>() { 191 public String visitConstructor(IConstructor c) throws VisitorException { 192 IList concrete = (IList) c.getAnnotation("concrete"); 193 if(concrete == null || concrete.length() == 0) { 194 if(isLeaf(c)) 195 return ((IString)c.get("strVal")).getValue(); 196 else if(isVar(c)) 197 return "<" + ((IString)c.get("name")).getValue() + ">"; 198 else { 199 StringBuffer result = new StringBuffer(); 200 for(IValue child : getArgs(c)) 201 result.append(child.accept(this)); 202 return result.toString(); 203 } 204 } 205 IList args; 206 if(isLeaf(c) || isVar(c)) 207 args = vf.list(c.removeAnnotation("concrete")); 208 else 209 args = getArgs(c); 210 211 StringBuffer result = new StringBuffer(); 212 for(IValue token : concrete) { 213 Type type = ((IConstructor)token).getConstructorType(); 214 if(type == Type_Token || type == Type_Layout || type == Type_Comment) { 215 result.append(((IConstructor)token).get("chars")); 216 } 217 else { 218 int index = ((IInteger)((IConstructor)token).get("index")).intValue(); 219 result.append(args.get(index).accept(this)); 220 } 221 } 222 return result.toString(); 223 }}); 224 } catch (VisitorException e) { 225 return null; 226 } 227 } 146 228 } -
trunk/MagnoliaPlugin/src/org/magnolialang/parsetree/XaTreeFactory.java
r418 r419 83 83 public static IConstructor termvar(String name, String sort) { 84 84 return vf.constructor(Type_TermVar, vf.string(name), vf.string(sort)); 85 } 86 87 public static IConstructor termvar(String name) { 88 return vf.constructor(Type_TermVar, vf.string(name), vf.string("_")); 85 89 } 86 90
Note:
See TracChangeset
for help on using the changeset viewer.