2 my-lily-parser.cc -- implement My_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 "my-lily-lexer.hh"
17 #include "my-lily-parser.hh"
18 #include "output-def.hh"
19 #include "paper-book.hh"
26 My_lily_parser::My_lily_parser (Sources *sources)
32 default_duration_ = Duration (2,0);
34 last_beam_start_ = SCM_EOL;
39 My_lily_parser::My_lily_parser (My_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 My_lily_lexer (*src.lexer_);
54 My_lily_parser::~My_lily_parser ()
56 // FIXME: Memleak: del lexer
59 #include "ly-smobs.icc"
61 IMPLEMENT_SMOBS (My_lily_parser);
62 IMPLEMENT_TYPE_P (My_lily_parser, "ly:my-lily-parser?");
63 IMPLEMENT_DEFAULT_EQUAL_P (My_lily_parser);
66 My_lily_parser::mark_smob (SCM s)
68 My_lily_parser *parser = (My_lily_parser*) ly_cdr (s);
74 My_lily_parser::print_smob (SCM s, SCM port, scm_print_state*)
76 scm_puts ("#<my_lily_parser ", port);
77 My_lily_parser *parser = (My_lily_parser*) ly_cdr (s);
79 scm_puts (" >", port);
84 /* Process one .ly file, or book. */
86 My_lily_parser::parse_file (String init, String name, String out_name)
88 lexer_ = new My_lily_lexer (sources_);
90 lexer_->set_identifier (ly_symbol2scm ("parser"),
92 output_basename_ = out_name;
94 lexer_->main_input_name_ = name;
96 progress_indication (_ ("Parsing..."));
97 progress_indication ("\n");
100 lexer_->new_input (init, sources_);
102 /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
103 OUT_FILE (unless IN_FILE redefines output file name). */
106 if (!define_spots_.is_empty ())
108 define_spots_.top ().warning (_ ("Braces don't match"));
112 error_level_ = error_level_ | lexer_->error_level_;
118 My_lily_parser::parse_string (String ly_code)
120 My_lily_lexer *parent = lexer_;
121 lexer_ = (parent == 0 ? new My_lily_lexer (sources_)
122 : new My_lily_lexer (*parent));
125 SCM oldmod = scm_current_module ();
126 scm_set_current_module (ly_car (lexer_->scopes_));
129 lexer_->set_identifier (ly_symbol2scm ("parser"),
132 lexer_->main_input_name_ = "<string>";
133 lexer_->main_input_b_ = true;
136 lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
139 if (!define_spots_.is_empty ())
141 define_spots_.top ().warning (_ ("Braces don't match"));
145 error_level_ = error_level_ | lexer_->error_level_;
149 parent->keytable_ = lexer_->keytable_;
150 parent->encoding_ = lexer_->encoding_;
151 parent->chordmodifier_tab_ = lexer_->chordmodifier_tab_;
152 parent->pitchname_tab_stack_ = lexer_->pitchname_tab_stack_;
153 parent->sources_ = lexer_->sources_;
154 parent->scopes_ = lexer_->scopes_;
155 parent->error_level_ = lexer_->error_level_;
156 parent->main_input_b_ = lexer_->main_input_b_;
159 scm_set_current_module (oldmod);
165 My_lily_parser::push_spot ()
167 define_spots_.push (here_input ());
171 My_lily_parser::here_str0 () const
173 return lexer_->here_str0 ();
177 My_lily_parser::parser_error (String s)
179 here_input ().error (s);
184 My_lily_parser::pop_spot ()
186 return define_spots_.pop ();
190 My_lily_parser::here_input () const
193 Parsing looks ahead , so we really want the previous location of the
194 lexer, not lexer_->here_input ().
198 Actually, that gets very icky when there are white space, because
199 the line-numbers are all wrong. Let's try the character before
200 the current token. That gets the right result for note/duration
201 stuff, but doesn't mess up for errors in the 1st token of the
204 Input hi (lexer_->here_input ());
206 char const * bla = hi.defined_str0_;
207 if (hi.line_number () > 1
208 || hi.column_number () > 1)
211 return Input (hi.source_file_, bla);
215 /****************************************************************/
221 bool store_locations_global_b;
223 /* Do not append `!' suffix, since 1st argument is not modified. */
224 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click",
226 "Set the options for Point-and-click source specials output. The\n"
227 "argument is a symbol. Possible options are @code{none} (no source specials),\n"
228 "@code{line} and @code{line-column}")
231 SCM val = SCM_BOOL_F;
232 if (ly_symbol2scm ("line-column") == what)
233 val = ly_scheme_function ("line-column-location");
234 else if (what == ly_symbol2scm ("line"))
235 val = ly_scheme_function ("line-location");
237 scm_module_define (global_lily_module, ly_symbol2scm ("point-and-click"),
239 store_locations_global_b = ly_c_procedure_p (val);
240 return SCM_UNSPECIFIED;
243 LY_DEFINE (ly_parse_file, "ly:parse-file",
245 "Parse a single @code{.ly} file. "
246 "Upon failure, throw @code{ly-file-failed} key.")
248 SCM_ASSERT_TYPE (ly_c_string_p (name), name, SCM_ARG1, __FUNCTION__, "string");
249 char const *file = SCM_STRING_CHARS (name);
250 char const *extensions[] = {"ly", "", 0};
252 String file_name = global_path.find (file, extensions);
254 /* By default, use base name of input file for output file name,
255 write output to cwd; do not use root and directory parts of input
257 File_name out_file_name (file_name);
259 global_path.append (out_file_name.dir_);
261 out_file_name.ext_ = "";
262 out_file_name.root_ = "";
263 out_file_name.dir_ = "";
265 if (!output_name_global.is_empty ())
266 out_file_name = File_name (output_name_global);
269 if (!init_name_global.is_empty ())
270 init = init_name_global;
274 String out_file = out_file_name.to_string ();
276 if (init.length () && global_path.find (init).is_empty ())
278 warning (_f ("can't find init file: `%s'", init));
279 warning (_f ("(search path: `%s')",
280 global_path.to_string ().to_str0 ()));
284 if ((file_name != "-") && global_path.find (file_name).is_empty ())
286 warning (_f ("can't find file: `%s'", file_name));
287 scm_throw (ly_symbol2scm ("ly-file-failed"),
288 scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
293 sources.set_path (&global_path);
295 progress_indication (_f ("Now processing `%s'", file_name.to_str0 ()));
296 progress_indication ("\n");
298 My_lily_parser *parser = new My_lily_parser (&sources);
300 parser->parse_file (init, file_name, out_file);
302 bool error = parser->error_level_;
305 /* TODO: pass renamed input file too. */
306 scm_throw (ly_symbol2scm ("ly-file-failed"),
307 scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
309 return SCM_UNSPECIFIED;
312 LY_DEFINE (ly_parse_string, "ly:parse-string",
313 1, 0, 0, (SCM ly_code),
314 "Parse the string LY_CODE. "
315 "Upon failure, throw @code{ly-file-failed} key.")
317 SCM_ASSERT_TYPE (ly_c_string_p (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
320 sources.set_path (&global_path);
321 My_lily_parser *parser = new My_lily_parser (&sources);
322 scm_module_define (global_lily_module, ly_symbol2scm ("parser"),
323 parser->self_scm ());
324 parser->parse_string (ly_scm2string (ly_code));
327 return SCM_UNSPECIFIED;
330 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
331 1, 0, 0, (SCM parser_smob),
332 "Return a clone of PARSER_SMOB.")
334 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
335 My_lily_parser *clone = new My_lily_parser (*parser);
337 /* FIXME: should copy scopes too. */
338 return scm_gc_unprotect_object (clone->self_scm ());
341 LY_DEFINE (ly_parser_define, "ly:parser-define",
342 3, 0, 0, (SCM parser_smob, SCM symbol, SCM val),
343 "Bind SYMBOL to VAL in PARSER_SMOB's module.")
345 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
346 SCM_ASSERT_TYPE (ly_c_symbol_p (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
347 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
349 parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
350 return SCM_UNSPECIFIED;
353 LY_DEFINE (ly_parser_lookup, "ly:parser-lookup",
354 2, 0, 0, (SCM parser_smob, SCM symbol),
355 "Lookup @var{symbol} in @var{parser_smob}'s module. "
358 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
360 SCM_ASSERT_TYPE (ly_c_symbol_p (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
361 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
363 SCM val= parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
364 if (val != SCM_UNDEFINED)
370 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
371 2, 0, 0, (SCM parser_smob, SCM ly_code),
372 "Parse the string LY_CODE with PARSER_SMOB."
373 "Upon failure, throw @code{ly-file-failed} key.")
375 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
377 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
378 SCM_ASSERT_TYPE (ly_c_string_p (ly_code), ly_code, SCM_ARG2, __FUNCTION__, "string");
380 parser->parse_string (ly_scm2string (ly_code));
382 return SCM_UNSPECIFIED;
386 get_paper (My_lily_parser *parser)
388 SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
389 Output_def *paper = unsmob_output_def (id);
390 paper = paper ? paper->clone () : new Output_def;
391 paper->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
393 paper->parent_ = unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultbookpaper"));
399 get_midi (My_lily_parser *parser)
401 SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
402 Output_def *paper = unsmob_output_def (id);
403 paper = paper ? paper->clone () : new Output_def;
404 paper->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
410 get_bookpaper (My_lily_parser *parser)
412 SCM id = parser->lexer_->lookup_identifier ("$defaultbookpaper");
413 Output_def *paper = unsmob_output_def (id);
415 paper = paper ? dynamic_cast<Output_def*> (paper->clone ()) : new Output_def;
416 paper->set_variable (ly_symbol2scm ("is-bookpaper"), SCM_BOOL_T);
421 /* TODO: move this to Scheme? Why take the parser arg, and all the back
422 & forth between scm and c++? */
423 LY_DEFINE (ly_parser_print_score, "ly:parser-print-score",
425 (SCM parser_smob, SCM score_smob),
426 "Print score, i.e., the classic way.")
428 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
429 Score *score = unsmob_score (score_smob);
431 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
432 SCM_ASSERT_TYPE (score, score_smob, SCM_ARG2, __FUNCTION__, "score");
434 SCM header = ly_c_module_p (score->header_) ? score->header_
435 : parser->lexer_->lookup_identifier ("$globalheader");
437 File_name outname (parser->output_basename_);
438 int *c = &parser->book_count_;
440 outname.base_ += "-" + to_string (*c);
443 SCM os = scm_makfrom0str (outname.to_string ().to_str0 ());
444 SCM bookpaper = get_bookpaper (parser)->self_scm ();
445 for (int i = 0; i < score->defs_.size (); i++)
446 default_rendering (score->music_, score->defs_[i]->self_scm (),
450 if (score->defs_.is_empty ())
452 Output_def *paper = get_paper (parser);
453 default_rendering (score->music_, paper->self_scm (),
454 get_bookpaper (parser)->self_scm (),
456 scm_gc_unprotect_object (paper->self_scm ());
458 return SCM_UNSPECIFIED;
462 LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
463 2, 0, 0, (SCM parser, SCM names),
464 "Replace current note names in @var{parser}. "
465 "@var{names} is an alist of symbols. "
466 "This only has effect if the current mode is notes.")
468 My_lily_parser *p = unsmob_my_lily_parser (parser);
469 SCM_ASSERT_TYPE(p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
471 if (p->lexer_->is_note_state ())
473 p->lexer_->pop_state ();
474 p->lexer_->push_note_state (alist_to_hashq (names));
477 return SCM_UNSPECIFIED;
480 LY_DEFINE (ly_parser_print_book, "ly:parser-print-book",
481 2, 0, 0, (SCM parser_smob, SCM book_smob),
484 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
485 Book *book = unsmob_book (book_smob);
486 Output_def *bp = unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultbookpaper"));
488 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "Lilypond parser");
489 SCM_ASSERT_TYPE (book, book_smob, SCM_ARG2, __FUNCTION__, "Book");
491 /* ugh. changing argument.*/
492 book->bookpaper_ = bp;
494 File_name outname (parser->output_basename_);
495 int *c = &parser->book_count_;
497 outname.base_ += "-" + to_string (*c);
500 Output_def *paper = get_paper (parser);
502 Paper_book* pb = book->process (outname.to_string (), paper);
504 pb->output (outname.to_string ());
506 scm_gc_unprotect_object (paper->self_scm ());
507 scm_gc_unprotect_object (pb->self_scm ());
509 return SCM_UNSPECIFIED;