# Copyright (C) 2008, The Perl Foundation.
# $Id: /mirror/trunk/languages/json/src/parser/grammar.pg 28974 2008-07-02T09:57:42.150805Z kjs  $

=begin overview

This is the grammar for json written as a sequence of Perl 6 rules.

From http://www.json.org/

=end overview

grammar json::Grammar is PCT::Grammar;

rule TOP {
    <value>*
    [ $ || <.panic: 'end of file expected'> ]
    {*}
}

rule object {
    '{' <members>?  '}'
    {*}
}

rule array  {
    '[' <elements>? ']'
    {*}
}

rule string {
    \"<characters>\"
    {*}
}

token characters {
    <char>*
}

rule members {
    <string> ':' <value> [',' <string> ':' <value> ]*
    {*}
}

rule elements {
    <value> [',' <value> ]*
    {*}
}

token value {
    | <object> {*}                  #= object
    | <array> {*}                   #= array
    | <string> {*}                  #= string
    | <number> {*}                  #= number
    | <constant> {*}                #= constant
}

token constant {
    | 'true' {*}      #= true
    | 'false' {*}     #= false
    | 'null' {*}      #= null
}

# XXX need to add "except control char" to the final charclass here.
token char {
    | \\<["\\/bfnrt]>
    | \\u<xdigit>**{4}
    | <-[\\"]>
}

token number {
    <.ws>
    '-'?
    [ <[1..9]> <[0..9]>+ | <[0..9]> ]
    [ '.' <[0..9]>+ ]?
    [ <[Ee]> <[+\-]>? <[0..9]>+ ]?
    <.ws>
    {*}
}

# Local Variables:
#   mode: cperl
#   cperl-indent-level: 4
#   fill-column: 100
# End:
# vim: expandtab shiftwidth=4:

