Syntax(3pm)
π Embperl::Syntax β the base class for defining custom syntaxes π
okay bestie, pull up a chair π because this is the page where you learn how to teach Embperl a whole new language. This is Embperl::Syntax, and it's the base class for defining custom syntaxes. Let's go π¦
β¨ SYNOPSIS β¨
not me leaving this one literally blank π β the original page has nothing under SYNOPSIS, so there's nothing to spill. Moving on, bestie π
πΈ DESCRIPTION πΈ
So Embperl::Syntax is the base class that every custom syntax should be derived from. Periodt. And rn, Embperl ships with these derived syntaxes π
- EmbperlHTML β every single HTML tag that Embperl recognizes by default β¨
- EmbperlBlocks β all the
[ ]blocks that Embperl supports π§± - Embperl β the default syntax! It's derived from
"EmbperlHtml"and"EmbperlBlocks"π«Ά - ASP β
<% %>and<%= %>, seeperldoc Embperl::Syntax::ASPπ - SSI β Server Side Includes, see
perldoc Embperl::Syntax::SSIπ - Perl β the file contains pure Perl (similar to
Apache::Registry), but it can be used insideEmbperlObjectπ - Text β the file contains only Text, no actions is taken on the Text π§
- Mail β defines the
<mail:send>tag, for sending mail. This is an example for a taglib, which could be a base for writing your own taglib to extent the number of available tags π - POD β parses POD out of any file and creates a XML tree similar to
pod2xml, which can be formatted by XSLT afterwards π
Now, how do you pick which syntax your page is using? You've got options, bestie:
- the
"EMBPERL_SYNTAX"configuration directive - the
"syntax"parameter to"Execute" - or the
[$ syntax $]metacommand
And yes, you can stack multiple syntaxes at once, no cap:
PerlSetEnv EMBPERL_SYNTAX "Embperl SSI"
Execute ({inputfile => '*', syntax => 'Embperl ASP'}) ;
The syntax metacommand lets you switch the syntax, or add or subtract syntaxes, like:
[$ syntax + Mail $]
that one adds the Mail taglib, so the <mail:send> tag is available after this line β¨
[$ syntax - Mail $]
and now the <mail:send> tag is unknown again π
[$ syntax SSI $]
now you can only use SSI commands inside your page. That's it, that's the whole vibe switch π
π οΈ Defining your own Syntax
Wanna build your own? Obsessed, let's do it. If you want to define your own syntax, you have to derive a new class from one of the existing ones and extent it with new tags/functionality. The best thing is to take a look at the syntax classes that comes with Embperl. (inside the directory Embperl/Syntax/.) π
For example:
- if you want to add new html tags, derive from
Embperl::Syntax::HTMLπ - if you want to add new metacommands, derive from
Embperl::Syntax::EmbperlBlocksπ§±
Some of the classes define additional methods to easily add new tags. See the respective pod file to find out which methods are available for a certain class π
Embperl::Syntax defines the basic methods to create a syntax π
π Methods
Embperl::Syntax -> new / $self -> new
Create a new syntax class. This method should only be called inside a constructor of a derived class. Don't be out here calling it wherever, bestie π
$self -> AddToRoot ($elements)
This adds a new element to the root of the parser tree. $elements must be a hashref. See Embperl::Syntax::ASP for an example π
$self -> AddInitCode ($compiletimecode, $initcode, $termcode, $procinfo)
This gives you the possibility to add some Perl code, that is always executed at the beginning of a document ($initcode), at the end of the document ($termcode) or at compile time ($compiletimecode). The three strings must be valid Perl code. See Embperl::Syntax::SSI for an example. $procinfo is a hashref that can consits of additional processor infos (see below) for the document π«Ά
$self -> GetRoot
Returns the root of the parser tree. π³
Embperl::Syntax::GetSyntax ($name, $oldname)
Returns a syntax object which is build form the syntaxes named in $name. If $oldname is given, $name can start with a "+" or "-" to add or subtract a syntax. This is normally only needed by Embperl itself or to implement a syntax switch statement (see Embperl::Syntax::SSI for an example.) π
$self -> CloneHash ($old, $replace)
Clones a hash which is given as hashref in $old, optional replace the tags given in the hashref $replace and return a hashref to the new hash. π§¬
...and bestie, that's only part 1 π¦ more is coming, so don't go anywhere π
π Syntax Structure and Parameter π
So like, under the hood the syntax object builds a whole data structure that's basically the blueprint the parser vibes off of. That structure is a list of tokens and options, and every option starts with a dash. Slay. β¨
π Tokens π
'-lsearch' => 1β Do a linear search instead of a binary search. You need this when the tokens can't be clearly separated, no cap.'-defnodetype' => ntypText,β Defines the default type for text nodes. If you don't specify anything, the type is CDATA, which means no escaping takes place at all. But with"ntypText"all special characters get escaped. π'-rootnode'β Name for a root node to insert always.<name> => \%tokendescriptionβ Everything that doesn't start with a slash is treated as a name. The name of a token is only descriptive and gets used in error messages. The item must contain a hashref which describes the token.
π¦ Tokendescription π¦
Every token can have the following members, bestie:
'text' => '<'β Start text'end' => '>'β End text'matchall'β when set to 1, a new token starts at the next character; when set to -1, a new token starts at the next character, but only if it is the first token inside another one.'nodename'β Text that should be outputed when node is stringifyed. Defaults totext. If the first character is a':'you can specify the sourounding delimiters for this tag with:<start>:<end>:<text>:<endtag>. Example:':{:}:NAME'. And if the nodename starts with a'!'a unique internal id is generated, so two or more nodename of the same text can have different meaning in different contexts. iconic behavior honestly'contains' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789'β Token consists of the following characters. Either"start"and"end"or"contains"could be specified.
NOTE: If a item that only specfifiescontainsbut notextshould be compiled, you must specfify anodname. β οΈ (yes that's how it's spelled, we don't fix the lore)
'unescape' => 1β IfoptRawInputisn't set, unescape the data of the inside the node'nodetype' => ntypEndTagβ Type of the node'cdatatype' => ntypAttrValueβ Type of nodes for data (which is not matched by'inside'definitions) inside this node. Set to zero to not generate any nodes for text inside of this node, other then these that are matched by a'inside'definition.'endtag'β Name of the tag that marks the end of a block. The parser uses this to track correct nesting.'follow' => \%tokenlistβ Hashref that specifices one or more tokens that must follow this token.'inside' => \%tokenlistβ Hashref that specifices one or more tokens that could occur inside a node that is started with this token.exitinsideβ when the token found, the parser stop searching in the current level and continues with the tokens that are defined in the hash from there the current one was "called" viainsidedonteatβ set to 1 to don't eat the start text, so it will be matched again, by any tokens set under"inside". Set 2 to don't the end text. Set to 3 for both.'procinfo' =>β Processor info. Hashref with information how to process this token.
β¨ Processor info β¨
The processor info gives information how to compile this token to valid code that can be executed later on by the processor. There could be information for multiple processors. At the moment only the embperl processor is defined. Normally you must not worry about different processor, because the syntax object knows inside that all procinfo is for the embperl processor. procinfo is a parameter to many methods, it is a hashref and can take the following items: π«Ά
perlcode => <string>or<arrayref>β Code to generate. You can also specify a arrayref of strings. The first string which contains matching attributes are used. The following special strings are replaced:%#<N>%β Text of childnode number <N> (starting with zero)%><N>%β Text of sibling node number <N> . 0 gives the current node, > 0 gives the Nth next node, < 0 gives the Nth previous node.%&<attr>%β Value of attribute <attr>.%^<stackname>%β Stringvalue of given stack%?<stackname>%β Set if stackvalue was used%$n%β Source Dom Tree, Index of current node.%$t%β Source Dom Tree%$x%β Index of current node%$l%β Index of last node%$c%β Sets the current node Index, if not already done%$q%β Index of source Dom Tree%$p%β Number of current checkpoint%%β Gives a single%
All of the above special values (expect those start with $) allows the following modifiers:
%<X>*<N>%β Attribute/Child etc. must exist.%<X>!<N>%β Attribute/Child etc. must not exist.%<X>=<N>:<value1>|<value2>|<value3>%β Attribute/Child etc. must have the value = <value1> or <value2> etc.%<X>~<N>:<value1>|<value2>|<value3>%β Attribute/Child etc. must contain the substring <value1> or <value2> etc. and a non alphanum character must follow the substring.
writing a minus sign (-) after * ! = or ~ will cause the child/attribute not to be included, but the condition is evaluated. Writing an ' will cause the value to be quoted. the little details really do be carrying the whole plot
perlcodeend => <string>β Code to generate at the end of the block.compiletimeperlcode => <string>or<arrayref>β Code that is executed at compile time. You can also specify a arrayref of string. The first string which contains matching attributes are used. The same special strings are replaced as in"perlcode".
$_[0] contains the Embperl request object. The method "Code" can be used to get or set the perl code that should be generated by this node.
If the code begins with #!- all newlines are removed in the code. This is basically useful to keep all code on the same line, so the line number in error reporting matches the line in the source.
compiletimeperlcodeend => <string>β Code that is executed at compile time, but at the end of the tag. The same special strings are replaced as in"perlcode".
$_[0] contains the Embperl request object. The method "Code" can be used to get or set the perl code that should be generated by this node.
If the code begins with #!- all newlines are removed in the code. This is basically useful to keep all code on the same line, so the linenumber in error reporting matches the line in the source.
perlcoderemove => 0/1β Remove perlcode ifperlcodeendcondition is not met.removenode => <removelevel>β Remove node after compiling. <removelevel> could be one of the following, values could be added:- Remove this node only
- Remove next node if it consists of only white spaces and
optKeepSpacesisn't set. - Replace next node with one space if next node consists only of white spaces and
optKeepSpacesisn't set. - Set this node to ignore for output.
- Remove all child nodes
- Set all child nodes to ignore for output.
- Calculate Attributes values of this node also for nodes that are set to ignore for output (makes only sense if 8 is also set).
removespaces => <removeflags>β Remove spaces before or after tag.- Remove all white spaces before tag
- Remove all white spaces after tag
- Remove spaces and tabs before tag
- Remove spaces and tabs after tag
- Remove all spaces and tabs but one before tag
- Remove all whihe space after text inside of tag
- Remove spaces and tabs after text inside of tag
mayjump => 0/1β If set, tells the compiler that this code may jump to another program location. (e.g.if,while,gotoetc.). Could also be a condition as described underperlcode.compilechilds => 0/1β Compile child nodes. Default: 1stackname => <name>β Name of stack for"push","stackmatch"stackname2 => <name>β Name of stack for"push2"push => <value>β Push value on stack which name is given with"stackname". Value could include the same specical values as"perlcode"push2 => <value>β Push value on stack which name is given with"stackname2". Value could include the same specical values as"perlcode"stackmatch => <value>β Check if value on stack which name is given with"stackname"is the same as the given value. If not give a error message about tag mismatch. Value could include the same specical values as"perlcode"switchcodetype => <1/2>β 1 means put the following code into normal code which is executed every time the page is requested
2 means put the following code put into code which is executed direct after compilation. This is mainly for defining subs, or using modules etc.
And then there's this whole lil squad of members that get listed with no description at all, just vibes and names, and we respect them for it π:
addflagscdatatypeforcetypeinsidemustexistmatchallexitinsideaddfirstchildstarttagendtagparsetimeperlcodecontains
okay bestie that's the whole page, you made it to the end and you absolutely ate π
This whole thing lives in perl v5.34.0, dated 2022-02-06, and it's the Syntax(3pm) page β so if you ever need to cite your sources, that's the receipt π§Ύβ¨ proud of you for reading all of it fr fr π¦
π¦ where this page comes from
- ubuntu jammy: libembperl-perl 2.5.0-16build1 Β· 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