]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-parser.cc
* scm/output-gnome.scm: New file.
[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 "book-paper-def.hh"
12 #include "file-name.hh"
13 #include "file-path.hh"
14 #include "lily-version.hh"
15 #include "ly-module.hh"
16 #include "ly-smobs.icc"
17 #include "main.hh"
18 #include "my-lily-lexer.hh"
19 #include "my-lily-parser.hh"
20 #include "paper-def.hh"
21 #include "parray.hh"
22 #include "parser.hh"
23 #include "scm-hash.hh"
24 #include "score.hh"
25 #include "source.hh"
26 #include "string.hh"
27 #include "warn.hh"
28
29 My_lily_parser::My_lily_parser (Sources *sources)
30 {
31   book_count_ = 0;
32   score_count_ = 0;
33   lexer_ = 0;
34   sources_ = sources;
35   default_duration_ = Duration (2,0);
36   error_level_ = 0;
37   last_beam_start_ = SCM_EOL;
38   header_ = SCM_EOL;
39
40   header_ = ly_make_anonymous_module (false);
41   smobify_self ();
42 }
43
44 My_lily_parser::My_lily_parser (My_lily_parser const &src)
45 {
46   book_count_ = src.book_count_;
47   score_count_ = src.score_count_;
48   lexer_ = src.lexer_;
49   sources_ = src.sources_;
50   default_duration_ = src.default_duration_;
51   error_level_ = src.error_level_;
52   last_beam_start_ = src.last_beam_start_;
53   header_ = src.header_;
54
55   smobify_self ();
56 }
57
58 My_lily_parser::~My_lily_parser ()
59 {
60   delete lexer_;
61 }
62
63 IMPLEMENT_SMOBS (My_lily_parser);
64 IMPLEMENT_TYPE_P (My_lily_parser, "ly:my-lily-parser?");
65 IMPLEMENT_DEFAULT_EQUAL_P (My_lily_parser);
66
67 SCM
68 My_lily_parser::mark_smob (SCM s)
69 {
70   My_lily_parser *parser = (My_lily_parser*) ly_cdr (s);
71   return parser->header_;
72 }
73
74 int
75 My_lily_parser::print_smob (SCM s, SCM port, scm_print_state*)
76 {
77   scm_puts ("#<my_lily_parser ", port);
78   My_lily_parser *parser = (My_lily_parser*) ly_cdr (s);
79   (void) parser;
80   scm_puts (" >", port);
81   return 1;
82 }
83
84
85 /* Process one .ly file, or book.  */
86 void
87 My_lily_parser::parse_file (String init, String name, String out_name)
88 {
89   lexer_ = new My_lily_lexer (sources_);
90   output_basename_ = out_name;
91   
92   lexer_->main_input_name_ = name;
93
94   progress_indication (_ ("Parsing..."));
95   progress_indication ("\n");
96
97   set_yydebug (0);
98   lexer_->new_input (init, sources_);
99
100   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
101      OUT_FILE (unless IN_FILE redefines output file name).  */
102   do_yyparse ();
103   
104   if (!define_spots_.is_empty ())
105     {
106       define_spots_.top ().warning (_ ("Braces don't match"));
107       error_level_ = 1;
108     }
109
110   error_level_ = error_level_ | lexer_->error_level_;
111 }
112
113 void
114 My_lily_parser::parse_string (String ly_code)
115 {
116   My_lily_lexer *parent = lexer_;
117   lexer_ = (parent == 0 ? new My_lily_lexer (sources_)
118             : new My_lily_lexer (*parent));
119
120   lexer_->main_input_name_ = "<string>";
121   lexer_->main_input_b_ = true;
122
123   set_yydebug (0);
124   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
125   do_yyparse ();
126   
127   if (!define_spots_.is_empty ())
128     {
129       define_spots_.top ().warning (_ ("Braces don't match"));
130       error_level_ = 1;
131     }
132
133   error_level_ = error_level_ | lexer_->error_level_;
134
135   if (parent != 0)
136     {
137       parent->keytable_ = lexer_->keytable_;
138       parent->encoding_ = lexer_->encoding_;
139       parent->chordmodifier_tab_ = lexer_->chordmodifier_tab_;
140       parent->pitchname_tab_stack_ = lexer_->pitchname_tab_stack_;
141       parent->sources_ = lexer_->sources_;
142       parent->scopes_ = lexer_->scopes_;
143       parent->error_level_ = lexer_->error_level_; 
144       parent->main_input_b_ = lexer_->main_input_b_;
145     }
146 }
147
148 void
149 My_lily_parser::push_spot ()
150 {
151   define_spots_.push (here_input ());
152 }
153
154 char const *
155 My_lily_parser::here_str0 () const
156 {
157   return lexer_->here_str0 ();
158 }
159
160 void
161 My_lily_parser::parser_error (String s)
162 {
163   here_input ().error (s);
164   error_level_ = 1;
165 }
166
167 Input
168 My_lily_parser::pop_spot ()
169 {
170   return define_spots_.pop ();
171 }
172
173 Input
174 My_lily_parser::here_input () const
175 {
176   /*
177     Parsing looks ahead , so we really want the previous location of the
178     lexer, not lexer_->here_input ().
179   */
180   
181   /*
182     Actually, that gets very icky when there are white space, because
183     the line-numbers are all wrong.  Let's try the character before
184     the current token. That gets the right result for note/duration
185     stuff, but doesn't mess up for errors in the 1st token of the
186     line.
187   */
188   Input hi (lexer_->here_input ());
189
190   char const * bla = hi.defined_str0_;
191   if (hi.line_number () > 1
192       || hi.column_number () > 1)
193     bla --;
194   
195   return Input (hi.source_file_, bla);
196 }
197
198
199 /****************************************************************/
200
201
202 /*
203   junkme?
204  */
205 bool store_locations_global_b;
206
207 /* Do not append `!' suffix, since 1st argument is not modified. */
208 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click", 1, 0, 0,
209           (SCM what),
210           "Set the options for Point-and-click source specials output. The\n"
211 "argument is a symbol.  Possible options are @code{none} (no source specials),\n"
212 "@code{line} and @code{line-column}")
213 {
214   /* UGH. */
215   SCM val = SCM_BOOL_F;
216   if (ly_symbol2scm ("line-column") == what)
217     val = ly_scheme_function ("line-column-location");
218   else if (what == ly_symbol2scm ("line"))
219     val = ly_scheme_function ("line-location");
220
221   scm_module_define (global_lily_module, ly_symbol2scm ("point-and-click"),
222                      val);
223   store_locations_global_b = ly_c_procedure_p (val);
224   return SCM_UNSPECIFIED;
225 }
226
227 LY_DEFINE (ly_parse_file, "ly:parse-file",
228            1, 0, 0,
229            (SCM name),
230            "Parse a single @code{.ly} file.  "
231            "Upon failure, throw @code{ly-file-failed} key.")
232 {
233   SCM_ASSERT_TYPE (ly_c_string_p (name), name, SCM_ARG1, __FUNCTION__, "string");
234   char const *file = SCM_STRING_CHARS (name);
235   char const *extensions[] = {"ly", "", 0};
236   String file_name = global_path.find (file, extensions);
237   
238   /* By default, use base name of input file for output file name,
239      write output to cwd; do not use root and directory parts of input
240      file name.  */
241   File_name out_file_name (file_name);
242   if (file_name != "-")
243     out_file_name.ext_ = output_format_global;
244   out_file_name.root_ = "";
245   out_file_name.dir_ = "";
246
247   if (!output_name_global.is_empty ())
248     out_file_name = File_name (output_name_global);
249   
250   String init;
251   if (!init_name_global.is_empty ())
252     init = init_name_global;
253   else
254     init = "init.ly";
255
256   String out_file = out_file_name.to_string ();
257
258   if (init.length () && global_path.find (init).is_empty ())
259     {
260       warning (_f ("can't find init file: `%s'", init));
261       warning (_f ("(search path: `%s')",
262                    global_path.to_string ().to_str0 ()));
263       exit (2);
264     }
265
266   if ((file_name != "-") && global_path.find (file_name).is_empty ())
267     {
268       warning (_f ("can't find file: `%s'", file_name));
269       scm_throw (ly_symbol2scm ("ly-file-failed"),
270                  scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
271     }
272   else
273     {
274       Sources sources;
275       sources.set_path (&global_path);
276   
277       progress_indication (_f ("Now processing `%s'", file_name.to_str0 ()));
278       progress_indication ("\n");
279
280       My_lily_parser *parser = new My_lily_parser (&sources);
281       scm_module_define (global_lily_module, ly_symbol2scm ("parser"),
282                          parser->self_scm ());
283       parser->parse_file (init, file_name, out_file);
284
285       bool error = parser->error_level_;
286       parser = 0;
287       if (error)
288         /* TODO: pass renamed input file too.  */
289         scm_throw (ly_symbol2scm ("ly-file-failed"),
290                    scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
291     }
292   return SCM_UNSPECIFIED;
293 }
294
295 LY_DEFINE (ly_parse_string, "ly:parse-string",
296            1, 0, 0,
297            (SCM ly_code),
298            "Parse the string LY_CODE.  "
299            "Upon failure, throw @code{ly-file-failed} key.")
300 {
301   SCM_ASSERT_TYPE (ly_c_string_p (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
302   
303   Sources sources;
304   sources.set_path (&global_path);
305   My_lily_parser *parser = new My_lily_parser (&sources);
306   scm_module_define (global_lily_module, ly_symbol2scm ("parser"),
307                      parser->self_scm ());
308   parser->parse_string (ly_scm2string (ly_code));
309   parser = 0;
310   
311   return SCM_UNSPECIFIED;
312 }
313
314 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
315            1, 0, 0, 
316            (SCM parser_smob),
317            "Return a clone of PARSER_SMOB.")
318 {
319   My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
320   My_lily_parser *clone = new My_lily_parser (*parser);
321   return scm_gc_unprotect_object (clone->self_scm ());
322 }
323
324 LY_DEFINE(ly_parser_define, "ly:parser-define",
325           3, 0, 0, 
326           (SCM parser_smob, SCM symbol, SCM val),
327           "Bind SYMBOL to VAL in PARSER_SMOB's module.")
328 {
329   My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
330   SCM_ASSERT_TYPE (ly_c_symbol_p (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
331   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");  
332
333
334   parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
335   return SCM_UNSPECIFIED;
336 }
337 LY_DEFINE(ly_parser_lookup, "ly:parser-lookup",
338           2, 0, 0, 
339           (SCM parser_smob, SCM symbol),
340           "Lookup @var{symbol} in @var{parser_smob}'s module. Undefined is '().")
341 {
342   My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
343
344   SCM_ASSERT_TYPE (ly_c_symbol_p (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
345   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");  
346
347   SCM val= parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
348   if (val != SCM_UNDEFINED)
349     return val;
350   else
351     return SCM_EOL;
352 }
353
354 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
355            2, 0, 0,
356            (SCM parser_smob, SCM ly_code),
357            "Parse the string LY_CODE with PARSER_SMOB."
358            "Upon failure, throw @code{ly-file-failed} key.")
359 {
360
361   My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
362
363   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
364   SCM_ASSERT_TYPE (ly_c_string_p (ly_code), ly_code, SCM_ARG2, __FUNCTION__, "string");
365   
366   parser->parse_string (ly_scm2string (ly_code));
367   
368   return SCM_UNSPECIFIED;
369 }
370
371 Music_output_def*
372 get_paper (My_lily_parser *parser)
373 {
374   SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
375   Music_output_def *paper = unsmob_music_output_def (id);
376   return paper ? paper->clone () : new Paper_def;
377 }
378
379
380 Book_paper_def*
381 get_bookpaper (My_lily_parser *parser)
382 {
383   SCM id = parser->lexer_->lookup_identifier ("$defaultbookpaper");
384   Book_paper_def *paper = unsmob_book_paper_def (id);
385   return paper->clone ();
386 }
387
388
389 /*
390   TODO: move this to Scheme? Why take the parser arg, and all the back
391   & forth between scm and c++?
392  */
393 LY_DEFINE (ly_parser_print_score, "ly:parser-print-score",
394            2, 0, 0,
395            (SCM parser_smob, SCM score_smob),
396            "Print score, i.e., the classic way.")
397 {
398   My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
399   Score *score = unsmob_score (score_smob);
400
401   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
402   SCM_ASSERT_TYPE (score, score_smob, SCM_ARG2, __FUNCTION__, "score");
403
404   SCM header = is_module (score->header_) ? score->header_
405     : parser->header_.to_SCM ();
406   
407   File_name outname (parser->output_basename_);
408   int *c = &parser->book_count_;
409   if (*c)
410     outname.base_ += "-" + to_string (*c);
411   (*c)++;
412
413   SCM os = scm_makfrom0str (outname.to_string ().to_str0 ());
414   for (int i = 0; i < score->defs_.size (); i++)
415     default_rendering (score->music_, score->defs_[i]->self_scm (),
416                        get_bookpaper (parser)->self_scm (),
417                        header, os);
418
419   if (score->defs_.is_empty ())
420     {
421       Music_output_def *paper = get_paper (parser);
422       default_rendering (score->music_, paper->self_scm (),
423                          get_bookpaper (parser)->self_scm (),
424                          header, os);
425       scm_gc_unprotect_object (paper->self_scm ());
426     }
427   return SCM_UNDEFINED;
428 }
429
430 LY_DEFINE (ly_parser_print_book, "ly:parser-print-book",
431            2, 0, 0,
432            (SCM parser_smob, SCM book_smob),
433            "Print book.")
434 {
435   My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
436   Book *book = unsmob_book (book_smob);
437   Book_paper_def *bp = unsmob_book_paper_def (parser->lexer_->lookup_identifier ("$defaultbookpaper"));
438   
439   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "Lilypond parser");
440   SCM_ASSERT_TYPE (book, book_smob, SCM_ARG2, __FUNCTION__, "Book");
441   
442   /*  ugh. changing argument.*/
443   book->bookpaper_ = bp;
444   
445   SCM header = parser->header_;
446   File_name outname (parser->output_basename_);
447   int *c = &parser->book_count_;
448   if (*c)
449     outname.base_ += "-" + to_string (*c);
450   (*c)++;
451   Music_output_def *paper = get_paper (parser);
452   book->process (outname.to_string (), paper, header);
453   scm_gc_unprotect_object (paper->self_scm ());
454   return SCM_UNDEFINED;
455 }