]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser.cc
(item-event): Add support from TLA. Support
[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 "grob-selector.hh"
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 "lily-lexer.hh"
18 #include "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
27 Lily_parser::Lily_parser (Sources *sources)
28 {
29   book_count_ = 0;
30   score_count_ = 0;
31   lexer_ = 0;
32   sources_ = sources;
33   default_duration_ = Duration (2, 0);
34   error_level_ = 0;
35   last_beam_start_ = SCM_EOL;
36
37   smobify_self ();
38 }
39
40 Lily_parser::Lily_parser (Lily_parser const &src)
41 {
42   book_count_ = src.book_count_;
43   score_count_ = src.score_count_;
44   lexer_ = 0;
45   sources_ = src.sources_;
46   default_duration_ = src.default_duration_;
47   error_level_ = src.error_level_;
48   last_beam_start_ = src.last_beam_start_;
49
50   smobify_self ();
51   if (src.lexer_)
52     lexer_ = new Lily_lexer (*src.lexer_);
53
54   scm_gc_unprotect_object (lexer_->self_scm ());
55 }
56
57 Lily_parser::~Lily_parser ()
58 {
59 }
60
61 #include "ly-smobs.icc"
62
63 IMPLEMENT_SMOBS (Lily_parser);
64 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
65 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
66
67 SCM
68 Lily_parser::mark_smob (SCM s)
69 {
70   Lily_parser *parser = (Lily_parser*) SCM_CELL_WORD_1 (s);
71   return (parser->lexer_) ? parser->lexer_->self_scm () : SCM_EOL;
72 }
73
74 int
75 Lily_parser::print_smob (SCM s, SCM port, scm_print_state*)
76 {
77   scm_puts ("#<my_lily_parser ", port);
78   Lily_parser *parser = (Lily_parser*) SCM_CELL_WORD_1 (s);
79   (void) parser;
80   scm_puts (" >", port);
81   return 1;
82 }
83
84
85 /* Process one .ly file, or book.  */
86 void
87 Lily_parser::parse_file (String init, String name, String out_name)
88 {
89   lexer_ = new Lily_lexer (sources_);
90   scm_gc_unprotect_object (lexer_->self_scm ());
91   // TODO: use $parser 
92   lexer_->set_identifier (ly_symbol2scm ("parser"),
93                           self_scm ());
94   output_basename_ = out_name;
95   
96   lexer_->main_input_name_ = name;
97
98   progress_indication (_ ("Parsing..."));
99   progress_indication ("\n");
100
101   set_yydebug (0);
102
103   lexer_->new_input (init, sources_);
104 #ifdef TWEAK
105   File_name f (name);
106   String s = global_path.find (f.base_ + ".twy");
107   if (s == "")
108     Grob_selector::set_tweaks (SCM_EOL);
109   else
110     {
111       s = gulp_file_to_string (s, false);
112       SCM tweaks = scm_eval_string (scm_makfrom0str (s.to_str0 ()));
113       Grob_selector::set_tweaks (tweaks);
114     }
115 #endif  
116
117   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
118      OUT_FILE (unless IN_FILE redefines output file name).  */
119   do_yyparse ();
120   
121   if (!define_spots_.is_empty ())
122     {
123       define_spots_.top ().warning (_ ("Braces don't match"));
124       error_level_ = 1;
125     }
126
127   error_level_ = error_level_ | lexer_->error_level_;
128   lexer_ = 0;
129 }
130
131 void
132 Lily_parser::parse_string (String ly_code)
133 {
134   Lily_lexer * parent = lexer_;
135   lexer_ = (parent == 0 ? new Lily_lexer (sources_)
136             : new Lily_lexer (*parent));
137   scm_gc_unprotect_object (lexer_->self_scm ());
138
139
140   SCM oldmod = scm_current_module ();
141   scm_set_current_module (scm_car (lexer_->scopes_));
142   
143   // TODO: use $parser 
144   lexer_->set_identifier (ly_symbol2scm ("parser"),
145                           self_scm ());
146   
147   lexer_->main_input_name_ = "<string>";
148   lexer_->main_input_b_ = true;
149
150   set_yydebug (0);
151   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
152   do_yyparse ();
153   
154   if (!define_spots_.is_empty ())
155     {
156       define_spots_.top ().warning (_ ("Braces don't match"));
157       error_level_ = 1;
158     }
159
160   error_level_ = error_level_ | lexer_->error_level_;
161
162   scm_set_current_module (oldmod);
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 (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
251   char const *file = scm_i_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 ("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       scm_gc_unprotect_object (parser->self_scm ());
306       parser = 0;
307       if (error)
308         /* TODO: pass renamed input file too.  */
309         scm_throw (ly_symbol2scm ("ly-file-failed"),
310                    scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
311     }
312   return SCM_UNSPECIFIED;
313 }
314
315 LY_DEFINE (ly_parse_string, "ly:parse-string",
316            1, 0, 0, (SCM ly_code),
317            "Parse the string LY_CODE.  "
318            "Upon failure, throw @code{ly-file-failed} key.")
319 {
320   SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
321   
322   Sources sources;
323   sources.set_path (&global_path);
324   Lily_parser *parser = new Lily_parser (&sources);
325   scm_module_define (global_lily_module, ly_symbol2scm ("parser"),
326                      parser->self_scm ());
327   parser->parse_string (ly_scm2string (ly_code));
328   scm_gc_unprotect_object (parser->self_scm ());
329   parser = 0;
330   
331   return SCM_UNSPECIFIED;
332 }
333
334 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
335            1, 0, 0, (SCM parser_smob),
336            "Return a clone of PARSER_SMOB.")
337 {
338   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
339   Lily_parser *clone = new Lily_parser (*parser);
340
341   /* FIXME: should copy scopes too. */
342   return scm_gc_unprotect_object (clone->self_scm ());
343 }
344
345 LY_DEFINE (ly_parser_define, "ly:parser-define",
346            3, 0, 0, (SCM parser_smob, SCM symbol, SCM val),
347            "Bind SYMBOL to VAL in PARSER_SMOB's module.")
348 {
349   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
350   SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
351   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");  
352
353   parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
354   return SCM_UNSPECIFIED;
355 }
356
357 LY_DEFINE (ly_parser_lookup, "ly:parser-lookup",
358            2, 0, 0, (SCM parser_smob, SCM symbol),
359            "Lookup @var{symbol} in @var{parser_smob}'s module.  "
360            "Undefined is '().")
361 {
362   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
363
364   SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
365   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");  
366
367   SCM val= parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
368   if (val != SCM_UNDEFINED)
369     return val;
370   else
371     return SCM_EOL;
372 }
373
374 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
375            2, 0, 0, (SCM parser_smob, SCM ly_code),
376            "Parse the string LY_CODE with PARSER_SMOB."
377            "Upon failure, throw @code{ly-file-failed} key.")
378 {
379   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
380
381   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
382   SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG2, __FUNCTION__, "string");
383   
384   parser->parse_string (ly_scm2string (ly_code));
385   
386   return SCM_UNSPECIFIED;
387 }
388
389 Output_def*
390 get_layout (Lily_parser *parser)
391 {
392   SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
393   Output_def *layout = unsmob_output_def (id);
394   layout = layout ? layout->clone () : new Output_def;
395   layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
396
397   return layout;
398 }
399
400
401 Output_def*
402 get_midi (Lily_parser *parser)
403 {
404   SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
405   Output_def *layout = unsmob_output_def (id);
406   layout = layout ? layout->clone () : new Output_def;
407   layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
408   return layout;
409 }
410
411
412 Output_def*
413 get_paper (Lily_parser *parser)
414 {
415   SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
416   Output_def *layout = unsmob_output_def (id);
417
418   layout = layout ? dynamic_cast<Output_def*> (layout->clone ()) : new Output_def;
419   layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
420   return layout;
421 }
422
423
424 /* TODO: move this to Scheme?  Why take the parser arg, and all the back
425    & forth between scm and c++? */
426 LY_DEFINE (ly_parser_print_score, "ly:parser-print-score",
427            2, 0, 0,
428            (SCM parser_smob, SCM score_smob),
429            "Print score, i.e., the classic way.")
430 {
431   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
432   Score *score = unsmob_score (score_smob);
433
434   if (score->error_found_)
435     return SCM_UNSPECIFIED;
436   
437   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
438   SCM_ASSERT_TYPE (score, score_smob, SCM_ARG2, __FUNCTION__, "score");
439
440   SCM header = ly_c_module_p (score->header_) ? score->header_
441     : parser->lexer_->lookup_identifier ("$globalheader");
442   
443   File_name outname (parser->output_basename_);
444   int *c = &parser->book_count_;
445   if (*c)
446     outname.base_ += "-" + to_string (*c);
447   (*c)++;
448
449   SCM os = scm_makfrom0str (outname.to_string ().to_str0 ());
450   SCM paper = get_paper (parser)->self_scm ();
451   for (int i = 0; i < score->defs_.size (); i++)
452     default_rendering (score->get_music (), score->defs_[i]->self_scm (),
453                        paper,
454                        header, os);
455
456   if (score->defs_.is_empty ())
457     {
458       Output_def *layout = get_layout (parser);
459       default_rendering (score->get_music(), layout->self_scm (),
460                          get_paper (parser)->self_scm (),
461                          header, os);
462       scm_gc_unprotect_object (layout->self_scm ());
463     }
464   return SCM_UNSPECIFIED;
465 }
466
467
468 LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
469            2, 0, 0, (SCM parser, SCM names),
470            "Replace current note names in @var{parser}. "
471            "@var{names} is an alist of symbols.  "
472            "This only has effect if the current mode is notes.")
473 {
474   Lily_parser *p = unsmob_my_lily_parser (parser);
475   SCM_ASSERT_TYPE(p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
476
477   if (p->lexer_->is_note_state ())
478     {
479       p->lexer_->pop_state ();
480       p->lexer_->push_note_state (alist_to_hashq (names));
481     }
482
483   return SCM_UNSPECIFIED;
484 }
485
486 LY_DEFINE (ly_parser_print_book, "ly:parser-print-book",
487            2, 0, 0, (SCM parser_smob, SCM book_smob),
488            "Print book.")
489 {
490   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
491   Book *book = unsmob_book (book_smob);
492   Output_def *bp = unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultpaper"));
493   
494   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "Lilypond parser");
495   SCM_ASSERT_TYPE (book, book_smob, SCM_ARG2, __FUNCTION__, "Book");
496   
497   /*  ugh. changing argument.*/
498   book->paper_ = bp;
499   
500   File_name outname (parser->output_basename_);
501   int *c = &parser->book_count_;
502   if (*c)
503     outname.base_ += "-" + to_string (*c);
504   (*c)++;
505
506   Output_def *layout = get_layout (parser);
507
508   Paper_book* pb = book->process (outname.to_string (), layout);
509   if (pb)
510     {
511       pb->output (outname.to_string ());
512       scm_gc_unprotect_object (pb->self_scm ());
513     }
514
515   scm_gc_unprotect_object (layout->self_scm ());
516   return SCM_UNSPECIFIED;
517 }
518