Devel::Declare::Parser(3pm) | User Contributed Perl Documentation | Devel::Declare::Parser(3pm) |
Devel::Declare::Parser - Higher level interface to Devel-Declare
Devel-Declare-Parser is a higher-level API sitting on top of Devel::Declare. It is used by Devel::Declare::Exporter to simplify exporting of Devel::Declare magic. Writing custom parsers usually only requires subclassing this module and overriding a couple methods.
package Devel::Declare::Parser::MyParser; use strict; use warnings; use base 'Devel::Declare::Parser'; use Devel::Declare::Interface; # Create an accessor (See INTERNALS WARNING below) __PACKAGE__->add_accessor( 'my_accessor' ); # Register the parser for use. Devel::Declare::Interface::register_parser( 'myparser' ); # Override the rewrite() method to take the parsed bits (parts) and put the # ones you want into new_parts. sub rewrite { my $self = shift; my $parts = $self->parts; $new_parts = $self->process_parts( $parts ); $self->new_parts( $new_parts ); 1; } 1;
This is a brief overview of how a parser is used.
This is usually the method you want to override.
'Parts' are datastructures created by the parse() method. Every argument on the line (space separated) up until an opening curly brace ({) or a semicolon (;) will be turned into a part. Here are the parts to expect:
Parts will either be a plain string, or an arrayref containing a string and the quote character used to define the string. "String" or [ "String", '"' ]. Variables and operators (excluding those containing only string characters) are typically the only parts left in a plain string form.
See the format_parts() method for an easy way to get what you need from a 'part' datastructure.
The structure will be an arrayref, the first element will be the string form of the bareword name, the second element will be undef.
Example:
# my_keyword My::Package; $part = [ 'My::Package', undef, ]; # my_keyword some_name; $part = [ "some_name", undef, ];
Example Structures:
# my_keyword "double quoted string"; $part = [ 'double quoted string', '"', ]; # my_keyword 'single quoted string'; $part = [ 'double quoted string', '"', ]; # my_keyword ... ( a => 'b', c => 'd' ); $part = [ " a => 'b', c => 'd' ", "(", ];
# my_keyword $variable $part = '$variable'; # my_keyword <=> $part = '<=>';
Parser is designed such that it will transform any and all uses of your keyword into proper function calls.
That is this:
function x { ... }
Will become this:
function( 'x', sub { ... });
Note Parser does not read in the entire codeblock, rather it injects a statement into the start of the block that uses a callback to attach the ');' to the end of the statement. This is per the documentation of Devel::Declare. Reading in the entire sub is not a desirable scenario.
Parser objects are blessed arrays, not hashrefs.
If you want to create a new accessor use the add_accessor() class method. It will take care of assigning an unused array element to the attribute, and will create a read/write accessor sub for you.
__PACKAGE__->add_accessor( 'my_accessor' );
There are many public and private methods on the parser base class. Only the public methods are fully documented. Be sure to refer often to the list of private methods at the end of this document, accidently overriding a private method could have devastating consequences.
NOTE: This has a global effect, all parsers will start debugging.
These are the read/write accessors used by Parser. Not all of these act on an array element, some will directly alter the current line.
These are methods you can, should, or may override in your baseclass.
This is usually the method you want to override.
These are used by pre_parse() to examine the line prior to any modification.
keyword( ... );
keyword word_or_string => ( ... );
These are methods that let you investigate the parts already parsed and placed in the parts() attribute.
This examines the line returning part structures and removing elements from the line each time they are called.
These methods help the parser determine what comes next in a line. In most cases these are non-modifying.
Do not use these, and definitely do not override them in a subclass.
This module is part of the Fennec project. See Fennec for more details. Fennec is a project to develop an extendable and powerful testing framework. Together the tools that make up the Fennec framework provide a potent testing environment.
The tools provided by Fennec are also useful on their own. Sometimes a tool created for Fennec is useful outside the greator framework. Such tools are turned into their own projects. This is one such project.
Chad Granum exodist7@gmail.com
Copyright (C) 2010 Chad Granum
Devel-Declare-Parser is free software; Standard perl licence.
Devel-Declare-Parser is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
2022-06-13 | perl v5.34.0 |