]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser.cc
* make/lilypond.fedora.spec.in: rename file.
[lilypond.git] / lily / lily-parser.cc
1 /*
2   lily-parser.cc -- implement Lily_parser
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7        Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include "book.hh"
11 #include "file-name.hh"
12 #include "file-path.hh"
13 #include "lily-version.hh"
14 #include "ly-module.hh"
15 #include "main.hh"
16 #include "lily-lexer.hh"
17 #include "lily-parser.hh"
18 #include "output-def.hh"
19 #include "paper-book.hh"
20 #include "parser.hh"
21 #include "score.hh"
22 #include "source.hh"
23 #include "warn.hh"
24
25
26 Lily_parser::Lily_parser (Sources *sources)
27 {
28   book_count_ = 0;
29   score_count_ = 0;
30   lexer_ = 0;
31   sources_ = sources;
32   default_duration_ = Duration (2, 0);
33   error_level_ = 0;
34   last_beam_start_ = SCM_EOL;
35
36   smobify_self ();
37 }
38
39 Lily_parser::Lily_parser (Lily_parser const &src)
40 {
41   book_count_ = src.book_count_;
42   score_count_ = src.score_count_;
43   lexer_ = 0;
44   sources_ = src.sources_;
45   default_duration_ = src.default_duration_;
46   error_level_ = src.error_level_;
47   last_beam_start_ = src.last_beam_start_;
48
49   smobify_self ();
50   if (src.lexer_)
51     lexer_ = new Lily_lexer (*src.lexer_);
52
53   scm_gc_unprotect_object (lexer_->self_scm ());
54 }
55
56 Lily_parser::~Lily_parser ()
57 {
58 }
59
60 #include "ly-smobs.icc"
61
62 IMPLEMENT_SMOBS (Lily_parser);
63 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
64 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
65
66 SCM
67 Lily_parser::mark_smob (SCM s)
68 {
69   Lily_parser *parser = (Lily_parser*) SCM_CELL_WORD_1 (s);
70   return (parser->lexer_) ? parser->lexer_->self_scm () : SCM_EOL;
71 }
72
73 int
74 Lily_parser::print_smob (SCM s, SCM port, scm_print_state*)
75 {
76   scm_puts ("#<my_lily_parser ", port);
77   Lily_parser *parser = (Lily_parser*) ly_cdr (s);
78   (void) parser;
79   scm_puts (" >", port);
80   return 1;
81 }
82
83
84 /* Process one .ly file, or book.  */
85 void
86 Lily_parser::parse_file (String init, String name, String out_name)
87 {
88   lexer_ = new Lily_lexer (sources_);
89   scm_gc_unprotect_object (lexer_->self_scm ());
90   // TODO: use $parser 
91   lexer_->set_identifier (ly_symbol2scm ("parser"),
92                           self_scm ());
93   output_basename_ = out_name;
94   
95   lexer_->main_input_name_ = name;
96
97   progress_indication (_ ("Parsing..."));
98   progress_indication ("\n");
99
100   set_yydebug (0);
101
102   lexer_->new_input (init, sources_);
103
104   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
105      OUT_FILE (unless IN_FILE redefines output file name).  */
106   do_yyparse ();
107   
108   if (!define_spots_.is_empty ())
109     {
110       define_spots_.top ().warning (_ ("Braces don't match"));
111       error_level_ = 1;
112     }
113
114   error_level_ = error_level_ | lexer_->error_level_;
115   lexer_ = 0;
116 }
117
118 void
119 Lily_parser::parse_string (String ly_code)
120 {
121   Lily_lexer * parent = lexer_;
122   lexer_ = (parent == 0 ? new Lily_lexer (sources_)
123             : new Lily_lexer (*parent));
124   scm_gc_unprotect_object (lexer_->self_scm ());
125
126
127   SCM oldmod = scm_current_module ();
128   scm_set_current_module (ly_car (lexer_->scopes_));
129   
130   // TODO: use $parser 
131   lexer_->set_identifier (ly_symbol2scm ("parser"),
132                           self_scm ());
133   
134   lexer_->main_input_name_ = "<string>";
135   lexer_->main_input_b_ = true;
136
137   set_yydebug (0);
138   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
139   do_yyparse ();
140   
141   if (!define_spots_.is_empty ())
142     {
143       define_spots_.top ().warning (_ ("Braces don't match"));
144       error_level_ = 1;
145     }
146
147   error_level_ = error_level_ | lexer_->error_level_;
148
149   scm_set_current_module (oldmod);
150   lexer_ = 0;
151 }
152
153 void
154 Lily_parser::push_spot ()
155 {
156   define_spots_.push (here_input ());
157 }
158
159 char const *
160 Lily_parser::here_str0 () const
161 {
162   return lexer_->here_str0 ();
163 }
164
165 void
166 Lily_parser::parser_error (String s)
167 {
168   here_input ().error (s);
169   error_level_ = 1;
170 }
171
172 Input
173 Lily_parser::pop_spot ()
174 {
175   return define_spots_.pop ();
176 }
177
178 Input
179 Lily_parser::here_input () const
180 {
181   /*
182     Parsing looks ahead , so we really want the previous location of the
183     lexer, not lexer_->here_input ().
184   */
185   
186   /*
187     Actually, that gets very icky when there are white space, because
188     the line-numbers are all wrong.  Let's try the character before
189     the current token. That gets the right result for note/duration
190     stuff, but doesn't mess up for errors in the 1st token of the
191     line.
192   */
193   Input hi (lexer_->here_input ());
194
195   char const * bla = hi.defined_str0_;
196   if (hi.line_number () > 1
197       || hi.column_number () > 1)
198     bla --;
199   
200   return Input (hi.source_file_, bla);
201 }
202
203
204 /****************************************************************/
205
206
207 /*
208   junkme?
209  */
210 bool store_locations_global_b;
211
212 /* Do not append `!' suffix, since 1st argument is not modified. */
213 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click",
214            1, 0, 0, (SCM what),
215           "Set the options for Point-and-click source specials output. The\n"
216 "argument is a symbol.  Possible options are @code{none} (no source specials),\n"
217 "@code{line} and @code{line-column}")
218 {
219   /* UGH. */
220   SCM val = SCM_BOOL_F;
221   if (ly_symbol2scm ("line-column") == what)
222     val = ly_scheme_function ("line-column-location");
223   else if (what == ly_symbol2scm ("line"))
224     val = ly_scheme_function ("line-location");
225
226   scm_module_define (global_lily_module, ly_symbol2scm ("point-and-click"),
227                      val);
228   store_locations_global_b = ly_c_procedure_p (val);
229   return SCM_UNSPECIFIED;
230 }
231
232 LY_DEFINE (ly_parse_file, "ly:parse-file",
233            1, 0, 0, (SCM name),
234            "Parse a single @code{.ly} file.  "
235            "Upon failure, throw @code{ly-file-failed} key.")
236 {
237   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
238   char const *file = scm_i_string_chars (name);
239   char const *extensions[] = {"ly", "", 0};
240
241   String file_name = global_path.find (file, extensions);
242
243   /* By default, use base name of input file for output file name,
244      write output to cwd; do not use root and directory parts of input
245      file name.  */
246   File_name out_file_name (file_name);
247
248   global_path.append (out_file_name.dir_);
249   
250   out_file_name.ext_ = "";
251   out_file_name.root_ = "";
252   out_file_name.dir_ = "";
253
254   if (!output_name_global.is_empty ())
255     out_file_name = File_name (output_name_global);
256   
257   String init;
258   if (!init_name_global.is_empty ())
259     init = init_name_global;
260   else
261     init = "init.ly";
262
263   String out_file = out_file_name.to_string ();
264
265   if (init.length () && global_path.find (init).is_empty ())
266     {
267       warning (_f ("can't find init file: `%s'", init));
268       warning (_f ("(search path: `%s')",
269                    global_path.to_string ().to_str0 ()));
270       exit (2);
271     }
272
273   if ((file_name != "-") && global_path.find (file_name).is_empty ())
274     {
275       warning (_f ("can't find file: `%s'", file_name));
276       scm_throw (ly_symbol2scm ("ly-file-failed"),
277                  scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
278     }
279   else
280     {
281       Sources sources;
282       sources.set_path (&global_path);
283   
284       progress_indication (_f ("Now processing `%s'", file_name.to_str0 ()));
285       progress_indication ("\n");
286
287       Lily_parser *parser = new Lily_parser (&sources);
288
289       parser->parse_file (init, file_name, out_file);
290
291       bool error = parser->error_level_;
292       scm_gc_unprotect_object (parser->self_scm ());
293       parser = 0;
294       if (error)
295         /* TODO: pass renamed input file too.  */
296         scm_throw (ly_symbol2scm ("ly-file-failed"),
297                    scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
298     }
299   return SCM_UNSPECIFIED;
300 }
301
302 LY_DEFINE (ly_parse_string, "ly:parse-string",
303            1, 0, 0, (SCM ly_code),
304            "Parse the string LY_CODE.  "
305            "Upon failure, throw @code{ly-file-failed} key.")
306 {
307   SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
308   
309   Sources sources;
310   sources.set_path (&global_path);
311   Lily_parser *parser = new Lily_parser (&sources);
312   scm_module_define (global_lily_module, ly_symbol2scm ("parser"),
313                      parser->self_scm ());
314   parser->parse_string (ly_scm2string (ly_code));
315   scm_gc_unprotect_object (parser->self_scm ());
316   parser = 0;
317   
318   return SCM_UNSPECIFIED;
319 }
320
321 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
322            1, 0, 0, (SCM parser_smob),
323            "Return a clone of PARSER_SMOB.")
324 {
325   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
326   Lily_parser *clone = new Lily_parser (*parser);
327
328   /* FIXME: should copy scopes too. */
329   return scm_gc_unprotect_object (clone->self_scm ());
330 }
331
332 LY_DEFINE (ly_parser_define, "ly:parser-define",
333            3, 0, 0, (SCM parser_smob, SCM symbol, SCM val),
334            "Bind SYMBOL to VAL in PARSER_SMOB's module.")
335 {
336   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
337   SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
338   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");  
339
340   parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
341   return SCM_UNSPECIFIED;
342 }
343
344 LY_DEFINE (ly_parser_lookup, "ly:parser-lookup",
345            2, 0, 0, (SCM parser_smob, SCM symbol),
346            "Lookup @var{symbol} in @var{parser_smob}'s module.  "
347            "Undefined is '().")
348 {
349   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
350
351   SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
352   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");  
353
354   SCM val= parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
355   if (val != SCM_UNDEFINED)
356     return val;
357   else
358     return SCM_EOL;
359 }
360
361 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
362            2, 0, 0, (SCM parser_smob, SCM ly_code),
363            "Parse the string LY_CODE with PARSER_SMOB."
364            "Upon failure, throw @code{ly-file-failed} key.")
365 {
366   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
367
368   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
369   SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG2, __FUNCTION__, "string");
370   
371   parser->parse_string (ly_scm2string (ly_code));
372   
373   return SCM_UNSPECIFIED;
374 }
375
376 Output_def*
377 get_paper (Lily_parser *parser)
378 {
379   SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
380   Output_def *paper = unsmob_output_def (id);
381   paper = paper ? paper->clone () : new Output_def;
382   paper->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
383
384   return paper;
385 }
386
387
388 Output_def*
389 get_midi (Lily_parser *parser)
390 {
391   SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
392   Output_def *paper = unsmob_output_def (id);
393   paper = paper ? paper->clone () : new Output_def;
394   paper->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
395   return paper;
396 }
397
398
399 Output_def*
400 get_bookpaper (Lily_parser *parser)
401 {
402   SCM id = parser->lexer_->lookup_identifier ("$defaultbookpaper");
403   Output_def *paper = unsmob_output_def (id);
404
405   paper = paper ? dynamic_cast<Output_def*> (paper->clone ()) : new Output_def;
406   paper->set_variable (ly_symbol2scm ("is-bookpaper"), SCM_BOOL_T);
407   return paper;
408 }
409
410
411 /* TODO: move this to Scheme?  Why take the parser arg, and all the back
412    & forth between scm and c++? */
413 LY_DEFINE (ly_parser_print_score, "ly:parser-print-score",
414            2, 0, 0,
415            (SCM parser_smob, SCM score_smob),
416            "Print score, i.e., the classic way.")
417 {
418   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
419   Score *score = unsmob_score (score_smob);
420
421   if (score->error_found_)
422     return SCM_UNSPECIFIED;
423   
424   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
425   SCM_ASSERT_TYPE (score, score_smob, SCM_ARG2, __FUNCTION__, "score");
426
427   SCM header = ly_c_module_p (score->header_) ? score->header_
428     : parser->lexer_->lookup_identifier ("$globalheader");
429   
430   File_name outname (parser->output_basename_);
431   int *c = &parser->book_count_;
432   if (*c)
433     outname.base_ += "-" + to_string (*c);
434   (*c)++;
435
436   SCM os = scm_makfrom0str (outname.to_string ().to_str0 ());
437   SCM bookpaper = get_bookpaper (parser)->self_scm ();
438   for (int i = 0; i < score->defs_.size (); i++)
439     default_rendering (score->get_music (), score->defs_[i]->self_scm (),
440                        bookpaper,
441                        header, os);
442
443   if (score->defs_.is_empty ())
444     {
445       Output_def *paper = get_paper (parser);
446       default_rendering (score->get_music(), paper->self_scm (),
447                          get_bookpaper (parser)->self_scm (),
448                          header, os);
449       scm_gc_unprotect_object (paper->self_scm ());
450     }
451   return SCM_UNSPECIFIED;
452 }
453
454
455 LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
456            2, 0, 0, (SCM parser, SCM names),
457            "Replace current note names in @var{parser}. "
458            "@var{names} is an alist of symbols.  "
459            "This only has effect if the current mode is notes.")
460 {
461   Lily_parser *p = unsmob_my_lily_parser (parser);
462   SCM_ASSERT_TYPE(p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
463
464   if (p->lexer_->is_note_state ())
465     {
466       p->lexer_->pop_state ();
467       p->lexer_->push_note_state (alist_to_hashq (names));
468     }
469
470   return SCM_UNSPECIFIED;
471 }
472
473 LY_DEFINE (ly_parser_print_book, "ly:parser-print-book",
474            2, 0, 0, (SCM parser_smob, SCM book_smob),
475            "Print book.")
476 {
477   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
478   Book *book = unsmob_book (book_smob);
479   Output_def *bp = unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultbookpaper"));
480   
481   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "Lilypond parser");
482   SCM_ASSERT_TYPE (book, book_smob, SCM_ARG2, __FUNCTION__, "Book");
483   
484   /*  ugh. changing argument.*/
485   book->bookpaper_ = bp;
486   
487   File_name outname (parser->output_basename_);
488   int *c = &parser->book_count_;
489   if (*c)
490     outname.base_ += "-" + to_string (*c);
491   (*c)++;
492
493   Output_def *paper = get_paper (parser);
494
495   Paper_book* pb = book->process (outname.to_string (), paper);
496   if (pb)
497     {
498       pb->output (outname.to_string ());
499       scm_gc_unprotect_object (pb->self_scm ());
500     }
501
502   scm_gc_unprotect_object (paper->self_scm ());
503   return SCM_UNSPECIFIED;
504 }
505