Annotation of loncom/homework/math_parser/ParseException.pm, revision 1.1

1.1     ! damieng     1: # The LearningOnline Network with CAPA - LON-CAPA
        !             2: # Parse exception
        !             3: #
        !             4: # Copyright (C) 2014 Michigan State University Board of Trustees
        !             5: #
        !             6: # This program is free software: you can redistribute it and/or modify
        !             7: # it under the terms of the GNU General Public License as published by
        !             8: # the Free Software Foundation, either version 3 of the License, or
        !             9: # (at your option) any later version.
        !            10: #
        !            11: # This program is distributed in the hope that it will be useful,
        !            12: # but WITHOUT ANY WARRANTY; without even the implied warranty of
        !            13: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
        !            14: # GNU General Public License for more details.
        !            15: #
        !            16: # You should have received a copy of the GNU General Public License
        !            17: # along with this program. If not, see <http://www.gnu.org/licenses/>.
        !            18: #
        !            19: 
        !            20: ##
        !            21: # Parse exception
        !            22: ##
        !            23: package Apache::math_parser::ParseException;
        !            24: 
        !            25: use strict;
        !            26: use warnings;
        !            27: use utf8;
        !            28: 
        !            29: use Apache::lonlocal;
        !            30: 
        !            31: use overload '""' => \&toString;
        !            32: 
        !            33: ##
        !            34: # Constructor
        !            35: # @param {string} msg - error message, using [_1] for the first parameter
        !            36: # @param {integer} from - Character index
        !            37: # @optional {string} to - Character index to (inclusive)
        !            38: # @optional {...string} param - parameters for the message
        !            39: ##
        !            40: sub new {
        !            41:     my $class = shift;
        !            42:     my $self = {
        !            43:         _msg => shift,
        !            44:         _from => shift,
        !            45:         _to => shift,
        !            46:         _params => [],
        !            47:     };
        !            48:     while (@_) {
        !            49:         push(@{$self->{_params}}, shift);
        !            50:     }
        !            51:     if (! defined $self->{_to}) {
        !            52:         $self->{_to} = $self->{_from};
        !            53:     }
        !            54:     bless $self, $class;
        !            55:     return $self;
        !            56: }
        !            57: 
        !            58: # Attribute helpers
        !            59: 
        !            60: sub msg {
        !            61:     my $self = shift;
        !            62:     return $self->{_msg};
        !            63: }
        !            64: sub from {
        !            65:     my $self = shift;
        !            66:     return $self->{_from};
        !            67: }
        !            68: sub to {
        !            69:     my $self = shift;
        !            70:     return $self->{_to};
        !            71: }
        !            72: sub params {
        !            73:     my $self = shift;
        !            74:     return $self->{_params};
        !            75: }
        !            76: 
        !            77: ##
        !            78: # Returns the exception as a string, for debug only.
        !            79: # @returns {string}
        !            80: ##
        !            81: sub toString {
        !            82:     my $self = shift;
        !            83:     my $s = "Parsing error: ".$self->msg." at ".$self->from." - ".$self->to;
        !            84:     if (scalar(@{$self->params}) > 0) {
        !            85:         $s .= ", ".join(", ", @{$self->params});
        !            86:     }
        !            87:     return $s;
        !            88: }
        !            89: 
        !            90: ##
        !            91: # Returns the error message localized for the user interface.
        !            92: # @returns {string}
        !            93: ##
        !            94: sub getLocalizedMessage {
        !            95:     my $self = shift;
        !            96:     return mt($self->msg, @{$self->params});
        !            97: }
        !            98: 
        !            99: 1;
        !           100: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>