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