]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-parser.cc
release: 1.1.13
[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--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7        Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include "my-lily-parser.hh"
11 #include "my-lily-lexer.hh"
12 #include "debug.hh"
13 #include "main.hh"
14 #include "music-list.hh"
15 #include "musical-request.hh"
16 #include "command-request.hh"
17 #include "parser.hh"
18 #include "scope.hh"
19 #include "file-results.hh"
20 #include "midi-def.hh"
21 #include "paper-def.hh"
22 #include "identifier.hh"
23
24 My_lily_parser::My_lily_parser (Sources * source_l)
25 {
26   first_b_ = true;
27   source_l_ = source_l;
28   lexer_p_ = 0;
29   abbrev_beam_type_i_ = 0;
30   default_duration_.durlog_i_ = 2;
31   default_abbrev_i_ = 0;
32   error_level_i_ = 0;
33   extender_req = 0;
34   fatal_error_i_ = 0;
35   default_header_p_ =0;
36 }
37
38 My_lily_parser::~My_lily_parser()
39 {
40   delete lexer_p_;
41   delete default_header_p_;
42 }
43
44
45
46 void
47 My_lily_parser::set_version_check (bool ig)
48 {
49   ignore_version_b_ = ig;
50 }
51
52 void
53 My_lily_parser::parse_file (String init, String s)
54 {
55   lexer_p_ = new My_lily_lexer;
56   init_str_ = init;
57   lexer_p_->main_input_str_ = s;
58
59   *mlog << _ ("Parsing...");
60
61   init_parse_b_ = false;
62   set_yydebug (!monitor->silent_b ("Parser") && check_debug);
63   lexer_p_->new_input (init, source_l_);
64   do_yyparse ();
65
66   if (!define_spot_array_.empty())
67     {
68       warning (_ ("braces don't match"));
69       error_level_i_ = 1;
70     }
71
72   inclusion_global_array = lexer_p_->filename_str_arr_;
73 }
74
75 void
76 My_lily_parser::remember_spot()
77 {
78   define_spot_array_.push (here_input());
79 }
80
81 char const *
82 My_lily_parser::here_ch_C() const
83 {
84   return lexer_p_->here_ch_C();
85 }
86
87 void
88 My_lily_parser::parser_error (String s)
89 {
90   here_input().error (s);
91   if (fatal_error_i_)
92     exit (fatal_error_i_);
93   error_level_i_ = 1;
94   exit_status_i_ = 1;
95 }
96
97 void
98 My_lily_parser::set_abbrev_beam (int type_i)
99 {
100   abbrev_beam_type_i_ = type_i;
101 }
102
103
104 void
105 My_lily_parser::set_last_duration (Duration const *d)
106 {
107   default_duration_ = *d;
108
109   /* 
110      forget plet part,
111      but keep sticky plet factor within plet brackets
112     */  
113   default_duration_.plet_ = plet_;
114 }
115
116
117 Simultaneous_music*
118 My_lily_parser::get_word_element (String s, Duration * duration_p)
119 {
120   Simultaneous_music* velt_p = new Request_chord;
121
122   Lyric_req* lreq_p = new Lyric_req;
123   lreq_p ->text_str_ = s;
124   lreq_p->duration_ = *duration_p;
125   lreq_p->set_spot (here_input());
126
127   velt_p->add_music (lreq_p);
128
129   delete  duration_p;
130   return velt_p;
131 }
132
133
134 Simultaneous_music *
135 My_lily_parser::get_rest_element (String s,  Duration * duration_p)
136 {
137   Simultaneous_music* velt_p = new Request_chord;
138   velt_p->set_spot (here_input());
139
140   if (s=="s")
141     { /* Space */
142       Skip_req * skip_p = new Skip_req;
143       skip_p->duration_ = *duration_p;
144
145       skip_p->set_spot (here_input());
146       velt_p->add_music (skip_p);
147     }
148   else
149     {
150       Rest_req * rest_req_p = new Rest_req;
151       rest_req_p->duration_ = *duration_p;
152       rest_req_p->set_spot (here_input());
153
154       velt_p->add_music (rest_req_p);
155     }
156
157   delete duration_p;
158   return velt_p;
159 }
160
161 Simultaneous_music *
162 My_lily_parser::get_chord (Musical_pitch tonic, Array<Musical_pitch>* add_arr_p, Array<Musical_pitch>* sub_arr_p, Duration d)
163 {
164   Simultaneous_music*v = new Request_chord;
165   v->set_spot (here_input ());
166
167   Note_req* n = new Note_req;
168   n->pitch_ = tonic;
169   n->duration_ = d;
170   v->add_music (n);
171
172   for (int i = 0; i < add_arr_p->size (); i++)
173     {
174       Musical_pitch p = tonic;
175       Musical_pitch q = (*add_arr_p)[i];
176       // duh, c7 should mean <c bes>
177       if (q.notename_i_ == 6)
178         q.accidental_i_--;
179       p.transpose (q);
180       (*add_arr_p)[i] = p;
181     }
182   add_arr_p->sort (Musical_pitch::compare);
183   for (int i = 0; i < sub_arr_p->size (); i++)
184     {
185       Musical_pitch p = tonic;
186       Musical_pitch q = (*add_arr_p)[i];
187       // duh, c7 should mean <c bes>
188       if (q.notename_i_ == 6)
189         q.accidental_i_--;
190       p.transpose (q);
191       (*sub_arr_p)[i] = p;
192     }
193   sub_arr_p->sort (Musical_pitch::compare);
194
195   Musical_pitch third (2);
196   Musical_pitch mthird (2, -1);
197   Musical_pitch missing;
198   missing = tonic;
199   missing.transpose (third);
200
201   Musical_pitch p;
202   p = tonic;
203   p.transpose (third);
204   p.transpose (mthird);
205
206   /*
207    must have minimum at 5 (3 is added automatically as missing)
208    */
209   if (!add_arr_p->size ())
210     add_arr_p->push (p);
211   else if ((add_arr_p->top () < p) && (add_arr_p->top ().notename_i_ != p.notename_i_))
212     add_arr_p->push (p);
213   add_arr_p->sort (Musical_pitch::compare);
214
215   Array<Musical_pitch> triads;
216   triads.push (third);   // c e 
217   triads.push (mthird);  // d f 
218   triads.push (mthird);  // e g 
219   triads.push (third);   // f a 
220   // 7 always seems means 7-...
221   triads.push (third);   // g b 
222 //  triads.push (mthird);   // g bes
223   triads.push (mthird);  // a c 
224   triads.push (mthird);  // b d 
225
226   /*
227     if first addition is 4, assume sus4 and don't add third implicitely
228    */
229   Musical_pitch sus (3);
230   sus.transpose (tonic);
231   if (add_arr_p->size ())
232     if ((*add_arr_p)[0] == sus)
233       missing.transpose (mthird);
234
235   /*
236    add missing triads
237    */
238   for (int i = 0; i < add_arr_p->size (); i++)
239     {
240       Musical_pitch p = (*add_arr_p)[i];
241       if (p > missing)
242         while (p > missing)
243           {
244             if (p.notename_i_ != missing.notename_i_)
245               {
246                 if ((missing.notename_i_ - tonic.notename_i_ + 7) % 7 == 6)
247                   {
248                     Musical_pitch special_seven = missing;
249                     Musical_pitch lower (0, -1);
250                     special_seven.transpose (lower);
251                     add_arr_p->insert (special_seven, i++);
252                   }
253                 else
254                   add_arr_p->insert (missing, i++);
255               }
256             missing.transpose (triads[(missing.notename_i_ - tonic.notename_i_ + 7) % 7]);
257           }
258       else
259         i++;
260     }
261
262   /*
263    add all that aren't subtracted
264    */
265   for (int i = 0; i < add_arr_p->size (); i++)
266     {
267       Musical_pitch p = (*add_arr_p)[i];
268       Note_req* n = new Note_req;
269       n->pitch_ = p;
270       n->duration_ = d;
271       for (int j = 0; j < sub_arr_p->size (); j++)
272         {
273           if (p == (*sub_arr_p)[j])
274             {
275               delete n;
276               n = 0;
277               break;
278             }
279         }
280       if (n)
281         v->add_music (n);
282     }
283
284   v->set_spot (here_input ());
285   return v;
286 }
287
288 Simultaneous_music *
289 My_lily_parser::get_note_element (Note_req *rq, Duration * duration_p)
290 {
291   Simultaneous_music*v = new Request_chord;
292   v->set_spot (here_input ());
293
294   v->add_music (rq);
295
296   // too bad parser reads (default) duration via member access,
297   // this hack will do for now..
298   if (abbrev_beam_type_i_)
299     {
300       assert (!duration_p->plet_b ());
301       duration_p->set_plet (1, 2);
302     }
303   rq->duration_ = *duration_p;
304   rq->set_spot (here_input ());
305   delete duration_p ;
306   return v;
307 }
308
309
310 /*
311   UGH.
312  */
313 Array<Request*>*
314 My_lily_parser::get_parens_request (int t)
315 {
316   Array<Request*>& reqs = *new Array<Request*>;
317   switch (t)
318     {
319     case '~':
320       reqs.push (new Tie_req);
321       break;
322     case BEAMPLET:
323     case MAEBTELP:
324       {
325         Plet_req* p = new Plet_req;
326         p->plet_i_ = plet_.type_i_;
327         reqs.push (p);
328       }
329       /* fall through */
330     case '[':
331     case ']':
332       {
333         if (!abbrev_beam_type_i_)
334           {
335             reqs.push (new Beam_req);
336           }
337         else
338           {
339             Abbreviation_beam_req* a = new Abbreviation_beam_req;
340             a->type_i_ = abbrev_beam_type_i_;
341             if (t==']')
342               abbrev_beam_type_i_ = 0;
343             reqs.push (a);
344           }
345       }
346       break;
347
348     case '>':
349     case '!':
350     case '<':
351       reqs.push (new Span_dynamic_req);
352       break;
353
354     case PLET:  
355     case TELP:
356       {
357         Plet_req* p = new Plet_req;
358         p->plet_i_ = plet_.type_i_;
359         reqs.push (p);
360       }
361       break;
362     case ')':
363     case '(':
364       {
365         reqs.push (new Slur_req);
366       }
367       break;
368     default:
369       assert (false);
370       break;
371     }
372
373   switch (t)
374     {
375     case BEAMPLET:
376       dynamic_cast<Span_req*> (reqs.top ())->spantype = Span_req::START;
377       /* fall through */
378     case '<':
379     case '>':
380     case '(':
381     case '[':
382     case PLET:
383       dynamic_cast<Span_req*> (reqs.top ())->spantype = Span_req::START;
384       break;
385     case MAEBTELP:
386       dynamic_cast<Span_req*> (reqs.top ())->spantype = Span_req::STOP;
387       /* fall through */
388     case '!':
389     case ')':
390     case ']':
391       dynamic_cast<Span_req*> (reqs[0])->spantype = Span_req::STOP;
392       break;
393
394     default:
395       break;
396     }
397
398   for (int i = 0; i < reqs.size (); i++)
399     if (dynamic_cast<Span_dynamic_req*> (reqs[i]))
400       {
401         Span_dynamic_req* s_l= dynamic_cast<Span_dynamic_req*> (reqs[i]);
402         s_l->dynamic_dir_ = (t == '<') ? UP:DOWN;
403       }
404
405   // ugh? don't we do this in the parser too?
406   reqs[0]->set_spot (here_input());
407   return &reqs;
408 }
409
410 void
411 My_lily_parser::add_requests (Simultaneous_music*v)
412 {
413   for (int i = 0; i < pre_reqs.size(); i++)
414     {
415       v->add_music (pre_reqs[i]);
416     }
417   pre_reqs.clear();
418   for (int i = 0; i <post_reqs.size(); i++)
419     {
420       v->add_music (post_reqs[i]);
421     }
422
423   post_reqs.clear();
424 }
425
426 Input
427 My_lily_parser::pop_spot()
428 {
429   return define_spot_array_.pop();
430 }
431
432 Input
433 My_lily_parser::here_input() const
434 {
435   Source_file * f_l= lexer_p_->source_file_l();
436   return Input (f_l, here_ch_C());
437 }
438
439 Paper_def*
440 My_lily_parser::default_paper_p ()
441 {
442   Identifier *id = lexer_p_->lookup_identifier ("$defaultpaper");
443   return id ? id->access_content_Paper_def (true) : new Paper_def ;
444 }
445
446 Midi_def*
447 My_lily_parser::default_midi_p ()
448 {
449   Identifier *id = lexer_p_->lookup_identifier ("$defaultmidi");
450   return id ? id->access_content_Midi_def (true) : new Midi_def ;
451 }
452