File: Control.pm

package info (click to toggle)
psp 0.5.5-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 4,820 kB
  • ctags: 2,333
  • sloc: perl: 21,074; ansic: 4,553; sh: 2,407; makefile: 461; php: 11; pascal: 6
file content (479 lines) | stat: -rw-r--r-- 9,722 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
package PSP::Parser::Control;

# Copyright (c) 2000, FundsXpress Financial Network, Inc.
# This library is free software released under the GNU Lesser General
# Public License, Version 2.1.  Please read the important licensing and
# disclaimer information included below.

# $Id: Control.pm,v 1.1.1.2 2003/12/06 19:47:26 hartmans Exp $

use strict;

=head1 NAME

 PSP::Parser::Control - Tag extension for control structures.

=head1 SYNOPSIS

=head1 DESCRIPTION

=cut

use Error qw(:try);
use PSP::Parser;
use PSP::Utils;

BEGIN {
  @PSP::Parser::Control::ISA = qw(PSP::Parser);
}

use vars qw(@handled @stacks @current @propagatable);
BEGIN {
  @handled = qw(script declare comment nop if else list while include block);
};

=head1 TAG HANDLERS

=head2 begin_pspscript

 [private] instance
 () begin_pspscript (string $tag, \%attributes, \@atrseq, string $orig);

DESCRIPTION:

See PSP specification.

psp:script allows text to pass through to output code after HTML-decoding.

=cut
  
sub begin_pspscript {
  my ($this, $tag, $attr, $attrseq, $orig) = @_;
  $this->debug_line($orig);
  $this->script_mode();
  $this->push_handlers( { 'psp:include' => \&begin_pspinclude },
			{ 'psp:script'  => \&end_pspscript    });
  			    
  $this->{is_cdata}++;
}

=head2 end_pspscript

 [private] instance
 () end_pspscript (string $tag);

DESCRIPTION:

Resets the tag handlers of the parser it had before the call to 
begin_pspscript().

=cut

sub end_pspscript {
  my ($this, $tag) = @_;
  $this->debug_line($tag);
  undef $this->{is_cdata};
  $this->pop_handlers();
  $this->script_mode(0);
  $this->code("\n");
}

=head2 begin_pspdeclare

 [private] instance
 () begin_pspdeclare (string $tag, \%attributes, \@atrseq, string $orig);

DESCRIPTION:

See PSP specification.

psp:declare allows text to pass through to file-scoped output code 
after HTML-decoding. 

=cut
  
sub begin_pspdeclare {
  my ($this, $tag, $attr, $attrseq, $orig) = @_;
  $this->debug_line($orig);
  $this->script_mode();
  $this->push_code_sub(\&code_pspdeclare);
  $this->push_handlers( { 'psp:include' => \&begin_pspinclude },
			{ 'psp:declare' => \&end_pspdeclare   });
  $this->{is_cdata}++;
}

=head2 code_pspdeclare

 [private] instance
 () code_pspdeclare (string $text);

DESCRIPTION:

This function decodes the text between handled tags.

=cut
  
sub code_pspdeclare {
  my ($this,$text) = @_;
  $this->debug_line($text);
  $this->decl($text);
}

=head2 end_pspdeclare

 [private] instance
 () end_pspdeclare (string $tag);

DESCRIPTION:

Resets the tag handlers of the parser it had before the call to 
begin_pspdeclare().

=cut

sub end_pspdeclare {
  my ($this, $tag) = @_;
  $this->debug_line($tag);
  undef $this->{is_cdata};
  $this->pop_handlers();
  $this->pop_code_sub();
  $this->script_mode(0);
  $this->code("\n");
}

=head2 begin_pspcomment

 [private] instance
 () begin_pspcomment (string $tag, \%attributes);

DESCRIPTION:

See PSP specification.

psp:comment allows all contained text to be ignored.

=cut

sub begin_pspcomment {
  my ($this, $tag, $attr) = @_;
  $this->push_text_sub(\&text_pspcomment);
  $this->push_handlers( { 'psp:include' => \&begin_pspinclude },
			{ 'psp:comment' => \&end_pspcomment   });
  $this->{is_cdata}++;
}

=head2 text_pspcomment

 [private] instance
 () text_pspcomment ();

DESCRIPTION:

See PSP specification.

Ignore all text within a psp:comment element.

=cut

sub text_pspcomment {
  # do not do anything with commented text.
}

=head2 end_pspcomment

 [private] instance
 () end_pspcomment (string $tag)

DESCRIPTION:

Resets the tag handlers of the parser it had before the call to 
begin_pspcomment().

=cut

sub end_pspcomment {
  my ($this, $orig_text) = @_;
  undef $this->{is_cdata};
  $this->pop_handlers();
  $this->pop_text_sub();
}

=head2 begin_pspnop

 [private] instance
 () begin_pspnop (string $tag, \%attributes);

DESCRIPTION:

See PSP specification.

psp:nop does nothing.

=cut

sub begin_pspnop {
  my ($this, $tag, $attr) = @_;
}

=head2 end_pspnop

 [private] instance
 () end_pspnop (string $tag)

DESCRIPTION:

/psp:nop does nothing.

=cut

sub end_pspnop {
  my ($this, $orig_txt) = @_;
}

=head1 IF, ELSIF, ELSE, WHILE, LIST

=head2 begin_pspif

 [private] instance
 () begin_pspif (string $tag, \%attributes)

DESCRIPTION:

See PSP specification.

=cut

