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