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