]> git.donarmstrong.com Git - lilypond.git/blob - lily/input.cc
Issue 4439/2: Let Input::error actually raise a fatal error
[lilypond.git] / lily / input.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond 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   LilyPond 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 LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "input.hh"
21
22 #include <cstdio>
23 using namespace std;
24
25 #include "international.hh"
26 #include "program-option.hh"
27 #include "source-file.hh"
28 #include "sources.hh"
29 #include "warn.hh"
30
31 Input::Input (Input const &i)
32 {
33   source_file_ = i.source_file_;
34   start_ = i.start_;
35   end_ = i.end_;
36 }
37
38 Input::Input ()
39 {
40   source_file_ = 0;
41   start_ = 0;
42   end_ = 0;
43 }
44
45 Input
46 Input::spot () const
47 {
48   return *this;
49 }
50
51 void
52 Input::set_spot (Input const &i)
53 {
54   *this = i;
55 }
56
57 void
58 Input::step_forward ()
59 {
60   if (end_ == start_)
61     end_++;
62   start_++;
63 }
64
65 void
66 Input::set_location (Input const &i_start, Input const &i_end)
67 {
68   source_file_ = i_start.source_file_;
69   start_ = i_start.start_;
70   end_ = i_end.end_;
71 }
72
73 /*
74   Produce GNU-compliant error message.  Correcting lilypond source is
75   such a breeze if you ('re edidor) know (s) the error column too
76
77   Format:
78
79   [file:line:column:][warning:]message
80 */
81 string
82 Input::message_string (const string &msg) const
83 {
84   if (source_file_)
85     return msg + "\n" + source_file_->quote_input (start_);
86   else
87     return msg;
88 }
89
90 string
91 Input::message_location () const
92 {
93   return (source_file_) ? location_string () : "";
94 }
95
96 void
97 Input::error (const string &s) const
98 {
99   ::error (message_string (s), message_location ());
100 }
101
102 void
103 Input::programming_error (const string &s) const
104 {
105   ::programming_error (message_string (s), message_location ());
106 }
107
108 void
109 Input::non_fatal_error (const string &s) const
110 {
111   ::non_fatal_error (message_string (s), message_location ());
112 }
113
114 void
115 Input::warning (const string &s) const
116 {
117   ::warning (message_string (s), message_location ());
118 }
119
120 void
121 Input::message (const string &s) const
122 {
123   ::message (message_string (s), true, message_location ());
124 }
125
126 void
127 Input::debug_output (const string &s) const
128 {
129   ::debug_output (message_string (s), true, message_location ());
130 }
131
132 string
133 Input::location_string () const
134 {
135   if (source_file_)
136     return source_file_->file_line_column_string (start_);
137   return " (" + _ ("position unknown") + ")";
138 }
139
140 string
141 Input::line_number_string () const
142 {
143   if (source_file_)
144     return ::to_string (source_file_->get_line (start_));
145   return "?";
146 }
147
148 string
149 Input::file_string () const
150 {
151   if (source_file_)
152     return source_file_->name_string ();
153   return "";
154 }
155
156 int
157 Input::line_number () const
158 {
159   if (source_file_)
160     return source_file_->get_line (start_);
161   return 0;
162 }
163
164 int
165 Input::column_number () const
166 {
167   int line, chr, col, offset = 0;
168   source_file_->get_counts (start_, &line, &chr, &col, &offset);
169
170   return col;
171 }
172
173 int
174 Input::end_line_number () const
175 {
176   if (source_file_)
177     return source_file_->get_line (end_);
178   return 0;
179 }
180
181 int
182 Input::end_column_number () const
183 {
184   int line, chr, col, offset = 0;
185   source_file_->get_counts (end_, &line, &chr, &col, &offset);
186
187   return col;
188 }
189
190 void
191 Input::get_counts (int *line, int *chr, int *col, int *offset) const
192 {
193   source_file_->get_counts (start_, line, chr, col, offset);
194 }
195
196 void
197 Input::set (Source_file *sf, char const *start, char const *end)
198 {
199   source_file_ = sf;
200   start_ = start;
201   end_ = end;
202 }
203
204 Source_file *
205 Input::get_source_file () const
206 {
207   return source_file_;
208 }
209
210 char const *
211 Input::start () const
212 {
213   return start_;
214 }
215
216 char const *
217 Input::end () const
218 {
219   return end_;
220 }