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