NAME

MarpaX::Demo::JSONParser - A JSON parser with a choice of grammars

Synopsis

use MarpaX::Demo::JSONParser;

use Try::Tiny;

my($bnf_file) = 'data/json.1.bnf'; # Or data/json.2.bnf.
my($string)   = 'My data';

my($result);

# Use try to catch die.

try
{
	$result = MarpaX::Demo::JSONParser -> new(bnf_file => $bnf_file) -> parse($string);
};

# $result is undef, or what you hope for.

See t/basic.tests.t for sample code.

Description

MarpaX::Demo::JSONParser demonstrates 2 grammars for parsing JSON.

Only 1 grammar is loaded per run, as specified by the bnf_file option to new().

See t/basic.tests.t for sample code.

Installation

Install MarpaX::Demo::JSONParser as you would for any Perl module:

Run:

cpanm MarpaX::Demo::JSONParser

or run:

sudo cpan MarpaX::Demo::JSONParser

or unpack the distro, and then either:

perl Build.PL
./Build
./Build test
sudo ./Build install

or:

perl Makefile.PL
make (or dmake or nmake)
make test
make install

Constructor and Initialization

new() is called as my($parser) = MarpaX::Demo::JSONParser -> new(k1 => v1, k2 => v2, ...).

It returns a new object of type MarpaX::Demo::JSONParser.

Key-value pairs accepted in the parameter list (see corresponding methods for details [e.g. bnf_file([$string])]):

o bnf_file aUserGrammarFileName

Specify the name of the file containing your Marpa::R2-style grammar.

See data/json.1.bnf and data/json.2.bnf for the 2 cases handled by the code.

This option is mandatory.

Default: ''.

Methods

parse($string)

Parses the given $string using the grammar whose file name was provided by the bnf_file option to new().

Dies if the parse fails, or returns the result of the parse if it succeeded.

FAQ

Which JSON BNF is best?

This is not really a fair question. They were developed under different circumstances.

data/json.1.bnf was devised by Peter Stuifzand, the author of MarpaX::Languages::C::AST, and published originally as a gist.

data/json.2.bnf was devised by Jeffrey Kegler, the author of Marpa::R2.

json.1.bnf is the first attempt, when the Marpa SLIF still did not handle utf8. And it's meant to be a practical grammar. The sophisticated test suite is his, too.

json.2.bnf was written later, after Jeffey had a chance to study json.1.bnf. He used it to help optimise Marpa, but with a minimal test suite, so it had a different purpose.

I (Ron) converted their code into forms suitable for building this module.

Where is Marpa's Homepage?

http://jeffreykegler.github.io/Ocean-of-Awareness-blog/.

Are there any articles discussing Marpa?

Yes, many by its author, and several others. See Marpa's homepage, just above, and:

The Marpa Guide, (in progress, by Peter Stuifzand and Ron Savage).

Parsing a here doc, by Peter Stuifzand.

An update of parsing here docs, by Peter Stuifzand.

Conditional preservation of whitespace, by Ron Savage.

See Also

Marpa::Demo::StringParser.

Marpa::Grammar::Parser.

MarpaX::Languages::C::AST.

Machine-Readable Change Log

The file Changes was converted into Changelog.ini by Module::Metadata::Changes.

Version Numbers

Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions.

Support

Email the author, or log a bug on RT:

https://rt.cpan.org/Public/Dist/Display.html?Name=MarpaX::Demo::JSONParser.

Author

MarpaX::Demo::JSONParser was written by Ron Savage <ron@savage.net.au> in 2013.

Home page: http://savage.net.au/.

Copyright

Australian copyright (c) 2013, Ron Savage.

All Programs of mine are 'OSI Certified Open Source Software';
you can redistribute them and/or modify them under the terms of
The Artistic License 2.0, a copy of which is available at:
http://www.opensource.org/licenses/index.html