Skip to content

Instantly share code, notes, and snippets.

@moritz
Created May 18, 2012 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moritz/2724830 to your computer and use it in GitHub Desktop.
Save moritz/2724830 to your computer and use it in GitHub Desktop.
JSON::Tiny grammar -- parsefail when doing % \,
use v6;
grammar JSON::Tiny::Grammar;
rule TOP { ^[ <object> | <array> ]$ }
rule object { '{' ~ '}' <pairlist> }
rule pairlist { <pair>* % \, }
rule pair { <string> ':' <value> }
rule array { '[' ~ ']' <arraylist> }
rule arraylist { <value>* % \, }
proto token value {*};
token value:sym<number> {
'-'?
[ 0 | <[1..9]> <[0..9]>* ]
[ \. <[0..9]>+ ]?
[ <[eE]> [\+|\-]? <[0..9]>+ ]?
}
token value:sym<true> { <sym> };
token value:sym<false> { <sym> };
token value:sym<null> { <sym> };
token value:sym<object> { <object> };
token value:sym<array> { <array> };
token value:sym<string> { <string> }
token string {
\" ~ \" ( <str> | \\ <str_escape> )*
}
token str {
<-["\\\t\n]>+
}
token str_escape {
<["\\/bfnrt]> | u <xdigit>**4
}
say so JSON::Tiny::Grammar.parse('[ "a", "b" ]');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment