]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-parser.cc
* input/regression/newaddlyrics.ly: 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 "file-path.hh"
12 #include "lily-version.hh"
13 #include "ly-module.hh"
14 #include "ly-smobs.icc"
15 #include "main.hh"
16 #include "my-lily-lexer.hh"
17 #include "my-lily-parser.hh"
18 #include "paper-def.hh"
19 #include "parray.hh"
20 #include "parser.hh"
21 #include "scm-hash.hh"
22 #include "score.hh"
23 #include "source.hh"
24 #include "string.hh"
25 #include "warn.hh"
26
27 My_lily_parser::My_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   header_ = SCM_EOL;
37
38   header_ = ly_make_anonymous_module ();
39   smobify_self ();
40 }
41
42 My_lily_parser::~My_lily_parser ()
43 {
44   delete lexer_;
45 }
46
47 IMPLEMENT_SMOBS (My_lily_parser);
48 IMPLEMENT_TYPE_P (My_lily_parser, "ly:my-lily-parser?");
49 IMPLEMENT_DEFAULT_EQUAL_P (My_lily_parser);
50
51 SCM
52 My_lily_parser::mark_smob (SCM s)
53 {
54   My_lily_parser *parser = (My_lily_parser*) ly_cdr (s);
55   return parser->header_;
56 }
57
58 int
59 My_lily_parser::print_smob (SCM s, SCM port, scm_print_state*)
60 {
61   scm_puts ("#<my_lily_parser ", port);
62   My_lily_parser *parser = (My_lily_parser*) ly_cdr (s);
63   (void) parser;
64   scm_puts (" >", port);
65   return 1;
66 }
67
68
69 /* Process one .ly file, or book.  */
70 void
71 My_lily_parser::parse_file (String init, String name, String out_name)
72 {
73   lexer_ = new My_lily_lexer (sources_);
74   output_basename_ = out_name;
75   
76   lexer_->main_input_name_ = name;
77
78   progress_indication (_ ("Parsing..."));
79   progress_indication ("\n");
80
81   set_yydebug (0);
82   lexer_->new_input (init, sources_);
83
84   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
85      OUT_FILE (unless IN_FILE redefines output file name).  */
86   do_yyparse ();
87   
88   if (!define_spots_.is_empty ())
89     {
90       define_spots_.top ().warning (_ ("Braces don't match"));
91       error_level_ = 1;
92     }
93
94   error_level_ = error_level_ | lexer_->error_level_;
95 }
96
97 void
98 My_lily_parser::parse_string (String ly_code)
99 {
100   lexer_ = new My_lily_lexer (sources_);
101   lexer_->main_input_name_ = "<string>";
102   lexer_->main_input_b_ = true;
103
104   set_yydebug (0);
105   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
106   do_yyparse ();
107   
108   if (!define_spots_.is_empty ())
109     {
110       define_spots_.top ().warning (_ ("Braces don't match"));
111       error_level_ = 1;
112     }
113
114   error_level_ = error_level_ | lexer_->error_level_;
115 }
116
117 void
118 My_lily_parser::push_spot ()
119 {
120   define_spots_.push (here_input ());
121 }
122
123 char const *
124 My_lily_parser::here_str0 () const
125 {
126   return lexer_->here_str0 ();
127 }
128
129 void
130 My_lily_parser::parser_error (String s)
131 {
132   here_input ().error (s);
133   error_level_ = 1;
134 }
135
136
137
138 Input
139 My_lily_parser::pop_spot ()
140 {
141   return define_spots_.pop ();
142 }
143
144 Input
145 My_lily_parser::here_input () const
146 {
147   /*
148     Parsing looks ahead , so we really want the previous location of the
149     lexer, not lexer_->here_input ().
150   */
151   /*
152     Actually, that gets very icky when there are white space, because
153     the line-numbers are all wrong.  Let's try the character before
154     the current token. That gets the right result for
155     note/duration stuff, but doesn't mess up for errors in the 1st token of the line. 
156     
157   */
158   Input hi (lexer_->here_input ());
159
160   char const * bla = hi.defined_str0_;
161   if (hi.line_number () > 1
162       || hi.column_number () > 1)
163     bla --;
164   
165   return Input (hi.source_file_, bla);
166 }
167
168
169 /****************************************************************/
170
171
172 /*
173   junkme?
174  */
175 bool store_locations_global_b;
176
177 /*
178   no ! suffix since it doesn't modify 1st argument.
179  */
180 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click", 1, 0, 0,
181           (SCM what),
182           "Set the options for Point-and-click source specials output. The\n"
183 "argument is a symbol.  Possible options are @code{none} (no source specials),\n"
184 "@code{line} and @code{line-column}")
185 {
186   /*
187     UGH.
188    */
189   SCM val = SCM_BOOL_F;
190   if (ly_symbol2scm ("line-column") == what)
191     val = scm_c_eval_string ("line-column-location");
192   else if (what == ly_symbol2scm ("line"))
193     val = scm_c_eval_string ("line-location");
194
195   scm_module_define (global_lily_module, ly_symbol2scm ("point-and-click"), val);
196   store_locations_global_b = ly_c_procedure_p (val);
197   return SCM_UNSPECIFIED;
198 }
199
200
201 /* Distill full input file name from command argument.  PATH describes
202    file name with added default extension, ".ly" if none.  "-" is
203    STDIN.  */
204 Path
205 distill_inname (String str)
206 {
207   Path p = split_path (str);
208   if (str.is_empty () || str == "-")
209     p.base = "-";
210   else
211     {
212       String orig_ext = p.ext;
213       char const *extensions[] = {"ly", "", 0};
214       for (int i = 0; extensions[i]; i++)
215         {
216           p.ext = orig_ext;
217           if (*extensions[i] && !p.ext.is_empty ())
218             p.ext += ".";
219           p.ext += extensions[i];
220           if (!global_path.find (p.to_string ()).is_empty ())
221               break;
222         }
223       /* Reshuffle extension */
224       p = split_path (p.to_string ());
225     }
226   return p;
227 }
228
229 LY_DEFINE (ly_parse_file, "ly:parse-file",
230            1, 0, 0,
231            (SCM name),
232            "Parse a single @code{.ly} file.  "
233            "Upon failure, throw @code{ly-file-failed} key.")
234 {
235   SCM_ASSERT_TYPE (ly_c_string_p (name), name, SCM_ARG1, __FUNCTION__, "string");
236   char const *file = SCM_STRING_CHARS (name);
237   
238   String infile (file);
239   Path inpath = distill_inname (infile);
240   
241   /* By default, use base name of input file for output file name */
242   Path outpath = inpath;
243   if (inpath.to_string () != "-")
244     outpath.ext = output_format_global;
245   
246   /* By default, write output to cwd; do not copy directory part
247      of input file name */
248   outpath.root = "";
249   outpath.dir = "";
250   
251   if (!output_name_global.is_empty ())
252     outpath = split_path (output_name_global);
253   
254   String init;
255   if (!init_name_global.is_empty ())
256     init = init_name_global;
257   else
258     init = "init.ly";
259   
260   String in_file = inpath.to_string ();
261   String out_file = outpath.to_string ();
262
263
264   if (init.length () && global_path.find (init).is_empty ())
265     {
266       warning (_f ("can't find init file: `%s'", init));
267       warning (_f ("(search path: `%s')", global_path.to_string ().to_str0 ()));
268       exit (2);
269     }
270
271   if ((in_file != "-") && global_path.find (in_file).is_empty ())
272     {
273       warning (_f ("can't find file: `%s'", in_file));
274       scm_throw (ly_symbol2scm ("ly-file-failed"), scm_list_1 (scm_makfrom0str (in_file.to_str0 ())));
275     }
276   else
277     {
278       Sources sources;
279       sources.set_path (&global_path);
280   
281       progress_indication (_f ("Now processing `%s'", in_file.to_str0 ()));
282       progress_indication ("\n");
283
284       My_lily_parser *parser = new My_lily_parser (&sources);
285       parser->parse_file (init, in_file, out_file);
286
287       bool error = parser->error_level_;
288       parser = 0;
289       if (error)
290         /* TODO: pass renamed input file too.  */
291         scm_throw (ly_symbol2scm ("ly-file-failed"),
292                    scm_list_1 (scm_makfrom0str (in_file.to_str0 ())));
293     }
294   return SCM_UNSPECIFIED;
295 }
296
297 LY_DEFINE (ly_parse_string, "ly:parse-string",
298            1, 0, 0,
299            (SCM ly_code),
300            "Parse the string LY_CODE.  "
301            "Upon failure, throw @code{ly-file-failed} key.")
302 {
303   SCM_ASSERT_TYPE (ly_c_string_p (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
304   
305   Sources sources;
306   sources.set_path (&global_path);
307   My_lily_parser *parser = new My_lily_parser (&sources);
308   parser->parse_string (ly_scm2string (ly_code));
309   parser = 0;
310   
311   return SCM_UNSPECIFIED;
312 }
313
314 static Music_output_def*
315 get_paper (My_lily_parser *parser)
316 {
317   SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
318   Music_output_def *paper = unsmob_music_output_def (id);
319   return paper ? paper->clone () : new Paper_def;
320 }
321
322 LY_DEFINE (ly_parser_print_score, "ly:parser-print-score",
323            2, 0, 0,
324            (SCM parser_smob, SCM score_smob),
325            "Print score, i.e., the classic way.")
326 {
327 #if 0
328   SCM_ASSERT_TYPE (ly_c_parser_p (parser), music, SCM_ARG1, __FUNCTION__, "parser");
329   SCM_ASSERT_TYPE (ly_c_music_p (music), music, SCM_ARG1, __FUNCTION__, "music");
330 #endif
331   My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
332   Score *score = unsmob_score (score_smob);
333
334   SCM header = is_module (score->header_) ? score->header_
335     : parser->header_.to_SCM ();
336   
337   Path outname = split_path (parser->output_basename_);
338   int *c = &parser->book_count_;
339   if (*c)
340     outname.base += "-" + to_string (*c);
341   (*c)++;
342
343   SCM os = scm_makfrom0str (outname.to_string ().to_str0 ());
344   for (int i = 0; i < score->defs_.size (); i++)
345     default_rendering (score->music_, score->defs_[i]->self_scm (), header,
346                        os);
347
348   if (score->defs_.is_empty ())
349     {
350       Music_output_def *paper = get_paper (parser);
351       default_rendering (score->music_, paper->self_scm (), header, os);
352       scm_gc_unprotect_object (paper->self_scm ());
353     }
354   return SCM_UNDEFINED;
355 }
356
357 LY_DEFINE (ly_parser_print_book, "ly:parser-print-book",
358            2, 0, 0,
359            (SCM parser_smob, SCM book_smob),
360            "Print book.")
361 {
362 #if 0
363   SCM_ASSERT_TYPE (ly_c_parser_p (parser_smob), parser_smob, SCM_ARG1, __FUNCTION__, "parser_smob");
364   SCM_ASSERT_TYPE (ly_c_music_p (book_smob), book_smob, SCM_ARG1, __FUNCTION__, "book_smob");
365 #endif
366   My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
367   Book *book = unsmob_book (book_smob);
368   
369   SCM header = parser->header_;
370   Path outname = split_path (parser->output_basename_);
371   int *c = &parser->book_count_;
372   if (*c)
373     outname.base += "-" + to_string (*c);
374   (*c)++;
375   Music_output_def *paper = get_paper (parser);
376   book->process (outname.to_string (), paper, header);
377   scm_gc_unprotect_object (paper->self_scm ());
378   return SCM_UNDEFINED;
379 }