[Swift-commit] r6026 - trunk/src/org/griphyn/vdl/engine

hategan at ci.uchicago.edu hategan at ci.uchicago.edu
Sat Nov 10 19:53:55 CST 2012


Author: hategan
Date: 2012-11-10 19:53:55 -0600 (Sat, 10 Nov 2012)
New Revision: 6026

Added:
   trunk/src/org/griphyn/vdl/engine/CompilerUtils.java
   trunk/src/org/griphyn/vdl/engine/Warnings.java
Modified:
   trunk/src/org/griphyn/vdl/engine/Karajan.java
   trunk/src/org/griphyn/vdl/engine/VariableScope.java
Log:
make error messages about variables use line numbers instead of cryptic java hash codes of some random object; warnings only displayed once per message

Added: trunk/src/org/griphyn/vdl/engine/CompilerUtils.java
===================================================================
--- trunk/src/org/griphyn/vdl/engine/CompilerUtils.java	                        (rev 0)
+++ trunk/src/org/griphyn/vdl/engine/CompilerUtils.java	2012-11-11 01:53:55 UTC (rev 6026)
@@ -0,0 +1,41 @@
+//----------------------------------------------------------------------
+//This code is developed as part of the Java CoG Kit project
+//The terms of the license can be found at http://www.cogkit.org/license
+//This message may not be removed or altered.
+//----------------------------------------------------------------------
+
+/*
+ * Created on Nov 10, 2012
+ */
+package org.griphyn.vdl.engine;
+
+import org.apache.xmlbeans.XmlObject;
+import org.w3c.dom.Node;
+
+public class CompilerUtils {
+    public static String getLine(Node n) {
+        if (n == null) {
+            return "line unknown";
+        }
+        Node src = n.getAttributes().getNamedItem("src");
+        if (src == null) {
+            return getLine(n.getParentNode());
+        }
+        else {
+            return src.getNodeValue();
+        }
+    }
+
+    public static String getLine(XmlObject src) {
+        if (src == null) {
+            return null;
+        }
+        String loc = src.getDomNode().getAttributes().getNamedItem("src").getNodeValue();
+        if (loc == null) {
+            return null;
+        }
+        else {
+            return loc.substring(loc.indexOf(' ') + 1);
+        }
+    }
+}

Modified: trunk/src/org/griphyn/vdl/engine/Karajan.java
===================================================================
--- trunk/src/org/griphyn/vdl/engine/Karajan.java	2012-11-10 20:23:23 UTC (rev 6025)
+++ trunk/src/org/griphyn/vdl/engine/Karajan.java	2012-11-11 01:53:55 UTC (rev 6026)
@@ -64,6 +64,8 @@
 import org.safehaus.uuid.UUIDGenerator;
 import org.w3c.dom.Node;
 
+import static org.griphyn.vdl.engine.CompilerUtils.*;
+
 public class Karajan {
 	public static final Logger logger = Logger.getLogger(Karajan.class);
 
@@ -172,26 +174,7 @@
 	protected StringTemplate template(String name) {
 		return m_templates.getInstanceOf(name);
 	}
-	
-	private void warn(XmlObject obj, String msg) {
-	    msg = "Warning: " + msg + ", at " + getLine(obj.getDomNode());
-	    logger.info(msg);
-	    System.err.println(msg);
-    }
 
-	private String getLine(Node n) {
-	    if (n == null) {
-	        return "line unknown";
-	    }
-        Node src = n.getAttributes().getNamedItem("src");
-        if (src == null) {
-            return getLine(n.getParentNode());
-        }
-        else {
-            return src.getNodeValue();
-        }
-    }
-
     private void processImports(Program prog) throws CompilationException {
 
 		Imports imports = prog.getImports();
@@ -351,10 +334,6 @@
 	    }
     }
 	
-	private String getLine(String src) {
-		return src.substring(src.indexOf(' ') + 1);
-	}
-	
 	private void setVariableUsed(String s) {
 	    usedVariables.add(s);
     }
@@ -364,7 +343,7 @@
 		VariableScope innerScope = new VariableScope(this, outerScope, VariableScope.ENCLOSURE_NONE);
 		StringTemplate procST = template("procedure");
 		containingScope.bodyTemplate.setAttribute("procedures", procST);
