2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2005--2012 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"
29 #include "ly-module.hh"
31 #include "program-option.hh"
35 LY_DEFINE (ly_parse_file, "ly:parse-file",
37 "Parse a single @code{.ly} file."
38 " Upon failure, throw @code{ly-file-failed} key.")
40 LY_ASSERT_TYPE (scm_is_string, name, 1);
41 string file = ly_scm2string (name);
42 char const *extensions[] = {"ly", "", 0};
44 string file_name = global_path.find (file, extensions);
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
49 File_name out_file_name (file_name);
51 out_file_name.ext_ = "";
52 out_file_name.root_ = "";
53 if (ly_get_option (ly_symbol2scm ("gui")) != SCM_BOOL_T
54 && ly_get_option (ly_symbol2scm ("strip-output-dir")) == SCM_BOOL_T)
56 out_file_name.dir_ = "";
59 /* When running from gui, generate output in .ly source directory. */
60 string output_name = output_name_global;
61 if (!output_name.empty ())
63 /* Interpret --output=DIR to mean --output=DIR/BASE. */
65 if (is_dir (output_name))
72 File_name out (output_name);
73 dir = out.dir_part ();
74 out_file_name = out.file_part ();
77 if (dir != "" && dir != "." && dir != get_working_directory ())
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));
87 out_file_name = File_name (output_name);
91 if (!init_name_global.empty ())
92 init = init_name_global;
96 string out_file = out_file_name.to_string ();
97 if (init.length () && global_path.find (init).empty ())
99 warning (_f ("cannot find init file: `%s'", init));
100 warning (_f ("(search path: `%s')",
101 global_path.to_string ().c_str ()));
106 if ((file_name != "-") && file_name.empty ())
108 warning (_f ("cannot find file: `%s'", file));
114 sources.set_path (&global_path);
116 string mapped_fn = map_file_name (file_name);
117 basic_progress (_f ("Processing `%s'", mapped_fn.c_str ()));
119 Lily_parser *parser = new Lily_parser (&sources);
121 parser->parse_file (init, file_name, out_file);
123 error = parser->error_level_;
126 parser->unprotect ();
130 outside the if-else to ensure cleanup fo Sources object,
133 /* TODO: pass renamed input file too. */
134 scm_throw (ly_symbol2scm ("ly-file-failed"),
135 scm_list_1 (ly_string2scm (file_name)));
137 return SCM_UNSPECIFIED;
140 LY_DEFINE (ly_parser_lexer, "ly:parser-lexer",
141 1, 0, 0, (SCM parser_smob),
142 "Return the lexer for @var{parser-smob}.")
144 Lily_parser *parser = unsmob_lily_parser (parser_smob);
145 return parser->lexer_->self_scm ();
148 LY_DEFINE (ly_parser_clone, "ly:parser-clone",
149 1, 1, 0, (SCM parser_smob, SCM closures),
150 "Return a clone of @var{parser-smob}. An association list"
151 " of port positions to closures can be specified in @var{closures}"
152 " in order to have @code{$} and @code{#} interpreted in their original"
153 " lexical environment.")
155 LY_ASSERT_SMOB (Lily_parser, parser_smob, 1);
156 Lily_parser *parser = unsmob_lily_parser (parser_smob);
157 if (SCM_UNBNDP (closures))
160 LY_ASSERT_TYPE (ly_is_list, closures, 2);
161 Lily_parser *clone = new Lily_parser (*parser, closures);
163 return clone->unprotect ();
166 LY_DEFINE (ly_parser_define_x, "ly:parser-define!",
167 3, 0, 0, (SCM parser_smob, SCM symbol, SCM val),
168 "Bind @var{symbol} to @var{val} in @var{parser-smob}'s module.")
171 LY_ASSERT_SMOB (Lily_parser, parser_smob, 1);
172 Lily_parser *parser = unsmob_lily_parser (parser_smob);
174 LY_ASSERT_TYPE (ly_is_symbol, symbol, 2);
176 parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
177 return SCM_UNSPECIFIED;
180 LY_DEFINE (ly_parser_lookup, "ly:parser-lookup",
181 2, 0, 0, (SCM parser_smob, SCM symbol),
182 "Look up @var{symbol} in @var{parser-smob}'s module."
183 " Return @code{'()} if not defined.")
185 LY_ASSERT_SMOB (Lily_parser, parser_smob, 1);
187 Lily_parser *parser = unsmob_lily_parser (parser_smob);
189 LY_ASSERT_TYPE (ly_is_symbol, symbol, 2);
191 SCM val = parser->lexer_->lookup_identifier (ly_symbol2string (symbol));
192 if (val != SCM_UNDEFINED)
198 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
199 2, 0, 0, (SCM parser_smob, SCM ly_code),
200 "Parse the string @var{ly-code} with @var{parser-smob}."
201 " Upon failure, throw @code{ly-file-failed} key.")
203 LY_ASSERT_SMOB (Lily_parser, parser_smob, 1);
204 Lily_parser *parser = unsmob_lily_parser (parser_smob);
205 LY_ASSERT_TYPE (scm_is_string, ly_code, 2);
207 if (!parser->lexer_->is_clean ())
208 parser->parser_error (_ ("ly:parser-parse-string is only valid with a new parser."
209 " Use ly:parser-include-string instead."));
211 parser->parse_string (ly_scm2string (ly_code));
213 return SCM_UNSPECIFIED;
216 LY_DEFINE (ly_parse_string_expression, "ly:parse-string-expression",
217 2, 2, 0, (SCM parser_smob, SCM ly_code, SCM filename,
219 "Parse the string @var{ly-code} with @var{parser-smob}."
220 " Return the contained music expression."
221 " @var{filename} and @var{line} are optional source indicators.")
223 LY_ASSERT_SMOB (Lily_parser, parser_smob, 1);
224 Lily_parser *parser = unsmob_lily_parser (parser_smob);
225 LY_ASSERT_TYPE (scm_is_string, ly_code, 2);
227 if (SCM_UNBNDP (filename) || !scm_is_string (filename))
230 fn = ly_scm2string (filename);
232 if (SCM_UNBNDP (line) || !scm_is_integer (line))
235 ln = scm_to_int (line);
237 if (!parser->lexer_->is_clean ())
239 parser->parser_error (_ ("ly:parse-string-expression is only valid with a new parser."
240 " Use ly:parser-include-string instead."));
241 return SCM_UNSPECIFIED;
244 return parser->parse_string_expression (ly_scm2string (ly_code),
248 LY_DEFINE (ly_parser_include_string, "ly:parser-include-string",
249 2, 0, 0, (SCM parser_smob, SCM ly_code),
250 "Include the string @var{ly-code} into the input stream"
251 " for @var{parser-smob}. Can only be used in immediate"
252 " Scheme expressions (@code{$} instead of @code{#}).")
254 LY_ASSERT_SMOB (Lily_parser, parser_smob, 1);
255 Lily_parser *parser = unsmob_lily_parser (parser_smob);
256 LY_ASSERT_TYPE (scm_is_string, ly_code, 2);
258 parser->include_string (ly_scm2string (ly_code));
260 return SCM_UNSPECIFIED;
263 LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
264 2, 0, 0, (SCM parser, SCM names),
265 "Replace current note names in @var{parser}."
266 " @var{names} is an alist of symbols. This only has effect"
267 " if the current mode is notes.")
269 LY_ASSERT_SMOB (Lily_parser, parser, 1);
270 Lily_parser *p = unsmob_lily_parser (parser);
272 if (p->lexer_->is_note_state ())
274 p->lexer_->pop_state ();
275 p->lexer_->push_note_state (names);
278 return SCM_UNSPECIFIED;
281 LY_DEFINE (ly_parser_output_name, "ly:parser-output-name",
282 1, 0, 0, (SCM parser),
283 "Return the base name of the output file.")
285 LY_ASSERT_SMOB (Lily_parser, parser, 1);
286 Lily_parser *p = unsmob_lily_parser (parser);
288 return ly_string2scm (p->output_basename_);
291 LY_DEFINE (ly_parser_error, "ly:parser-error",
292 2, 1, 0, (SCM parser, SCM msg, SCM input),
293 "Display an error message and make the parser fail.")
295 LY_ASSERT_SMOB (Lily_parser, parser, 1);
296 Lily_parser *p = unsmob_lily_parser (parser);
298 LY_ASSERT_TYPE (scm_is_string, msg, 2);
299 string s = ly_scm2string (msg);
301 Input *i = unsmob_input (input);
303 p->parser_error (*i, s);
310 LY_DEFINE (ly_parser_clear_error, "ly:parser-clear-error",
311 1, 0, 0, (SCM parser),
312 "Clear the error flag for the parser.")
314 LY_ASSERT_SMOB (Lily_parser, parser, 1);
315 Lily_parser *p = unsmob_lily_parser (parser);
318 p->lexer_->error_level_ = 0;
320 return SCM_UNSPECIFIED;
323 LY_DEFINE (ly_parser_has_error_p, "ly:parser-has-error?",
324 1, 0, 0, (SCM parser),
325 "Does @var{parser} have an error flag?")
327 LY_ASSERT_SMOB (Lily_parser, parser, 1);
328 Lily_parser *p = unsmob_lily_parser (parser);
330 return scm_from_bool (p->error_level_ || p->lexer_->error_level_);