You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@systemml.apache.org by ni...@apache.org on 2015/12/08 22:32:17 UTC

incubator-systemml git commit: More elegant way to throw parse errors.

Repository: incubator-systemml
Updated Branches:
  refs/heads/master c27e74547 -> 3095fd4e5


More elegant way to throw parse errors.

Thanks to Luciano's tip :)

Project: http://git-wip-us.apache.org/repos/asf/incubator-systemml/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-systemml/commit/3095fd4e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-systemml/tree/3095fd4e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-systemml/diff/3095fd4e

Branch: refs/heads/master
Commit: 3095fd4e5c302cefb80479adb93607e3a00e431a
Parents: c27e745
Author: Niketan Pansare <np...@us.ibm.com>
Authored: Tue Dec 8 13:32:16 2015 -0800
Committer: Niketan Pansare <np...@us.ibm.com>
Committed: Tue Dec 8 13:32:16 2015 -0800

----------------------------------------------------------------------
 .../org/apache/sysml/parser/ParseException.java |  5 +++-
 .../sysml/parser/antlr4/DMLParserWrapper.java   | 25 ++++---------------
 .../sysml/parser/python/PyDMLParserWrapper.java | 26 +++++---------------
 3 files changed, 15 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/3095fd4e/src/main/java/org/apache/sysml/parser/ParseException.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/parser/ParseException.java b/src/main/java/org/apache/sysml/parser/ParseException.java
index 5937689..414dabf 100644
--- a/src/main/java/org/apache/sysml/parser/ParseException.java
+++ b/src/main/java/org/apache/sysml/parser/ParseException.java
@@ -63,7 +63,10 @@ public class ParseException extends Exception
   public ParseException(String message) {
     super(message);
   }
-
+  
+  public ParseException(String message, Exception e) {
+	  super(message, e);
+  }
   /**
    * This is the last token that has been consumed successfully.  If
    * this object has been created due to a parse error, the token

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/3095fd4e/src/main/java/org/apache/sysml/parser/antlr4/DMLParserWrapper.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/parser/antlr4/DMLParserWrapper.java b/src/main/java/org/apache/sysml/parser/antlr4/DMLParserWrapper.java
index ff16c0d..be7eb3d 100644
--- a/src/main/java/org/apache/sysml/parser/antlr4/DMLParserWrapper.java
+++ b/src/main/java/org/apache/sysml/parser/antlr4/DMLParserWrapper.java
@@ -174,11 +174,11 @@ public class DMLParserWrapper extends AParserWrapper
 //				in = new org.antlr.v4.runtime.ANTLRInputStream(new java.io.FileInputStream(fileName));
 //			}
 		} catch (FileNotFoundException e) {
-			throw new ParseException("ERROR: Cannot find file:" + fileName);
+			throw new ParseException("ERROR: Cannot find file:" + fileName, e);
 		} catch (IOException e) {
-			throw new ParseException("ERROR: Cannot open file:" + fileName);
+			throw new ParseException("ERROR: Cannot open file:" + fileName, e);
 		} catch (LanguageException e) {
-			throw new ParseException("ERROR: " + e.getMessage());
+			throw new ParseException("ERROR: " + e.getMessage(), e);
 		}
 
 		DmlprogramContext ast = null;
@@ -230,7 +230,7 @@ public class DMLParserWrapper extends AParserWrapper
 			}
 		}
 		catch(Exception e) {
-			throw getParseException(e, "ERROR: Cannot parse the program:" + fileName);
+			throw new ParseException("ERROR: Cannot parse the program:" + fileName, e);
 		}
 		
 
@@ -250,27 +250,12 @@ public class DMLParserWrapper extends AParserWrapper
 			dmlPgm = createDMLProgram(ast);
 		}
 		catch(Exception e) {
-			throw getParseException(e, "ERROR: Cannot translate the parse tree into DMLProgram");
+			throw new ParseException("ERROR: Cannot translate the parse tree into DMLProgram" + e.getMessage(), e);
 		}
 		
 		return dmlPgm;
 	}
 	
-	// Alternative is to uncomment the try/catch. But this method is preferred as it allows throwing "ParseException" as
-	// well as providing a given message (such as "Cannot translate the parse tree").
-	private ParseException getParseException(Exception e, String message) {
-		String stackTrace = null;
-		try {
-			PrintWriter printWriter = new PrintWriter(new StringWriter());
-			e.printStackTrace(printWriter);
-			stackTrace = printWriter.toString();
-		} catch(Exception e1) {}
-		if(stackTrace != null)
-			return new ParseException(message + ":\n" + stackTrace);
-		else
-			return new ParseException(message);
-	}
-
 	private DMLProgram createDMLProgram(DmlprogramContext ast) {
 
 		DMLProgram dmlPgm = new DMLProgram();

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/3095fd4e/src/main/java/org/apache/sysml/parser/python/PyDMLParserWrapper.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/parser/python/PyDMLParserWrapper.java b/src/main/java/org/apache/sysml/parser/python/PyDMLParserWrapper.java
index 72a9f0a..ffcf38c 100644
--- a/src/main/java/org/apache/sysml/parser/python/PyDMLParserWrapper.java
+++ b/src/main/java/org/apache/sysml/parser/python/PyDMLParserWrapper.java
@@ -151,11 +151,11 @@ public class PyDMLParserWrapper extends AParserWrapper
 //				in = new ANTLRInputStream(new FileInputStream(fileName));
 //			}
 		} catch (FileNotFoundException e) {
-			throw new ParseException("ERROR: Cannot find file:" + fileName);
+			throw new ParseException("ERROR: Cannot find file:" + fileName, e);
 		} catch (IOException e) {
-			throw new ParseException("ERROR: Cannot open file:" + fileName);
+			throw new ParseException("ERROR: Cannot open file:" + fileName, e);
 		} catch (LanguageException e) {
-			throw new ParseException("ERROR: " + e.getMessage());
+			throw new ParseException("ERROR: " + e.getMessage(), e);
 		}
 
 		PmlprogramContext ast = null;
@@ -205,7 +205,7 @@ public class PyDMLParserWrapper extends AParserWrapper
 			}
 		}
 		catch(Exception e) {
-			throw getParseException(e, "ERROR: Cannot parse the program:" + fileName);
+			throw new ParseException("ERROR: Cannot parse the program:" + fileName, e);
 		}
 		
 
@@ -225,26 +225,12 @@ public class PyDMLParserWrapper extends AParserWrapper
 			dmlPgm = createDMLProgram(ast);
 		}
 		catch(Exception e) {
-			throw getParseException(e, "ERROR: Cannot translate the parse tree into DMLProgram");
+			throw new ParseException("ERROR: Cannot translate the parse tree into DMLProgram" + e.getMessage(), e);
 		}
 		
 		return dmlPgm;
 	}
-	
-	// Alternative is to uncomment the try/catch. But this method is preferred as it allows throwing "ParseException" as
-	// well as providing a given message (such as "Cannot translate the parse tree").
-	private ParseException getParseException(Exception e, String message) {
-		String stackTrace = null;
-		try {
-			PrintWriter printWriter = new PrintWriter(new StringWriter());
-			e.printStackTrace(printWriter);
-			stackTrace = printWriter.toString();
-		} catch(Exception e1) {}
-		if(stackTrace != null)
-			return new ParseException(message + ":\n" + stackTrace);
-		else
-			return new ParseException(message);
-	}
+
 
 	private DMLProgram createDMLProgram(PmlprogramContext ast) {