2 lily-parser.cc -- implement Lily_parser
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
10 #include "lily-parser.hh"
13 #include "lilypond-key.hh"
14 #include "file-name.hh"
15 #include "file-path.hh"
16 #include "lily-version.hh"
17 #include "ly-module.hh"
19 #include "lily-lexer.hh"
20 #include "output-def.hh"
21 #include "paper-book.hh"
27 Lily_parser::Lily_parser (Sources *sources)
33 default_duration_ = Duration (2, 0);
35 last_beam_start_ = SCM_EOL;
40 Lily_parser::Lily_parser (Lily_parser const &src)
42 book_count_ = src.book_count_;
43 score_count_ = src.score_count_;
45 sources_ = src.sources_;
46 default_duration_ = src.default_duration_;
47 error_level_ = src.error_level_;
48 last_beam_start_ = src.last_beam_start_;
52 lexer_ = new Lily_lexer (*src.lexer_);
54 scm_gc_unprotect_object (lexer_->self_scm ());
57 Lily_parser::~Lily_parser ()
61 #include "ly-smobs.icc"
63 IMPLEMENT_SMOBS (Lily_parser);
64 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
65 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
68 Lily_parser::mark_smob (SCM s)
70 Lily_parser *parser = (Lily_parser*) SCM_CELL_WORD_1 (s);
71 return (parser->lexer_) ? parser->lexer_->self_scm () : SCM_EOL;
75 Lily_parser::print_smob (SCM s, SCM port, scm_print_state*)
77 scm_puts ("#<my_lily_parser ", port);
78 Lily_parser *parser = (Lily_parser*) SCM_CELL_WORD_1 (s);
80 scm_puts (" >", port);
85 /* Process one .ly file, or book. */
87 Lily_parser::parse_file (String init, String name, String out_name)
89 lexer_ = new Lily_lexer (sources_);
90 scm_gc_unprotect_object (lexer_->self_scm ());
92 lexer_->set_identifier (ly_symbol2scm ("parser"),
94 output_basename_ = out_name;
96 lexer_->main_input_name_ = name;
98 progress_indication (_ ("Parsing..."));
99 progress_indication ("\n");
103 lexer_->new_input (init, sources_);
106 String s = global_path.find (f.base_ + ".twy");
107 s = gulp_file_to_string (s, false);
108 scm_eval_string (scm_makfrom0str (s.to_str0 ()));
110 /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
111 OUT_FILE (unless IN_FILE redefines output file name). */
114 if (!define_spots_.is_empty ())
116 define_spots_.top ().warning (_ ("Braces don't match"));
120 error_level_ = error_level_ | lexer_->error_level_;
125 Lily_parser::parse_string (String ly_code)
127 Lily_lexer * parent = lexer_;
128 lexer_ = (parent == 0 ? new Lily_lexer (sources_)
129 : new Lily_lexer (*parent));
130 scm_gc_unprotect_object (lexer_->self_scm ());
133 SCM oldmod = scm_current_module ();
134 scm_set_current_module (scm_car (lexer_->scopes_));
137 lexer_->set_identifier (ly_symbol2scm ("parser"),
140 lexer_->main_input_name_ = "<string>";
141 lexer_->main_input_b_ = true;
144 lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
147 if (!define_spots_.is_empty ())
149 define_spots_.top ().warning (_ ("Braces don't match"));
153 error_level_ = error_level_ | lexer_->error_level_;
155 scm_set_current_module (oldmod);
160 Lily_parser::push_spot ()
162 define_spots_.push (here_input ());
166 Lily_parser::here_str0 () const
168 return lexer_->here_str0 ();
172 Lily_parser::parser_error (String s)
174 here_input ().error (s);
179 Lily_parser::pop_spot ()
181 return define_spots_.pop ();
185 Lily_parser::here_input () const
188 Parsing looks ahead , so we really want the previous location of the
189 lexer, not lexer_->here_input ().
193 Actually, that gets very icky when there are white space, because
194 the line-numbers are all wrong. Let's try the character before
195 the current token. That gets the right result for note/duration
196 stuff, but doesn't mess up for errors in the 1st token of the
199 Input hi (lexer_->here_input ());
201 char const * bla = hi.defined_str0_;
202 if (hi.line_number () > 1
203 || hi.column_number () > 1)
206 return Input (hi.source_file_, bla);
210 /****************************************************************/
216 bool store_locations_global_b;
218 /* Do not append `!' suffix, since 1st argument is not modified. */
219 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click",
221 "Set the options for Point-and-click source specials output. The\n"
222 "argument is a symbol. Possible options are @code{none} (no source specials),\n"
223 "@code{line} and @code{line-column}")
226 SCM val = SCM_BOOL_F;
227 if (ly_symbol2scm ("line-column") == what)
228 val = ly_scheme_function ("line-column-location");
229 else if (what == ly_symbol2scm ("line"))
230 val = ly_scheme_function ("line-location");
232 scm_module_define (global_lily_module, ly_symbol2scm ("point-and-click"),
234 store_locations_global_b = ly_c_procedure_p (val);
235 return SCM_UNSPECIFIED;
238 LY_DEFINE (ly_parse_file, "ly:parse-file",
240 "Parse a single @code{.ly} file. "
241 "Upon failure, throw @code{ly-file-failed} key.")
243 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
244 char const *file = scm_i_string_chars (name);
245 char const *extensions[] = {"ly", "", 0};
247 String file_name = global_path.find (file, extensions);
249 /* By default, use base name of input file for output file name,
250 write output to cwd; do not use root and directory parts of input
252 File_name out_file_name (file_name);
254 global_path.append (out_file_name.dir_);
256 out_file_name.ext_ = "";
257 out_file_name.root_ = "";
258 out_file_name.dir_ = "";
260 if (!output_name_global.is_empty ())
261 out_file_name = File_name (output_name_global);
264 if (!init_name_global.is_empty ())
265 init = init_name_global;
269 String out_file = out_file_name.to_string ();
271 if (init.length () && global_path.find (init).is_empty ())
273 warning (_f ("can't find init file: `%s'", init));
274 warning (_f ("(search path: `%s')",
275 global_path.to_string ().to_str0 ()));
279 if ((file_name != "-") && global_path.find (file_name).is_empty ())
281 warning (_f ("can't find file: `%s'", file_name));
282 scm_throw (ly_symbol2scm ("ly-file-failed"),
283 scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
288 sources.set_path (&global_path);
290 progress_indication (_f ("Processing `%s'", file_name.to_str0 ()));
291 progress_indication ("\n");
293 Lily_parser *parser = new Lily_parser (&sources);
295 parser->parse_file (init, file_name, out_file);
297 bool error = parser->error_level_;
298 scm_gc_unprotect_object (parser->self_scm ());
301 /* TODO: pass renamed input file too. */
302 scm_throw (ly_symbol2scm ("ly-file-failed"),
303 scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
305 return SCM_UNSPECIFIED;
308 LY_DEFINE (ly_parse_string, "ly:parse-string",
309 1, 0, 0, (SCM ly_code),
310 "Parse the string LY_CODE. "
311 "Upon failure, throw @code{ly-file-failed} key.")
313 SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
316 sources.set_path (&global_path);
317 Lily_parser *parser = new Lily_parser (&sources);
318 scm_module_define (global_lily_module, ly_symbol2scm ("parser"),
319 parser->self_scm ());
320 parser->parse_string (ly_scm2string (ly_code));
321 scm_gc_unprotect_object (parser->self_scm ());
324 return SCM_UNSPECIFIED;
327 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
328 1, 0, 0, (SCM parser_smob),
329 "Return a clone of PARSER_SMOB.")
331 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
332 Lily_parser *clone = new Lily_parser (*parser);
334 /* FIXME: should copy scopes too. */
335 return scm_gc_unprotect_object (clone->self_scm ());
338 LY_DEFINE (ly_parser_define, "ly:parser-define",
339 3, 0, 0, (SCM parser_smob, SCM symbol, SCM val),
340 "Bind SYMBOL to VAL in PARSER_SMOB's module.")
342 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
343 SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
344 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
346 parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
347 return SCM_UNSPECIFIED;
350 LY_DEFINE (ly_parser_lookup, "ly:parser-lookup",
351 2, 0, 0, (SCM parser_smob, SCM symbol),
352 "Lookup @var{symbol} in @var{parser_smob}'s module. "
355 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
357 SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
358 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
360 SCM val= parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
361 if (val != SCM_UNDEFINED)
367 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
368 2, 0, 0, (SCM parser_smob, SCM ly_code),
369 "Parse the string LY_CODE with PARSER_SMOB."
370 "Upon failure, throw @code{ly-file-failed} key.")
372 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
374 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
375 SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG2, __FUNCTION__, "string");
377 parser->parse_string (ly_scm2string (ly_code));
379 return SCM_UNSPECIFIED;
383 get_layout (Lily_parser *parser)
385 SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
386 Output_def *layout = unsmob_output_def (id);
387 layout = layout ? layout->clone () : new Output_def;
388 layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
395 get_midi (Lily_parser *parser)
397 SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
398 Output_def *layout = unsmob_output_def (id);
399 layout = layout ? layout->clone () : new Output_def;
400 layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
406 get_paper (Lily_parser *parser)
408 SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
409 Output_def *layout = unsmob_output_def (id);
411 layout = layout ? dynamic_cast<Output_def*> (layout->clone ()) : new Output_def;
412 layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
417 /* TODO: move this to Scheme? Why take the parser arg, and all the back
418 & forth between scm and c++? */
419 LY_DEFINE (ly_parser_print_score, "ly:parser-print-score",
421 (SCM parser_smob, SCM score_smob),
422 "Print score, i.e., the classic way.")
424 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
425 Score *score = unsmob_score (score_smob);
427 Object_key * key = new Lilypond_general_key (0, score->user_key_, 0);
429 if (score->error_found_)
430 return SCM_UNSPECIFIED;
432 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
433 SCM_ASSERT_TYPE (score, score_smob, SCM_ARG2, __FUNCTION__, "score");
435 SCM header = ly_c_module_p (score->header_) ? score->header_
436 : parser->lexer_->lookup_identifier ("$globalheader");
438 File_name outname (parser->output_basename_);
439 int *c = &parser->book_count_;
441 outname.base_ += "-" + to_string (*c);
444 SCM os = scm_makfrom0str (outname.to_string ().to_str0 ());
445 SCM paper = get_paper (parser)->self_scm ();
446 for (int i = 0; i < score->defs_.size (); i++)
447 default_rendering (score->get_music (), score->defs_[i]->self_scm (),
452 if (score->defs_.is_empty ())
454 Output_def *layout = get_layout (parser);
455 default_rendering (score->get_music(), layout->self_scm (),
456 get_paper (parser)->self_scm (),
457 header, os, key->self_scm ());
458 scm_gc_unprotect_object (layout->self_scm ());
461 scm_gc_unprotect_object (key->self_scm ());
462 return SCM_UNSPECIFIED;
466 LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
467 2, 0, 0, (SCM parser, SCM names),
468 "Replace current note names in @var{parser}. "
469 "@var{names} is an alist of symbols. "
470 "This only has effect if the current mode is notes.")
472 Lily_parser *p = unsmob_my_lily_parser (parser);
473 SCM_ASSERT_TYPE(p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
475 if (p->lexer_->is_note_state ())
477 p->lexer_->pop_state ();
478 p->lexer_->push_note_state (alist_to_hashq (names));
481 return SCM_UNSPECIFIED;
484 LY_DEFINE (ly_parser_print_book, "ly:parser-print-book",
485 2, 0, 0, (SCM parser_smob, SCM book_smob),
488 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
489 Book *book = unsmob_book (book_smob);
490 Output_def *bp = unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultpaper"));
492 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "Lilypond parser");
493 SCM_ASSERT_TYPE (book, book_smob, SCM_ARG2, __FUNCTION__, "Book");
495 /* ugh. changing argument.*/
498 File_name outname (parser->output_basename_);
499 int *c = &parser->book_count_;
501 outname.base_ += "-" + to_string (*c);
504 Output_def *layout = get_layout (parser);
505 Paper_book* pb = book->process (outname.to_string (), layout);
509 pb->output (outname.to_string ());
510 scm_gc_unprotect_object (pb->self_scm ());
513 scm_gc_unprotect_object (layout->self_scm ());
514 return SCM_UNSPECIFIED;