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"
15 #include "ly-smobs.icc"
17 #include "my-lily-lexer.hh"
18 #include "my-lily-parser.hh"
19 #include "paper-def.hh"
22 #include "scm-hash.hh"
28 My_lily_parser::My_lily_parser (Sources *sources)
34 default_duration_ = Duration (2,0);
36 last_beam_start_ = SCM_EOL;
39 header_ = ly_make_anonymous_module ();
43 My_lily_parser::My_lily_parser (My_lily_parser const &src)
45 book_count_ = src.book_count_;
46 score_count_ = src.score_count_;
48 sources_ = src.sources_;
49 default_duration_ = src.default_duration_;
50 error_level_ = src.error_level_;
51 last_beam_start_ = src.last_beam_start_;
52 header_ = src.header_;
57 My_lily_parser::~My_lily_parser ()
62 IMPLEMENT_SMOBS (My_lily_parser);
63 IMPLEMENT_TYPE_P (My_lily_parser, "ly:my-lily-parser?");
64 IMPLEMENT_DEFAULT_EQUAL_P (My_lily_parser);
67 My_lily_parser::mark_smob (SCM s)
69 My_lily_parser *parser = (My_lily_parser*) ly_cdr (s);
70 return parser->header_;
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_);
89 output_basename_ = out_name;
91 lexer_->main_input_name_ = name;
93 progress_indication (_ ("Parsing..."));
94 progress_indication ("\n");
97 lexer_->new_input (init, sources_);
99 /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
100 OUT_FILE (unless IN_FILE redefines output file name). */
103 if (!define_spots_.is_empty ())
105 define_spots_.top ().warning (_ ("Braces don't match"));
109 error_level_ = error_level_ | lexer_->error_level_;
113 My_lily_parser::parse_string (String ly_code)
115 My_lily_lexer *parent = lexer_;
116 lexer_ = (parent == 0 ? new My_lily_lexer (sources_)
117 : new My_lily_lexer (*parent));
119 lexer_->main_input_name_ = "<string>";
120 lexer_->main_input_b_ = true;
123 lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
126 if (!define_spots_.is_empty ())
128 define_spots_.top ().warning (_ ("Braces don't match"));
132 error_level_ = error_level_ | lexer_->error_level_;
136 parent->keytable_ = lexer_->keytable_;
137 parent->encoding_ = lexer_->encoding_;
138 parent->chordmodifier_tab_ = lexer_->chordmodifier_tab_;
139 parent->pitchname_tab_stack_ = lexer_->pitchname_tab_stack_;
140 parent->sources_ = lexer_->sources_;
141 parent->scopes_ = lexer_->scopes_;
142 parent->error_level_ = lexer_->error_level_;
143 parent->main_input_b_ = lexer_->main_input_b_;
148 My_lily_parser::push_spot ()
150 define_spots_.push (here_input ());
154 My_lily_parser::here_str0 () const
156 return lexer_->here_str0 ();
160 My_lily_parser::parser_error (String s)
162 here_input ().error (s);
167 My_lily_parser::pop_spot ()
169 return define_spots_.pop ();
173 My_lily_parser::here_input () const
176 Parsing looks ahead , so we really want the previous location of the
177 lexer, not lexer_->here_input ().
180 Actually, that gets very icky when there are white space, because
181 the line-numbers are all wrong. Let's try the character before
182 the current token. That gets the right result for
183 note/duration stuff, but doesn't mess up for errors in the 1st token of the line.
186 Input hi (lexer_->here_input ());
188 char const * bla = hi.defined_str0_;
189 if (hi.line_number () > 1
190 || hi.column_number () > 1)
193 return Input (hi.source_file_, bla);
197 /****************************************************************/
203 bool store_locations_global_b;
205 /* Do not append `!' suffix, since 1st argument is not modified. */
206 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click", 1, 0, 0,
208 "Set the options for Point-and-click source specials output. The\n"
209 "argument is a symbol. Possible options are @code{none} (no source specials),\n"
210 "@code{line} and @code{line-column}")
213 SCM val = SCM_BOOL_F;
214 if (ly_symbol2scm ("line-column") == what)
215 val = scm_c_eval_string ("line-column-location");
216 else if (what == ly_symbol2scm ("line"))
217 val = scm_c_eval_string ("line-location");
219 scm_module_define (global_lily_module, ly_symbol2scm ("point-and-click"),
221 store_locations_global_b = ly_c_procedure_p (val);
222 return SCM_UNSPECIFIED;
225 LY_DEFINE (ly_parse_file, "ly:parse-file",
228 "Parse a single @code{.ly} file. "
229 "Upon failure, throw @code{ly-file-failed} key.")
231 SCM_ASSERT_TYPE (ly_c_string_p (name), name, SCM_ARG1, __FUNCTION__, "string");
232 char const *file = SCM_STRING_CHARS (name);
233 char const *extensions[] = {"ly", "", 0};
234 String file_name = global_path.find (file, extensions);
236 /* By default, use base name of input file for output file name,
237 write output to cwd; do not use root and directory parts of input
239 File_name out_file_name (file_name);
240 if (file_name != "-")
241 out_file_name.ext_ = output_format_global;
242 out_file_name.root_ = "";
243 out_file_name.dir_ = "";
245 if (!output_name_global.is_empty ())
246 out_file_name = File_name (output_name_global);
249 if (!init_name_global.is_empty ())
250 init = init_name_global;
254 String out_file = out_file_name.to_string ();
256 if (init.length () && global_path.find (init).is_empty ())
258 warning (_f ("can't find init file: `%s'", init));
259 warning (_f ("(search path: `%s')",
260 global_path.to_string ().to_str0 ()));
264 if ((file_name != "-") && global_path.find (file_name).is_empty ())
266 warning (_f ("can't find file: `%s'", file_name));
267 scm_throw (ly_symbol2scm ("ly-file-failed"),
268 scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
273 sources.set_path (&global_path);
275 progress_indication (_f ("Now processing `%s'", file_name.to_str0 ()));
276 progress_indication ("\n");
278 My_lily_parser *parser = new My_lily_parser (&sources);
279 scm_module_define (global_lily_module, ly_symbol2scm ("parser"),
280 parser->self_scm ());
281 parser->parse_file (init, file_name, out_file);
283 bool error = parser->error_level_;
286 /* TODO: pass renamed input file too. */
287 scm_throw (ly_symbol2scm ("ly-file-failed"),
288 scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
290 return SCM_UNSPECIFIED;
293 LY_DEFINE (ly_parse_string, "ly:parse-string",
296 "Parse the string LY_CODE. "
297 "Upon failure, throw @code{ly-file-failed} key.")
299 SCM_ASSERT_TYPE (ly_c_string_p (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
302 sources.set_path (&global_path);
303 My_lily_parser *parser = new My_lily_parser (&sources);
304 scm_module_define (global_lily_module, ly_symbol2scm ("parser"),
305 parser->self_scm ());
306 parser->parse_string (ly_scm2string (ly_code));
309 return SCM_UNSPECIFIED;
312 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
314 (SCM parser_smob, SCM ly_code),
315 "Parse the string LY_CODE with PARSER_SMOB."
316 "Upon failure, throw @code{ly-file-failed} key.")
319 SCM_ASSERT_TYPE (ly_c_parser_p (parser), music, SCM_ARG1, __FUNCTION__, "parser");
321 SCM_ASSERT_TYPE (ly_c_string_p (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
324 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
325 parser->parse_string (ly_scm2string (ly_code));
327 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
328 My_lily_parser *clone = new My_lily_parser (*parser);
329 clone->parse_string (ly_scm2string (ly_code));
333 return SCM_UNSPECIFIED;
337 get_paper (My_lily_parser *parser)
339 SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
340 Music_output_def *paper = unsmob_music_output_def (id);
341 return paper ? paper->clone () : new Paper_def;
344 LY_DEFINE (ly_parser_print_score, "ly:parser-print-score",
346 (SCM parser_smob, SCM score_smob),
347 "Print score, i.e., the classic way.")
350 SCM_ASSERT_TYPE (ly_c_parser_p (parser), music, SCM_ARG1, __FUNCTION__, "parser");
351 SCM_ASSERT_TYPE (ly_c_music_p (music), music, SCM_ARG1, __FUNCTION__, "music");
353 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
354 Score *score = unsmob_score (score_smob);
356 SCM header = is_module (score->header_) ? score->header_
357 : parser->header_.to_SCM ();
359 File_name outname (parser->output_basename_);
360 int *c = &parser->book_count_;
362 outname.base_ += "-" + to_string (*c);
365 SCM os = scm_makfrom0str (outname.to_string ().to_str0 ());
366 for (int i = 0; i < score->defs_.size (); i++)
367 default_rendering (score->music_, score->defs_[i]->self_scm (), header,
370 if (score->defs_.is_empty ())
372 Music_output_def *paper = get_paper (parser);
373 default_rendering (score->music_, paper->self_scm (), header, os);
374 scm_gc_unprotect_object (paper->self_scm ());
376 return SCM_UNDEFINED;
379 LY_DEFINE (ly_parser_print_book, "ly:parser-print-book",
381 (SCM parser_smob, SCM book_smob),
385 SCM_ASSERT_TYPE (ly_c_parser_p (parser_smob), parser_smob, SCM_ARG1, __FUNCTION__, "parser_smob");
386 SCM_ASSERT_TYPE (ly_c_music_p (book_smob), book_smob, SCM_ARG1, __FUNCTION__, "book_smob");
388 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
389 Book *book = unsmob_book (book_smob);
391 SCM header = parser->header_;
392 File_name outname (parser->output_basename_);
393 int *c = &parser->book_count_;
395 outname.base_ += "-" + to_string (*c);
397 Music_output_def *paper = get_paper (parser);
398 book->process (outname.to_string (), paper, header);
399 scm_gc_unprotect_object (paper->self_scm ());
400 return SCM_UNDEFINED;