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>
11 #include "file-name.hh"
12 #include "file-path.hh"
13 #include "lily-version.hh"
14 #include "ly-module.hh"
16 #include "lily-lexer.hh"
17 #include "lily-parser.hh"
18 #include "output-def.hh"
19 #include "paper-book.hh"
26 Lily_parser::Lily_parser (Sources *sources)
32 default_duration_ = Duration (2,0);
34 last_beam_start_ = SCM_EOL;
39 Lily_parser::Lily_parser (Lily_parser const &src)
41 book_count_ = src.book_count_;
42 score_count_ = src.score_count_;
44 sources_ = src.sources_;
45 default_duration_ = src.default_duration_;
46 error_level_ = src.error_level_;
47 last_beam_start_ = src.last_beam_start_;
51 lexer_ = new Lily_lexer (*src.lexer_);
53 scm_gc_unprotect_object (lexer_->self_scm ());
56 Lily_parser::~Lily_parser ()
60 #include "ly-smobs.icc"
62 IMPLEMENT_SMOBS (Lily_parser);
63 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
64 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
67 Lily_parser::mark_smob (SCM s)
69 Lily_parser *parser = (Lily_parser*) ly_cdr (s);
70 return (parser->lexer_) ? parser->lexer_->self_scm () : SCM_EOL;
74 Lily_parser::print_smob (SCM s, SCM port, scm_print_state*)
76 scm_puts ("#<my_lily_parser ", port);
77 Lily_parser *parser = (Lily_parser*) ly_cdr (s);
79 scm_puts (" >", port);
84 /* Process one .ly file, or book. */
86 Lily_parser::parse_file (String init, String name, String out_name)
88 lexer_ = new Lily_lexer (sources_);
89 scm_gc_unprotect_object (lexer_->self_scm ());
91 lexer_->set_identifier (ly_symbol2scm ("parser"),
93 output_basename_ = out_name;
95 lexer_->main_input_name_ = name;
97 progress_indication (_ ("Parsing..."));
98 progress_indication ("\n");
102 lexer_->new_input (init, sources_);
104 /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
105 OUT_FILE (unless IN_FILE redefines output file name). */
108 if (!define_spots_.is_empty ())
110 define_spots_.top ().warning (_ ("Braces don't match"));
114 error_level_ = error_level_ | lexer_->error_level_;
119 Lily_parser::parse_string (String ly_code)
121 SCM parent_prot = lexer_ ? lexer_->self_scm () : SCM_EOL;
122 Lily_lexer * parent = lexer_;
123 lexer_ = (parent == 0 ? new Lily_lexer (sources_)
124 : new Lily_lexer (*parent));
125 scm_gc_unprotect_object (lexer_->self_scm ());
128 SCM oldmod = scm_current_module ();
129 scm_set_current_module (ly_car (lexer_->scopes_));
132 lexer_->set_identifier (ly_symbol2scm ("parser"),
135 lexer_->main_input_name_ = "<string>";
136 lexer_->main_input_b_ = true;
139 lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
142 if (!define_spots_.is_empty ())
144 define_spots_.top ().warning (_ ("Braces don't match"));
148 error_level_ = error_level_ | lexer_->error_level_;
151 if (Lily_lexer::unsmob (parent_prot))
154 what the fuck is this good for?
156 parent->encoding_ = lexer_->encoding_;
157 parent->chordmodifier_tab_ = lexer_->chordmodifier_tab_;
158 parent->pitchname_tab_stack_ = lexer_->pitchname_tab_stack_;
159 parent->sources_ = lexer_->sources_;
160 parent->scopes_ = lexer_->scopes_;
161 parent->error_level_ = lexer_->error_level_;
162 parent->main_input_b_ = lexer_->main_input_b_;
165 scm_remember_upto_here_1 (parent_prot);
168 scm_set_current_module (oldmod);
173 Lily_parser::push_spot ()
175 define_spots_.push (here_input ());
179 Lily_parser::here_str0 () const
181 return lexer_->here_str0 ();
185 Lily_parser::parser_error (String s)
187 here_input ().error (s);
192 Lily_parser::pop_spot ()
194 return define_spots_.pop ();
198 Lily_parser::here_input () const
201 Parsing looks ahead , so we really want the previous location of the
202 lexer, not lexer_->here_input ().
206 Actually, that gets very icky when there are white space, because
207 the line-numbers are all wrong. Let's try the character before
208 the current token. That gets the right result for note/duration
209 stuff, but doesn't mess up for errors in the 1st token of the
212 Input hi (lexer_->here_input ());
214 char const * bla = hi.defined_str0_;
215 if (hi.line_number () > 1
216 || hi.column_number () > 1)
219 return Input (hi.source_file_, bla);
223 /****************************************************************/
229 bool store_locations_global_b;
231 /* Do not append `!' suffix, since 1st argument is not modified. */
232 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click",
234 "Set the options for Point-and-click source specials output. The\n"
235 "argument is a symbol. Possible options are @code{none} (no source specials),\n"
236 "@code{line} and @code{line-column}")
239 SCM val = SCM_BOOL_F;
240 if (ly_symbol2scm ("line-column") == what)
241 val = ly_scheme_function ("line-column-location");
242 else if (what == ly_symbol2scm ("line"))
243 val = ly_scheme_function ("line-location");
245 scm_module_define (global_lily_module, ly_symbol2scm ("point-and-click"),
247 store_locations_global_b = ly_c_procedure_p (val);
248 return SCM_UNSPECIFIED;
251 LY_DEFINE (ly_parse_file, "ly:parse-file",
253 "Parse a single @code{.ly} file. "
254 "Upon failure, throw @code{ly-file-failed} key.")
256 SCM_ASSERT_TYPE (ly_c_string_p (name), name, SCM_ARG1, __FUNCTION__, "string");
257 char const *file = SCM_STRING_CHARS (name);
258 char const *extensions[] = {"ly", "", 0};
260 String file_name = global_path.find (file, extensions);
262 /* By default, use base name of input file for output file name,
263 write output to cwd; do not use root and directory parts of input
265 File_name out_file_name (file_name);
267 global_path.append (out_file_name.dir_);
269 out_file_name.ext_ = "";
270 out_file_name.root_ = "";
271 out_file_name.dir_ = "";
273 if (!output_name_global.is_empty ())
274 out_file_name = File_name (output_name_global);
277 if (!init_name_global.is_empty ())
278 init = init_name_global;
282 String out_file = out_file_name.to_string ();
284 if (init.length () && global_path.find (init).is_empty ())
286 warning (_f ("can't find init file: `%s'", init));
287 warning (_f ("(search path: `%s')",
288 global_path.to_string ().to_str0 ()));
292 if ((file_name != "-") && global_path.find (file_name).is_empty ())
294 warning (_f ("can't find file: `%s'", file_name));
295 scm_throw (ly_symbol2scm ("ly-file-failed"),
296 scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
301 sources.set_path (&global_path);
303 progress_indication (_f ("Now processing `%s'", file_name.to_str0 ()));
304 progress_indication ("\n");
306 Lily_parser *parser = new Lily_parser (&sources);
308 parser->parse_file (init, file_name, out_file);
310 bool error = parser->error_level_;
311 scm_gc_unprotect_object (parser->self_scm ());
314 /* TODO: pass renamed input file too. */
315 scm_throw (ly_symbol2scm ("ly-file-failed"),
316 scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
318 return SCM_UNSPECIFIED;
321 LY_DEFINE (ly_parse_string, "ly:parse-string",
322 1, 0, 0, (SCM ly_code),
323 "Parse the string LY_CODE. "
324 "Upon failure, throw @code{ly-file-failed} key.")
326 SCM_ASSERT_TYPE (ly_c_string_p (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
329 sources.set_path (&global_path);
330 Lily_parser *parser = new Lily_parser (&sources);
331 scm_module_define (global_lily_module, ly_symbol2scm ("parser"),
332 parser->self_scm ());
333 parser->parse_string (ly_scm2string (ly_code));
334 scm_gc_unprotect_object (parser->self_scm ());
337 return SCM_UNSPECIFIED;
340 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
341 1, 0, 0, (SCM parser_smob),
342 "Return a clone of PARSER_SMOB.")
344 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
345 Lily_parser *clone = new Lily_parser (*parser);
347 /* FIXME: should copy scopes too. */
348 return scm_gc_unprotect_object (clone->self_scm ());
351 LY_DEFINE (ly_parser_define, "ly:parser-define",
352 3, 0, 0, (SCM parser_smob, SCM symbol, SCM val),
353 "Bind SYMBOL to VAL in PARSER_SMOB's module.")
355 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
356 SCM_ASSERT_TYPE (ly_c_symbol_p (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
357 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
359 parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
360 return SCM_UNSPECIFIED;
363 LY_DEFINE (ly_parser_lookup, "ly:parser-lookup",
364 2, 0, 0, (SCM parser_smob, SCM symbol),
365 "Lookup @var{symbol} in @var{parser_smob}'s module. "
368 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
370 SCM_ASSERT_TYPE (ly_c_symbol_p (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
371 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
373 SCM val= parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
374 if (val != SCM_UNDEFINED)
380 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
381 2, 0, 0, (SCM parser_smob, SCM ly_code),
382 "Parse the string LY_CODE with PARSER_SMOB."
383 "Upon failure, throw @code{ly-file-failed} key.")
385 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
387 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
388 SCM_ASSERT_TYPE (ly_c_string_p (ly_code), ly_code, SCM_ARG2, __FUNCTION__, "string");
390 parser->parse_string (ly_scm2string (ly_code));
392 return SCM_UNSPECIFIED;
396 get_paper (Lily_parser *parser)
398 SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
399 Output_def *paper = unsmob_output_def (id);
400 paper = paper ? paper->clone () : new Output_def;
401 paper->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
403 paper->parent_ = unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultbookpaper"));
409 get_midi (Lily_parser *parser)
411 SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
412 Output_def *paper = unsmob_output_def (id);
413 paper = paper ? paper->clone () : new Output_def;
414 paper->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
420 get_bookpaper (Lily_parser *parser)
422 SCM id = parser->lexer_->lookup_identifier ("$defaultbookpaper");
423 Output_def *paper = unsmob_output_def (id);
425 paper = paper ? dynamic_cast<Output_def*> (paper->clone ()) : new Output_def;
426 paper->set_variable (ly_symbol2scm ("is-bookpaper"), SCM_BOOL_T);
431 /* TODO: move this to Scheme? Why take the parser arg, and all the back
432 & forth between scm and c++? */
433 LY_DEFINE (ly_parser_print_score, "ly:parser-print-score",
435 (SCM parser_smob, SCM score_smob),
436 "Print score, i.e., the classic way.")
438 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
439 Score *score = unsmob_score (score_smob);
441 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
442 SCM_ASSERT_TYPE (score, score_smob, SCM_ARG2, __FUNCTION__, "score");
444 SCM header = ly_c_module_p (score->header_) ? score->header_
445 : parser->lexer_->lookup_identifier ("$globalheader");
447 File_name outname (parser->output_basename_);
448 int *c = &parser->book_count_;
450 outname.base_ += "-" + to_string (*c);
453 SCM os = scm_makfrom0str (outname.to_string ().to_str0 ());
454 SCM bookpaper = get_bookpaper (parser)->self_scm ();
455 for (int i = 0; i < score->defs_.size (); i++)
456 default_rendering (score->music_, score->defs_[i]->self_scm (),
460 if (score->defs_.is_empty ())
462 Output_def *paper = get_paper (parser);
463 default_rendering (score->music_, paper->self_scm (),
464 get_bookpaper (parser)->self_scm (),
466 scm_gc_unprotect_object (paper->self_scm ());
468 return SCM_UNSPECIFIED;
472 LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
473 2, 0, 0, (SCM parser, SCM names),
474 "Replace current note names in @var{parser}. "
475 "@var{names} is an alist of symbols. "
476 "This only has effect if the current mode is notes.")
478 Lily_parser *p = unsmob_my_lily_parser (parser);
479 SCM_ASSERT_TYPE(p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
481 if (p->lexer_->is_note_state ())
483 p->lexer_->pop_state ();
484 p->lexer_->push_note_state (alist_to_hashq (names));
487 return SCM_UNSPECIFIED;
490 LY_DEFINE (ly_parser_print_book, "ly:parser-print-book",
491 2, 0, 0, (SCM parser_smob, SCM book_smob),
494 Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
495 Book *book = unsmob_book (book_smob);
496 Output_def *bp = unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultbookpaper"));
498 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "Lilypond parser");
499 SCM_ASSERT_TYPE (book, book_smob, SCM_ARG2, __FUNCTION__, "Book");
501 /* ugh. changing argument.*/
502 book->bookpaper_ = bp;
504 File_name outname (parser->output_basename_);
505 int *c = &parser->book_count_;
507 outname.base_ += "-" + to_string (*c);
510 Output_def *paper = get_paper (parser);
512 Paper_book* pb = book->process (outname.to_string (), paper);
514 pb->output (outname.to_string ());
516 scm_gc_unprotect_object (paper->self_scm ());
517 scm_gc_unprotect_object (pb->self_scm ());
519 return SCM_UNSPECIFIED;