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