]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-parser.cc
* lily/my-lily-parser.cc (parse_string): init parser variable when
[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 #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 "my-lily-lexer.hh"
17 #include "my-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 My_lily_parser::My_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 My_lily_parser::My_lily_parser (My_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   lexer_ = new My_lily_lexer (*src.lexer_);
51 }
52
53 My_lily_parser::~My_lily_parser ()
54 {
55   // FIXME: Memleak: del lexer
56 }
57
58 #include "ly-smobs.icc"
59
60 IMPLEMENT_SMOBS (My_lily_parser);
61 IMPLEMENT_TYPE_P (My_lily_parser, "ly:my-lily-parser?");
62 IMPLEMENT_DEFAULT_EQUAL_P (My_lily_parser);
63
64 SCM
65 My_lily_parser::mark_smob (SCM s)
66 {
67   My_lily_parser *parser = (My_lily_parser*) ly_cdr (s);
68   (void) parser;
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   // TODO: use $parser 
89   lexer_->set_identifier (ly_symbol2scm ("parser"),
90                           self_scm ());
91   output_basename_ = out_name;
92   
93   lexer_->main_input_name_ = name;
94
95   progress_indication (_ ("Parsing..."));
96   progress_indication ("\n");
97
98   set_yydebug (0);
99   lexer_->new_input (init, sources_);
100
101   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
102      OUT_FILE (unless IN_FILE redefines output file name).  */
103   do_yyparse ();
104   
105   if (!define_spots_.is_empty ())
106     {
107       define_spots_.top ().warning (_ ("Braces don't match"));
108       error_level_ = 1;
109     }
110
111   error_level_ = error_level_ | lexer_->error_level_;
112   delete lexer_;
113   lexer_ = 0;
114 }
115
116 void
117 My_lily_parser::parse_string (String ly_code)
118 {
119   My_lily_lexer *parent = lexer_;
120   lexer_ = (parent == 0 ? new My_lily_lexer (sources_)
121             : new My_lily_lexer (*parent));
122
123   // TODO: use $parser 
124   lexer_->set_identifier (ly_symbol2scm ("parser"),
125                           self_scm ());
126   
127   lexer_->main_input_name_ = "<string>";
128   lexer_->main_input_b_ = true;
129
130   set_yydebug (0);
131   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
132   do_yyparse ();
133   
134   if (!define_spots_.is_empty ())
135     {
136       define_spots_.top ().warning (_ ("Braces don't match"));
137       error_level_ = 1;
138     }
139
140   error_level_ = error_level_ | lexer_->error_level_;
141
142   if (parent != 0)
143     {
144       parent->keytable_ = lexer_->keytable_;
145       parent->encoding_ = lexer_->encoding_;
146       parent->chordmodifier_tab_ = lexer_->chordmodifier_tab_;
147       parent->pitchname_tab_stack_ = lexer_->pitchname_tab_stack_;
148       parent->sources_ = lexer_->sources_;
149       parent->scopes_ = lexer_->scopes_;
150       parent->error_level_ = lexer_->error_level_; 
151       parent->main_input_b_ = lexer_->main_input_b_;
152     }
153
154   delete lexer_;
155   lexer_ = 0;
156 }
157
158 void
159 My_lily_parser::push_spot ()
160 {
161   define_spots_.push (here_input ());
162 }
163
164 char const *
165 My_lily_parser::here_str0 () const
166 {
167   return lexer_->here_str0 ();
168 }
169
170 void
171 My_lily_parser::parser_error (String s)
172 {
173   here_input ().error (s);
174   error_level_ = 1;
175 }
176
177 Input
178 My_lily_parser::pop_spot ()
179 {
180   return define_spots_.pop ();
181 }
182
183 Input
184 My_lily_parser::here_input () const
185 {
186   /*
187     Parsing looks ahead , so we really want the previous location of the
188     lexer, not lexer_->here_input ().
189   */
190   
191   /*
192     Actually, that gets very icky when there are white space, because
193     the line-numbers are all wrong.  Let's try the character before
194     the current token. That gets the right result for note/duration
195     stuff, but doesn't mess up for errors in the 1st token of the
196     line.
197   */
198   Input hi (lexer_->here_input ());
199
200   char const * bla = hi.defined_str0_;
201   if (hi.line_number () > 1
202       || hi.column_number () > 1)
203     bla --;
204   
205   return Input (hi.source_file_, bla);
206 }
207
208
209 /****************************************************************/
210
211
212 /*
213   junkme?
214  */
215 bool store_locations_global_b;
216
217 /* Do not append `!' suffix, since 1st argument is not modified. */
218 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click",
219            1, 0, 0, (SCM what),
220           "Set the options for Point-and-click source specials output. The\n"
221 "argument is a symbol.  Possible options are @code{none} (no source specials),\n"
222 "@code{line} and @code{line-column}")
223 {
224   /* UGH. */
225   SCM val = SCM_BOOL_F;
226   if (ly_symbol2scm ("line-column") == what)
227     val = ly_scheme_function ("line-column-location");
228   else if (what == ly_symbol2scm ("line"))
229     val = ly_scheme_function ("line-location");
230
231   scm_module_define (global_lily_module, ly_symbol2scm ("point-and-click"),
232                      val);
233   store_locations_global_b = ly_c_procedure_p (val);
234   return SCM_UNSPECIFIED;
235 }
236
237 LY_DEFINE (ly_parse_file, "ly:parse-file",
238            1, 0, 0, (SCM name),
239            "Parse a single @code{.ly} file.  "
240            "Upon failure, throw @code{ly-file-failed} key.")
241 {
242   SCM_ASSERT_TYPE (ly_c_string_p (name), name, SCM_ARG1, __FUNCTION__, "string");
243   char const *file = SCM_STRING_CHARS (name);
244   char const *extensions[] = {"ly", "", 0};
245   String file_name = global_path.find (file, extensions);
246   
247   /* By default, use base name of input file for output file name,
248      write output to cwd; do not use root and directory parts of input
249      file name.  */
250   File_name out_file_name (file_name);
251   out_file_name.ext_ = "";
252   out_file_name.root_ = "";
253   out_file_name.dir_ = "";
254
255   if (!output_name_global.is_empty ())
256     out_file_name = File_name (output_name_global);
257   
258   String init;
259   if (!init_name_global.is_empty ())
260     init = init_name_global;
261   else
262     init = "init.ly";
263
264   String out_file = out_file_name.to_string ();
265
266   if (init.length () && global_path.find (init).is_empty ())
267     {
268       warning (_f ("can't find init file: `%s'", init));
269       warning (_f ("(search path: `%s')",
270                    global_path.to_string ().to_str0 ()));
271       exit (2);
272     }
273
274   if ((file_name != "-") && global_path.find (file_name).is_empty ())
275     {
276       warning (_f ("can't find file: `%s'", file_name));
277       scm_throw (ly_symbol2scm ("ly-file-failed"),
278                  scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
279     }
280   else
281     {
282       Sources sources;
283       sources.set_path (&global_path);
284   
285       progress_indication (_f ("Now processing `%s'", file_name.to_str0 ()));
286       progress_indication ("\n");
287
288       My_lily_parser *parser = new My_lily_parser (&sources);
289
290       parser->parse_file (init, file_name, out_file);
291
292       bool error = parser->error_level_;
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 (ly_c_string_p (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
308   
309   Sources sources;
310   sources.set_path (&global_path);
311   My_lily_parser *parser = new My_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   parser = 0;
316   
317   return SCM_UNSPECIFIED;
318 }
319
320 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
321            1, 0, 0, (SCM parser_smob),
322            "Return a clone of PARSER_SMOB.")
323 {
324   My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
325   My_lily_parser *clone = new My_lily_parser (*parser);
326
327   /* FIXME: should copy scopes too. */
328   return scm_gc_unprotect_object (clone->self_scm ());
329 }
330
331 LY_DEFINE (ly_parser_define, "ly:parser-define",
332            3, 0, 0, (SCM parser_smob, SCM symbol, SCM val),
333            "Bind SYMBOL to VAL in PARSER_SMOB's module.")
334 {
335   My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
336   SCM_ASSERT_TYPE (ly_c_symbol_p (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
337   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");  
338
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   My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
350
351   SCM_ASSERT_TYPE (ly_c_symbol_p (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   My_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 (ly_c_string_p (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 (My_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   paper->parent_ = unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultbookpaper"));
385   return paper;
386 }
387
388
389 Output_def*
390 get_midi (My_lily_parser *parser)
391 {
392   SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
393   Output_def *paper = unsmob_output_def (id);
394   paper = paper ? paper->clone () : new Output_def;
395   paper->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
396   return paper;
397 }
398
399
400 Output_def*
401 get_bookpaper (My_lily_parser *parser)
402 {
403   SCM id = parser->lexer_->lookup_identifier ("$defaultbookpaper");
404   Output_def *paper = unsmob_output_def (id);
405
406   paper = paper ? dynamic_cast<Output_def*> (paper->clone ()) : new Output_def;
407   paper->set_variable (ly_symbol2scm ("is-bookpaper"), SCM_BOOL_T);
408   return paper;
409 }
410
411
412 /* TODO: move this to Scheme?  Why take the parser arg, and all the back
413    & forth between scm and c++? */
414 LY_DEFINE (ly_parser_print_score, "ly:parser-print-score",
415            2, 0, 0,
416            (SCM parser_smob, SCM score_smob),
417            "Print score, i.e., the classic way.")
418 {
419   My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
420   Score *score = unsmob_score (score_smob);
421
422   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
423   SCM_ASSERT_TYPE (score, score_smob, SCM_ARG2, __FUNCTION__, "score");
424
425   SCM header = ly_c_module_p (score->header_) ? score->header_
426     : parser->lexer_->lookup_identifier ("$globalheader");
427   
428   File_name outname (parser->output_basename_);
429   int *c = &parser->book_count_;
430   if (*c)
431     outname.base_ += "-" + to_string (*c);
432   (*c)++;
433
434   SCM os = scm_makfrom0str (outname.to_string ().to_str0 ());
435   SCM bookpaper = get_bookpaper (parser)->self_scm ();
436   for (int i = 0; i < score->defs_.size (); i++)
437     default_rendering (score->music_, score->defs_[i]->self_scm (),
438                        bookpaper,
439                        header, os);
440
441   if (score->defs_.is_empty ())
442     {
443       Output_def *paper = get_paper (parser);
444       default_rendering (score->music_, paper->self_scm (),
445                          get_bookpaper (parser)->self_scm (),
446                          header, os);
447       scm_gc_unprotect_object (paper->self_scm ());
448     }
449   return SCM_UNSPECIFIED;
450 }
451
452
453 LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
454            2, 0, 0, (SCM parser, SCM names),
455            "Replace current note names in @var{parser}. "
456            "@var{names} is an alist of symbols.  "
457            "This only has effect if the current mode is notes.")
458 {
459   My_lily_parser *p = unsmob_my_lily_parser (parser);
460   SCM_ASSERT_TYPE(p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
461
462   if (p->lexer_->is_note_state ())
463     {
464       p->lexer_->pop_state ();
465       p->lexer_->push_note_state (alist_to_hashq (names));
466     }
467
468   return SCM_UNSPECIFIED;
469 }
470
471 LY_DEFINE (ly_parser_print_book, "ly:parser-print-book",
472            2, 0, 0, (SCM parser_smob, SCM book_smob),
473            "Print book.")
474 {
475   My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
476   Book *book = unsmob_book (book_smob);
477   Output_def *bp = unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultbookpaper"));
478   
479   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "Lilypond parser");
480   SCM_ASSERT_TYPE (book, book_smob, SCM_ARG2, __FUNCTION__, "Book");
481   
482   /*  ugh. changing argument.*/
483   book->bookpaper_ = bp;
484   
485   File_name outname (parser->output_basename_);
486   int *c = &parser->book_count_;
487   if (*c)
488     outname.base_ += "-" + to_string (*c);
489   (*c)++;
490
491   Output_def *paper = get_paper (parser);
492
493   Paper_book* pb = book->process (outname.to_string (), paper);
494
495   pb->output (outname.to_string ());
496   
497   scm_gc_unprotect_object (paper->self_scm ());
498   scm_gc_unprotect_object (pb->self_scm ());
499
500   return SCM_UNSPECIFIED;
501 }
502