Skip to content
Browse files

Fix parsing of escaped Unicode chars in JSON

  • Loading branch information...
1 parent d251f2a commit 37b61c8eb5c7ca652a665a8ff4f70811615ab118 @mbrubeck mbrubeck committed
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 37b61c8

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