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 (false);
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 ().
181 Actually, that gets very icky when there are white space, because
182 the line-numbers are all wrong. Let's try the character before
183 the current token. That gets the right result for note/duration
184 stuff, but doesn't mess up for errors in the 1st token of the
187 Input hi (lexer_->here_input ());
189 char const * bla = hi.defined_str0_;
190 if (hi.line_number () > 1
191 || hi.column_number () > 1)
194 return Input (hi.source_file_, bla);
198 /****************************************************************/
204 bool store_locations_global_b;
206 /* Do not append `!' suffix, since 1st argument is not modified. */
207 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click", 1, 0, 0,
209 "Set the options for Point-and-click source specials output. The\n"
210 "argument is a symbol. Possible options are @code{none} (no source specials),\n"
211 "@code{line} and @code{line-column}")
214 SCM val = SCM_BOOL_F;
215 if (ly_symbol2scm ("line-column") == what)
216 val = ly_scheme_function ("line-column-location");
217 else if (what == ly_symbol2scm ("line"))
218 val = ly_scheme_function ("line-location");
220 scm_module_define (global_lily_module, ly_symbol2scm ("point-and-click"),
222 store_locations_global_b = ly_c_procedure_p (val);
223 return SCM_UNSPECIFIED;
226 LY_DEFINE (ly_parse_file, "ly:parse-file",
229 "Parse a single @code{.ly} file. "
230 "Upon failure, throw @code{ly-file-failed} key.")
232 SCM_ASSERT_TYPE (ly_c_string_p (name), name, SCM_ARG1, __FUNCTION__, "string");
233 char const *file = SCM_STRING_CHARS (name);
234 char const *extensions[] = {"ly", "", 0};
235 String file_name = global_path.find (file, extensions);
237 /* By default, use base name of input file for output file name,
238 write output to cwd; do not use root and directory parts of input
240 File_name out_file_name (file_name);
241 if (file_name != "-")
242 out_file_name.ext_ = output_format_global;
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 SCM_ASSERT_TYPE (ly_c_symbol_p (symbol), symbol, SCM_ARG1, __FUNCTION__, "symbol");
329 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
330 parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
331 return SCM_UNSPECIFIED;
334 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
336 (SCM parser_smob, SCM ly_code),
337 "Parse the string LY_CODE with PARSER_SMOB."
338 "Upon failure, throw @code{ly-file-failed} key.")
341 SCM_ASSERT_TYPE (ly_c_parser_p (parser), music, SCM_ARG1, __FUNCTION__, "parser");
343 SCM_ASSERT_TYPE (ly_c_string_p (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
346 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
347 parser->parse_string (ly_scm2string (ly_code));
349 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
350 My_lily_parser *clone = new My_lily_parser (*parser);
351 clone->parse_string (ly_scm2string (ly_code));
355 return SCM_UNSPECIFIED;
359 get_paper (My_lily_parser *parser)
361 SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
362 Music_output_def *paper = unsmob_music_output_def (id);
363 return paper ? paper->clone () : new Paper_def;
366 LY_DEFINE (ly_parser_print_score, "ly:parser-print-score",
368 (SCM parser_smob, SCM score_smob),
369 "Print score, i.e., the classic way.")
372 SCM_ASSERT_TYPE (ly_c_parser_p (parser), music, SCM_ARG1, __FUNCTION__, "parser");
373 SCM_ASSERT_TYPE (ly_c_music_p (music), music, SCM_ARG1, __FUNCTION__, "music");
375 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
376 Score *score = unsmob_score (score_smob);
378 SCM header = is_module (score->header_) ? score->header_
379 : parser->header_.to_SCM ();
381 File_name outname (parser->output_basename_);
382 int *c = &parser->book_count_;
384 outname.base_ += "-" + to_string (*c);
387 SCM os = scm_makfrom0str (outname.to_string ().to_str0 ());
388 for (int i = 0; i < score->defs_.size (); i++)
389 default_rendering (score->music_, score->defs_[i]->self_scm (), header,
392 if (score->defs_.is_empty ())
394 Music_output_def *paper = get_paper (parser);
395 default_rendering (score->music_, paper->self_scm (), header, os);
396 scm_gc_unprotect_object (paper->self_scm ());
398 return SCM_UNDEFINED;
401 LY_DEFINE (ly_parser_print_book, "ly:parser-print-book",
403 (SCM parser_smob, SCM book_smob),
407 SCM_ASSERT_TYPE (ly_c_parser_p (parser_smob), parser_smob, SCM_ARG1, __FUNCTION__, "parser_smob");
408 SCM_ASSERT_TYPE (ly_c_music_p (book_smob), book_smob, SCM_ARG1, __FUNCTION__, "book_smob");
410 My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
411 Book *book = unsmob_book (book_smob);
413 SCM header = parser->header_;
414 File_name outname (parser->output_basename_);
415 int *c = &parser->book_count_;
417 outname.base_ += "-" + to_string (*c);
419 Music_output_def *paper = get_paper (parser);
420 book->process (outname.to_string (), paper, header);
421 scm_gc_unprotect_object (paper->self_scm ());
422 return SCM_UNDEFINED;