]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-parser.cc
patch::: 1.1.15.jcn1: pats
[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
111 Simultaneous_music*
112 My_lily_parser::get_word_element (String s, Duration * duration_p)
113 {
114   Simultaneous_music* velt_p = new Request_chord;
115
116   Lyric_req* lreq_p = new Lyric_req;
117   lreq_p ->text_str_ = s;
118   lreq_p->duration_ = *duration_p;
119   lreq_p->set_spot (here_input());
120
121   velt_p->add_music (lreq_p);
122
123   delete  duration_p;
124   return velt_p;
125 }
126
127
128 Simultaneous_music *
129 My_lily_parser::get_rest_element (String s,  Duration * duration_p)
130 {
131   Simultaneous_music* velt_p = new Request_chord;
132   velt_p->set_spot (here_input());
133
134   if (s=="s")
135     { /* Space */
136       Skip_req * skip_p = new Skip_req;
137       skip_p->duration_ = *duration_p;
138
139       skip_p->set_spot (here_input());
140       velt_p->add_music (skip_p);
141     }
142   else
143     {
144       Rest_req * rest_req_p = new Rest_req;
145       rest_req_p->duration_ = *duration_p;
146       rest_req_p->set_spot (here_input());
147
148       velt_p->add_music (rest_req_p);
149     }
150
151   delete duration_p;
152   return velt_p;
153 }
154
155 Simultaneous_music *
156 My_lily_parser::get_chord (Musical_pitch tonic, Array<Musical_pitch>* add_arr_p, Array<Musical_pitch>* sub_arr_p, Musical_pitch* inversion_p, Duration d)
157 {
158   Simultaneous_music*v = new Request_chord;
159   v->set_spot (here_input ());
160
161   for (int i = 0; i < add_arr_p->size (); i++)
162     {
163       Musical_pitch p = tonic;
164       Musical_pitch q = (*add_arr_p)[i];
165       // duh, c7 should mean <c bes>
166       if (q.notename_i_ == 6)
167         q.accidental_i_--;
168       p.transpose (q);
169       (*add_arr_p)[i] = p;
170     }
171   add_arr_p->sort (Musical_pitch::compare);
172   for (int i = 0; i < sub_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       (*sub_arr_p)[i] = p;
181     }
182   sub_arr_p->sort (Musical_pitch::compare);
183
184   Musical_pitch third (2);
185   Musical_pitch mthird (2, -1);
186   Musical_pitch missing;
187   missing = tonic;
188   missing.transpose (third);
189
190   Musical_pitch p;
191   p = tonic;
192   p.transpose (third);
193   p.transpose (mthird);
194
195   /*
196    must have minimum at 5 (3 is added automatically as missing)
197    */
198   if (!add_arr_p->size ())
199     add_arr_p->push (p);
200   else if ((add_arr_p->top () < p) && (add_arr_p->top ().notename_i_ != p.notename_i_))
201     add_arr_p->push (p);
202   add_arr_p->sort (Musical_pitch::compare);
203
204   Array<Musical_pitch> triads;
205   triads.push (third);   // c e 
206   triads.push (mthird);  // d f 
207   triads.push (mthird);  // e g 
208   triads.push (third);   // f a 
209   triads.push (third);   // g b 
210   triads.push (mthird);  // a c 
211   triads.push (mthird);  // b d 
212
213   /*
214     if first addition is 4, assume sus4 and don't add third implicitely
215    */
216   Musical_pitch sus (3);
217   sus.transpose (tonic);
218   if (add_arr_p->size ())
219     if ((*add_arr_p)[0] == sus)
220       missing.transpose (mthird);
221
222   /*
223    add missing triads
224    */
225   for (int i = 0; i < add_arr_p->size (); i++)
226     {
227       Musical_pitch p = (*add_arr_p)[i];
228       if (p > missing)
229         while (p > missing)
230           {
231             if (p.notename_i_ != missing.notename_i_)
232               {
233                 if ((missing.notename_i_ - tonic.notename_i_ + 7) % 7 == 6)
234                   {
235                     Musical_pitch special_seven = missing;
236                     Musical_pitch lower (0, -1);
237                     special_seven.transpose (lower);
238                     add_arr_p->insert (special_seven, i++);
239                   }
240                 else
241                   add_arr_p->insert (missing, i++);
242               }
243             missing.transpose (triads[(missing.notename_i_ - tonic.notename_i_ + 7) % 7]);
244           }
245       else if (p.notename_i_ == missing.notename_i_)
246         missing.transpose (triads[(missing.notename_i_ - tonic.notename_i_ + 7) % 7]);
247       else
248         i++;
249     }
250
251   /*
252     add tonic
253    */
254   if (!add_arr_p->size () || ((*add_arr_p)[0] != tonic))
255     add_arr_p->insert (tonic, 0);
256
257   Array<Musical_pitch> pitch_arr;
258   /*
259    add all that aren't subtracted
260    */
261   for (int i = 0; i < add_arr_p->size (); i++)
262     {
263       Musical_pitch p = (*add_arr_p)[i];
264       int j = 0;
265       for (; j < sub_arr_p->size (); j++)
266         if (p == (*sub_arr_p)[j])
267           break;
268       if (j == sub_arr_p->size ())
269         pitch_arr.push (p);
270     }
271
272   if (inversion_p)
273     {
274       int i = 0;
275       for (; i < pitch_arr.size (); i++)
276         if ((pitch_arr[i].notename_i_ == inversion_p->notename_i_)
277           && (pitch_arr[i].accidental_i_ == inversion_p->accidental_i_))
278           break;
279       if (i == pitch_arr.size ())
280         warning (_ ("invalid inversion pitch (not part of chord)"));
281       else
282         {
283           Array<Musical_pitch> pitches;
284           Musical_pitch last (0, 0, -5);
285           for (int j = 0; j < pitch_arr.size (); j++)
286             {
287               Musical_pitch p = pitch_arr[(j + i) % pitch_arr.size ()];
288               if (p < last)
289                 {
290                   p.octave_i_ = last.octave_i_;
291                   if (p < last)
292                     p.octave_i_++;
293                 }
294               pitches.push (p);
295               last = p;
296             }
297           pitch_arr = pitches;
298         }
299       delete inversion_p;
300     }
301
302   for (int i = 0; i < pitch_arr.size (); i++)
303     {
304       Musical_pitch p = pitch_arr[i];
305       Note_req* n = new Note_req;
306       n->pitch_ = p;
307       n->duration_ = d;
308       v->add_music (n);
309     }
310
311   v->set_spot (here_input ());
312   return v;
313 }
314
315 Simultaneous_music *
316 My_lily_parser::get_note_element (Note_req *rq, Duration * duration_p)
317 {
318   Simultaneous_music*v = new Request_chord;
319   v->set_spot (here_input ());
320
321   v->add_music (rq);
322
323   rq->duration_ = *duration_p;
324   rq->set_spot (here_input ());
325   delete duration_p ;
326   return v;
327 }
328
329
330 /*
331   UGH.
332  */
333 Array<Request*>*
334 My_lily_parser::get_parens_request (int t)
335 {
336   Array<Request*>& reqs = *new Array<Request*>;
337   switch (t)
338     {
339     case '~':
340       reqs.push (new Tie_req);
341       break;
342
343       /* fall through */
344     case '[':
345     case ']':
346       {
347         if (!abbrev_beam_type_i_)
348           {
349             reqs.push (new Beam_req);
350           }
351         else
352           {
353             Abbreviation_beam_req* a = new Abbreviation_beam_req;
354             a->type_i_ = abbrev_beam_type_i_;
355             if (t==']')
356               abbrev_beam_type_i_ = 0;
357             reqs.push (a);
358           }
359       }
360       break;
361
362     case '>':
363     case '!':
364     case '<':
365       reqs.push (new Span_dynamic_req);
366       break;
367
368     case ')':
369     case '(':
370       {
371         reqs.push (new Slur_req);
372       }
373       break;
374     default:
375       assert (false);
376       break;
377     }
378
379   switch (t)
380     {
381     case '<':
382     case '>':
383     case '(':
384     case '[':
385       dynamic_cast<Span_req*> (reqs[0])->spantype_ = START;
386       break;
387       
388     case '!':
389     case ')':
390     case ']':
391       dynamic_cast<Span_req*> (reqs[0])->spantype_ = 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