sub begin_pspif {
  my ($this, $tag, $attr, $tagseq, $orig) = @_;
  $this->debug_line($orig);
  defined $attr->{test} or throw
    Error::Simple("<$tag> requires a TEST tag.");
  $this->debug_line($attr->{test});
  $this->code("if ($attr->{test})");
  $this->begin_pspblock("if");
}
sub end_pspif {
  my ($this, $orig_txt) = @_;
  return $this->end_pspblock("if");
}

=head2 begin_pspelsif

 [private] instance
 () begin_pspelsif (string $tag, \%attributes);

DESCRIPTION:

See PSP specification.

=cut

sub begin_pspelsif {
  my ($this, $tag, $attr) = @_;
  defined $attr->{test} or throw
    Error::Simple("<$tag> requires a TEST attribute.");
  $this->debug_line($attr->{test});
  $this->prev_context() eq "else" and throw
    Error::Simple("<$tag> called after an ELSE context.");

#  this hack would have allowed a "psp:else" without a "psp:if" or "psp:elsif"
#  (($this->context() eq "if") or ($this->context() eq "elsif")) and
#    $this->end_pspblock($this->context());

  $this->{text_to_flush} =~ s/^\s+//s;
  $this->code("elsif ($attr->{test})");
  $this->begin_pspblock("elsif");
}
sub end_pspelsif {
  my ($this, $orig_txt) = @_;
  return $this->end_pspblock("elsif");
}

=head2 begin_pspelse

 [private] instance
 () begin_pspelse (string $tag, \%attributes)

DESCRIPTION:

See PSP specification.

=cut

sub begin_pspelse {
  my ($this, $tag, $attr) = @_;
  defined $attr->{test} and
    return $this->begin_pspelsif($tag,$attr);
  $this->debug_line("");

  $this->prev_context() eq "else" and throw
    Error::Simple("<$tag> called in a <$tag> context.");

#   this hack allows a "psp:else" without a "/psp:if"
#  ($this->context() eq "if") and
#    $this->end_pspblock($this->context());

  $this->{text_to_flush} =~ s/^\s+//s;
  $this->code("else");
  $this->begin_pspblock("else");
}
sub end_pspelse {
  my ($this, $orig_txt) = @_;
  return $this->end_pspblock("else");
}

=head2 begin_psplist

 [private] instance
 () begin_psplist (string $tag, \%attributes)

DESCRIPTION:

See PSP specification.

=cut

sub begin_psplist {
  my ($this, $tag, $attr) = @_;
  defined $attr->{list} or throw
    Error::Simple("<$tag> requires a LIST attribute.");
  if (defined $attr->{iterator}) {
    $this->code("for my $attr->{iterator} ($attr->{list})");
  } else {
    $this->code("for ($attr->{list})");
  }
  $this->begin_pspblock("list");
}
sub end_psplist {
  my ($this, $orig_txt) = @_;
  return $this->end_pspblock("list");
}

=head2 begin_pspwhile

 [private] instance
 () begin_pspwhile (string $tag, \%attributes)

DESCRIPTION:

See PSP specification.

=cut

sub begin_pspwhile {
  my ($this, $tag, $attr) = @_;
  $attr->{test} or throw
    Error::Simple("<$tag> requires a TEST attribute.");
  $this->code("while ($attr->{test})");
  $this->begin_pspblock("while");
}
sub end_pspwhile {
  my ($this, $orig_txt) = @_;
  return $this->end_pspblock("while");
}

=head2 begin_pspinclude

 [private] instance
 () begin_pspinclude (string $tag, \%attributes)

DESCRIPTION:

See PSP specification.

This function will start a new parser of its own and hand the specified
file off to it.  The file will be searched for in the following paths:

 1) current directory of parent parser.
 2) directories specified on command line via -I argument.

=cut

sub begin_pspinclude {
  my ($this, $tag, $attr, $tagseq, $orig) = @_;
  $this->debug_line($orig);

  #validate input.
  defined(my $src = $attr->{src}) or throw
    Error::Simple("<$tag> requires a SRC attribute.");

  return $this->include_file($src);
}
sub end_pspinclude {
  my ($this, $orig_txt) = @_;
}

=head2 begin_pspblock

 [private] instance
 () begin_pspblock  (string $tag)

DESCRIPTION:

A general purpose block beginner.

=cut

sub begin_pspblock {
  my ($this, $tag) = @_;
  $this->debug_line($tag);
  $this->code("{ # $tag\n");
  $this->code_add_indent("  ");
  $this->push_context($tag);
}

=head2 end_pspblock

 [private] instance
 () end_pspblock  (string $tag)

DESCRIPTION:

A general purpose block ender.

=cut

sub end_pspblock {
  my ($this, $tag) = @_;
  $this->debug_line($tag);
  $this->pop_context() or throw
    Error::Simple("Stack underflow. (</$tag> called without <$tag>?");
  $this->code_del_indent("  ");
  $this->code("} # $tag\n");
}

1;
__END__

=head1 BUGS

No known bugs, but this does not mean no bugs exist.

=head1 SEE ALSO

L<HTML::Parser>, L<HTMLIO::Utils>

=head1 COPYRIGHT

 PSP - Perl Server Pages
 Copyright (c) 2000, FundsXpress Financial Network, Inc.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2 of the License, or (at your option) any later version.

 BECAUSE THIS LIBRARY IS LICENSED FREE OF CHARGE, THIS LIBRARY IS
 BEING PROVIDED "AS IS WITH ALL FAULTS," WITHOUT ANY WARRANTIES
 OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT
 LIMITATION, ANY IMPLIED WARRANTIES OF TITLE, NONINFRINGEMENT,
 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, AND THE
 ENTIRE RISK AS TO SATISFACTORY QUALITY, PERFORMANCE, ACCURACY,
 AND EFFORT IS WITH THE YOU.  See the GNU Lesser General Public
 License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

=cut