]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser.cc
remove ly_scm2str0.
[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*) ly_cdr (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 #if 0
122   SCM parent_prot = lexer_ ? lexer_->self_scm () : SCM_EOL;
123 #endif
124
125   Lily_lexer * parent = lexer_;
126   lexer_ = (parent == 0 ? new Lily_lexer (sources_)
127             : new Lily_lexer (*parent));
128   scm_gc_unprotect_object (lexer_->self_scm ());
129
130
131   SCM oldmod = scm_current_module ();
132   scm_set_current_module (ly_car (lexer_->scopes_));
133   
134   // TODO: use $parser 
135   lexer_->set_identifier (ly_symbol2scm ("parser"),
136                           self_scm ());
137   
138   lexer_->main_input_name_ = "<string>";
139   lexer_->main_input_b_ = true;
140
141   set_yydebug (0);
142   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
143   do_yyparse ();
144   
145   if (!define_spots_.is_empty ())
146     {
147       define_spots_.top ().warning (_ ("Braces don't match"));
148       error_level_ = 1;
149     }
150
151   error_level_ = error_level_ | lexer_->error_level_;
152
153 #if 0
154   if (Lily_lexer::unsmob (parent_prot))
155     {
156       /*
157         what the fuck is this good for?
158        */
159       parent->encoding_ = lexer_->encoding_;
160       parent->chordmodifier_tab_ = lexer_->chordmodifier_tab_;
161       parent->pitchname_tab_stack_ = lexer_->pitchname_tab_stack_;
162       parent->sources_ = lexer_->sources_;
163       parent->scopes_ = lexer_->scopes_;
164       parent->error_level_ = lexer_->error_level_; 
165       parent->main_input_b_ = lexer_->main_input_b_;
166     }
167   
168   scm_remember_upto_here_1 (parent_prot);
169 #endif
170   
171   scm_set_current_module (oldmod);
172   lexer_ = 0;
173 }
174
175 void
176 Lily_parser::push_spot ()
177 {
178   define_spots_.push (here_input ());
179 }
180
181 char const *
182 Lily_parser::here_str0 () const
183 {
184   return lexer_->here_str0 ();
185 }
186
187 void
188 Lily_parser::parser_error (String s)
189 {
190   here_input ().error (s);
191   error_level_ = 1;
192 }
193
194 Input
195 Lily_parser::pop_spot ()
196 {
197   return define_spots_.pop ();
198 }
199
200 Input
201 Lily_parser::here_input () const
202 {
203   /*
204     Parsing looks ahead , so we really want the previous location of the
205     lexer, not lexer_->here_input ().
206   */
207   
208   /*
209     Actually, that gets very icky when there are white space, because
210     the line-numbers are all wrong.  Let's try the character before
211     the current token. That gets the right result for note/duration
212     stuff, but doesn't mess up for errors in the 1st token of the
213     line.
214   */
215   Input hi (lexer_->here_input ());
216
217   char const * bla = hi.defined_str0_;
218   if (hi.line_number () > 1
219       || hi.column_number () > 1)
220     bla --;
221   
222   return Input (hi.source_file_, bla);
223 }
224
225
226 /****************************************************************/
227
228
229 /*
230   junkme?
231  */
232 bool store_locations_global_b;
233
234 /* Do not append `!' suffix, since 1st argument is not modified. */
235 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click",
236            1, 0, 0, (SCM what),
237           "Set the options for Point-and-click source specials output. The\n"
238 "argument is a symbol.  Possible options are @code{none} (no source specials),\n"
239 "@code{line} and @code{line-column}")
240 {
241   /* UGH. */
242   SCM val = SCM_BOOL_F;
243   if (ly_symbol2scm ("line-column") == what)
244     val = ly_scheme_function ("line-column-location");
245   else if (what == ly_symbol2scm ("line"))
246     val = ly_scheme_function ("line-location");
247
248   scm_module_define (global_lily_module, ly_symbol2scm ("point-and-click"),
249                      val);
250   store_locations_global_b = ly_c_procedure_p (val);
251   return SCM_UNSPECIFIED;
252 }
253
254 LY_DEFINE (ly_parse_file, "ly:parse-file",
255            1, 0, 0, (SCM name),
256            "Parse a single @code{.ly} file.  "
257            "Upon failure, throw @code{ly-file-failed} key.")
258 {
259   SCM_ASSERT_TYPE (ly_c_string_p (name), name, SCM_ARG1, __FUNCTION__, "string");
260   char const *file = scm_i_string_chars (name);
261   char const *extensions[] = {"ly", "", 0};
262
263   String file_name = global_path.find (file, extensions);
264
265   /* By default, use base name of input file for output file name,
266      write output to cwd; do not use root and directory parts of input
267      file name.  */
268   File_name out_file_name (file_name);
269
270   global_path.append (out_file_name.dir_);
271   
272   out_file_name.ext_ = "";
273   out_file_name.root_ = "";
274   out_file_name.dir_ = "";
275
276   if (!output_name_global.is_empty ())
277     out_file_name = File_name (output_name_global);
278   
279   String init;
280   if (!init_name_global.is_empty ())
281     init = init_name_global;
282   else
283     init = "init.ly";
284
285   String out_file = out_file_name.to_string ();
286
287   if (init.length () && global_path.find (init).is_empty ())
288     {
289       warning (_f ("can't find init file: `%s'", init));
290       warning (_f ("(search path: `%s')",
291                    global_path.to_string ().to_str0 ()));
292       exit (2);
293     }
294
295   if ((file_name != "-") && global_path.find (file_name).is_empty ())
296     {
297       warning (_f ("can't find file: `%s'", file_name));
298       scm_throw (ly_symbol2scm ("ly-file-failed"),
299                  scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
300     }
301   else
302     {
303       Sources sources;
304       sources.set_path (&global_path);
305   
306       progress_indication (_f ("Now processing `%s'", file_name.to_str0 ()));
307       progress_indication ("\n");
308
309       Lily_parser *parser = new Lily_parser (&sources);
310
311       parser->parse_file (init, file_name, out_file);
312
313       bool error = parser->error_level_;
314       scm_gc_unprotect_object (parser->self_scm ());
315       parser = 0;
316       if (error)
317         /* TODO: pass renamed input file too.  */
318         scm_throw (ly_symbol2scm ("ly-file-failed"),
319                    scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
320     }
321   return SCM_UNSPECIFIED;
322 }
323
324 LY_DEFINE (ly_parse_string, "ly:parse-string",
325            1, 0, 0, (SCM ly_code),
326            "Parse the string LY_CODE.  "
327            "Upon failure, throw @code{ly-file-failed} key.")
328 {
329   SCM_ASSERT_TYPE (ly_c_string_p (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
330   
331   Sources sources;
332   sources.set_path (&global_path);
333   Lily_parser *parser = new Lily_parser (&sources);
334   scm_module_define (global_lily_module, ly_symbol2scm ("parser"),
335                      parser->self_scm ());
336   parser->parse_string (ly_scm2string (ly_code));
337   scm_gc_unprotect_object (parser->self_scm ());
338   parser = 0;
339   
340   return SCM_UNSPECIFIED;
341 }
342
343 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
344            1, 0, 0, (SCM parser_smob),
345            "Return a clone of PARSER_SMOB.")
346 {
347   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
348   Lily_parser *clone = new Lily_parser (*parser);
349
350   /* FIXME: should copy scopes too. */
351   return scm_gc_unprotect_object (clone->self_scm ());
352 }
353
354 LY_DEFINE (ly_parser_define, "ly:parser-define",
355            3, 0, 0, (SCM parser_smob, SCM symbol, SCM val),
356            "Bind SYMBOL to VAL in PARSER_SMOB's module.")
357 {
358   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
359   SCM_ASSERT_TYPE (ly_c_symbol_p (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
360   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");  
361
362   parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
363   return SCM_UNSPECIFIED;
364 }
365
366 LY_DEFINE (ly_parser_lookup, "ly:parser-lookup",
367            2, 0, 0, (SCM parser_smob, SCM symbol),
368            "Lookup @var{symbol} in @var{parser_smob}'s module.  "
369            "Undefined is '().")
370 {
371   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
372
373   SCM_ASSERT_TYPE (ly_c_symbol_p (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
374   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");  
375
376   SCM val= parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
377   if (val != SCM_UNDEFINED)
378     return val;
379   else
380     return SCM_EOL;
381 }
382
383 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
384            2, 0, 0, (SCM parser_smob, SCM ly_code),
385            "Parse the string LY_CODE with PARSER_SMOB."
386            "Upon failure, throw @code{ly-file-failed} key.")
387 {
388   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
389
390   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
391   SCM_ASSERT_TYPE (ly_c_string_p (ly_code), ly_code, SCM_ARG2, __FUNCTION__, "string");
392   
393   parser->parse_string (ly_scm2string (ly_code));
394   
395   return SCM_UNSPECIFIED;
396 }
397
398 Output_def*
399 get_paper (Lily_parser *parser)
400 {
401   SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
402   Output_def *paper = unsmob_output_def (id);
403   paper = paper ? paper->clone () : new Output_def;
404   paper->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
405
406   paper->parent_ = unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultbookpaper"));
407   return paper;
408 }
409
410
411 Output_def*
412 get_midi (Lily_parser *parser)
413 {
414   SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
415   Output_def *paper = unsmob_output_def (id);
416   paper = paper ? paper->clone () : new Output_def;
417   paper->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
418   return paper;
419 }
420
421
422 Output_def*
423 get_bookpaper (Lily_parser *parser)
424 {
425   SCM id = parser->lexer_->lookup_identifier ("$defaultbookpaper");
426   Output_def *paper = unsmob_output_def (id);
427
428   paper = paper ? dynamic_cast<Output_def*> (paper->clone ()) : new Output_def;
429   paper->set_variable (ly_symbol2scm ("is-bookpaper"), SCM_BOOL_T);
430   return paper;
431 }
432
433
434 /* TODO: move this to Scheme?  Why take the parser arg, and all the back
435    & forth between scm and c++? */
436 LY_DEFINE (ly_parser_print_score, "ly:parser-print-score",
437            2, 0, 0,
438            (SCM parser_smob, SCM score_smob),
439            "Print score, i.e., the classic way.")
440 {
441   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
442   Score *score = unsmob_score (score_smob);
443
444   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
445   SCM_ASSERT_TYPE (score, score_smob, SCM_ARG2, __FUNCTION__, "score");
446
447   SCM header = ly_c_module_p (score->header_) ? score->header_
448     : parser->lexer_->lookup_identifier ("$globalheader");
449   
450   File_name outname (parser->output_basename_);
451   int *c = &parser->book_count_;
452   if (*c)
453     outname.base_ += "-" + to_string (*c);
454   (*c)++;
455
456   SCM os = scm_makfrom0str (outname.to_string ().to_str0 ());
457   SCM bookpaper = get_bookpaper (parser)->self_scm ();
458   for (int i = 0; i < score->defs_.size (); i++)
459     default_rendering (score->music_, score->defs_[i]->self_scm (),
460                        bookpaper,
461                        header, os);
462
463   if (score->defs_.is_empty ())
464     {
465       Output_def *paper = get_paper (parser);
466       default_rendering (score->music_, paper->self_scm (),
467                          get_bookpaper (parser)->self_scm (),
468                          header, os);
469       scm_gc_unprotect_object (paper->self_scm ());
470     }
471   return SCM_UNSPECIFIED;
472 }
473
474
475 LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
476            2, 0, 0, (SCM parser, SCM names),
477            "Replace current note names in @var{parser}. "
478            "@var{names} is an alist of symbols.  "
479            "This only has effect if the current mode is notes.")
480 {
481   Lily_parser *p = unsmob_my_lily_parser (parser);
482   SCM_ASSERT_TYPE(p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
483
484   if (p->lexer_->is_note_state ())
485     {
486       p->lexer_->pop_state ();
487       p->lexer_->push_note_state (alist_to_hashq (names));
488     }
489
490   return SCM_UNSPECIFIED;
491 }
492
493 LY_DEFINE (ly_parser_print_book, "ly:parser-print-book",
494            2, 0, 0, (SCM parser_smob, SCM book_smob),
495            "Print book.")
496 {
497   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
498   Book *book = unsmob_book (book_smob);
499   Output_def *bp = unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultbookpaper"));
500   
501   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "Lilypond parser");
502   SCM_ASSERT_TYPE (book, book_smob, SCM_ARG2, __FUNCTION__, "Book");
503   
504   /*  ugh. changing argument.*/
505   book->bookpaper_ = bp;
506   
507   File_name outname (parser->output_basename_);
508   int *c = &parser->book_count_;
509   if (*c)
510     outname.base_ += "-" + to_string (*c);
511   (*c)++;
512
513   Output_def *paper = get_paper (parser);
514
515   Paper_book* pb = book->process (outname.to_string (), paper);
516
517   pb->output (outname.to_string ());
518   
519   scm_gc_unprotect_object (paper->self_scm ());
520   scm_gc_unprotect_object (pb->self_scm ());
521
522   return SCM_UNSPECIFIED;
523 }
524