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