Repository: incubator-systemml
Updated Branches:
  refs/heads/master d853d74b2 -> 1b48ca0a9


Fixed incorrect handling of escape characters in the parser

The escape characters were not propogated by the parser to the runtime.
Consider the following script:
print("Foo:\t" + "blah\n---")

The above script will output:
Foo:\tblah\n---

Handling escape characters is especially important when the user wants
to provide tab seperated file. For example:
X = read("testInput.csv", format="csv", sep="\t")

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

Branch: refs/heads/master
Commit: 1b48ca0a91997f1efa72210dbb1d289359f043e6
Parents: d853d74
Author: Niketan Pansare <[email protected]>
Authored: Mon Apr 25 12:16:52 2016 -0700
Committer: Niketan Pansare <[email protected]>
Committed: Mon Apr 25 12:16:52 2016 -0700

----------------------------------------------------------------------
 .../parser/common/CommonSyntacticValidator.java | 21 ++++++++++++++------
 .../java/org/apache/sysml/parser/dml/Dml.g4     |  2 +-
 .../java/org/apache/sysml/parser/pydml/Pydml.g4 |  2 +-
 3 files changed, 17 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/1b48ca0a/src/main/java/org/apache/sysml/parser/common/CommonSyntacticValidator.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/parser/common/CommonSyntacticValidator.java 
b/src/main/java/org/apache/sysml/parser/common/CommonSyntacticValidator.java
index a7da73a..d7fdc5e 100644
--- a/src/main/java/org/apache/sysml/parser/common/CommonSyntacticValidator.java
+++ b/src/main/java/org/apache/sysml/parser/common/CommonSyntacticValidator.java
@@ -282,16 +282,25 @@ public abstract class CommonSyntacticValidator {
                }
        }
 
-       protected void constStringIdExpressionHelper(ParserRuleContext ctx, 
ExpressionInfo me) {
-               String val = "";
-               String text = ctx.getText();
+       protected String extractStringInQuotes(String text) {
+               String val = null;
                if(     (text.startsWith("\"") && text.endsWith("\"")) ||
                        (text.startsWith("\'") && text.endsWith("\'"))) {
                        if(text.length() > 2) {
-                               val = text.substring(1, text.length()-1);
+                               val = text.substring(1, text.length()-1)
+                                       .replaceAll("\\\\b","\b")
+                                       .replaceAll("\\\\t","\t")
+                                       .replaceAll("\\\\n","\n")
+                                       .replaceAll("\\\\f","\f")
+                                       .replaceAll("\\\\r","\r");
                        }
                }
-               else {
+               return val;
+       }
+       
+       protected void constStringIdExpressionHelper(ParserRuleContext ctx, 
ExpressionInfo me) {
+               String val = extractStringInQuotes(ctx.getText());
+               if(val == null) {
                        notifyErrorListeners("incorrect string literal ", 
ctx.start);
                        return;
                }
@@ -425,7 +434,7 @@ public abstract class CommonSyntacticValidator {
                String text = varValue;
                if(     (text.startsWith("\"") && text.endsWith("\"")) || 
(text.startsWith("\'") && text.endsWith("\'"))) {
                        if(text.length() > 2) {
-                               val = text.substring(1, text.length()-1);
+                               val = extractStringInQuotes(text);
                        }
                }
                else {

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/1b48ca0a/src/main/java/org/apache/sysml/parser/dml/Dml.g4
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/parser/dml/Dml.g4 
b/src/main/java/org/apache/sysml/parser/dml/Dml.g4
index 0ee12c6..31fcec0 100644
--- a/src/main/java/org/apache/sysml/parser/dml/Dml.g4
+++ b/src/main/java/org/apache/sysml/parser/dml/Dml.g4
@@ -210,7 +210,7 @@ COMMANDLINE_POSITION_ID: '$' DIGIT+;
 
 // supports single and double quoted string with escape characters
 STRING: '"' ( ESC | ~[\\"] )*? '"' | '\'' ( ESC | ~[\\'] )*? '\'';
-fragment ESC : '\\' [abtnfrv"'\\] ;
+fragment ESC : '\\' [btnfr"'\\] ;
 // Comments, whitespaces and new line
 LINE_COMMENT : '#' .*? '\r'? '\n' -> skip ;
 MULTILINE_BLOCK_COMMENT : '/*' .*? '*/' -> skip ;

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/1b48ca0a/src/main/java/org/apache/sysml/parser/pydml/Pydml.g4
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/parser/pydml/Pydml.g4 
b/src/main/java/org/apache/sysml/parser/pydml/Pydml.g4
index 9072f74..fda5c06 100644
--- a/src/main/java/org/apache/sysml/parser/pydml/Pydml.g4
+++ b/src/main/java/org/apache/sysml/parser/pydml/Pydml.g4
@@ -325,7 +325,7 @@ COMMANDLINE_POSITION_ID: '$' DIGIT+;
 
 // supports single and double quoted string with escape characters
 STRING: '"' ( ESC | ~[\\"] )*? '"' | '\'' ( ESC | ~[\\'] )*? '\'';
-fragment ESC : '\\' [abtnfrv"'\\] ;
+fragment ESC : '\\' [btnfr"'\\] ;
 // Comments, whitespaces and new line
 // LINE_COMMENT : '#' .*? '\r'? '\n' -> skip ;
 // MULTILINE_BLOCK_COMMENT : '/*' .*? '*/' -> skip ;

Reply via email to