.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Devel::Declare::Parser 3pm" .TH Devel::Declare::Parser 3pm "2023-10-26" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Devel::Declare::Parser \- Higher level interface to Devel\-Declare .SH "DESCRIPTION" .IX Header "DESCRIPTION" Devel-Declare-Parser is a higher-level \s-1API\s0 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. .SH "DOCUMENTATION" .IX Header "DOCUMENTATION" .IP "Devel::Declare::Interface" 4 .IX Item "Devel::Declare::Interface" This is the primary interface for those who want to use Devel-Declare-Parser magic, and don't want to use Exporter-Declare. .IP "Devel::Declare::Parser" 4 .IX Item "Devel::Declare::Parser" This Document covers the \s-1API\s0 for Devel::Declare::Parser. This \s-1API\s0 is a useful reference when writing or modifying a custom parser. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 3 \& package Devel::Declare::Parser::MyParser; \& use strict; \& use warnings; \& \& use base \*(AqDevel::Declare::Parser\*(Aq; \& use Devel::Declare::Interface; \& \& # Create an accessor (See INTERNALS WARNING below) \& _\|_PACKAGE_\|_\->add_accessor( \*(Aqmy_accessor\*(Aq ); \& \& # Register the parser for use. \& Devel::Declare::Interface::register_parser( \*(Aqmyparser\*(Aq ); \& \& # 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; .Ve .SH "OVERVIEW" .IX Header "OVERVIEW" This is a brief overview of how a parser is used. .SS "\s-1WORKFLOW\s0" .IX Subsection "WORKFLOW" .IP "Parser is constructed" 4 .IX Item "Parser is constructed" Name, Declarator, and Offset are provided by Devel::Declare. .IP "The \fBprocess()\fR method is called" 4 .IX Item "The process() method is called" The process method calls all of the following in sequence, if any returns false, \fBprocess()\fR will return. .RS 4 .IP "\fBpre_parse()\fR" 8 .IX Item "pre_parse()" Check if we want to process the line at all. .IP "\fBparse()\fR" 8 .IX Item "parse()" Turn the line into 'parts' (see below). .IP "\fBpost_parse()\fR" 8 .IX Item "post_parse()" Hook, currently does nothing. .IP "\fBrewrite()\fR" 8 .IX Item "rewrite()" Hook, currently takes all the arguments between the declarator and the codeblock/semicolon (which have been turned into 'parts' structures in the \&\fBparts()\fR attribute) and puts them into the \fBnew_parts()\fR attribute. .Sp This is usually the method you want to override. .IP "\fBwrite_line()\fR" 8 .IX Item "write_line()" Opens, fills in, and closes the line as a string, then rewrites the actual line using Devel::Declare. .IP "\fBedit_line()\fR" 8 .IX Item "edit_line()" Hook, currently does nothing. .RE .RS 4 .RE .ie n .SS """\s-1PARTS""\s0" .el .SS "``\s-1PARTS''\s0" .IX Subsection "PARTS" \&'Parts' are datastructures created by the \fBparse()\fR 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: .PP Parts will either be a plain string, or an arrayref containing a string and the quote character used to define the string. \*(L"String\*(R" or [ \*(L"String\*(R", '"' ]. Variables and operators (excluding those containing only string characters) are typically the only parts left in a plain string form. .PP See the \fBformat_parts()\fR method for an easy way to get what you need from a \&'part' datastructure. .IP "Bareword or Package Name" 4 .IX Item "Bareword or Package Name" A bareword name is anything that starts with [a\-zA\-z] and contains only alpha-numerics plus underscore. It is also not quoted. Examples include my_name, something5, etc. .Sp The structure will be an arrayref, the first element will be the string form of the bareword name, the second element will be undef. .Sp Example: .Sp .Vb 5 \& # my_keyword My::Package; \& $part = [ \& \*(AqMy::Package\*(Aq, \& undef, \& ]; \& \& # my_keyword some_name; \& $part = [ \& "some_name", \& undef, \& ]; .Ve .IP "Quoted or Enclosed Element" 4 .IX Item "Quoted or Enclosed Element" A quoted or enclosed element includes strings quoted with single or double quotes, and text contained within opening and closing brackets, braces or parens (excluding the curly brace '{'). .Sp Example Structures: .Sp .Vb 5 \& # my_keyword "double quoted string"; \& $part = [ \& \*(Aqdouble quoted string\*(Aq, \& \*(Aq"\*(Aq, \& ]; \& \& # my_keyword \*(Aqsingle quoted string\*(Aq; \& $part = [ \& \*(Aqdouble quoted string\*(Aq, \& \*(Aq"\*(Aq, \& ]; \& \& # my_keyword ... ( a => \*(Aqb\*(Aq, c => \*(Aqd\*(Aq ); \& $part = [ \& " a => \*(Aqb\*(Aq, c => \*(Aqd\*(Aq ", \& "(", \& ]; .Ve .IP "Variable or Operator" 4 .IX Item "Variable or Operator" Anything starting with a non-alphanumeric, non-quoting character will be placed as-is (not interpolated) into a string. This catches most variables and operators, the exception are alpha-numeric operators such as 'eq', 'gt', 'cmp', etc. Eventually I plan to add logic to catch all operators, but it appears I will have to hard-code them. .Sp .Vb 2 \& # my_keyword $variable \& $part = \*(Aq$variable\*(Aq; \& \& # my_keyword <=> \& $part = \*(Aq<=>\*(Aq; .Ve .SS "\s-1EVENTUAL OUTPUT\s0" .IX Subsection "EVENTUAL OUTPUT" Parser is designed such that it will transform any and all uses of your keyword into proper function calls. .PP That is this: .PP .Vb 1 \& function x { ... } .Ve .PP Will become this: .PP .Vb 1 \& function( \*(Aqx\*(Aq, sub { ... }); .Ve .PP \&\fBNote\fR 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. .SH "DEVEL-DECLARE-PARSER API" .IX Header "DEVEL-DECLARE-PARSER API" .SS "\s-1INTERNALS WARNING\s0" .IX Subsection "INTERNALS WARNING" \&\fBParser objects are blessed arrays, not hashrefs.\fR .PP If you want to create a new accessor use the \fBadd_accessor()\fR 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. .PP .Vb 1 \& _\|_PACKAGE_\|_\->add_accessor( \*(Aqmy_accessor\*(Aq ); .Ve .PP 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. .SS "\s-1CLASS METHODS\s0" .IX Subsection "CLASS METHODS" .ie n .IP "$class\->new( $name, $declarator, $offset )" 4 .el .IP "\f(CW$class\fR\->new( \f(CW$name\fR, \f(CW$declarator\fR, \f(CW$offset\fR )" 4 .IX Item "$class->new( $name, $declarator, $offset )" The constructor, \*(L"\s-1DO NOT OVERRIDE THIS\s0!\*(R" .ie n .IP "$class\->\s-1DEBUG\s0($bool)" 4 .el .IP "\f(CW$class\fR\->\s-1DEBUG\s0($bool)" 4 .IX Item "$class->DEBUG($bool)" Turn debugging on/off. This will output the line after it has been modified, as well as some context information. .Sp \&\fB\s-1NOTE:\s0\fR This has a global effect, all parsers will start debugging. .SS "\s-1UTILITY METHODS\s0" .IX Subsection "UTILITY METHODS" .ie n .IP "bail( @messages )" 4 .el .IP "bail( \f(CW@messages\fR )" 4 .IX Item "bail( @messages )" Like croak, dies providing you context information. Since any death occurs inside the parser, carp provides useless information. .ie n .IP "diag( @message )" 4 .el .IP "diag( \f(CW@message\fR )" 4 .IX Item "diag( @message )" Like carp, warns providing you context information. Since the warn occurs inside the parser carp provides useless information. .IP "end_quote($start_char)" 4 .IX Item "end_quote($start_char)" Find the end-character for the provide starting quote character. As in '{' returns '}' and '(' returns ')'. If there is no counter-part the start character is returned: \*(L"'\*(R" returns \*(L"'\*(R". .IP "\fBfilename()\fR" 4 .IX Item "filename()" Filename the rewrite is occurring against. .IP "\fBlinenum()\fR" 4 .IX Item "linenum()" Linenum the rewrite is occurring on. .IP "\fBformat_part()\fR" 4 .IX Item "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. .SS "\s-1ACCESSORS\s0" .IX Subsection "ACCESSORS" These are the read/write accessors used by Parser. \fBNot all of these act on an array element, some will directly alter the current line.\fR .IP "\fBline()\fR" 4 .IX Item "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. .IP "\fBname()\fR" 4 .IX Item "name()" Name of the declarator as provided via the parser. .IP "\fBdeclarator()\fR" 4 .IX Item "declarator()" Name of the declarator as provided via the Devel-Declare. .IP "\fBoriginal_offset()\fR" 4 .IX Item "original_offset()" Offset on the line when the parsing was started. .IP "\fBoffset()\fR" 4 .IX Item "offset()" Current line offset. .IP "\fBparts()\fR" 4 .IX Item "parts()" Arrayref of parts (may be undef) .IP "\fBnew_parts()\fR" 4 .IX Item "new_parts()" Arrayref of new parts (may be undef) .IP "\fBend_char()\fR" 4 .IX Item "end_char()" Will be set to the character just after the completely parsed line (usually '{' or ';') .IP "\fBprototype()\fR" 4 .IX Item "prototype()" Used internally for prototype tracking. .IP "\fBcontained()\fR" 4 .IX Item "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. .SS "\s-1OVERRIDABLE METHODS\s0" .IX Subsection "OVERRIDABLE METHODS" These are methods you can, should, or may override in your baseclass. .IP "\fBquote_chars()\fR" 4 .IX Item "quote_chars()" Specify the starting characters for quoted strings. (returns a list) .IP "\fBend_chars()\fR" 4 .IX Item "end_chars()" Characters to recognise as end of statement characters (';' and '{') (returns a list) .IP "\fBinject()\fR" 4 .IX Item "inject()" Code to inject into functions enhanced by this parser. .IP "\fBpre_parse()\fR" 4 .IX Item "pre_parse()" Check if we want to process the line at all. .IP "\fBparse()\fR" 4 .IX Item "parse()" Turn the line into 'parts'. .IP "\fBpost_parse()\fR" 4 .IX Item "post_parse()" Hook, currently does nothing. .IP "\fBrewrite()\fR" 4 .IX Item "rewrite()" Hook, currently takes all the arguments between the declarator and the codeblock/semicolon (which have been turned into 'parts' structures in the \&\fBparts()\fR attribute) and puts them into the \fBnew_parts()\fR attribute. .Sp This is usually the method you want to override. .IP "\fBwrite_line()\fR" 4 .IX Item "write_line()" Opens, fills in, and closes the line as a string, then rewrites the actual line using Devel::Declare. .IP "\fBedit_line()\fR" 4 .IX Item "edit_line()" Hook, currently does nothing. .IP "\fBopen_line()\fR" 4 .IX Item "open_line()" Usually returns '('. This is how to start a line following your keyword .IP "\fBclose_line()\fR" 4 .IX Item "close_line()" End the line, this means either re-inserting the opening '{' on the codeblock, along with any injections, or returning ');' .SS "\s-1POSITION TRACKING\s0" .IX Subsection "POSITION TRACKING" .ie n .IP "advance( $num_chars )" 4 .el .IP "advance( \f(CW$num_chars\fR )" 4 .IX Item "advance( $num_chars )" Advances the offset by \f(CW$num_chars\fR. .IP "\fBskip_declarator()\fR" 4 .IX Item "skip_declarator()" Skips the declarator at the start of the line. .IP "\fBskipspace()\fR" 4 .IX Item "skipspace()" Advances the offset past any whitespace. .SS "\s-1LINE EXAMINATION\s0 (NON-MODIFYING)" .IX Subsection "LINE EXAMINATION (NON-MODIFYING)" These are used by \fBpre_parse()\fR to examine the line prior to any modification. .IP "\fBis_contained()\fR" 4 .IX Item "is_contained()" True if the line is of the format: .Sp .Vb 1 \& keyword( ... ); .Ve .IP "\fBis_arrow_contained()\fR" 4 .IX Item "is_arrow_contained()" True if the line is of the format: .Sp .Vb 1 \& keyword word_or_string => ( ... ); .Ve .IP "\fBis_defenition()\fR" 4 .IX Item "is_defenition()" True if the line matches the regex m/sub[\es\en]+$name/sm .SS "\s-1PART EXAMINATION\s0" .IX Subsection "PART EXAMINATION" These are methods that let you investigate the parts already parsed and placed in the \fBparts()\fR attribute. .IP "\fBhas_non_string_or_quote_parts()\fR" 4 .IX Item "has_non_string_or_quote_parts()" Returns a list of parts that are not strings, quotes, or barewords. .IP "\fBhas_string_or_quote_parts()\fR" 4 .IX Item "has_string_or_quote_parts()" Returns a list of parts that are strings, quotes, or barewords. .ie n .IP "has_keyword( $word )" 4 .el .IP "has_keyword( \f(CW$word\fR )" 4 .IX Item "has_keyword( $word )" Check for a keyword in the parts .IP "\fBhas_comma()\fR" 4 .IX Item "has_comma()" .PD 0 .IP "\fBhas_fat_comma()\fR" 4 .IX Item "has_fat_comma()" .PD .SS "\s-1LINE EXAMINATION\s0 (\s-1MODIFYING\s0)" .IX Subsection "LINE EXAMINATION (MODIFYING)" This examines the line returning part structures and removing elements from the line each time they are called. .IP "\fBstrip_item()\fR" 4 .IX Item "strip_item()" .PD 0 .IP "\fBstrip_length()\fR" 4 .IX Item "strip_length()" .IP "\fBstrip_remaining_items()\fR" 4 .IX Item "strip_remaining_items()" .PD .SS "\s-1LOOKING AHEAD\s0" .IX Subsection "LOOKING AHEAD" These methods help the parser determine what comes next in a line. In most cases these are non-modifying. .IP "\fBpeek_is_block()\fR" 4 .IX Item "peek_is_block()" .PD 0 .IP "\fBpeek_is_end()\fR" 4 .IX Item "peek_is_end()" .IP "\fBpeek_is_other()\fR" 4 .IX Item "peek_is_other()" .IP "\fBpeek_is_quote()\fR" 4 .IX Item "peek_is_quote()" .IP "\fBpeek_is_word()\fR" 4 .IX Item "peek_is_word()" .IP "\fBpeek_item()\fR" 4 .IX Item "peek_item()" .IP "\fBpeek_item_type()\fR" 4 .IX Item "peek_item_type()" .IP "\fBpeek_num_chars()\fR" 4 .IX Item "peek_num_chars()" .IP "\fBpeek_other()\fR" 4 .IX Item "peek_other()" .IP "\fBpeek_quote()\fR" 4 .IX Item "peek_quote()" .IP "\fBpeek_remaining()\fR" 4 .IX Item "peek_remaining()" .IP "\fBpeek_word()\fR" 4 .IX Item "peek_word()" .PD .SS "\s-1PRIVATE METHODS\s0" .IX Subsection "PRIVATE METHODS" Do not use these, and definitely do not override them in a subclass. .IP "\fB_block_end_injection()\fR" 4 .IX Item "_block_end_injection()" .PD 0 .IP "\fB_debug()\fR" 4 .IX Item "_debug()" .IP "\fB_edit_block_end()\fR" 4 .IX Item "_edit_block_end()" .IP "\fB_item_via_()\fR" 4 .IX Item "_item_via_()" .IP "\fB_linestr_offset_from_dd()\fR" 4 .IX Item "_linestr_offset_from_dd()" .IP "\fB_move_via_()\fR" 4 .IX Item "_move_via_()" .IP "\fB_peek_is_package()\fR" 4 .IX Item "_peek_is_package()" .IP "\fB_peek_is_word()\fR" 4 .IX Item "_peek_is_word()" .IP "\fB_quoted_from_dd()\fR" 4 .IX Item "_quoted_from_dd()" .IP "\fB_scope_end()\fR" 4 .IX Item "_scope_end()" .IP "\fB_stash()\fR" 4 .IX Item "_stash()" .IP "\fB_unstash()\fR" 4 .IX Item "_unstash()" .PD .SH "FENNEC PROJECT" .IX Header "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. .PP 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. .IP "Fennec \- The core framework" 2 .IX Item "Fennec - The core framework" The primary Fennec project that ties them all together. .SH "AUTHORS" .IX Header "AUTHORS" Chad Granum exodist7@gmail.com .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright (C) 2010 Chad Granum .PP Devel-Declare-Parser is free software; Standard perl licence. .PP Devel-Declare-Parser is distributed in the hope that it will be useful, but \&\s-1WITHOUT ANY WARRANTY\s0; without even the implied warranty of \s-1MERCHANTABILITY\s0 or \&\s-1FITNESS FOR A PARTICULAR PURPOSE.\s0 See the license for more details.