-		procST.setAttribute("line", getLine(proc.getSrc()));
+		procST.setAttribute("line", getLine(proc));
 		procST.setAttribute("name", mangle(proc.getName()));
 		for (int i = 0; i < proc.sizeOfOutputArray(); i++) {
 			FormalParameter param = proc.getOutputArray(i);
@@ -376,7 +355,7 @@
 				procST.setAttribute("arguments", paramST);
 			String type = normalize(param.getType().getLocalPart());
             checkIsTypeDefined(type);
-            innerScope.addVariable(param.getName(), type);
+            innerScope.addVariable(param.getName(), type, param);
 		}
 		for (int i = 0; i < proc.sizeOfInputArray(); i++) {
 			FormalParameter param = proc.getInputArray(i);
@@ -388,7 +367,7 @@
 				procST.setAttribute("arguments", paramST);
 			String type = normalize(param.getType().getLocalPart());
 			checkIsTypeDefined(type);
-			outerScope.addVariable(param.getName(), type);
+			outerScope.addVariable(param.getName(), type, param);
 		}
 		
 		Binding bind;
@@ -464,7 +443,7 @@
 	public void variableForSymbol(Variable var, VariableScope scope) throws CompilationException {
 
 		checkIsTypeDefined(var.getType().getLocalPart());
-		scope.addVariable(var.getName(), var.getType().getLocalPart(), var.getIsGlobal());
+		scope.addVariable(var.getName(), var.getType().getLocalPart(), var.getIsGlobal(), var);
 	}
 
 	public void variable(Variable var, VariableScope scope) throws CompilationException {
@@ -472,7 +451,7 @@
 		variableST.setAttribute("name", var.getName());
 		variableST.setAttribute("type", var.getType().getLocalPart());
 		variableST.setAttribute("isGlobal", Boolean.valueOf(var.getIsGlobal()));
-		variableST.setAttribute("line", getLine(var.getSrc()));
+		variableST.setAttribute("line", getLine(var));
 		variables.add(variableST);
 
 		if(!var.isNil()) {
@@ -519,7 +498,7 @@
 						scope.bodyTemplate.setAttribute("declarations",variableDeclarationST);
 						StringTemplate paramValueST=expressionToKarajan(param.getAbstractExpression(),scope);
 						String paramValueType = datatype(paramValueST);
-						scope.addVariable(parameterVariableName, paramValueType);
+						scope.addVariable(parameterVariableName, paramValueType, param);
 						variableDeclarationST.setAttribute("type", paramValueType);
 						StringTemplate variableReferenceST = template("id");
 						variableReferenceST.setAttribute("var",parameterVariableName);
@@ -575,7 +554,7 @@
 						" to a variable of type " + datatype(varST));
 			assignST.setAttribute("var", varST);
 			assignST.setAttribute("value", valueST);
-			assignST.setAttribute("line", getLine(assign.getSrc()));
+			assignST.setAttribute("line", getLine(assign));
 			String rootvar = abstractExpressionToRootVariable(assign.getAbstractExpressionArray(0));
 			scope.addWriter(rootvar, new Integer(callID++), rootVariableIsPartial(assign.getAbstractExpressionArray(0)));
 			scope.appendStatement(assignST);
@@ -716,7 +695,7 @@
 				("Unknown procedure invocation mode "+proc.getInvocationMode());
 			}
 			callST.setAttribute("func", mangle(procName));
-			callST.setAttribute("line", getLine(call.getSrc()));
+			callST.setAttribute("line", getLine(call));
 			/* Does number of input arguments match */
 			for (int i = 0; i < proc.sizeOfInputArray(); i++) {
 				if (proc.getInputArray(i).isOptional())
@@ -919,10 +898,10 @@
 		VariableScope loopScope = new VariableScope(this, scope, VariableScope.ENCLOSURE_LOOP);
 		VariableScope innerScope = new VariableScope(this, loopScope, VariableScope.ENCLOSURE_LOOP);
 
-		loopScope.addVariable(iterate.getVar(), "int");
+		loopScope.addVariable(iterate.getVar(), "int", iterate);
 
 		StringTemplate iterateST = template("iterate");
-		iterateST.setAttribute("line", getLine(iterate.getSrc()));
+		iterateST.setAttribute("line", getLine(iterate));
 
 		iterateST.setAttribute("var", iterate.getVar());
 		innerScope.bodyTemplate = iterateST;
@@ -959,11 +938,11 @@
 			if (itemType == null) {
 			    throw new CompilationException("You can iterate through an array structure only");
 			}
-			innerScope.addVariable(foreach.getVar(), itemType);
+			innerScope.addVariable(foreach.getVar(), itemType, foreach);
 			foreachST.setAttribute("indexVar", foreach.getIndexVar());
 			foreachST.setAttribute("indexVarType", keyType);
 			if(foreach.getIndexVar() != null) {
-				innerScope.addVariable(foreach.getIndexVar(), keyType);
+				innerScope.addVariable(foreach.getIndexVar(), keyType, foreach);
 			}
 
 			innerScope.bodyTemplate = foreachST;
@@ -992,7 +971,7 @@
 		StringTemplate ifST = template("if");
 		StringTemplate conditionST = expressionToKarajan(ifstat.getAbstractExpression(), scope);
 		ifST.setAttribute("condition", conditionST.toString());
-		ifST.setAttribute("line", getLine(ifstat.getSrc()));
+		ifST.setAttribute("line", getLine(ifstat));
 		if (!datatype(conditionST).equals("boolean"))
 			throw new CompilationException ("Condition in if statement has to be of boolean type.");
 
@@ -1157,7 +1136,7 @@
 	public StringTemplate function(Function func, VariableScope scope) throws CompilationException {
 		StringTemplate funcST = template("function");
 		funcST.setAttribute("name", mangle(func.getName()));
-		funcST.setAttribute("line", getLine(func.getSrc()));
+		funcST.setAttribute("line", getLine(func));
 		ProcedureSignature funcSignature =  functionsMap.get(func.getName());
 		if(funcSignature == null) {
 			throw new CompilationException("Unknown function: @"+func.getName());
@@ -1506,7 +1485,7 @@
 				/* Functions have only one output parameter */
 				st.setAttribute("datatype", funcSignature.getOutputArray(0).getType());
 				if (funcSignature.isDeprecated()) {
-				    warn(f, "Function " + name + " is deprecated");
+				    Warnings.warn(f, "Function " + name + " is deprecated");
 				}
 			} else
 				throw new CompilationException("Function " + name + " is not defined.");
@@ -1530,12 +1509,12 @@
 		        		"return value to be used in an expression.");
 		    }
 		    
-		    warn(c, "Procedure " + name + " is deprecated");
+		    Warnings.warn(c, "Procedure " + name + " is deprecated");
 
 		    StringTemplate call = template("callexpr");
 
 		    String type = funcSignature.getOutputArray(0).getType();
-		    subscope.addInternalVariable("swift#callintermediate", type);
+		    subscope.addInternalVariable("swift#callintermediate", type, null);
 
 		    call.setAttribute("datatype", type);
 		    call.setAttribute("call", call(c, subscope, true));

Modified: trunk/src/org/griphyn/vdl/engine/VariableScope.java
===================================================================
--- trunk/src/org/griphyn/vdl/engine/VariableScope.java	2012-11-10 20:23:23 UTC (rev 6025)
+++ trunk/src/org/griphyn/vdl/engine/VariableScope.java	2012-11-11 01:53:55 UTC (rev 6026)
@@ -17,14 +17,7 @@
 
 package org.griphyn.vdl.engine;
 
-import org.antlr.stringtemplate.StringTemplate;
-
-import org.apache.log4j.Logger;
-
-import org.griphyn.vdl.karajan.CompilationException;
-
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -32,7 +25,12 @@
 import java.util.Map;
 import java.util.Set;
 
+import org.antlr.stringtemplate.StringTemplate;
+import org.apache.log4j.Logger;
+import org.apache.xmlbeans.XmlObject;
+import org.griphyn.vdl.karajan.CompilationException;
 
