MarpaX::Demo::JSONParser - A JSON parser with a choice of grammars
MarpaX::Demo::JSONParser
#!/usr/bin/env perl use strict; use warnings; use MarpaX::Demo::JSONParser; use Try::Tiny; my($app_name) = 'MarpaX-Demo-JSONParser'; my($bnf_name) = 'json.1.bnf'; # Or 'json.2.bnf'. See scripts/find.grammars.pl below. my($bnf_file) = "data/$bnf_name"; my($string) = '{"test":"1.25e4"}'; my($message); my($result); # Use try to catch die. try { $message = ''; $result = MarpaX::Demo::JSONParser -> new(bnf_file => $bnf_file) -> parse($string); } catch { $message = $_; $result = 0; }; print $result ? "Result: test => $$result{test}. Expect: 1.25e4. \n" : "Parse failed. $message";
This script ships as scripts/demo.pl.
You can test failure by deleting the '{' character in line 17 of demo.pl and re-running it.
See also t/basic.tests.t for more sample code.
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().
bnf_file
new()
See t/basic.tests.t for sample code.
Install MarpaX::Demo::JSONParser as you would for any Perl module:
Perl
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
new() is called as my($parser) = MarpaX::Demo::JSONParser -> new(k1 => v1, k2 => v2, ...).
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])]):
Specify the name of the file containing your Marpa::R2-style grammar.
See data/json.1.bnf, data/json.2.bnf and data/json.3.bnf for the cases handled by the code.
This option is mandatory.
Default: ''.
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.
These JSON grammars are discussed in the "FAQ" below.
This JSON grammar was devised by Peter Stuifzand.
This JSON grammar was devised by Jeffrey Kegler.
This program is exactly what is displayed in the "Synopsis" above.
Before installation of this module, run it with:
shell> perl -Ilib scripts/demo.pl
And after installation, just use:
shell> perl scripts/demo.pl
After installation of the module, run it with:
shell> perl scripts/find.grammars.pl (Defaults to json.1.bnf) shell> perl scripts/find.grammars.pl json.1.bnf
Or use json.2.bnf or json.2.bnf.
It will print the name of the path to given grammar file.
They are not installed (when the source code is). They are shipped in the data/ dir.
I used to use File::ShareDir and Module::Install to install them, but Module::Install is now unusable. See Changes for details.
This is not really a fair question. They were developed under different circumstances.
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.
He developed this in August, 2014, after recent significant progress in the writing of Marpa.
http://savage.net.au/Marpa.html.
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.
MarpaX::Demo::StringParser.
MarpaX::Grammar::Parser.
MarpaX::Languages::C::AST.
The file Changes was converted into Changelog.ini by Module::Metadata::Changes.
https://github.com/ronsavage/MarpaX-Demo-JSONParser
Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions.
Email the author, or log a bug on RT:
https://rt.cpan.org/Public/Dist/Display.html?Name=MarpaX::Demo::JSONParser.
MarpaX::Demo::JSONParser was written by Ron Savage <ron@savage.net.au> in 2013.
Home page: http://savage.net.au/.
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 Perl License, a copy of which is available at: http://www.opensource.org/licenses/index.html
To install MarpaX::Demo::JSONParser, copy and paste the appropriate command in to your terminal.
cpanm
CPAN shell
perl -MCPAN -e shell install MarpaX::Demo::JSONParser
For more information on module installation, please visit the detailed CPAN module installation guide.