Skip to content
Browse files

fix parsing of \n's in strings (make sure they don't get double escaped)

  • Loading branch information...
1 parent 9d8363f commit 58904b3c0695825caa59b04dfbabf9db94192c27 @smtlaissezfaire committed
Showing with 50 additions and 1 deletion.
  1. +38 −1 lib/loop/grammar/token-builders.js
  2. +12 −0 spec/integration/string-spec.js
View
39 lib/loop/grammar/token-builders.js
@@ -39,10 +39,47 @@ tokenBuilders.makeNumber = function(intPart, fractionalPart, sourceInfo) {
};
};
+tokenBuilders.parseString = function(string) {
+ var i;
+ var ch;
+ var newStr = '';
+
+ // remove quotes
+ string = string.slice(1, -1);
+
+ for (i = 0; i < string.length; i++) {
+ ch = string[i];
+
+ // escape double backslashes
+ if (ch === '\\') {
+ i++;
+ ch = string[i];
+
+ switch (ch) {
+ // taken from uglify
+ case "n" : newStr += "\n"; break;
+ case "r" : newStr += "\r"; break;
+ case "t" : newStr += "\t"; break;
+ case "b" : newStr += "\b"; break;
+ case "v" : newStr += "\u000b"; break;
+ case "f" : newStr += "\f"; break;
+ // case "0" : newStr += "\0"; break;
+ case "x" : newStr += String.fromCharCode(hex_bytes(2)); break;
+ case "u" : newStr += String.fromCharCode(hex_bytes(4)); break;
+ // case "\n": newStr += ""; break;
+ }
+ } else {
+ newStr += ch;
+ }
+ }
+
+ return newStr;
+};
+
tokenBuilders.makeString = function(str, sourceInfo) {
return {
type: 'string',
- contents: str.slice(1, -1),
+ contents: tokenBuilders.parseString(str),
sourceInfo: sourceInfo
};
};
View
12 spec/integration/string-spec.js
@@ -0,0 +1,12 @@
+var vows = require("vows");
+var assert = require("assert");
+var loop = require(__dirname + "/../../lib/loop");
+
+vows.describe("strings").addBatch({
+ 'it should parse a simple double quoted string as a string': function() {
+ assert.equal(loop.compile("foo"), "foo");
+ },
+ 'it should keep control characters in a string': function() {
+ assert.equal(loop.compile('(define x "\\n")'), 'var x="\\n"');
+ }
+}).export(module);

0 comments on commit 58904b3

Please sign in to comment.
Something went wrong with that request. Please try again.