Skip to content

Commit

Permalink
Make Pegex::JSON modern
Browse files Browse the repository at this point in the history
  • Loading branch information
ingydotnet committed Feb 26, 2014
1 parent b5420af commit 139d07a
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 77 deletions.
1 change: 0 additions & 1 deletion Makefile.PL

This file was deleted.

25 changes: 0 additions & 25 deletions README

This file was deleted.

29 changes: 29 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# NAME

Pegex::JSONY - Pegex Loader for JSON

# SYNOPSIS

my $data = Pegex::JSON->new->load($json);

# DESCRIPTION

Pegex::JSON is a JSON parser written in Pegex.

# SEE ALSO

- [Pegex](http://search.cpan.org/perldoc?Pegex)
- [JSON](http://search.cpan.org/perldoc?JSON)

# AUTHOR

Ingy döt Net (ingy) <ingy@cpan.org>

# COPYRIGHT

Copyright (c) 2011-2014 Ingy döt Net

# LICENSE

This library is free software and may be distributed under the same terms as
perl itself.
63 changes: 41 additions & 22 deletions lib/Pegex/JSON.pm
Original file line number Diff line number Diff line change
@@ -1,36 +1,55 @@
##
# name: Pegex::JSON
# abstract: Pegex Parser for JSON
# author: Ingy döt Net <ingy@cpan.org>
# license: perl
# copyright: 2011, 2012
# see:
# - Pegex
# - JSON
package Pegex::JSON;
# VERSION

use 5.010;
use Pegex::Base;

use Pegex 0.21 ();
use boolean 0.28 ();
use Pegex::Parser;
use Pegex::JSON::Grammar;
use Pegex::JSON::Data;

package Pegex::JSON;
use Pegex::Base;
extends 'Pegex::Module';
sub load {
my ($self, $json) = @_;
Pegex::Parser->new(
grammar => Pegex::JSON::Grammar->new,
receiver => Pegex::JSON::Data->new,
)->parse($json);
}

require Pegex::JSON::Grammar;
require Pegex::JSON::Data;
1;

our $VERSION = '0.16';
=encoding utf8
has grammar_class => 'Pegex::JSON::Grammar';
has receiver_class => 'Pegex::JSON::Data';
=head1 NAME
1;
Pegex::JSONY - Pegex Loader for JSON
=head1 SYNOPSIS
my $data = Pegex::JSON->parse($json);
my $data = Pegex::JSON->new->load($json);
=head1 DESCRIPTION
Pegex::JSON is a JSON parser written in Pegex.
=head1 SEE ALSO
=over
=item L<Pegex>
=item L<JSON>
=back
=head1 AUTHOR
Ingy döt Net (ingy) <ingy@cpan.org>
=head1 COPYRIGHT
Copyright (c) 2011-2014 Ingy döt Net
=head1 LICENSE
This library is free software and may be distributed under the same terms as
perl itself.
7 changes: 0 additions & 7 deletions lib/Pegex/JSON/Grammar.pm
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
##
# name: Pegex::JSON::Grammar
# abstract: Pegex Grammar for JSON
# author: Ingy döt Net <ingy@cpan.org>
# license: perl
# copyright: 2011, 2012

package Pegex::JSON::Grammar;
use Pegex::Base;
extends 'Pegex::Grammar';
Expand Down
5 changes: 0 additions & 5 deletions t/common.yaml

This file was deleted.

6 changes: 3 additions & 3 deletions t/scalar.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use Test::More tests => 3;

use Pegex::JSON;

is + Pegex::JSON->parse('["a\bc\nd"]')->[0], "a\x{08}c\x0ad",
is + Pegex::JSON->new->load('["a\bc\nd"]')->[0], "a\x{08}c\x0ad",
"Parse backslash escapes work";

is + Pegex::JSON->parse('["\u2122"]')->[0], "\x{2122}",
is + Pegex::JSON->new->load('["\u2122"]')->[0], "\x{2122}",
"Unicode escape works";

is + Pegex::JSON->parse('["\u0001\uf4a1"]')->[0], "\x{1f4a1}",
is + Pegex::JSON->new->load('["\u0001\uf4a1"]')->[0], "\x{1f4a1}",
"Unicode escape works with surrogate pairs";
42 changes: 28 additions & 14 deletions t/test.t
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
use TestML -run,
-require_or_skip => 'YAML::XS';
use lib 't', 'inc';
use TestML;
use TestML::Bridge;

use Pegex::JSON;
TestML->new(
testml => join('', <DATA>),
bridge => 'TestML::Bridge',
)->run;

# $Pegex::Parser::Debug = 1;
sub parse {
return Pegex::JSON->new->parse((shift)->value);
}

sub yaml {
my $yaml = YAML::XS::Dump((shift)->value);
$yaml =~ s/^---\s+//;
return $yaml;
{
package TestML::Bridge;
use TestML::Util;
use YAML::XS;

use Pegex::JSON;

# $Pegex::Parser::Debug = 1;
sub load {
my ($self, $str) = @_;
return str 'Pegex::JSON'->new->load($str->value);
}

sub yaml {
my ($self, $str) = @_;
my $yaml = YAML::XS::Dump($str->value);
$yaml =~ s/^---\s+//;
return str $yaml;
}
}

__DATA__
%TestML 1.0
%TestML 1.0.0
Plan = 11;
*json.parse.yaml == *yaml;
*json.load.yaml == *yaml;
=== Simple Mapping
--- json: {"a":1,"b":2}
Expand Down

0 comments on commit 139d07a

Please sign in to comment.