Skip to content

Commit

Permalink
[grammars] add =begin/=end tags around code
Browse files Browse the repository at this point in the history
  • Loading branch information
perlpilot committed Jan 8, 2010
1 parent 6ef4d99 commit c8151a8
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/grammars.pod
Expand Up @@ -5,6 +5,8 @@ example demonstrates how to parse JSON, a data exchange format already
introduced in the chapter on multi dispatch (TODO: make this a proper
reference).

=begin programlisting

# file lib/JSON/Tiny/Grammar.pg

grammar JSON::Tiny::Grammar {
Expand Down Expand Up @@ -85,6 +87,8 @@ reference).
say "Not quite...";
}

=end programlisting

A grammar contains various named regexes, one of which is
called C<TOP>, and is called by C<JSON::Tiny.parse($string)>.

Expand Down Expand Up @@ -123,6 +127,8 @@ C<:sigspace> modifier. Which in turn internally replaces all whitespaces in
the regex to calls to the C<ws> token. So all you've got to do is to override
that:

=begin programlisting

grammar JSON::Tiny::Grammar::WithComments
is JSON::Tiny::Grammar {

Expand All @@ -140,6 +146,8 @@ that:
say "It's valid (modified) JSON";
}

=end programlisting

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
Expand All @@ -159,6 +167,8 @@ have to write a function that traverses the match tree recursively, and
search for bits and pieces you are interested in. Since this is a cumbersome
task, an alternative solution exist: I<actions method>.

=begin programlisting

class JSON::Tiny::Actions {
method TOP($/, $what) {
make $/{$what}.ast;
Expand Down Expand Up @@ -231,6 +241,8 @@ task, an alternative solution exist: I<actions method>.
# RAKUDO doesn't call action methods at the
# end of each each rule yet

=end programlisting

We pass an actions object to the grammar's C<parse> method. Whenever the
grammar engine finishes parsing one rule, or encounters a C<{*}> token in the
grammar, it calls an method of the match object, with the same name as
Expand All @@ -252,6 +264,8 @@ Although the rules and action methods live in different namespaces (and in a
real-world project probably even in separate files), we show them side by
side to make the correspondence easier to see.

=begin programlisting

rule TOP {
^ [
| <object> {*} #= object
Expand All @@ -262,20 +276,28 @@ side to make the correspondence easier to see.
make $/{$what}.ast;
}

=end programlisting

The rule has an alternation with two branches, labeled by C<#= object>
and C<#= array>. The first argument to the C<TOP> method is the current match
object, the second is the label. The action method accesses the capture with
the same name as branch, obtains the AST attached to it, and sets it to the
AST of current match object by calling C<make>.

=begin programlisting

rule object { '{' ~ '}' <pairlist> {*} }
method object($/) {
make %($<pairlist>.ast)
}

=end programlisting

The action method for C<object> extracts the AST of the C<pairlist> submatch,
and turns it into a hash by putting it inside C<%( ... )>.

=begin programlisting

rule pairlist {
<pair> ** ','
{*}
Expand All @@ -284,17 +306,23 @@ and turns it into a hash by putting it inside C<%( ... )>.
make = $<pair>».ast;
}

=end programlisting

The C<pairlist> rule just matches multiple pairs, and the corresponding action
method calls the C<.ast> method on each matched pair, and installs the result
list in its own AST.

=begin programlisting

rule pair {
<string> ':' <value> {*}
}
method pair($/) {
make ( $<string>.ast => $<value>.ast );
}

=end programlisting

A pair consists of a string key and a value, so the action method constructs a
Perl 6 pair with the C<< => >> operator.

Expand Down

0 comments on commit c8151a8

Please sign in to comment.