+
 public class VariableScope {
 
 	/** permit array up-assignment, but not entire variables */
@@ -76,7 +74,7 @@
 	private Set<String> inhibitClosing = new HashSet<String>();
 
 	public VariableScope(Karajan c, VariableScope parent) {
-		this(c,parent,ENCLOSURE_ALL);
+		this(c, parent, ENCLOSURE_ALL);
 	}
 
 	/** Creates a new variable scope.
@@ -87,11 +85,14 @@
 			scopes will be handled.
 	*/
 	public VariableScope(Karajan c, VariableScope parent, int a) {
-		if(parentScope!=null) {
-			logger.debug("New scope "+hashCode()+" with parent scope "+parentScope.hashCode());
-		} else {
-			logger.debug("New scope "+hashCode()+" with no parent.");
-		}
+	    if (logger.isDebugEnabled()) {
+    		if (parentScope != null) {
+    			logger.debug("New scope " + hashCode() + " with parent scope " + parentScope.hashCode());
+    		} 
+    		else {
+    			logger.debug("New scope " + hashCode() + " with no parent.");
+    		}
+	    }
 		compiler = c;
 		parentScope = parent;
 		enclosureType = a;
@@ -105,13 +106,13 @@
 
 	/** Set of variables (String token names) that are declared in
 	    this scope - not variables in parent scopes, though. */
-	Set<String> variables = new HashSet<String>();
+	Map<String, XmlObject> variables = new HashMap<String, XmlObject>();
 	Map<String, String> varTypes = new HashMap<String, String>();
 
 	/** Set of variables (String token names) which are global and
 	    declared in this scope (which must be a root scope). Variables
 	    in this set must also appear in the variables set. */
-	Set<String> globals = new HashSet<String>();
+	Map<String, XmlObject> globals = new HashMap<String, XmlObject>();
 
 	/** Asserts that a named variable is declared in this scope.
 	    Might also eventually contain more information about the
@@ -119,41 +120,41 @@
 	    declaration already exists. Perhaps error in same scope and
 	    warning if it shadows an outer scope? */
 
-	public void addVariable(String name, String type) throws CompilationException {
-		addVariable(name,type,false);
+	public void addVariable(String name, String type, XmlObject src) throws CompilationException {
+		addVariable(name, type, false, src);
 	}
 	
 	public void inhibitClosing(String name) {
 		inhibitClosing.add(name);
 	}
 
-	public void addVariable(String name, String type, boolean global) throws CompilationException {
-		logger.debug("Adding variable "+name+" of type "+type+" to scope "+hashCode());
+	public void addVariable(String name, String type, boolean global, XmlObject src) throws CompilationException {
+	    if (logger.isDebugEnabled()) {
+	        logger.debug("Adding variable " + name + " of type " + type + " to scope " + hashCode());
+	    }
 		
 		if(isVariableDefined(name)) {
-			throw new CompilationException("Variable "+name+" is already defined.");
+			throw new CompilationException("Variable " + name + ", on line " 
+			    + CompilerUtils.getLine(src) + ", was already defined on line " + getDefinitionLine(name));
 		}
 
 		// TODO does this if() ever fire? or is it always taken
 		// by the above if? in which case isVariableDefined should
 		// be replaced by is locally defined test.
 		if(parentScope != null && parentScope.isVariableDefined(name)) {
-			logger.warn("Variable "+name+" defined in scope "+hashCode()
-			+ " shadows variable of same name in scope "
-			+ parentScope.hashCode());
+		    Warnings.warn("Variable " + name + ", defined on line " + CompilerUtils.getLine(src)
+			+ ", shadows variable of same name on line " + parentScope.getDefinitionLine(name));
 		}
 
 		if(global && this != rootScope) {
-			throw new CompilationException("Global "+name+" can only be declared in the root scope of a program.");
+			throw new CompilationException("Global " + name + " can only be declared in the root scope of a program.");
 		}
 
-		boolean added = variables.add(name);
+		variables.put(name, src);
 		varTypes.put(name, type);
-		if(!added) throw new CompilationException("Could not add variable "+name+" to scope.");
-
-		if(global) {
-			added = globals.add(name);
-			if(!added) throw new CompilationException("Could not add global variable "+name+" to scope.");
+		
+		if (global) {
+			globals.put(name, src);
 		}
 	}
 	
@@ -161,22 +162,47 @@
 	 * Does pretty much the same as addVariable() except it doesn't throw
 	 * an exception if the variable is defined in a parent scope
 	 */
-	public void addInternalVariable(String name, String type) throws CompilationException {
-		logger.debug("Adding internal variable " + name + " of type " + type + " to scope " + hashCode());
+	public void addInternalVariable(String name, String type, XmlObject src) throws CompilationException {
+	    if (logger.isDebugEnabled()) {
+	        logger.debug("Adding internal variable " + name + " of type " + type + " to scope " + hashCode());
+	    }
 		
 		if(isVariableLocallyDefined(name)) {
-			throw new CompilationException("Variable " + name + " is already defined.");
+			throw new CompilationException("Variable " + name + ", on line " 
+			    + CompilerUtils.getLine(src) + ", was already defined on line " + getDefinitionLine(name));
 		}
 
-		boolean added = variables.add(name);
+		variables.put(name, src);
 		varTypes.put(name, type);
-		if(!added) throw new CompilationException("Could not add variable "+name+" to scope.");
 	}
+	
+	public String getDefinitionLine(String name) {
+	    XmlObject src = rootScope.getGlobalSrc(name);
+	    if (src == null) {
+	        src = getSrcRecursive(name);
+	    }
+	    if (src != null) {
+	        return CompilerUtils.getLine(src);
+	    }
+	    else {
+	        return "unknown";
+	    }
+	}
 
 	public boolean isVariableDefined(String name) {
 		if(rootScope.isGlobalDefined(name)) return true;
 		return isVariableDefinedRecursive(name);
 	}
+	
+	private XmlObject getSrcRecursive(String name) {
+	    XmlObject src = variables.get(name);
+	    if (src == null) {
+	        if (enclosureType != ENCLOSURE_PROCEDURE && parentScope != null) {
+	            src = parentScope.getSrcRecursive(name);
+	        }
+	    }
+	    return src;
+	}
 
 	private boolean isVariableDefinedRecursive(String name) {
 		if(isVariableLocallyDefined(name)) return true;
@@ -185,8 +211,12 @@
 	}
 
 	public boolean isGlobalDefined(String name) {
-		return globals.contains(name);
+		return globals.containsKey(name);
 	}
+	
+	public XmlObject getGlobalSrc(String name) {
+	    return globals.get(name);
+	}
 
 	/** note that this will return variable types for variables in
 	    containing scopes even if they are not accessible. non-null
@@ -207,7 +237,7 @@
 	public boolean isVariableWriteable(String name, boolean partialWriter) {
 		if(isVariableLocallyDefined(name)) return true;
 		if(parentScope != null && parentScope.isVariableWriteable(name, true) && enclosureType == ENCLOSURE_CONDITION) {
-		    logger.warn("Warning: variable " + name + " might have multiple writers");
+		    Warnings.warn("Variable " + name + ", defined on line " + getDefinitionLine(name) + ", might have multiple writers");
 		    return true;
 		}
 		if(parentScope != null && parentScope.isVariableWriteable(name, partialWriter) && enclosureType == ENCLOSURE_ALL) return true;
@@ -217,7 +247,7 @@
 
 
 	public boolean isVariableLocallyDefined(String name) {
-		return variables.contains(name);
+		return variables.containsKey(name);
 	}
 
 	/** List of templates to be executed in sequence after the present
@@ -234,18 +264,22 @@
 	public void addWriter(String variableName, Object closeID, boolean partialWriter) throws CompilationException
 	{
 		if(!isVariableDefined(variableName)) {
-			throw new CompilationException("Variable "+variableName+" is not defined");
+			throw new CompilationException("Variable " + variableName + " is not defined");
 		}
 
 		if(isVariableLocallyDefined(variableName)) {
 			StringTemplate ld = getLocalDeclaration(variableName);
-			if(ld != null) {
-				if(!partialWriter && ld.getAttribute("waitfor")!=null) {
-					throw new CompilationException("variable "+variableName+" has multiple writers.");
+			if (ld != null) {
+				if(!partialWriter && ld.getAttribute("waitfor") != null) {
+					throw new CompilationException("variable " + variableName + ", defined on line " 
+					    + getDefinitionLine(variableName) + ", has multiple writers.");
 				}
 				ld.setAttribute("waitfor", closeID);
-			} else {
-				logger.debug("Variable "+variableName+" is local but has no template.");
+			} 
+			else {
+			    if (logger.isDebugEnabled()) {
+			        logger.debug("Variable " + variableName + " is local but has no template.");
+			    }
 			}
 			outputs.add(variableName);
 			if (!inhibitClosing.contains(variableName)) {
@@ -276,10 +310,13 @@
 				if(!statementList.contains(closeID)) {
 					statementList.add(closeID);
 				}
-				logger.debug("added "+closeID+" to variable "+variableName+" in scope "+hashCode());
+				if (logger.isDebugEnabled()) {
+				    logger.debug("added  " + closeID + " to variable " 
+				        + variableName + " in scope " + hashCode());
+				}
 			} else {
 			    isVariableWriteable(variableName, partialWriter);
-				throw new CompilationException("variable "+variableName+" is not writeable in this scope");
+				throw new CompilationException("variable " + variableName + " is not writeable in this scope");
 			}
 		}
 	}
@@ -306,15 +343,19 @@
 		     @SuppressWarnings("unchecked")
 		     List<StringTemplate> list = (List<StringTemplate>) decls;
 		     for (StringTemplate declST : list) { 
-		         logger.debug("looking at declaration "+declST);
+		         if (logger.isDebugEnabled()) {
+		             logger.debug("looking at declaration " + declST);
+		         }
 		         try {
 		             if(declST.getAttribute("name").equals(name)) {
-		                 logger.debug("thats the declaration for "+name);
+		                 if (logger.isDebugEnabled()) {
+		                     logger.debug("thats the declaration for " + name);
+		                 }
 		                 return declST;
 		             }
-		         } catch(java.util.NoSuchElementException nse) {
-		             logger.debug
-		             ("it so definitely wasn't in that one, we got an exception.");
+		         } 
+		         catch (java.util.NoSuchElementException nse) {
+		             logger.debug("it so definitely wasn't in that one, we got an exception.");
 		             // TODO this is not a nice use of exceptions...
 		         }
 		     }
@@ -387,7 +428,7 @@
 
     public List<String> getCleanups() {
         List<String> cleanups = new ArrayList<String>();
-        for (String var : variables) {
+        for (String var : variables.keySet()) {
             String type = getVariableType(var);
             if (!org.griphyn.vdl.type.Types.isPrimitive(type)) {
                 cleanups.add(var);

Added: trunk/src/org/griphyn/vdl/engine/Warnings.java
===================================================================
--- trunk/src/org/griphyn/vdl/engine/Warnings.java	                        (rev 0)
+++ trunk/src/org/griphyn/vdl/engine/Warnings.java	2012-11-11 01:53:55 UTC (rev 6026)
@@ -0,0 +1,41 @@
+//----------------------------------------------------------------------
+//This code is developed as part of the Java CoG Kit project
+//The terms of the license can be found at http://www.cogkit.org/license
+//This message may not be removed or altered.
+//----------------------------------------------------------------------
+
+/*
+ * Created on Nov 10, 2012
+ */
+package org.griphyn.vdl.engine;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.log4j.Logger;
+import org.apache.xmlbeans.XmlObject;
+import org.w3c.dom.Node;
+
+public class Warnings {
+    public static final Logger logger = Logger.getLogger(Warnings.class);
+    
+    private static Set<String> warnings = new HashSet<String>();
+    
+    public static void warn(XmlObject obj, String msg) {
+        if (!warnings.contains(msg)) {
+            warnings.add(msg);
+            msg = "Warning: " + msg + ", at " + CompilerUtils.getLine(obj.getDomNode());
+            logger.info(msg);
+            System.err.println(msg);
+        }
+    }
+    
+    public static void warn(String msg) {
+        if (!warnings.contains(msg)) {
+            warnings.add(msg);
+            msg = "Warning: " + msg;
+            logger.info(msg);
+            System.err.println(msg);
+        }
+    }
+}




More information about the Swift-commit mailing list