~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/lib/Foswiki/Infix/Node.pm

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
=begin TML
 
3
 
 
4
---+ package Foswiki::Infix::Node
 
5
 
 
6
Base class for node types generated by Infix::Parser. You don't *have* to use
 
7
it, but it may be useful.
 
8
 
 
9
=cut
 
10
 
 
11
package Foswiki::Infix::Node;
 
12
 
 
13
use strict;
 
14
 
 
15
# 1 for debug
 
16
sub MONITOR_EVAL { 0 }
 
17
 
 
18
# Leaf token types
 
19
use vars qw ($NAME $STRING $NUMBER);
 
20
$NAME   = 1;
 
21
$NUMBER = 2;
 
22
$STRING = 3;
 
23
 
 
24
=begin TML
 
25
 
 
26
---++ ClassMethod newNode( $o, @p ) -> \$if
 
27
 
 
28
Construct a new parse node (contract with Infix::Parser)
 
29
 
 
30
=cut
 
31
 
 
32
sub newNode {
 
33
    my $class = shift;
 
34
    my $op    = shift;
 
35
    my $this  = bless( {}, $class );
 
36
    @{ $this->{params} } = @_;
 
37
    $this->{op} = $op;
 
38
    return $this;
 
39
}
 
40
 
 
41
=begin TML
 
42
 
 
43
---++ ClassMethod newLeaf( $val, $type ) -> \$if
 
44
 
 
45
Construct a new terminal node (contract with Infix::Parser)
 
46
 
 
47
=cut
 
48
 
 
49
sub newLeaf {
 
50
    my ( $class, $val, $type ) = @_;
 
51
    return newNode( $class, $type, $val );
 
52
}
 
53
 
 
54
=begin TML
 
55
 
 
56
---++ ObjectMethod evaluate(...) -> $result
 
57
 
 
58
Execute the parse node. The parameter array is passed on, by reference,
 
59
to the evaluation functions.
 
60
 
 
61
=cut
 
62
 
 
63
sub evaluate {
 
64
    my ( $this, $clientData ) = @_;
 
65
 
 
66
    my $result;
 
67
    if ( !ref( $this->{op} ) ) {
 
68
        $result = $this->{params}[0];
 
69
        if (MONITOR_EVAL) {
 
70
            print STDERR "LEAF: ", ( defined($result) ? $result : 'undef' ),
 
71
              "\n";
 
72
        }
 
73
    }
 
74
    else {
 
75
        my $fn = $this->{op}->{evaluate};
 
76
        $result = &$fn( $clientData, @{ $this->{params} } );
 
77
        if (MONITOR_EVAL) {
 
78
            print STDERR "NODE: ", $this->stringify(), " -> ",
 
79
              ( defined($result) ? $result : 'undef' ), "\n";
 
80
        }
 
81
    }
 
82
    return $result;
 
83
}
 
84
 
 
85
sub stringify {
 
86
    my $this = shift;
 
87
 
 
88
    unless ( ref( $this->{op} ) ) {
 
89
        if ( $this->{op} == $Foswiki::Infix::Node::STRING ) {
 
90
            return "'$this->{params}[0]'";
 
91
        }
 
92
        else {
 
93
            return $this->{params}[0];
 
94
        }
 
95
    }
 
96
 
 
97
    return
 
98
      $this->{op}->{name} . '{'
 
99
      . join( ',', map { $_->stringify() } @{ $this->{params} } ) . '}';
 
100
}
 
101
 
 
102
1;
 
103
__DATA__
 
104
 
 
105
Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/, http://Foswiki.org/
 
106
 
 
107
# Copyright (C) 2008-2009 Foswiki Contributors. All Rights Reserved.
 
108
# Foswiki Contributors are listed in the AUTHORS file in the root
 
109
# of this distribution. NOTE: Please extend that file, not this notice.
 
110
#
 
111
# Additional copyrights apply to some or all of the code in this
 
112
# file as follows:
 
113
#
 
114
# Copyright (C) 2005-2007 TWiki Contributors. All Rights Reserved.
 
115
# TWiki Contributors are listed in the AUTHORS file in the root
 
116
# of this distribution. NOTE: Please extend that file, not this notice.
 
117
#
 
118
This program is free software; you can redistribute it and/or
 
119
modify it under the terms of the GNU General Public License
 
120
as published by the Free Software Foundation; either version 2
 
121
of the License, or (at your option) any later version. For
 
122
more details read LICENSE in the root of this distribution.
 
123
 
 
124
This program is distributed in the hope that it will be useful,
 
125
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
126
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
127
 
 
128
As per the GPL, removal of this notice is prohibited.
 
129
 
 
130
Author: Crawford Currie http://c-dot.co.uk