]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser-scheme.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / lily-parser-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--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 <unistd.h>
21
22 #include "lily-parser.hh"
23
24 #include "file-name-map.hh"
25 #include "file-name.hh"
26 #include "file-path.hh"
27 #include "international.hh"
28 #include "lily-lexer.hh"
29 #include "main.hh"
30 #include "program-option.hh"
31 #include "sources.hh"
32 #include "warn.hh"
33 #include "lily-imports.hh"
34
35 using std::string;
36
37 LY_DEFINE (ly_parse_file, "ly:parse-file",
38            1, 0, 0, (SCM name),
39            "Parse a single @code{.ly} file."
40            "  Upon failure, throw @code{ly-file-failed} key.")
41 {
42   LY_ASSERT_TYPE (scm_is_string, name, 1);
43   string file = ly_scm2string (name);
44   char const *extensions[] = {"ly", "", 0};
45
46   string file_name = global_path.find (file, extensions);
47
48   /* By default, use base name of input file for output file name,
49      write output to cwd; do not use root and directory parts of input
50      file name.  */
51   File_name out_file_name (file_name);
52
53   out_file_name.ext_ = "";
54   out_file_name.root_ = "";
55   if (!to_boolean (ly_get_option (ly_symbol2scm ("gui")))
56       && to_boolean (ly_get_option (ly_symbol2scm ("strip-output-dir"))))
57     {
58       out_file_name.dir_ = "";
59     }
60
61   /* When running from gui, generate output in .ly source directory.  */
62   string output_name = output_name_global;
63   if (!output_name.empty ())
64     {
65       /* Interpret --output=DIR to mean --output=DIR/BASE.  */
66       string dir;
67       if (is_dir (output_name))
68         {
69           dir = output_name;
70           output_name = "";
71         }
72       else
73         {
74           File_name out (output_name);
75           dir = out.dir_part ();
76           out_file_name = out.file_part ();
77         }
78
79       if (dir != "" && dir != "." && dir != get_working_directory ())
80         {
81           global_path.prepend (get_working_directory ());
82           message (_f ("Changing working directory to: `%s'", dir));
83           // If we can't change to the output dir (not existing, wrong
84           // permissions), exit lilypond
85           if (chdir (dir.c_str ()) != 0)
86             error (_f ("unable to change directory to: `%s'", dir));
87         }
88       else
89         out_file_name = File_name (output_name);
90     }
91
92   string init;
93   if (!init_name_global.empty ())
94     init = init_name_global;
95   else
96     init = "init.ly";
97
98   string out_file = out_file_name.to_string ();
99   if (init.length () && global_path.find (init).empty ())
100     {
101       warning (_f ("cannot find init file: `%s'", init));
102       warning (_f ("(search path: `%s')",
103                    global_path.to_string ().c_str ()));
104       exit (2);
105     }
106
107   bool error = false;
108   if ((file_name != "-") && file_name.empty ())
109     {
110       warning (_f ("cannot find file: `%s'", file));
111       error = true;
112     }
113   else
114     {
115       Sources sources;
116       sources.set_path (&global_path);
117
118       string mapped_fn = map_file_name (file_name);
119       basic_progress (_f ("Processing `%s'", mapped_fn.c_str ()));
120
121       Lily_parser *parser = new Lily_parser (&sources);
122
123       parser->parse_file (init, file_name, out_file);
124
125       error = parser->error_level_;
126
127       parser->clear ();
128       parser->unprotect ();
129     }
130
131   /*
132     outside the if-else to ensure cleanup fo Sources object,
133   */
134   if (error)
135     /* TODO: pass renamed input file too.  */
136     scm_throw (ly_symbol2scm ("ly-file-failed"),
137                scm_list_1 (ly_string2scm (file_name)));
138
139   return SCM_UNSPECIFIED;
140 }
141
142 LY_DEFINE (ly_parser_lexer, "ly:parser-lexer",
143            0, 1, 0, (SCM parser),
144            "Return the lexer for @var{parser}, defaulting to current parser")
145 {
146   if (SCM_UNBNDP (parser))
147     parser = scm_fluid_ref (Lily::f_parser);
148   Lily_parser *p = LY_ASSERT_SMOB (Lily_parser, parser, 1);
149   return p->lexer_->self_scm ();
150 }
151
152 LY_DEFINE (ly_parser_clone, "ly:parser-clone",
153            0, 2, 0, (SCM closures, SCM location),
154            "Return a clone of current parser.  An association list"
155            " of port positions to closures can be specified in @var{closures}"
156            " in order to have @code{$} and @code{#} interpreted in their original"
157            " lexical environment.  If @var{location} is a valid location,"
158            " it becomes the source of all music expressions inside.")
159 {
160   SCM parser = scm_fluid_ref (Lily::f_parser);
161   Lily_parser *p = LY_ASSERT_SMOB (Lily_parser, parser, 0);
162
163   if (SCM_UNBNDP (closures))
164     closures = SCM_EOL;
165   else
166     LY_ASSERT_TYPE (ly_is_list, closures, 2);
167   Lily_parser *clone = new Lily_parser (*p, closures, location);
168
169   return clone->unprotect ();
170 }
171
172 LY_DEFINE (ly_parser_define_x, "ly:parser-define!",
173            2, 0, 0, (SCM symbol, SCM val),
174            "Bind @var{symbol} to @var{val} in current parser's module.")
175 {
176   SCM parser = scm_fluid_ref (Lily::f_parser);
177   Lily_parser *p = LY_ASSERT_SMOB (Lily_parser, parser, 0);
178
179   LY_ASSERT_TYPE (ly_is_symbol, symbol, 1);
180
181   p->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
182   return SCM_UNSPECIFIED;
183 }
184
185 LY_DEFINE (ly_parser_lookup, "ly:parser-lookup",
186            1, 0, 0, (SCM symbol),
187            "Look up @var{symbol} in current parser's module."
188            "  Return @code{'()} if not defined.")
189 {
190   SCM parser = scm_fluid_ref (Lily::f_parser);
191   Lily_parser *p = LY_ASSERT_SMOB (Lily_parser, parser, 0);
192
193   LY_ASSERT_TYPE (ly_is_symbol, symbol, 1);
194
195   SCM val = p->lexer_->lookup_identifier (ly_symbol2string (symbol));
196   if (!SCM_UNBNDP (val))
197     return val;
198   else
199     return SCM_EOL;
200 }
201
202 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
203            2, 0, 0, (SCM parser_smob, SCM ly_code),
204            "Parse the string @var{ly-code} with @var{parser-smob}."
205            "  Upon failure, throw @code{ly-file-failed} key.")
206 {
207   LY_ASSERT_SMOB (Lily_parser, parser_smob, 1);
208   Lily_parser *parser = unsmob<Lily_parser> (parser_smob);
209   LY_ASSERT_TYPE (scm_is_string, ly_code, 2);
210
211   if (!parser->lexer_->is_clean ())
212     parser->parser_error (_ ("ly:parser-parse-string is only valid with a new parser."
213                              "  Use ly:parser-include-string instead."));
214   else
215     parser->parse_string (ly_scm2string (ly_code));
216
217   return SCM_UNSPECIFIED;
218 }
219
220 LY_DEFINE (ly_parse_string_expression, "ly:parse-string-expression",
221            2, 2, 0, (SCM parser_smob, SCM ly_code, SCM filename,
222                      SCM line),
223            "Parse the string @var{ly-code} with @var{parser-smob}."
224            " Return the contained music expression."
225            " @var{filename} and @var{line} are optional source indicators.")
226 {
227   LY_ASSERT_SMOB (Lily_parser, parser_smob, 1);
228   Lily_parser *parser = unsmob<Lily_parser> (parser_smob);
229   LY_ASSERT_TYPE (scm_is_string, ly_code, 2);
230   string fn;
231   if (SCM_UNBNDP (filename) || !scm_is_string (filename))
232     fn = "<string>";
233   else
234     fn = ly_scm2string (filename);
235   int ln;
236   if (SCM_UNBNDP (line) || !scm_is_integer (line))
237     ln = 0;
238   else
239     ln = scm_to_int (line);
240
241   if (!parser->lexer_->is_clean ())
242     {
243       parser->parser_error (_ ("ly:parse-string-expression is only valid with a new parser."
244                                "  Use ly:parser-include-string instead."));
245       return SCM_UNSPECIFIED;
246     }
247
248   return parser->parse_string_expression (ly_scm2string (ly_code),
249                                           fn, ln);
250 }
251
252 LY_DEFINE (ly_parser_include_string, "ly:parser-include-string",
253            1, 0, 0, (SCM ly_code),
254            "Include the string @var{ly-code} into the input stream"
255            " for current parser.  Can only be used in immediate"
256            " Scheme expressions (@code{$} instead of @code{#}).")
257 {
258   SCM parser = scm_fluid_ref (Lily::f_parser);
259   Lily_parser *p = LY_ASSERT_SMOB (Lily_parser, parser, 0);
260
261   LY_ASSERT_TYPE (scm_is_string, ly_code, 1);
262
263   p->include_string (ly_scm2string (ly_code));
264
265   return SCM_UNSPECIFIED;
266 }
267
268 LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
269            1, 0, 0, (SCM names),
270            "Replace current note names in parser."
271            "  @var{names} is an alist of symbols.  This only has effect"
272            " if the current mode is notes.")
273 {
274   SCM parser = scm_fluid_ref (Lily::f_parser);
275   Lily_parser *p = LY_ASSERT_SMOB (Lily_parser, parser, 0);
276
277   if (p->lexer_->is_note_state ())
278     {
279       p->lexer_->pop_state ();
280       p->lexer_->push_note_state (names);
281     }
282
283   return SCM_UNSPECIFIED;
284 }
285
286 LY_DEFINE (ly_parser_output_name, "ly:parser-output-name",
287            0, 1, 0, (SCM parser),
288            "Return the base name of the output file.  If @code{parser} is left off, use currently active parser.")
289 {
290   if (SCM_UNBNDP (parser))
291     parser = scm_fluid_ref (Lily::f_parser);
292
293   Lily_parser *p = LY_ASSERT_SMOB (Lily_parser, parser, 1);
294
295   return ly_string2scm (p->output_basename_);
296 }
297
298 LY_DEFINE (ly_parser_error, "ly:parser-error",
299            1, 1, 0, (SCM msg, SCM input),
300            "Display an error message and make current parser fail."
301            " Without a current parser, trigger an ordinary error.")
302 {
303   SCM parser = scm_fluid_ref (Lily::f_parser);
304
305   Lily_parser *p = unsmob<Lily_parser> (parser);
306
307   LY_ASSERT_TYPE (scm_is_string, msg, 1);
308   string s = ly_scm2string (msg);
309
310   Input *i = unsmob<Input> (input);
311   if (p)
312     {
313       if (i)
314         p->parser_error (*i, s);
315       else
316         p->parser_error (s);
317     }
318   else
319     {
320       if (i)
321         i->non_fatal_error (s);
322       else
323         scm_misc_error ("ly:parser-error", "~A", scm_list_1 (msg));
324     }
325   return SCM_UNSPECIFIED;
326 }
327
328 LY_DEFINE (ly_parser_clear_error, "ly:parser-clear-error",
329            0, 1, 0, (SCM parser),
330            "Clear error flag for @var{parser}, defaulting to current parser.")
331 {
332   if (SCM_UNBNDP (parser))
333     parser = scm_fluid_ref (Lily::f_parser);
334
335   Lily_parser *p = LY_ASSERT_SMOB (Lily_parser, parser, 1);
336
337   p->error_level_ = 0;
338   p->lexer_->error_level_ = 0;
339
340   return SCM_UNSPECIFIED;
341 }
342
343 LY_DEFINE (ly_parser_has_error_p, "ly:parser-has-error?",
344            0, 1, 0, (SCM parser),
345            "Does @var{parser} (defaulting to current parser) have an error flag?")
346 {
347   if (SCM_UNBNDP (parser))
348     parser = scm_fluid_ref (Lily::f_parser);
349   Lily_parser *p = LY_ASSERT_SMOB (Lily_parser, parser, 1);
350
351   return scm_from_bool (p->error_level_ || p->lexer_->error_level_);
352 }