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>
12 #include "file-name.hh"
13 #include "file-path.hh"
14 #include "lily-version.hh"
15 #include "ly-module.hh"
17 #include "my-lily-lexer.hh"
18 #include "my-lily-parser.hh"
19 #include "output-def.hh"
20 #include "paper-book.hh"
26 #include "ly-smobs.icc"
29 My_lily_parser::My_lily_parser (Sources *sources)
35 default_duration_ = Duration (2,0);
37 last_beam_start_ = SCM_EOL;
42 My_lily_parser::My_lily_parser (My_lily_parser const &src)
44 book_count_ = src.book_count_;
45 score_count_ = src.score_count_;
47 sources_ = src.sources_;
48 default_duration_ = src.default_duration_;
49 error_level_ = src.error_level_;
50 last_beam_start_ = src.last_beam_start_;
53 lexer_ = new My_lily_lexer (*src.lexer_);
56 My_lily_parser::~My_lily_parser ()
58 // FIXME: Memleak: del lexer
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);
73 My_lily_parser::print_smob (SCM s, SCM port, scm_print_state*)
75 scm_puts ("#<my_lily_parser ", port);
76 My_lily_parser *parser = (My_lily_parser*) ly_cdr (s);
78 scm_puts (" >", port);
83 /* Process one .ly file, or book. */
85 My_lily_parser::parse_file (String init, String name, String out_name)
87 lexer_ = new My_lily_lexer (sources_);
88 output_basename_ = out_name;
90 lexer_->main_input_name_ = name;
92 progress_indication (_ ("Parsing..."));
93 progress_indication ("\n");
96 lexer_->new_input (init, sources_);
98 /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
99 OUT_FILE (unless IN_FILE redefines output file name). */
102 if (!define_spots_.is_empty ())
104 define_spots_.top ().warning (_ ("Braces don't match"));
108 error_level_ = error_level_ | lexer_->error_level_;
114 My_lily_parser::parse_string (String ly_code)
116 My_lily_lexer *parent = lexer_;
117 lexer_ = (parent == 0 ? new My_lily_lexer (sources_)
118 : new My_lily_lexer (*parent));
120 lexer_->main_input_name_ = "<string>";
121 lexer_->main_input_b_ = true;
124 lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
127 if (!define_spots_.is_empty ())
129 define_spots_.top ().warning (_ ("Braces don't match"));
133 error_level_ = error_level_ | lexer_->error_level_;
137 parent->keytable_ = lexer_->keytable_;
138 parent->encoding_ = lexer_->encoding_;
139 parent->chordmodifier_tab_ = lexer_->chordmodifier_tab_;
140 parent->pitchname_tab_stack_ = lexer_->pitchname_tab_stack_;
141 parent->sources_ = lexer_->sources_;
142 parent->scopes_ = lexer_->scopes_;
143 parent->error_level_ = lexer_->error_level_;
144 parent->main_input_b_ = lexer_->main_input_b_;
149 My_lily_parser::push_spot ()
151 define_spots_.push (here_input ());
155 My_lily_parser::here_str0 () const
157 return lexer_->here_str0 ();
161 My_lily_parser::parser_error (String s)
163 here_input ().error (s);
168 My_lily_parser::pop_spot ()
170 return define_spots_.pop ();
174 My_lily_parser::here_input () const
177 Parsing looks ahead , so we really want the previous location of the
178 lexer, not lexer_->here_input ().
182 Actually, that gets very icky when there are white space, because
183 the line-numbers are all wrong. Let's try the character before
184 the current token. That gets the right result for note/duration
185 stuff, but doesn't mess up for errors in the 1st token of the
188 Input hi (lexer_->here_input ());
190 char const * bla = hi.defined_str0_;
191 if (hi.line_number () > 1
192 || hi.column_number () > 1)
195 return Input (hi.source_file_, bla);
199 /****************************************************************/
205 bool store_locations_global_b;
207 /* Do not append `!' suffix, since 1st argument is not modified. */
208 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click", 1, 0, 0,
210 "Set the options for Point-and-click source specials output. The\n"
211 "argument is a symbol. Possible options are @code{none} (no source specials),\n"
212 "@code{line} and @code{line-column}")
215 SCM val = SCM_BOOL_F;
216 if (ly_symbol2scm ("line-column") == what)
217 val = ly_scheme_function ("line-column-location");
218 else if (what == ly_symbol2scm ("line"))
219 val = ly_scheme_function ("line-location");
221 scm_module_define (global_lily_module, ly_symbol2scm ("point-and-click"),
223 store_locations_global_b = ly_c_procedure_p (val);
224 return SCM_UNSPECIFIED;
227 LY_DEFINE (ly_parse_file, "ly:parse-file",
230 "Parse a single @code{.ly} file. "
231 "Upon failure, throw @code{ly-file-failed} key.")
233 SCM_ASSERT_TYPE (ly_c_string_p (name), name, SCM_ARG1, __FUNCTION__, "string");
234 char const *file = SCM_STRING_CHARS (name);
235 char const *extensions[] = {"ly", "", 0};
236 String file_name = global_path.find (file, extensions);
238 /* By default, use base name of input file for output file name,
239 write output to cwd; do not use root and directory parts of input
241 File_name out_file_name (file_name);
242 out_file_name.ext_ = "";
243 out_file_name.root_ = "";
244 out_file_name.dir_ = "";
246 if (!output_name_global.is_empty ())
247 out_file_name = File_name (output_name_global);
250 if (!init_name_global.is_empty ())
251 init = init_name_global;
255 String out_file = out_file_name.to_string ();
257 if (init.length () && global_path.find (init).is_empty ())
259 warning (_f ("can't find init file: `%s'", init));
260 warning (_f ("(search path: `%s')",
261 global_path.to_string ().to_str0 ()));
265 if ((file_name != "-") && global_path.find (file_name).is_empty ())
267 warning (_f ("can't find file: `%s'", file_name));
268 scm_throw (ly_symbol2scm ("ly-file-failed"),
269 scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
274 sources.set_path (&global_path);
276 progress_indication (_f ("Now processing `%s'", file_name.to_str0 ()));
277 progress_indication ("\n");
279 My_lily_parser *parser = new My_lily_parser (&sources);
280 scm_module_define (global_lily_module, ly_symbol2scm ("parser"),
281 parser->self_scm ());
282 parser->parse_file (init, file_name, out_file);
284 bool error = parser->error_level_;
287 /* TODO: pass renamed input file too. */
288 scm_throw (ly_symbol2scm ("ly-file-failed"),
289 scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
291 return SCM_UNSPECIFIED;
294 LY_DEFINE (ly_parse_string, "ly:parse-string",
297 "Parse the string LY_CODE. "
298 "Upon failure, throw @code{ly-file-failed} key.")
300 SCM_ASSERT_TYPE (ly_c_string_p (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
303 sources.set_path (&global_path);
304 My_lily_parser *parser = new My_lily_parser (&sources);
305 scm_module_define (global_lily_module, ly_symbol2scm ("parser"),
306 parser->self_scm ());
307 parser->parse_string (ly_scm2string (ly_code));
310 return SCM_UNSPECIFIED;
313 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
316 "Return a clone of PARSER_SMOB.")
318 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
319 My_lily_parser *clone = new My_lily_parser (*parser);
320 return scm_gc_unprotect_object (clone->self_scm ());
323 LY_DEFINE(ly_parser_define, "ly:parser-define",
325 (SCM parser_smob, SCM symbol, SCM val),
326 "Bind SYMBOL to VAL in PARSER_SMOB's module.")
328 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
329 SCM_ASSERT_TYPE (ly_c_symbol_p (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
330 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
333 parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
334 return SCM_UNSPECIFIED;
336 LY_DEFINE(ly_parser_lookup, "ly:parser-lookup",
338 (SCM parser_smob, SCM symbol),
339 "Lookup @var{symbol} in @var{parser_smob}'s module. Undefined is '().")
341 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
343 SCM_ASSERT_TYPE (ly_c_symbol_p (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
344 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
346 SCM val= parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
347 if (val != SCM_UNDEFINED)
353 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
355 (SCM parser_smob, SCM ly_code),
356 "Parse the string LY_CODE with PARSER_SMOB."
357 "Upon failure, throw @code{ly-file-failed} key.")
360 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
362 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
363 SCM_ASSERT_TYPE (ly_c_string_p (ly_code), ly_code, SCM_ARG2, __FUNCTION__, "string");
365 parser->parse_string (ly_scm2string (ly_code));
367 return SCM_UNSPECIFIED;
371 get_paper (My_lily_parser *parser)
373 SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
374 Output_def *paper = unsmob_output_def (id);
375 paper = paper ? paper->clone () : new Output_def;
376 paper->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
378 paper->parent_ =unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultbookpaper"));
384 get_midi (My_lily_parser *parser)
386 SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
387 Output_def *paper = unsmob_output_def (id);
388 paper = paper ? paper->clone () : new Output_def;
389 paper->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
395 get_bookpaper (My_lily_parser *parser)
397 SCM id = parser->lexer_->lookup_identifier ("$defaultbookpaper");
398 Output_def *paper = unsmob_output_def (id);
400 paper = paper ? dynamic_cast<Output_def*> (paper->clone ()) : new Output_def;
401 paper->set_variable (ly_symbol2scm ("is-bookpaper"), SCM_BOOL_T);
407 TODO: move this to Scheme? Why take the parser arg, and all the back
408 & forth between scm and c++?
410 LY_DEFINE (ly_parser_print_score, "ly:parser-print-score",
412 (SCM parser_smob, SCM score_smob),
413 "Print score, i.e., the classic way.")
415 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
416 Score *score = unsmob_score (score_smob);
418 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
419 SCM_ASSERT_TYPE (score, score_smob, SCM_ARG2, __FUNCTION__, "score");
421 SCM header = ly_c_module_p (score->header_) ? score->header_
422 : parser->lexer_->lookup_identifier ("$globalheader");
424 File_name outname (parser->output_basename_);
425 int *c = &parser->book_count_;
427 outname.base_ += "-" + to_string (*c);
430 SCM os = scm_makfrom0str (outname.to_string ().to_str0 ());
431 SCM bookpaper = get_bookpaper (parser)->self_scm ();
432 for (int i = 0; i < score->defs_.size (); i++)
433 default_rendering (score->music_, score->defs_[i]->self_scm (),
437 if (score->defs_.is_empty ())
439 Output_def *paper = get_paper (parser);
440 default_rendering (score->music_, paper->self_scm (),
441 get_bookpaper (parser)->self_scm (),
443 scm_gc_unprotect_object (paper->self_scm ());
445 return SCM_UNDEFINED;
448 LY_DEFINE (ly_parser_print_book, "ly:parser-print-book",
450 (SCM parser_smob, SCM book_smob),
453 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
454 Book *book = unsmob_book (book_smob);
455 Output_def *bp = unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultbookpaper"));
457 SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "Lilypond parser");
458 SCM_ASSERT_TYPE (book, book_smob, SCM_ARG2, __FUNCTION__, "Book");
460 /* ugh. changing argument.*/
461 book->bookpaper_ = bp;
463 File_name outname (parser->output_basename_);
464 int *c = &parser->book_count_;
466 outname.base_ += "-" + to_string (*c);
469 Output_def *paper = get_paper (parser);
471 Paper_book* pb = book->process (outname.to_string (), paper);
473 pb->output (outname.to_string ());
475 scm_gc_unprotect_object (paper->self_scm ());
476 scm_gc_unprotect_object (pb->self_scm ());
478 return SCM_UNDEFINED;