]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-parser.cc
* scm/ly-from-scheme.scm (read-lily-expression): A variable
[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 (false);
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   /*
181     Actually, that gets very icky when there are white space, because
182     the line-numbers are all wrong.  Let's try the character before
183     the current token. That gets the right result for note/duration
184     stuff, but doesn't mess up for errors in the 1st token of the
185     line.
186   */
187   Input hi (lexer_->here_input ());
188
189   char const * bla = hi.defined_str0_;
190   if (hi.line_number () > 1
191       || hi.column_number () > 1)
192     bla --;
193   
194   return Input (hi.source_file_, bla);
195 }
196
197
198 /****************************************************************/
199
200
201 /*
202   junkme?
203  */
204 bool store_locations_global_b;
205
206 /* Do not append `!' suffix, since 1st argument is not modified. */
207 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click", 1, 0, 0,
208           (SCM what),
209           "Set the options for Point-and-click source specials output. The\n"
210 "argument is a symbol.  Possible options are @code{none} (no source specials),\n"
211 "@code{line} and @code{line-column}")
212 {
213   /* UGH. */
214   SCM val = SCM_BOOL_F;
215   if (ly_symbol2scm ("line-column") == what)
216     val = ly_scheme_function ("line-column-location");
217   else if (what == ly_symbol2scm ("line"))
218     val = ly_scheme_function ("line-location");
219
220   scm_module_define (global_lily_module, ly_symbol2scm ("point-and-click"),
221                      val);
222   store_locations_global_b = ly_c_procedure_p (val);
223   return SCM_UNSPECIFIED;
224 }
225
226 LY_DEFINE (ly_parse_file, "ly:parse-file",
227            1, 0, 0,
228            (SCM name),
229            "Parse a single @code{.ly} file.  "
230            "Upon failure, throw @code{ly-file-failed} key.")
231 {
232   SCM_ASSERT_TYPE (ly_c_string_p (name), name, SCM_ARG1, __FUNCTION__, "string");
233   char const *file = SCM_STRING_CHARS (name);
234   char const *extensions[] = {"ly", "", 0};
235   String file_name = global_path.find (file, extensions);
236   
237   /* By default, use base name of input file for output file name,
238      write output to cwd; do not use root and directory parts of input
239      file name.  */
240   File_name out_file_name (file_name);
241   if (file_name != "-")
242     out_file_name.ext_ = output_format_global;
243   out_file_name.root_ = "";
244   out_file_name.dir_ = "";
245
246   if (!output_name_global.is_empty ())
247     out_file_name = File_name (output_name_global);
248   
249   String init;
250   if (!init_name_global.is_empty ())
251     init = init_name_global;
252   else
253     init = "init.ly";
254
255   String out_file = out_file_name.to_string ();
256
257   if (init.length () && global_path.find (init).is_empty ())
258     {
259       warning (_f ("can't find init file: `%s'", init));
260       warning (_f ("(search path: `%s')",
261                    global_path.to_string ().to_str0 ()));
262       exit (2);
263     }
264
265   if ((file_name != "-") && global_path.find (file_name).is_empty ())
266     {
267       warning (_f ("can't find file: `%s'", file_name));
268       scm_throw (ly_symbol2scm ("ly-file-failed"),
269                  scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
270     }
271   else
272     {
273       Sources sources;
274       sources.set_path (&global_path);
275   
276       progress_indication (_f ("Now processing `%s'", file_name.to_str0 ()));
277       progress_indication ("\n");
278
279       My_lily_parser *parser = new My_lily_parser (&sources);
280       scm_module_define (global_lily_module, ly_symbol2scm ("parser"),
281                          parser->self_scm ());
282       parser->parse_file (init, file_name, out_file);
283
284       bool error = parser->error_level_;
285       parser = 0;
286       if (error)
287         /* TODO: pass renamed input file too.  */
288         scm_throw (ly_symbol2scm ("ly-file-failed"),
289                    scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
290     }
291   return SCM_UNSPECIFIED;
292 }
293
294 LY_DEFINE (ly_parse_string, "ly:parse-string",
295            1, 0, 0,
296            (SCM ly_code),
297            "Parse the string LY_CODE.  "
298            "Upon failure, throw @code{ly-file-failed} key.")
299 {
300   SCM_ASSERT_TYPE (ly_c_string_p (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
301   
302   Sources sources;
303   sources.set_path (&global_path);
304   My_lily_parser *parser = new My_lily_parser (&sources);
305   scm_module_define (global_lily_module, ly_symbol2scm ("parser"),
306                      parser->self_scm ());
307   parser->parse_string (ly_scm2string (ly_code));
308   parser = 0;
309   
310   return SCM_UNSPECIFIED;
311 }
312
313 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
314            1, 0, 0, 
315            (SCM parser_smob),
316            "Return a clone of PARSER_SMOB.")
317 {
318   My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
319   My_lily_parser *clone = new My_lily_parser (*parser);
320   return scm_gc_unprotect_object (clone->self_scm ());
321 }
322
323 LY_DEFINE(ly_parser_define, "ly:parser-define",
324           3, 0, 0, 
325           (SCM parser_smob, SCM symbol, SCM val),
326           "Bind SYMBOL to VAL in PARSER_SMOB's module.")
327 {
328   SCM_ASSERT_TYPE (ly_c_symbol_p (symbol), symbol, SCM_ARG1, __FUNCTION__, "symbol");
329   My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
330   parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
331   return SCM_UNSPECIFIED;
332 }
333
334 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
335            2, 0, 0,
336            (SCM parser_smob, SCM ly_code),
337            "Parse the string LY_CODE with PARSER_SMOB."
338            "Upon failure, throw @code{ly-file-failed} key.")
339 {
340 #if 0
341   SCM_ASSERT_TYPE (ly_c_parser_p (parser), music, SCM_ARG1, __FUNCTION__, "parser");
342 #endif
343   SCM_ASSERT_TYPE (ly_c_string_p (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
344
345 #if 1
346   My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
347   parser->parse_string (ly_scm2string (ly_code));
348 #else
349   My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
350   My_lily_parser *clone = new My_lily_parser (*parser);
351   clone->parse_string (ly_scm2string (ly_code));
352   clone = 0;
353 #endif
354   
355   return SCM_UNSPECIFIED;
356 }
357
358 Music_output_def*
359 get_paper (My_lily_parser *parser)
360 {
361   SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
362   Music_output_def *paper = unsmob_music_output_def (id);
363   return paper ? paper->clone () : new Paper_def;
364 }
365
366 LY_DEFINE (ly_parser_print_score, "ly:parser-print-score",
367            2, 0, 0,
368            (SCM parser_smob, SCM score_smob),
369            "Print score, i.e., the classic way.")
370 {
371 #if 0
372   SCM_ASSERT_TYPE (ly_c_parser_p (parser), music, SCM_ARG1, __FUNCTION__, "parser");
373   SCM_ASSERT_TYPE (ly_c_music_p (music), music, SCM_ARG1, __FUNCTION__, "music");
374 #endif
375   My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
376   Score *score = unsmob_score (score_smob);
377
378   SCM header = is_module (score->header_) ? score->header_
379     : parser->header_.to_SCM ();
380   
381   File_name outname (parser->output_basename_);
382   int *c = &parser->book_count_;
383   if (*c)
384     outname.base_ += "-" + to_string (*c);
385   (*c)++;
386
387   SCM os = scm_makfrom0str (outname.to_string ().to_str0 ());
388   for (int i = 0; i < score->defs_.size (); i++)
389     default_rendering (score->music_, score->defs_[i]->self_scm (), header,
390                        os);
391
392   if (score->defs_.is_empty ())
393     {
394       Music_output_def *paper = get_paper (parser);
395       default_rendering (score->music_, paper->self_scm (), header, os);
396       scm_gc_unprotect_object (paper->self_scm ());
397     }
398   return SCM_UNDEFINED;
399 }
400
401 LY_DEFINE (ly_parser_print_book, "ly:parser-print-book",
402            2, 0, 0,
403            (SCM parser_smob, SCM book_smob),
404            "Print book.")
405 {
406 #if 0
407   SCM_ASSERT_TYPE (ly_c_parser_p (parser_smob), parser_smob, SCM_ARG1, __FUNCTION__, "parser_smob");
408   SCM_ASSERT_TYPE (ly_c_music_p (book_smob), book_smob, SCM_ARG1, __FUNCTION__, "book_smob");
409 #endif
410   My_lily_parser *parser = unsmob_my_lily_parser (parser_smob);
411   Book *book = unsmob_book (book_smob);
412   
413   SCM header = parser->header_;
414   File_name outname (parser->output_basename_);
415   int *c = &parser->book_count_;
416   if (*c)
417     outname.base_ += "-" + to_string (*c);
418   (*c)++;
419   Music_output_def *paper = get_paper (parser);
420   book->process (outname.to_string (), paper, header);
421   scm_gc_unprotect_object (paper->self_scm ());
422   return SCM_UNDEFINED;
423 }