lib/TWiki/Infix/Node.pm
changeset 0 414e01d06fd5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lib/TWiki/Infix/Node.pm	Sat Jan 26 15:50:53 2008 +0100
     1.3 @@ -0,0 +1,119 @@
     1.4 +=pod
     1.5 +
     1.6 +---+ package TWiki::Infix::Node
     1.7 +
     1.8 +Base class for node types generated by Infix::Parser. You don't *have* to use
     1.9 +it, but it may be useful.
    1.10 +
    1.11 +=cut
    1.12 +
    1.13 +package TWiki::Infix::Node;
    1.14 +
    1.15 +use strict;
    1.16 +
    1.17 +# 1 for debug
    1.18 +sub MONITOR_EVAL   { 0 };
    1.19 +
    1.20 +# Leaf token types
    1.21 +use vars qw ($NAME $STRING $NUMBER);
    1.22 +$NAME = 1;
    1.23 +$NUMBER = 2;
    1.24 +$STRING = 3;
    1.25 +
    1.26 +=pod
    1.27 +
    1.28 +---++ ClassMethod newNode( $o, @p ) -> \$if
    1.29 +
    1.30 +Construct a new parse node (contract with Infix::Parser)
    1.31 +
    1.32 +=cut
    1.33 +
    1.34 +sub newNode {
    1.35 +    my $class = shift;
    1.36 +    my $op = shift;
    1.37 +    my $this = bless( {}, $class );
    1.38 +    @{$this->{params}} = @_;
    1.39 +    $this->{op} = $op;
    1.40 +    return $this;
    1.41 +}
    1.42 +
    1.43 +=pod
    1.44 +
    1.45 +---++ ClassMethod newLeaf( $val, $type ) -> \$if
    1.46 +
    1.47 +Construct a new terminal node (contract with Infix::Parser)
    1.48 +
    1.49 +=cut
    1.50 +
    1.51 +sub newLeaf {
    1.52 +    my( $class, $val, $type ) = @_;
    1.53 +    return newNode( $class, $type, $val );
    1.54 +}
    1.55 +
    1.56 +=pod
    1.57 +
    1.58 +---++ ObjectMethod evaluate(...) -> $result
    1.59 +
    1.60 +Execute the parse node. The parameter array is passed on, by reference,
    1.61 +to the evaluation functions.
    1.62 +
    1.63 +=cut
    1.64 +
    1.65 +sub evaluate {
    1.66 +    my( $this, $clientData ) = @_;
    1.67 +
    1.68 +    my $result;
    1.69 +    if (!ref( $this->{op})) {
    1.70 +        $result = $this->{params}[0];
    1.71 +        if (MONITOR_EVAL) {
    1.72 +            print STDERR "LEAF: ",(defined($result)?$result:'undef'),"\n";
    1.73 +        }
    1.74 +    } else {
    1.75 +        my $fn = $this->{op}->{evaluate};
    1.76 +        $result = &$fn( $clientData, @{$this->{params}} );
    1.77 +        if (MONITOR_EVAL) {
    1.78 +            print STDERR "NODE: ",$this->stringify()," -> ",
    1.79 +              (defined($result)?$result:'undef'),"\n";
    1.80 +        }
    1.81 +    }
    1.82 +    return $result;
    1.83 +}
    1.84 +
    1.85 +sub stringify {
    1.86 +    my $this = shift;
    1.87 +
    1.88 +    unless( ref( $this->{op} )) {
    1.89 +        if( $this->{op} == $TWiki::Infix::Node::STRING ) {
    1.90 +            return "'$this->{params}[0]'";
    1.91 +        } else {
    1.92 +            return $this->{params}[0];
    1.93 +        }
    1.94 +    }
    1.95 +
    1.96 +    return $this->{op}->{name}.'{'.
    1.97 +      join(',', map { $_->stringify() } @{$this->{params}}).'}';
    1.98 +}
    1.99 +
   1.100 +1;
   1.101 +__DATA__
   1.102 +
   1.103 +Module of TWiki Enterprise Collaboration Platform, http://TWiki.org/
   1.104 +
   1.105 +Copyright (C) 2005-2007 TWiki Contributors. All Rights Reserved.
   1.106 +TWiki Contributors
   1.107 +are listed in the AUTHORS file in the root of this distribution.
   1.108 +NOTE: Please extend that file, not this notice.
   1.109 +
   1.110 +This program is free software; you can redistribute it and/or
   1.111 +modify it under the terms of the GNU General Public License
   1.112 +as published by the Free Software Foundation; either version 2
   1.113 +of the License, or (at your option) any later version. For
   1.114 +more details read LICENSE in the root of this distribution.
   1.115 +
   1.116 +This program is distributed in the hope that it will be useful,
   1.117 +but WITHOUT ANY WARRANTY; without even the implied warranty of
   1.118 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   1.119 +
   1.120 +As per the GPL, removal of this notice is prohibited.
   1.121 +
   1.122 +Author: Crawford Currie http://c-dot.co.uk