.\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.35) .\" .\" 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 "Regexp::Grammars 3pm" .TH Regexp::Grammars 3pm "2020-05-23" "perl v5.30.2" "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" Regexp::Grammars \- Add grammatical parsing features to Perl 5.10 regexes .SH "VERSION" .IX Header "VERSION" This document describes Regexp::Grammars version 1.057 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Regexp::Grammars; \& \& my $parser = qr{ \& (?: \& # Parse and save a Verb in a scalar \& <.ws> # Parse but don\*(Aqt save whitespace \& # Parse and save a Noun in a scalar \& \& 0.5 ? \*(AqVN\*(Aq : \*(AqVerbNoun\*(Aq })> \& # Save result of expression in a scalar \& | \& (?: \& <[Noun]> # Parse a Noun and save result in a list \& (saved under the key \*(AqNoun\*(Aq) \& <[PostNoun=ws]> # Parse whitespace, save it in a list \& # (saved under the key \*(AqPostNoun\*(Aq) \& )+ \& \& # Parse a Verb and save result in a scalar \& (saved under the key \*(AqVerb\*(Aq) \& \& # Save a literal in a scalar \& | \& # Turn on the integrated debugger here \& <.Cmd= (?: mv? )> # Parse but don\*(Aqt capture a subpattern \& (name it \*(AqCmd\*(Aq for debugging purposes) \& <[File]>+ # Parse 1+ Files and save them in a list \& (saved under the key \*(AqFile\*(Aq) \& # Turn off the integrated debugger here \& # Parse a File and save it in a scalar \& (saved under the key \*(AqDest\*(Aq) \& ) \& \& ################################################################ \& \& # Define a subrule named File \& <.ws> # \- Parse but don\*(Aqt capture whitespace \& # \- Parse the subpattern and capture \& # matched text as the result of the \& # subrule \& \& # Define a subrule named Noun \& cat | dog | fish # \- Match an alternative (as usual) \& \& # Define a whitespace\-sensitive subrule \& eats # \- Match a literal (after any space) \& ? # \- Parse optional subrule Noun and \& # save result under the key \*(AqObject\*(Aq \& | # Or else... \& # \- Parse subrule AUX and save result \& # \- Match a literal, save under \*(Aqpart\*(Aq \& \& # Define a whitespace\-insensitive subrule \& (has | is) # \- Match an alternative and capture \& (?{ $MATCH = uc $^N }) # \- Use captured text as subrule result \& \& }x; \& \& # Match the grammar against some text... \& if ($text =~ $parser) { \& # If successful, the hash %/ will have the hierarchy of results... \& process_data_in( %/ ); \& } .Ve .SH "QUICKSTART CHEATSHEET" .IX Header "QUICKSTART CHEATSHEET" .SS "In your program..." .IX Subsection "In your program..." .Vb 2 \& use Regexp::Grammars; Allow enhanced regexes in lexical scope \& %/ Result\-hash for successful grammar match .Ve .SS "Defining and using named grammars..." .IX Subsection "Defining and using named grammars..." .Vb 2 \& Define a named grammar that can be inherited \& Current grammar inherits named grammar\*(Aqs rules .Ve .SS "Defining rules in your grammar..." .IX Subsection "Defining rules in your grammar..." .Vb 2 \& Define rule with magic whitespace \& Define rule without magic whitespace \& \& Define rule that blesses return\-hash into class \& Define token that blesses return\-hash into class \& \& Shortcut for above (rule name derived from class) \& Shortcut for above (token name derived from class) .Ve .SS "Matching rules in your grammar..." .IX Subsection "Matching rules in your grammar..." .Vb 2 \& Call named subrule (may be fully qualified) \& save result to $MATCH{RULENAME} \& \& Call named subrule, passing args to it \& \& Call subrule and fail if it matches \& (shorthand for (?!<.RULENAME>) ) \& \& <:IDENT> Match contents of $ARG{IDENT} as a pattern \& <\e:IDENT> Match contents of $ARG{IDENT} as a literal \& Match closing delimiter for $ARG{IDENT} \& \& <%HASH> Match longest possible key of hash \& <%HASH {PAT}> Match any key of hash that also matches PAT \& \& Match closing delimiter for $MATCH{IDENT} \& <\e_IDENT> Match the literal contents of $MATCH{IDENT} \& \& Call subrule, save result in $MATCH{ALIAS} \& Match a hash key, save key in $MATCH{ALIAS} \& Match pattern, save match in $MATCH{ALIAS} \& Execute code, save value in $MATCH{ALIAS} \& Save specified string in $MATCH{ALIAS} \& Save specified number in $MATCH{ALIAS} \& Match closing delim, save as $MATCH{ALIAS} \& Match \*(Aq$MATCH{IDENT}\*(Aq, save as $MATCH{ALIAS} \& \& <.SUBRULE> Call subrule (one of the above forms), \& but don\*(Aqt save the result in %MATCH \& \& \& <[SUBRULE]> Call subrule (one of the above forms), but \& append result instead of overwriting it \& \& + % Match one or more repetitions of SUBRULE1 \& as long as they\*(Aqre separated by SUBRULE2 \& ** Same (only for backwards compatibility) \& \& * % Match zero or more repetitions of SUBRULE1 \& as long as they\*(Aqre separated by SUBRULE2 \& \& * %% Match zero or more repetitions of SUBRULE1 \& as long as they\*(Aqre separated by SUBRULE2 \& and allow an optional trailing SUBRULE2 .Ve .SS "In your grammar's code blocks..." .IX Subsection "In your grammar's code blocks..." .Vb 7 \& $CAPTURE Alias for $^N (the most recent paren capture) \& $CONTEXT Another alias for $^N \& $INDEX Current index of next matching position in string \& %MATCH Current rule\*(Aqs result\-hash \& $MATCH Magic override value (returned instead of result\-hash) \& %ARG Current rule\*(Aqs argument hash \& $DEBUG Current match\-time debugging mode .Ve .SS "Directives..." .IX Subsection "Directives..." .Vb 12 \& Fail if code evaluates false \& Fail after specified number of seconds \& Change match\-time debugging mode \& Change debugging log file (default: STDERR) \& Queue error message and fail parse \& Queue error message and backtrack \& Queue warning message and continue \& Explicitly add a message to debugging log \& Override automatic whitespace matching \& Simplify the result of a subrule match \& Switch on context substring retention \& Switch off context substring retention .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module adds a small number of new regex constructs that can be used within Perl 5.10 patterns to implement complete recursive-descent parsing. .PP Perl 5.10 already supports recursive-descent \fImatching\fR, via the new \&\f(CW\*(C`(?...)\*(C'\fR and \f(CW\*(C`(?&name)\*(C'\fR constructs. For example, here is a simple matcher for a subset of the LaTeX markup language: .PP .Vb 2 \& $matcher = qr{ \& (?&File) \& \& (?(DEFINE) \& (? (?&Element)* ) \& \& (? \es* (?&Command) \& | \es* (?&Literal) \& ) \& \& (? \e\e \es* (?&Literal) \es* (?&Options)? \es* (?&Args)? ) \& \& (? \e[ \es* (?:(?&Option) (?:\es*,\es* (?&Option) )*)? \es* \e]) \& \& (? \e{ \es* (?&Element)* \es* \e} ) \& \& (?