Devel:...Parser(3pm)
โจ Devel:...Parser(3pm) โจ
User Contributed Perl Documentation ยท Devel:...Parser(3pm)
okay bestie, buckle up, this one's a whole journey ๐
๐ NAME ๐
Devel::Declare::Parser โ Higher level interface to Devel-Declare
๐ฆ DESCRIPTION ๐ฆ
So like. Devel-Declare-Parser is a higher-level API that sits right on top of Devel::Declare. It's the glow-up layer, the little lift, the extra sparkle โจ
And who uses it? Devel::Declare::Exporter, that's who โ it leans on Parser to make exporting Devel::Declare magic wayyy simpler than it has any right to be.
Here's the tea though: writing your own custom parsers is usually not that deep. Like, most of the time you just subclass this module, override a couple of methods, and boom โ you're done. Iconic behaviour, honestly. ๐
๐ DOCUMENTATION ๐
Devel::Declare::Interfaceโ this is the primary interface for anyone who wants to useDevel-Declare-Parsermagic and does not want to mess withExporter-Declare. It's giving "just give me the powers, thanks" ๐ซถDevel::Declare::Parserโ and this doc right here? This is the API reference forDevel::Declare::Parseritself. Super handy when you're writing or modifying a custom parser, iykyk ๐
โญ SYNOPSIS โญ
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;
psst โ see that (See INTERNALS WARNING below) comment in there? It's pointing at a section that lives further down in this doc, so don't go looking for it in this part, bestie ๐
๐ OVERVIEW ๐
A quick lil tour of how a parser actually gets used, because the vibe is: know the flow, slay the flow ๐ซ
๐ WORKFLOW ๐
- Parser is constructed โ Name, Declarator, and Offset are provided by
Devel::Declare. - The
process()method is called โprocesscalls all of the following in sequence. And here's the catch: if any of them returns false,process()will return. No further steps, it just dips. ๐ช๐จpre_parse()โ check if we even want to process the line at all. Vibe check first, always.parse()โ turn the line into 'parts' (more on that below).post_parse()โ a hook. Currently does nothing. It's just standing there, being a hook. ๐rewrite()โ a hook, and it currently takes all the arguments between the declarator and the codeblock/semicolon (which have been turned into 'parts' structures in theparts()attribute) and puts them into thenew_parts()attribute.
this is usually the method you want to override. Like, if you only read one line of this whole doc, it's this one, fr fr ๐ฅ
write_line()โ opens, fills in, and closes the line as a string, then rewrites the actual line usingDevel::Declare. Very "assembly line but make it cute" ๐ญโจedit_line()โ another hook. Also currently does nothing. Two hooks, zero jobs. ๐ญ
๐ง "PARTS" ๐ง
'Parts' are data structures created by the parse() method. Every argument on the line (space separated) up until an opening curly brace ({) or a semicolon (;) gets turned into a part. Here's what you should expect to see:
A part will either be a plain string, or an arrayref containing a string plus the quote character that was used to define the string. So: "String" or [ "String", '"' ]. Variables and operators (except for the ones that contain only string characters) are typically the only parts left hanging around in plain-string form.
And if you want an easy way to get what you need out of a 'part' data structure? Go look at the format_parts() method. It's there for you, bestie ๐ซถ
๐ธ Bareword or Package Name ๐ธ
A bareword name is anything that starts with [a-zA-z] and contains only alphanumerics plus underscore. It's also not quoted. Examples include my_name, something5, etc.
The structure will be an arrayref: first element is the string form of the bareword name, second element is undef.
Examples:
# my_keyword My::Package;
$part = [
'My::Package',
undef,
];
# my_keyword some_name;
$part = [
"some_name",
undef,
];
๐ Quoted or Enclosed Element ๐
A quoted or enclosed element covers strings quoted with single or double quotes, plus text contained within opening and closing brackets, braces or parens โ and heads up, that's excluding the curly brace '{'.
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' ",
"(",
];
๐ Variable or Operator ๐
Anything starting with a non-alphanumeric, non-quoting character gets placed as-is (not interpolated) into a string. This catches most variables and most operators โ the exception being alpha-numeric operators like 'eq', 'gt', 'cmp', etc.
And the author's note on that: eventually there's a plan to add logic to catch all operators, but it looks like they'll have to hard-code them. So, you know. Future plans. ๐ ๏ธ
# my_keyword $variable
$part = '$variable';
# my_keyword <=>
$part = '<=>';
๐ EVENTUAL OUTPUT ๐
Parser is designed so that it will transform any and all uses of your keyword into proper function calls. That's the whole point. That's the assignment. ๐
So this:
function x { ... }
Will become this:
function( 'x', sub { ... });
important note: Parser does not read in the entire codeblock. Instead it injects a statement into the start of the block that uses a callback to attach the');'to the end of the statement. That's per the documentation ofDevel::Declare. And reading in the entire sub is not a desirable scenario โ so like, don't do that, bestie ๐ญ๐ซ
๐ฎ DEVEL-DECLARE-PARSER API ๐ฎ
๐จ INTERNALS WARNING ๐จ
okay bestie, sit down for this one because it matters ๐
- Parser objects are blessed arrays, not hashrefs. ikr. arrays. not hashes. this is not a drill.
- 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 it will create a read/write accessor sub for you. so you literally don't have to do it yourself. king behavior fr fr.
__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 ๐ญ like not even joking, that's the actual wording, it's that serious.
๐ฆ CLASS METHODS ๐ฆ
$class->new( $name, $declarator, $offset )โ The constructor. And the docs literally scream "DO NOT OVERRIDE THIS!" at you, so like. don't. ๐$class->DEBUG($bool)โ Turn debugging on/off. This will output the line after it has been modified, as well as some context information. it's giving โจinsightโจ
NOTE: This has a global effect โ all parsers will start debugging. every single one. so use it with intention, bestie.
๐งฐ UTILITY METHODS ๐งฐ
bail( @messages )โ Likecroak, dies providing you context information. Since any death occurs inside the parser,carpprovides useless information. sobailis the glow-up version ๐diag( @message )โ Likecarp, warns providing you context information. Since the warn occurs inside the parsercarpprovides useless information. same energy, but for warnings.end_quote($start_char)โ Find the end-character for the provided starting quote character. As in'{'returns'}'and'('returns')'. If there is no counter-part, the start character is returned:"'"returns"'". matched sets only, no cap.filename()โ Filename the rewrite is occurring against.linenum()โ Linenum the rewrite is occurring on.format_part()โ Returns the stringified form of a part datastructure. For variables and operators that is just the item itself as a string. For barewords or package names it is the item itself with single quotes wrapped around it. For quoted items it is the string wrapped in its proper quoting characters. If a second parameter is provided (and true) no single quotes will be added to barewords. it's the little formatting fairy of your dreams ๐ง
๐ ACCESSORS ๐
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. messy? maybe. iconic? absolutely.
line()โ This will retrieve the current line from Devel-Declare. If given a value, that value will be set as the current line using Devel-Declare.name()โ Name of the declarator as provided via the parser.declarator()โ Name of the declarator as provided via the Devel-Declare.original_offset()โ Offset on the line when the parsing was started.offset()โ Current line offset.parts()โ Arrayref of parts (may beundef)new_parts()โ Arrayref of new parts (may beundef)end_char()โ Will be set to the character just after the completely parsed line (usually'{'or';')prototype()โ Used internally for prototype tracking.contained()โ True if the parser determined this was a contained call. This means your keyword was followed by an opening paren, and the statement ended with a closing paren and semicolon. By default Parser will not modify such lines.
๐ฆ OVERRIDABLE METHODS ๐ฆ
These are methods you can, should, or may override in your baseclass. your moment to shine, bestie.
quote_chars()โ Specify the starting characters for quoted strings. (returns a list)end_chars()โ Characters to recognise as end of statement characters (';'and'{') (returns a list)inject()โ Code to inject into functions enhanced by this parser.pre_parse()โ Check if we want to process the line at all.parse()โ Turn the line into'parts'.post_parse()โ Hook, currently does nothing.rewrite()โ Hook, currently takes all the arguments between the declarator and the codeblock/semicolon (which have been turned into'parts'structures in theparts()attribute) and puts them into thenew_parts()attribute.
This is usually the method you want to override. ๐ yes, THIS one. the main character method.
write_line()โ Opens, fills in, and closes the line as a string, then rewrites the actual line using Devel::Declare.edit_line()โ Hook, currently does nothing.open_line()โ Usually returns'('. This is how to start a line following your keywordclose_line()โ End the line, this means either re-inserting the opening'{'on the codeblock, along with any injections, or returning');'
๐ POSITION TRACKING ๐
advance( $num_chars )โ Advances the offset by$num_chars.skip_declarator()โ Skips the declarator at the start of the line.skipspace()โ Advances the offset past any whitespace.
๐ LINE EXAMINATION (NON-MODIFYING) ๐
These are used by pre_parse() to examine the line prior to any modification. looking, not touching ๐
is_contained()โ True if the line is of the format:
keyword( ... );
is_arrow_contained()โ True if the line is of the format:
keyword word_or_string => ( ... );
is_defenition()โ True if the line matches the regexm/sub[\s\n]+$name/sm
๐งฉ PART EXAMINATION ๐งฉ
These are methods that let you investigate the parts already parsed and placed in the parts() attribute.
has_non_string_or_quote_parts()โ Returns a list of parts that are not strings, quotes, or barewords.has_string_or_quote_parts()โ Returns a list of parts that are strings, quotes, or barewords.has_keyword( $word )โ Check for a keyword in the partshas_comma()has_fat_comma()
โ๏ธ LINE EXAMINATION (MODIFYING) โ๏ธ
This examines the line returning part structures and removing elements from the line each time they are called. it's giving chaotic and we love that for it.
strip_item()strip_length()strip_remaining_items()
๐ LOOKING AHEAD ๐
These methods help the parser determine what comes next in a line. In most cases these are non-modifying. manifesting the future of your line โจ
peek_is_block()peek_is_end()peek_is_other()peek_is_quote()peek_is_word()peek_item()peek_item_type()peek_num_chars()peek_other()peek_quote()peek_remaining()peek_word()
๐ PRIVATE METHODS ๐
Do not use these, and definitely do not override them in a subclass. like fr fr do NOT.
_block_end_injection()_debug()_edit_block_end()_item_via_()_linestr_offset_from_dd()_move_via_()_peek_is_package()_peek_is_word()_quoted_from_dd()_scope_end()_stash()_unstash()
๐ FENNEC PROJECT ๐
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. ๐ฆ
- Fennec - The core framework โ The primary Fennec project that ties them all together.
๐ AUTHORS ๐
Chad Granum exodist7@gmail.com
ยฉ๏ธ COPYRIGHT ยฉ๏ธ
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.
okay that's the whole thing, bestie ๐ซถ you made it to the end, slay. this page is from perl v5.36.0 and it's dated 2023-10-26 โ so if you're reading this, hi from the archive, hope your parser is behaving ๐
๐ฆ where this page comes from
- debian trixie: libdevel-declare-parser-perl 0.021-1 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libdevel-declare-parser-perl 0.021-1 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libdevel-declare-parser-perl 0.021-1 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libdevel-declare-parser-perl 0.021-1 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms