Skip to content

Commit

Permalink
[grammars] explain grammar inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz committed Oct 24, 2009
1 parent 077ab7c commit 0116ca3
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/grammars.pod
Expand Up @@ -72,7 +72,6 @@ reference).
}

# test it:
# TODO: look up actual population of Austria
my $tester = '{
"country": "Austria",
"cities": [ "Wien", "Salzburg", "Innsbruck" ],
Expand Down Expand Up @@ -128,12 +127,28 @@ that:
is JSON::Tiny::Grammar {

token ws {
| \s* '//' \N* \n
| <?ww> \s+
| <!ww> \s*
\s* [ '//' \N* \n ]?
}
}

# TODO: explanation
my $tester = '{
"country": "Austria",
"cities": [ "Wien", "Salzburg", "Innsbruck" ],
"population": 8353243 // data from 2009-01
}';
if JSON::Tiny::Grammar::WithComments.parse($tester) {
say "It's valid (modified) JSON";
}

The first two lines introduce a grammar that inherits from
C<JSON::Tiny::Grammar>. The inheritance is specified with the C<is> trait.
This means that the grammar rules are now called from the child grammar if it
exists there, and from the parent grammar otherwise - just like normal method
calls.

In (our relaxed) JSON whitespaces are never mandatory, so the C<ws> is allowed
to match nothing at all. After optional spaces two slashes C<'//'> introduce a
comment, which is followed by an arbitrary number of characters that are not
newlines, and then a newline -- in prose: it extends to the rest of the line.

=for vim: spell
=for vim: spell spelllang=en tw=78

0 comments on commit 0116ca3

Please sign in to comment.