Skip to content

Instantly share code, notes, and snippets.

@shayne
Created April 1, 2016 18:49
Show Gist options
  • Save shayne/699f3fd22aae26f4e6233042138d3077 to your computer and use it in GitHub Desktop.
Save shayne/699f3fd22aae26f4e6233042138d3077 to your computer and use it in GitHub Desktop.
Perl 6 JSON Grammar - playing with Perl6
use LWP::Simple;
grammar JsonGrammer {
token TOP { <object-value>|<array-value> }
token any-value { \s* <string-value>|<numeric-value>|<null-value>|<object-value>|<array-value> \s* }
token object-value { '{' <property> [\s* \, \s* <property> \s*]*'}' }
token array-value { '[' <any-value> [\s* \, \s* <any-value> \s*]*']' }
token property { \s* <key> \s* ':' \s* <any-value> \s* }
token key { \" \w+ \" }
token any-quotes { <["']> }
token string-value { <["']> <-["']>* <["']> }
token numeric-value { \d+[\.\d+]? }
token null-value { 'null' }
}
sub MAIN() {
my $json = LWP::Simple.get('http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22YHOO%22%2C%22AAPL%22%2C%22GOOG%22%2C%22MSFT%22)%0A%09%09&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json');
say JsonGrammer.parse($json);
# say JsonGrammer.parse('{"foo": "bar"}');
# say JsonGrammer.parse('{"foo": "bar", "baz": 1}');
# say JsonGrammer.parse('{"foo": "bar", "baz": 1.54}');
# say JsonGrammer.parse('{"foo": "bar", "baz": 1, "qux": null}');
# say JsonGrammer.parse('{"foo": "bar", "baz": 1, "qux": null, "foo": {"bar": "baz"}}');
# say JsonGrammer.parse('{"foo": "bar", "baz": 1, "qux": null, "foo": ["bar", \'baz\', 123, 45.67]}');
# say JsonGrammer.parse('{"foo":"+0.25","baz":1,"qux":null,"foo":["bar",\'baz\',123,45.67,[1,2,3],{"foo": "bar"}]}');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment