Decl

 view release on metacpan or  search on metacpan

lib/Decl/Semantics/Parse.pm  view on Meta::CPAN

   
   $tree = new Decl;
   $tree->load (<<EOF);
      parse calculate
         ...
   EOF
   
   $result = $tree->parser('calculate')->parse('1 + 2 * (4 - 5)');
   
Here, C<$result> gets the value of -1.  If you call a non-code-generating parser like this, you'll get a Decl::Node structure back.


=head2 EXAMPLE: CLASS::DECLARATIVE'S OWN PARSER
The standard parser for a C<C::D> line is this:

   parse Dline
      tokens
         WORD
         LPAREN    "\("
         RPAREN    "\)"
         LBRACK    "\["
         RBRACK    "\]"
         COMMA     ","
         EQUALS    "="
         STRING    
         PARSEFLAG "<"
      actions
      rules

That's the actual parser used by default in C<Decl>.  You can override the line parser for a given tag; we use this for the 'select'
tag, for instance.  The indentation structure and bracketing is currently handled by C<Parse::Indented>, and that probably won't change (but you never
know).

=head2 EXAMPLE: SELECT PARSER

The select tag uses SQL to retrieve information from data iterators, and since SQL is, well, a standard query language (kind of), it's supported
natively in C<Decl>, mostly because we already have this fancy parser just sitting around ready to do that kind of thing.  The nice thing,
of course, is that means you don't have to write an SQL parser, because I've already done it for you.

   parse SQLselect

=head1 IMPLEMENTATION

This particular class implements the C<parse> node in the specification structure; the class L<Decl::Parser> implements the parser itself.
In other words, here we are concerned with building a C<Decl::Parser> object that will then be asked to do actual parsing.  The tags claimed
by user-defined parsers are also registered in this phase, constituting macros.

=head2 defines(), tags_defined()

Called by Decl::Semantics during import, to find out what xmlapi tags this plugin claims to implement.

=cut
sub defines { ('parse') }
sub tags_defined { Decl->new_data(<<EOF); }
parse (body=vanilla)
EOF

=head2 build_payload ()

The C<build> function is then called when this object's payload is built (i.e. in the stage when we're adding semantics to our
parsed syntax).  It builds the parser and registers its tag with the application.  Instances are handled by L<Decl::Semantics::Macro>.

=cut

sub build_payload {
   my ($self) = @_;
   
   my $p = Decl::Parser->new();
   $self->{payload} = $p;
   
   my $t = $self->find ('tokens');
   foreach ($t->elements) {
      $p->add_tokenizer ($_->name, $_->label); # TODO: error handling and default definitions for selected tokenizers
   }
   
   if ($self->name) {
      my $root = $self->root();
      $root->build_handler($self->name . "*", "", sub { Decl::Macro->new($self, @_) });
   }
}


=head1 AUTHOR

Michael Roberts, C<< <michael at vivtek.com> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-decl at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Decl>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.

=head1 LICENSE AND COPYRIGHT

Copyright 2010 Michael Roberts.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

=cut

1; # End of Decl::Semantics::Parse



( run in 1.740 second using v1.01-cache-2.11-cpan-39bf76dae61 )