2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2005--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
22 #include "lily-parser.hh"
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"
30 #include "program-option.hh"
34 LY_DEFINE (ly_parse_file, "ly:parse-file",
36 "Parse a single @code{.ly} file."
37 " Upon failure, throw @code{ly-file-failed} key.")
39 LY_ASSERT_TYPE (scm_is_string, name, 1);
40 string file = ly_scm2string (name);
41 char const *extensions[] = {"ly", "", 0};
43 string file_name = global_path.find (file, extensions);
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
48 File_name out_file_name (file_name);
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"))))
55 out_file_name.dir_ = "";
58 /* When running from gui, generate output in .ly source directory. */
59 string output_name = output_name_global;
60 if (!output_name.empty ())
62 /* Interpret --output=DIR to mean --output=DIR/BASE. */
64 if (is_dir (output_name))
71 File_name out (output_name);
72 dir = out.dir_part ();
73 out_file_name = out.file_part ();
76 if (dir != "" && dir != "." && dir != get_working_directory ())
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));
86 out_file_name = File_name (output_name);
90 if (!init_name_global.empty ())
91 init = init_name_global;
95 string out_file = out_file_name.to_string ();
96 if (init.length () && global_path.find (init).empty ())
98 warning (_f ("cannot find init file: `%s'", init));
99 warning (_f ("(search path: `%s')",
100 global_path.to_string ().c_str ()));
105 if ((file_name != "-") && file_name.empty ())
107 warning (_f ("cannot find file: `%s'", file));
113 sources.set_path (&global_path);
115 string mapped_fn = map_file_name (file_name);
116 basic_progress (_f ("Processing `%s'", mapped_fn.c_str ()));
118 Lily_parser *parser = new Lily_parser (&sources);
120 parser->parse_file (init, file_name, out_file);
122 error = parser->error_level_;
125 parser->unprotect ();
129 outside the if-else to ensure cleanup fo Sources object,
132 /* TODO: pass renamed input file too. */
133 scm_throw (ly_symbol2scm ("ly-file-failed"),
134 scm_list_1 (ly_string2scm (file_name)));
136 return SCM_UNSPECIFIED;
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")
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 ();
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.")
157 SCM parser = scm_fluid_ref (ly_lily_module_constant("%parser"));
158 Lily_parser *p = LY_ASSERT_SMOB (Lily_parser, parser, 0);
160 if (SCM_UNBNDP (closures))
163 LY_ASSERT_TYPE (ly_is_list, closures, 2);
164 Lily_parser *clone = new Lily_parser (*p, closures, location);
166 return clone->unprotect ();
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.")
173 SCM parser = scm_fluid_ref (ly_lily_module_constant ("%parser"));
174 Lily_parser *p = LY_ASSERT_SMOB (Lily_parser, parser, 0);
176 LY_ASSERT_TYPE (ly_is_symbol, symbol, 1);
178 p->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
179 return SCM_UNSPECIFIED;
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.")
187 SCM parser = scm_fluid_ref (ly_lily_module_constant ("%parser"));
188 Lily_parser *p = LY_ASSERT_SMOB (Lily_parser, parser, 0);
190 LY_ASSERT_TYPE (ly_is_symbol, symbol, 1);
192 SCM val = p->lexer_->lookup_identifier (ly_symbol2string (symbol));
193 if (!SCM_UNBNDP (val))
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.")
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);
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."));
212 parser->parse_string (ly_scm2string (ly_code));
214 return SCM_UNSPECIFIED;
217 LY_DEFINE (ly_parse_string_expression, "ly:parse-string-expression",
218 2, 2, 0, (SCM parser_smob, SCM ly_code, SCM filename,
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.")
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);
228 if (SCM_UNBNDP (filename) || !scm_is_string (filename))
231 fn = ly_scm2string (filename);
233 if (SCM_UNBNDP (line) || !scm_is_integer (line))
236 ln = scm_to_int (line);
238 if (!parser->lexer_->is_clean ())
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;
245 return parser->parse_string_expression (ly_scm2string (ly_code),
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{#}).")
255 SCM parser = scm_fluid_ref (ly_lily_module_constant ("%parser"));
256 Lily_parser *p = LY_ASSERT_SMOB (Lily_parser, parser, 0);
258 LY_ASSERT_TYPE (scm_is_string, ly_code, 1);
260 p->include_string (ly_scm2string (ly_code));
262 return SCM_UNSPECIFIED;
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.")
271 SCM parser = scm_fluid_ref (ly_lily_module_constant ("%parser"));
272 Lily_parser *p = LY_ASSERT_SMOB (Lily_parser, parser, 0);
274 if (p->lexer_->is_note_state ())
276 p->lexer_->pop_state ();
277 p->lexer_->push_note_state (names);
280 return SCM_UNSPECIFIED;
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.")
287 if (SCM_UNBNDP (parser))
288 parser = scm_fluid_ref (ly_lily_module_constant ("%parser"));
290 Lily_parser *p = LY_ASSERT_SMOB (Lily_parser, parser, 1);
292 return ly_string2scm (p->output_basename_);
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.")
300 SCM parser = scm_fluid_ref (ly_lily_module_constant ("%parser"));
302 Lily_parser *p = unsmob<Lily_parser> (parser);
304 LY_ASSERT_TYPE (scm_is_string, msg, 1);
305 string s = ly_scm2string (msg);
307 Input *i = unsmob<Input> (input);
311 p->parser_error (*i, s);
318 i->non_fatal_error (s);
320 scm_misc_error ("ly:parser-error", "~A", scm_list_1 (msg));
322 return SCM_UNSPECIFIED;
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.")
329 if (SCM_UNBNDP (parser))
330 parser = scm_fluid_ref (ly_lily_module_constant ("%parser"));
332 Lily_parser *p = LY_ASSERT_SMOB (Lily_parser, parser, 1);
335 p->lexer_->error_level_ = 0;
337 return SCM_UNSPECIFIED;
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?")
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);
348 return scm_from_bool (p->error_level_ || p->lexer_->error_level_);