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