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