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