Permalink
Browse files

Released version 0.15

  • Loading branch information...
ingydotnet committed Sep 9, 2012
1 parent 9f6e6b4 commit 244b65fe3c09dbb35969651dc94925bb2c61590c
Showing with 34 additions and 4 deletions.
  1. +5 −0 Changes
  2. +1 −1 lib/Pegex/JSON.pm
  3. +15 −2 lib/Pegex/JSON/Data.pm
  4. +1 −1 lib/Pegex/JSON/Grammar.pm
  5. +12 −0 t/scalar.t
View
@@ -1,4 +1,9 @@
---
+version: 0.15
+date: Sun Sep 9 16:13:29 PDT 2012
+changes:
+- Handle string escaping properly including unicode escapes and surrogate pairs.
+---
version: 0.14
date: Thu Aug 30 00:14:27 PDT 2012
changes:
View
@@ -20,7 +20,7 @@ extends 'Pegex::Module';
require Pegex::JSON::Grammar;
require Pegex::JSON::Data;
-our $VERSION = '0.14';
+our $VERSION = '0.15';
use constant grammar => 'Pegex::JSON::Grammar';
use constant receiver => 'Pegex::JSON::Data';
View
@@ -14,10 +14,23 @@ use boolean;
sub got_map { +{map @$_, map @$_, @{(pop)}} }
sub got_seq { [map @$_, @{(pop)}] }
+my %escapes = (
+ '"' => '"',
+ '/' => "/",
+ "\\" => "\\",
+ b => "\b",
+ f => "\x12",
+ n => "\n",
+ r => "\r",
+ t => "\t",
+);
+
sub got_string {
my $string = pop;
- # XXX need to decode other string escapes here
- $string =~ s/\\n/\n/g;
+ $string =~ s/\\(["\/\\bfnrt])/$escapes{$1}/ge;
+ # This handles JSON encoded Unicode surrogate pairs
+ $string =~ s/\\u([0-9a-f]{4})\\u([0-9a-f]{4})/pack "U*", hex("$1$2")/ge;
+ $string =~ s/\\u([0-9a-f]{4})/pack "U*", hex($1)/ge;
return $string;
}
@@ -122,7 +122,7 @@ sub make_tree {
]
},
'string' => {
- '.rgx' => qr/(?-xism:\G"((?:\\\\|\\"|[^"\n])*)")/
+ '.rgx' => qr/(?-xism:\G"((?:\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})|[^"\x00-\x1f])*)")/
},
'true' => {
'.rgx' => qr/(?-xism:\Gtrue)/
View
@@ -0,0 +1,12 @@
+use Test::More tests => 3;
+
+use Pegex::JSON;
+
+is + Pegex::JSON->parse('["a\bc\nd"]')->[0], "a\x{08}c\x0ad",
+ "Parse backslash escapes work";
+
+is + Pegex::JSON->parse('["\u2122"]')->[0], "\x{2122}",
+ "Unicode escape works";
+
+is + Pegex::JSON->parse('["\u0001\uf4a1"]')->[0], "\x{1f4a1}",
+ "Unicode escape works with surrogate pairs";

0 comments on commit 244b65f

Please sign in to comment.