]> git.donarmstrong.com Git - lilypond.git/blob - lily/input.cc
Web-ja: update introduction
[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 "lily-imports.hh"
27 #include "program-option.hh"
28 #include "source-file.hh"
29 #include "sources.hh"
30 #include "warn.hh"
31
32 Input::Input (Input const &i)
33 {
34   source_file_ = i.source_file_;
35   start_ = i.start_;
36   end_ = i.end_;
37 }
38
39 Input::Input ()
40 {
41   source_file_ = 0;
42   start_ = 0;
43   end_ = 0;
44 }
45
46 Input
47 Input::spot () const
48 {
49   return *this;
50 }
51
52 void
53 Input::set_spot (Input const &i)
54 {
55   *this = i;
56 }
57
58 void
59 Input::step_forward ()
60 {
61   if (end_ == start_)
62     end_++;
63   start_++;
64 }
65
66 void
67 Input::set_location (Input const &i_start, Input const &i_end)
68 {
69   source_file_ = i_start.source_file_;
70   start_ = i_start.start_;
71   end_ = i_end.end_;
72 }
73
74 /*
75   Produce GNU-compliant error message.  Correcting lilypond source is
76   such a breeze if you ('re edidor) know (s) the error column too
77
78   Format:
79
80   [file:line:column:][warning:]message
81 */
82 string
83 Input::message_string (const string &msg) const
84 {
85   if (source_file_)
86     return msg + "\n" + source_file_->quote_input (start_);
87   else
88     return msg;
89 }
90
91 string
92 Input::message_location () const
93 {
94   return (source_file_) ? location_string () : "";
95 }
96
97 void
98 Input::error (const string &s) const
99 {
100   ::error (message_string (s), message_location ());
101 }
102
103 void
104 Input::programming_error (const string &s) const
105 {
106   ::programming_error (message_string (s), message_location ());
107 }
108
109 void
110 Input::non_fatal_error (const string &s) const
111 {
112   ::non_fatal_error (message_string (s), message_location ());
113 }
114
115 void
116 Input::warning (const string &s) const
117 {
118   ::warning (message_string (s), message_location ());
119 }
120
121 void
122 Input::message (const string &s) const
123 {
124   ::message (message_string (s), true, message_location ());
125 }
126
127 void
128 Input::debug_output (const string &s) const
129 {
130   ::debug_output (message_string (s), true, message_location ());
131 }
132
133 string
134 Input::location_string () const
135 {
136   if (source_file_)
137     return source_file_->file_line_column_string (start_);
138   return " (" + _ ("position unknown") + ")";
139 }
140
141 string
142 Input::line_number_string () const
143 {
144   if (source_file_)
145     return ::to_string (source_file_->get_line (start_));
146   return "?";
147 }
148
149 string
150 Input::file_string () const
151 {
152   if (source_file_)
153     return source_file_->name_string ();
154   return "";
155 }
156
157 int
158 Input::line_number () const
159 {
160   if (source_file_)
161     return source_file_->get_line (start_);
162   return 0;
163 }
164
165 int
166 Input::column_number () const
167 {
168   int line, chr, col, offset = 0;
169   source_file_->get_counts (start_, &line, &chr, &col, &offset);
170
171   return col;
172 }
173
174 int
175 Input::end_line_number () const
176 {
177   if (source_file_)
178     return source_file_->get_line (end_);
179   return 0;
180 }
181
182 int
183 Input::end_column_number () const
184 {
185   int line, chr, col, offset = 0;
186   source_file_->get_counts (end_, &line, &chr, &col, &offset);
187
188   return col;
189 }
190
191 void
192 Input::get_counts (int *line, int *chr, int *col, int *offset) const
193 {
194   source_file_->get_counts (start_, line, chr, col, offset);
195 }
196
197 void
198 Input::set (Source_file *sf, char const *start, char const *end)
199 {
200   source_file_ = sf;
201   start_ = start;
202   end_ = end;
203 }
204
205 Source_file *
206 Input::get_source_file () const
207 {
208   return source_file_;
209 }
210
211 char const *
212 Input::start () const
213 {
214   return start_;
215 }
216
217 char const *
218 Input::end () const
219 {
220   return end_;
221 }
222
223 static SCM
224 with_location_hook_0 (void *it)
225 {
226   SCM *args = static_cast <SCM *> (it);
227   return scm_call_0 (args[0]);
228 }
229
230 SCM
231 with_location (SCM loc, SCM proc)
232 {
233   return scm_c_with_fluid (Lily::f_location,
234                            unsmob<Input> (loc) ? loc : SCM_BOOL_F,
235                            with_location_hook_0,
236                            static_cast <void *> (&proc));
237 }
238
239 static SCM
240 with_location_hook_1 (void *it)
241 {
242   SCM *args = static_cast <SCM *> (it);
243   return scm_call_1 (args[0], args[1]);
244 }
245
246 SCM
247 with_location (SCM loc, SCM proc, SCM arg1)
248 {
249   SCM args[] = { proc, arg1 };
250   return scm_c_with_fluid (Lily::f_location,
251                            unsmob<Input> (loc) ? loc : SCM_BOOL_F,
252                            with_location_hook_1,
253                            static_cast <void *> (&args));
254 }
255
256 static SCM
257 with_location_hook_2 (void *it)
258 {
259   SCM *args = static_cast <SCM *> (it);
260   return scm_call_2 (args[0], args[1], args[2]);
261 }
262
263 SCM
264 with_location (SCM loc, SCM proc, SCM arg1, SCM arg2)
265 {
266   SCM args[] = { proc, arg1, arg2 };
267   return scm_c_with_fluid (Lily::f_location,
268                            unsmob<Input> (loc) ? loc : SCM_BOOL_F,
269                            with_location_hook_2,
270                            static_cast <void *> (&args));
271 }
272
273 static SCM
274 with_location_hook_3 (void *it)
275 {
276   SCM *args = static_cast <SCM *> (it);
277   return scm_call_3 (args[0], args[1], args[2], args[3]);
278 }
279
280 SCM
281 with_location (SCM loc, SCM proc, SCM arg1, SCM arg2, SCM arg3)
282 {
283   SCM args[] = { proc, arg1, arg2, arg3 };
284   return scm_c_with_fluid (Lily::f_location,
285                            unsmob<Input> (loc) ? loc : SCM_BOOL_F,
286                            with_location_hook_3,
287                            static_cast <void *> (&args));
288 }
289
290 static SCM
291 with_location_hook_4 (void *it)
292 {
293   SCM *args = static_cast <SCM *> (it);
294   return scm_call_4 (args[0], args[1], args[2], args[3], args[4]);
295 }
296
297 SCM
298 with_location (SCM loc, SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4)
299 {
300   SCM args[] = { proc, arg1, arg2, arg3, arg4 };
301   return scm_c_with_fluid (Lily::f_location,
302                            unsmob<Input> (loc) ? loc : SCM_BOOL_F,
303                            with_location_hook_4,
304                            static_cast <void *> (&args));
305 }
306
307 static SCM
308 with_location_hook_n (void *it)
309 {
310   SCM *args = static_cast <SCM *> (it);
311   return scm_apply_0 (args[0], args[1]);
312 }
313
314 SCM
315 with_location (SCM loc, SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4, SCM arg5)
316 {
317   SCM args[] = { proc, scm_list_5 (arg1, arg2, arg3, arg4, arg5) };
318   return scm_c_with_fluid (Lily::f_location,
319                            unsmob<Input> (loc) ? loc : SCM_BOOL_F,
320                            with_location_hook_n,
321                            static_cast <void *> (&args));
322 }