| 1 | |||
| 2 | ################################################################################### | ||
| 3 | # | ||
| 4 | # Embperl - Copyright (c) 1997-2008 Gerald Richter / ecos gmbh www.ecos.de | ||
| 5 | # Embperl - Copyright (c) 2008-2015 Gerald Richter | ||
| 6 | # Embperl - Copyright (c) 2015-2023 actevy.io | ||
| 7 | # | ||
| 8 | # You may distribute under the terms of either the GNU General Public | ||
| 9 | # License or the Artistic License, as specified in the Perl README file. | ||
| 10 | # | ||
| 11 | # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR | ||
| 12 | # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | ||
| 13 | # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
| 14 | # | ||
| 15 | |||
| 16 | ################################################################################### | ||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | package Embperl::Syntax ; | ||
| 21 | |||
| 22 | use strict ; | ||
| 23 | use vars qw{@ISA @EXPORT_OK %EXPORT_TAGS %DocumentRoot %Syntax} ; | ||
| 24 | |||
| 25 | no warnings "uninitialized" ; | ||
| 26 | |||
| 27 | @ISA = qw{Exporter} ; | ||
| 28 | |||
| 29 | use constant ntypTag => 1 ; | ||
| 30 | use constant ntypStartTag => 1 + 0x20 ; | ||
| 31 | use constant ntypStartEndTag => 1 + 0x80 ; | ||
| 32 | use constant ntypEndTag => 1 + 0x40 ; | ||
| 33 | use constant ntypEndStartTag => 1 + 0x60 ; | ||
| 34 | use constant ntypAttr => 2 ; | ||
| 35 | use constant ntypAttrValue => 2 + 0x20 ; | ||
| 36 | use constant ntypText => 3 ; | ||
| 37 | use constant ntypCDATA => 4 ; | ||
| 38 | use constant ntypEntityRef => 5 ; | ||
| 39 | use constant ntypEntity => 6 ; | ||
| 40 | use constant ntypProcessingInstr => 7 ; | ||
| 41 | use constant ntypComment => 8 ; | ||
| 42 | use constant ntypDocument => 9 ; | ||
| 43 | use constant ntypDocumentType => 10 ; | ||
| 44 | use constant ntypDocumentFraq => 11 ; | ||
| 45 | use constant ntypNotation => 12 ; | ||
| 46 | |||
| 47 | use constant aflgSingleQuote => 8 ; | ||
| 48 | |||
| 49 | |||
| 50 | @EXPORT_OK = qw{ | ||
| 51 | ntypTag | ||
| 52 | ntypStartTag | ||
| 53 | ntypStartEndTag | ||
| 54 | ntypEndTag | ||
| 55 | ntypEndStartTag | ||
| 56 | ntypAttr | ||
| 57 | ntypAttrValue | ||
| 58 | ntypText | ||
| 59 | ntypCDATA | ||
| 60 | ntypEntityRef | ||
| 61 | ntypEntity | ||
| 62 | ntypProcessingInstr | ||
| 63 | ntypComment | ||
| 64 | ntypDocument | ||
| 65 | ntypDocumentType | ||
| 66 | ntypDocumentFraq | ||
| 67 | ntypNotation | ||
| 68 | |||
| 69 | aflgSingleQuote | ||
| 70 | } ; | ||
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | %EXPORT_TAGS = ( | ||
| 75 | types => \@EXPORT_OK, | ||
| 76 | ) ; | ||
| 77 | |||
| 78 | |||
| 79 | ################################################################################### | ||
| 80 | # | ||
| 81 | # Methods | ||
| 82 | # | ||
| 83 | ################################################################################### | ||
| 84 | |||
| 85 | # --------------------------------------------------------------------------------- | ||
| 86 | # | ||
| 87 | # Create new Syntax Object | ||
| 88 | # | ||
| 89 | # --------------------------------------------------------------------------------- | ||
| 90 | |||
| 91 | sub new | ||
| 92 | |||
| 93 | { | ||
| 94 | my $class = shift ; | ||
| 95 | |||
| 96 | my $self = $class ; | ||
| 97 | if (!ref $class) | ||
| 98 | { | ||
| 99 | $self = { | ||
| 100 | -root => $class -> CloneHash (\%DocumentRoot) , | ||
| 101 | -procinfotype => 'embperl', | ||
| 102 | } ; | ||
| 103 | |||
| 104 | bless $self, $class ; | ||
| 105 | } | ||
| 106 | |||
| 107 | return $self ; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | |||
| 112 | # --------------------------------------------------------------------------------- | ||
| 113 | # | ||
| 114 | # Add new elemets to root | ||
| 115 | # | ||
| 116 | # --------------------------------------------------------------------------------- | ||
| 117 | |||
| 118 | |||
| 119 | sub AddToRoot | ||
| 120 | |||
| 121 | { | ||
| 122 | my ($self, $elements) = @_ ; | ||
| 123 | |||
| 124 | my $root = $self -> {-root} ; | ||
| 125 | |||
| 126 | while (my ($k, $v) = each (%$elements)) | ||
| 127 | { | ||
| 128 | $root -> {$k} = $v ; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | # --------------------------------------------------------------------------------- | ||
| 133 | # | ||
| 134 | # Adds code that is execute every time after the compile of a document | ||
| 135 | # start and end of the execution of a document | ||
| 136 | # | ||
| 137 | # --------------------------------------------------------------------------------- | ||
| 138 | |||
| 139 | |||
| 140 | sub AddInitCode | ||
| 141 | |||
| 142 | { | ||
| 143 | my ($self, $compiletimecode, $initcode, $termcode, $procinfo) = @_ ; | ||
| 144 | |||
| 145 | my $root = $self -> {-root} ; | ||
| 146 | my $ttref ; | ||
| 147 | foreach my $tagtype ('Document', 'DocumentFraq') | ||
| 148 | { | ||
| 149 | die "'$tagtype' unknown" if (!($ttref = $self -> {-root}{$tagtype})) ; | ||
| 150 | my $pinfo = ($ttref -> {'procinfo'}{$self -> {-procinfotype}} ||= {}) ; | ||
| 151 | $pinfo -> {'compiletimeperlcode'} .= $compiletimecode if ($compiletimecode) ; | ||
| 152 | $pinfo -> {'perlcode'} .= $initcode if ($initcode) ; | ||
| 153 | $pinfo -> {'perlcodeend'} .= $termcode if ($termcode) ; | ||
| 154 | if ($procinfo) | ||
| 155 | { | ||
| 156 | while (my ($k, $v) = each (%$procinfo)) | ||
| 157 | { | ||
| 158 | $pinfo -> {$k} = $v ; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | |||
| 165 | |||
| 166 | # --------------------------------------------------------------------------------- | ||
| 167 | # | ||
| 168 | # Get root | ||
| 169 | # | ||
| 170 | # --------------------------------------------------------------------------------- | ||
| 171 | |||
| 172 | |||
| 173 | sub GetRoot | ||
| 174 | |||
| 175 | { | ||
| 176 | my ($self) = @_ ; | ||
| 177 | |||
| 178 | return $self -> {-root} ; | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | # --------------------------------------------------------------------------------- | ||
| 183 | # | ||
| 184 | # Get/create named syntax | ||
| 185 | # | ||
| 186 | # --------------------------------------------------------------------------------- | ||
| 187 | |||
| 188 | |||
| 189 | sub GetSyntax | ||
| 190 | |||
| 191 | { | ||
| 192 | my ($name, $oldname) = @_ ; | ||
| 193 | |||
| 194 | my %names ; | ||
| 195 | my $op = '' ; | ||
| 196 | if ($name =~ /^(\+|\-)\s*(.*?)$/) | ||
| 197 | { | ||
| 198 | $op = $1 ; | ||
| 199 | $name = $2; | ||
| 200 | } | ||
| 201 | $name = "$oldname $name" if ($op eq '+') ; | ||
| 202 | |||
| 203 | my @split = split (/\s/, $name) ; | ||
| 204 | if ($op eq '-') | ||
| 205 | { | ||
| 206 | my @mnames = map { /::/?$_:'Embperl::Syntax::'. $_ } @split ; | ||
| 207 | foreach (@mnames) | ||
| 208 | { | ||
| 209 | $names{$_} = 1 ; | ||
| 210 | } | ||
| 211 | @split = split (/\s/, $oldname) ; | ||
| 212 | } | ||
| 213 | |||
| 214 | my @xnames = map { /::/?$_:'Embperl::Syntax::'. $_ } @split ; | ||
| 215 | my @names ; | ||
| 216 | foreach (@xnames) | ||
| 217 | { | ||
| 218 | push @names, $1 if (!$names{$_} && (/^\s*([a-zA-Z_0-9:]+)\s*$/)) ; | ||
| 219 | $names{$_} = 1 ; | ||
| 220 | } | ||
| 221 | |||
| 222 | $name = join (' ', @names) ; | ||
| 223 | |||
| 224 | print Embperl::LOG "[$$]SYNTAX: switch to $name\n" ; | ||
| 225 | |||
| 226 | return undef if (!$name) ; | ||
| 227 | return $Syntax{$name} if (exists ($Syntax{$name})) ; | ||
| 228 | |||
| 229 | foreach my $n (@names) | ||
| 230 | { | ||
| 231 | eval "require $n" ; | ||
| 232 | if ($@) | ||
| 233 | { | ||
| 234 | warn $@ ; | ||
| 235 | return undef ; | ||
| 236 | } | ||
| 237 | } | ||
| 238 | |||
| 239 | my $first = shift @names ; | ||
| 240 | |||
| 241 | my $self = $first -> new ; | ||
| 242 | |||
| 243 | foreach my $n (@names) | ||
| 244 | { | ||
| 245 | no strict ; | ||
| 246 | &{"${n}::new"}($self) ; | ||
| 247 | use strict ; | ||
| 248 | } | ||
| 249 | |||
| 250 | $self -> {-name} = $name ; | ||
| 251 | |||
| 252 | BuildTokenTable ($self) ; | ||
| 253 | $Syntax{$name} = $self ; | ||
| 254 | return $self ; | ||
| 255 | } | ||
| 256 | |||
| 257 | # --------------------------------------------------------------------------------- | ||
| 258 | # | ||
| 259 | # Deep clone a hash and make replacements | ||
| 260 | # | ||
| 261 | # --------------------------------------------------------------------------------- | ||
| 262 | |||
| 263 | sub CloneHash | ||
| 264 | { | ||
| 265 | my ($self, $old, $replace, $seen, $new) = @_ ; | ||
| 266 | |||
| 267 | |||
| 268 | $new ||= {} ; | ||
| 269 | $replace ||= {} ; | ||
| 270 | $seen ||= {$old => $new} ; | ||
| 271 | |||
| 272 | my ($v, $k) ; | ||
| 273 | |||
| 274 | |||
| 275 | while (($k, $v) = each (%$old)) | ||
| 276 | { | ||
| 277 | if ($replace -> {$k}) | ||
| 278 | { | ||
| 279 | $new -> {$k} = $replace -> {$k} ; | ||
| 280 | } | ||
| 281 | else | ||
| 282 | { | ||
| 283 | if (ref ($v) eq 'HASH') | ||
| 284 | { | ||
| 285 | if ($seen -> {$v}) | ||
| 286 | { | ||
| 287 | $new -> {$k} = $seen -> {$v} ; | ||
| 288 | } | ||
| 289 | else | ||
| 290 | { | ||
| 291 | my $sub = {} ; | ||
| 292 | $seen -> {$v} = $sub ; | ||
| 293 | $self -> CloneHash ($v, $replace, $seen, $sub) ; | ||
| 294 | $new -> {$k} = $sub ; | ||
| 295 | } | ||
| 296 | } | ||
| 297 | elsif (ref ($v) eq 'ARRAY') | ||
| 298 | { | ||
| 299 | $new -> {$k} = [@$v] ; | ||
| 300 | } | ||
| 301 | else | ||
| 302 | { | ||
| 303 | $new -> {$k} = $v ; | ||
| 304 | } | ||
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | return $new ; | ||
| 309 | } | ||
| 310 | |||
| 311 | |||
| 312 | |||
| 313 | ################################################################################### | ||
| 314 | # | ||
| 315 | # Definitions for documents | ||
| 316 | # | ||
| 317 | ################################################################################### | ||
| 318 | |||
| 319 | |||
| 320 | %DocumentRoot = ( | ||
| 321 | '-lsearch' => 1, | ||
| 322 | |||
| 323 | # The document node is generated always and is not parsed, but can be used to include code | ||
| 324 | 'Document' => { | ||
| 325 | 'nodename' => 'Document', | ||
| 326 | 'nodetype' => ntypDocument, | ||
| 327 | 'procinfo' => { | ||
| 328 | embperl => { | ||
| 329 | perlcode => q{ | ||
| 330 | # any initialisation could be put here | ||
| 331 | #$DB::single = 1 ; | ||
| 332 | $maxrow=100;$maxcol=10; | ||
| 333 | no warnings "uninitialized" ; | ||
| 334 | }, | ||
| 335 | compiletimeperlcode => q{ | ||
| 336 | no warnings "uninitialized" ; | ||
| 337 | use vars ('$_ep_DomTree', '@ISA', '@param') ; | ||
| 338 | *_ep_rp=\\&XML::Embperl::DOM::Node::iReplaceChildWithCDATA; | ||
| 339 | *_ep_rpid=\\&XML::Embperl::DOM::Node::iReplaceChildWithMsgId; | ||
| 340 | *_ep_rpurl=\\&XML::Embperl::DOM::Node::iReplaceChildWithUrlDATA; | ||
| 341 | *_ep_cp=\\&XML::Embperl::DOM::Tree::iCheckpoint; | ||
| 342 | *_ep_dcp=\\&XML::Embperl::DOM::Tree::iDiscardAfterCheckpoint; | ||
| 343 | *_ep_opt=\\&Embperl::Cmd::Option; | ||
| 344 | *_ep_hid=\\&Embperl::Cmd::Hidden; | ||
| 345 | *_ep_ac=\\&XML::Embperl::DOM::Node::iAppendChild; | ||
| 346 | *_ep_sa=\\&XML::Embperl::DOM::Element::iSetAttribut; | ||
| 347 | Embperl::Util::CreateAliases ; | ||
| 348 | }, | ||
| 349 | perlcodeend => q{# Include here any cleanup code | ||
| 350 | $DB::single = 0 ; | ||
| 351 | }, | ||
| 352 | stackname => 'metacmd', | ||
| 353 | stackmatch => 'Document', | ||
| 354 | 'push' => 'Document', | ||
| 355 | mayjump => 1, | ||
| 356 | } | ||
| 357 | }, | ||
| 358 | }, | ||
| 359 | # The document fraq node is generated always and is not parsed, but can be used to include code | ||
| 360 | 'DocumentFraq' => { | ||
| 361 | 'nodename' => 'DocumentFraq', | ||
| 362 | 'nodetype' => ntypDocumentFraq, | ||
| 363 | 'procinfo' => { | ||
| 364 | embperl => { | ||
| 365 | perlcode => q{ | ||
| 366 | #my $_ep_param_save = \@param ; | ||
| 367 | #*param = $Embperl::req -> component -> param -> param || []; | ||
| 368 | no warnings "uninitialized" ; | ||
| 369 | }, | ||
| 370 | compiletimeperlcode => q{ | ||
| 371 | no warnings "uninitialized" ; | ||
| 372 | use vars ('$_ep_DomTree', '@ISA', '@param') ; | ||
| 373 | *_ep_rp=\\&XML::Embperl::DOM::Node::iReplaceChildWithCDATA; | ||
| 374 | *_ep_rpid=\\&XML::Embperl::DOM::Node::iReplaceChildWithMsgId; | ||
| 375 | *_ep_rpurl=\\&XML::Embperl::DOM::Node::iReplaceChildWithUrlDATA; | ||
| 376 | *_ep_cp=\\&XML::Embperl::DOM::Tree::iCheckpoint; | ||
| 377 | *_ep_dcp=\\&XML::Embperl::DOM::Tree::iDiscardAfterCheckpoint; | ||
| 378 | *_ep_opt=\\&Embperl::Cmd::Option; | ||
| 379 | *_ep_hid=\\&Embperl::Cmd::Hidden; | ||
| 380 | *_ep_ac=\\&XML::Embperl::DOM::Node::iAppendChild; | ||
| 381 | *_ep_sa=\\&XML::Embperl::DOM::Element::iSetAttribut; | ||
| 382 | Embperl::Util::CreateAliases ; | ||
| 383 | }, | ||
| 384 | perlcodeend => q{ | ||
| 385 | #*param = $_ep_param_save ; | ||
| 386 | }, | ||
| 387 | stackname => 'metacmd', | ||
| 388 | stackmatch => 'DocumentFraq', | ||
| 389 | 'push' => 'DocumentFraq', | ||
| 390 | mayjump => 1, | ||
| 391 | } | ||
| 392 | }, | ||
| 393 | }, | ||
| 394 | ) ; | ||
| 395 | |||
| 396 | 1; | ||
| 397 | |||
| 398 | __END__ |