Permalink
Browse files

Fix parsing of string literals with a leading combining character.

Closes #25.
  • Loading branch information...
1 parent 4b5bff1 commit d8b3e130f99d74ba7e516133c9387b7630789b07 @moritz committed Dec 31, 2016
Showing with 26 additions and 10 deletions.
  1. +13 −1 lib/JSON/Tiny/Actions.pm
  2. +2 −1 lib/JSON/Tiny/Grammar.pm
  3. +2 −0 t/01-parse.t
  4. +9 −8 t/03-unicode.t
@@ -24,9 +24,21 @@ method arraylist($/) {
}
method string($/) {
- make +@$<str> == 1
+ my $str = +@$<str> == 1
?? $<str>[0].made
!! $<str>>>.made.join;
+
+ # see https://github.com/moritz/json/issues/25
+ # when a combining character comes after an opening quote,
+ # it doesn't become part of the quoted string, because
+ # it's stuffed into the same grapheme as the quote.
+ # so we need to extract those combining character(s)
+ # from the match of the opening quote, and stuff it into the string.
+ if $0.Str ne '"' {
+ my @chars := $0.Str.NFC;
+ $str = @chars[1..*].chrs ~ $str;
+ }
+ make $str
}
method value:sym<number>($/) { make +$/.Str }
method value:sym<string>($/) { make $<string>.made }
@@ -23,7 +23,8 @@ token value:sym<array> { <array> };
token value:sym<string> { <string> }
token string {
- \" ~ \" [ <str> | \\ <str=.str_escape> ]*
+ # see https://github.com/moritz/json/issues/25
+ (:ignoremark '"') ~ \" [ <str> | \\ <str=.str_escape> ]*
}
token str {
View
@@ -16,6 +16,8 @@ my @t =
' [ ] ',
# https://github.com/moritz/json/issues/24
qq/["\x[85]"]/,
+ # https://github.com/moritz/json/issues/25
+ qq/["̥̞ͫͩ̉̎͊ỏ̟͙̞̻̻̹̹r̵̨͇̫̦̺̮̼ͭ,̷̧̬̲͈̤̤̅̉͛ͫ͠ ̣͕̝̳̗̲̩̣͗̋͘e̼ͩ̔̿̾̋̾͘a̸̙͙͔̜͙̳̻͊ͯͪͤͬ͊͆͠c̦̃̓̈̾͗́͝͝ḩ͉͓̦̯̞̆ ̵͇͍ͪ̀́w̷ͩ͗̃͒͏̲̼̯̕i̞̫̜̮̣̰͎̅̂͋̀ͅń̢̨̪̭̠̼͕͎̐́͆̅ͅd̰̤̝͎̹͕ͩ̃̏̃̍͊̔ȏ̶̼̺̪̾ͨ̈̄ͤ̄̚̕w̙̻̱̙̺͎͚̙ͧͪ̊̒̕͝ ̴̼̰̱͈̠ͤ̋̏̇͛&̠̺̟̞͎̳̻̙̽̓́̔̇ͩ͛ͧ̚ ̡̡̫̻͐̌̐̌ͬͦ̂͞"]/,
# stolen from JSON::XS, 18_json_checker.t, and adapted a bit
Q<<[
"JSON Test Pattern pass1",
View
@@ -1,17 +1,18 @@
use v6;
-use JSON::Tiny::Grammar;
-use JSON::Tiny::Actions;
+use JSON::Tiny;
use Test;
-
my @t =
'{ "a" : "b\u00E5" }' => { 'a' => '' },
- '[ "\u2685" ]' => [ '' ];
+ '[ "\u2685" ]' => [ '' ],
+ # issue #25
+ qq{"x\c[ZERO WIDTH JOINER]a"} => "x\c[ZERO WIDTH JOINER]a",
+ qq{"\c[ZERO WIDTH JOINER]"} => "\c[ZERO WIDTH JOINER]",
+ ;
plan (+@t);
for @t -> $p {
- my $a = JSON::Tiny::Actions.new();
- my $o = JSON::Tiny::Grammar.parse($p.key, :actions($a));
- is-deeply $o.made, $p.value, "Correct data structure for «{$p.key}»"
- or say "# Got: {$o.made.perl}\n# Expected: {$p.value.perl}";
+ my $got = from-json($p.key);
+ is-deeply $got, $p.value, "Correct data structure for «{$p.key}»"
+ or say "# Got: $got.perl()\n# Expected: {$p.value.perl}";
}

0 comments on commit d8b3e13

Please sign in to comment.