Browse files

Merge pull request #24 from mbrubeck/master

Fix parsing of escaped Unicode chars in JSON
  • Loading branch information...
2 parents d251f2a + 37b61c8 commit 19625f1bf00ec6c5c6090dc187f55d65f9b2b17f @groovecoder groovecoder committed Oct 4, 2012
Showing with 26 additions and 1 deletion.
  1. +4 −1 lib/kumascript/parser.pegjs
  2. +22 −0 tests/test-parser.js
View
5 lib/kumascript/parser.pegjs
@@ -83,7 +83,10 @@ JSONString
JSONChar
= [^"\\] /
- "\\" ( ["bfnrt\\/] / "u" [0-9] [0-9] [0-9] [0-9] )
+ "\\" ( ["bfnrt\\/] / "u" HexDigit HexDigit HexDigit HexDigit )
+
+HexDigit
+ = [0-9A-Fa-f]
JSONNumber
= "-" ? ( "0" / [1-9] [0-9]* ) ( "." [0-9]+ )? ( [eE] [+-]? [0-9]+ )?
View
22 tests/test-parser.js
@@ -53,5 +53,27 @@ module.exports = nodeunit.testCase({
test.deepEqual(tokens, [{type: "MACRO", name: "f", args: [{a: []}], offset: 0}],
"Empty JSON objects are parsed correctly");
test.done();
+ },
+
+ "Escaped Unicode codepoints are parsed correctly": function (test) {
+ var tokens = ks_parser.parse('{{ f({ "a": "\\u00f3" }) }}');
+ test.deepEqual(tokens,
+ [{type: "MACRO", name: "f", args: [{a: "\u00f3"}], offset: 0}],
+ "Lowercase hex digits are parsed correctly");
+
+ var tokens = ks_parser.parse('{{ f({ "a": "\\u00F3" }) }}');
+ test.deepEqual(tokens,
+ [{type: "MACRO", name: "f", args: [{a: "\u00f3"}], offset: 0}],
+ "Uppercase hex digits are parsed correctly");
+
+ test.throws(function() {
+ ks_parser.parse('{{ f({ "a": "\\uGHIJ" }) }}');
+ }, PEG.parser.SyntaxError, "Non-hexadecimal characters are not allowed");
+
+ test.throws(function() {
+ ks_parser.parse('{{ f({ "a": "\\uFF" }) }}');
+ }, PEG.parser.SyntaxError, "Four digits are required");
+
+ test.done();
}
});

0 comments on commit 19625f1

Please sign in to comment.