]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-parser.cc
release: 1.1.15
[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, Duration d)
157 {
158   Simultaneous_music*v = new Request_chord;
159   v->set_spot (here_input ());
160
161   Note_req* n = new Note_req;
162   n->pitch_ = tonic;
163   n->duration_ = d;
164   v->add_music (n);
165
166   for (int i = 0; i < add_arr_p->size (); i++)
167     {
168       Musical_pitch p = tonic;
169       Musical_pitch q = (*add_arr_p)[i];
170       // duh, c7 should mean <c bes>
171       if (q.notename_i_ == 6)
172         q.accidental_i_--;
173       p.transpose (q);
174       (*add_arr_p)[i] = p;
175     }
176   add_arr_p->sort (Musical_pitch::compare);
177   for (int i = 0; i < sub_arr_p->size (); i++)
178     {
179       Musical_pitch p = tonic;
180       Musical_pitch q = (*add_arr_p)[i];
181       // duh, c7 should mean <c bes>
182       if (q.notename_i_ == 6)
183         q.accidental_i_--;
184       p.transpose (q);
185       (*sub_arr_p)[i] = p;
186     }
187   sub_arr_p->sort (Musical_pitch::compare);
188
189   Musical_pitch third (2);
190   Musical_pitch mthird (2, -1);
191   Musical_pitch missing;
192   missing = tonic;
193   missing.transpose (third);
194
195   Musical_pitch p;
196   p = tonic;
197   p.transpose (third);
198   p.transpose (mthird);
199
200   /*
201    must have minimum at 5 (3 is added automatically as missing)
202    */
203   if (!add_arr_p->size ())
204     add_arr_p->push (p);
205   else if ((add_arr_p->top () < p) && (add_arr_p->top ().notename_i_ != p.notename_i_))
206     add_arr_p->push (p);
207   add_arr_p->sort (Musical_pitch::compare);
208
209   Array<Musical_pitch> triads;
210   triads.push (third);   // c e 
211   triads.push (mthird);  // d f 
212   triads.push (mthird);  // e g 
213   triads.push (third);   // f a 
214   triads.push (third);   // g b 
215   triads.push (mthird);  // a c 
216   triads.push (mthird);  // b d 
217
218   /*
219     if first addition is 4, assume sus4 and don't add third implicitely
220    */
221   Musical_pitch sus (3);
222   sus.transpose (tonic);
223   if (add_arr_p->size ())
224     if ((*add_arr_p)[0] == sus)
225       missing.transpose (mthird);
226
227   /*
228    add missing triads
229    */
230   for (int i = 0; i < add_arr_p->size (); i++)
231     {
232       Musical_pitch p = (*add_arr_p)[i];
233       if (p > missing)
234         while (p > missing)
235           {
236             if (p.notename_i_ != missing.notename_i_)
237               {
238                 if ((missing.notename_i_ - tonic.notename_i_ + 7) % 7 == 6)
239                   {
240                     Musical_pitch special_seven = missing;
241                     Musical_pitch lower (0, -1);
242                     special_seven.transpose (lower);
243                     add_arr_p->insert (special_seven, i++);
244                   }
245                 else
246                   add_arr_p->insert (missing, i++);
247               }
248             missing.transpose (triads[(missing.notename_i_ - tonic.notename_i_ + 7) % 7]);
249           }
250       else if (p.notename_i_ == missing.notename_i_)
251         missing.transpose (triads[(missing.notename_i_ - tonic.notename_i_ + 7) % 7]);
252       else
253         i++;
254     }
255
256   /*
257    add all that aren't subtracted
258    */
259   for (int i = 0; i < add_arr_p->size (); i++)
260     {
261       Musical_pitch p = (*add_arr_p)[i];
262       Note_req* n = new Note_req;
263       n->pitch_ = p;
264       n->duration_ = d;
265       for (int j = 0; j < sub_arr_p->size (); j++)
266         {
267           if (p == (*sub_arr_p)[j])
268             {
269               delete n;
270               n = 0;
271               break;
272             }
273         }
274       if (n)
275         v->add_music (n);
276     }
277
278   v->set_spot (here_input ());
279   return v;
280 }
281
282 Simultaneous_music *
283 My_lily_parser::get_note_element (Note_req *rq, Duration * duration_p)
284 {
285   Simultaneous_music*v = new Request_chord;
286   v->set_spot (here_input ());
287
288   v->add_music (rq);
289
290   rq->duration_ = *duration_p;
291   rq->set_spot (here_input ());
292   delete duration_p ;
293   return v;
294 }
295
296
297 /*
298   UGH.
299  */
300 Array<Request*>*
301 My_lily_parser::get_parens_request (int t)
302 {
303   Array<Request*>& reqs = *new Array<Request*>;
304   switch (t)
305     {
306     case '~':
307       reqs.push (new Tie_req);
308       break;
309
310       /* fall through */
311     case '[':
312     case ']':
313       {
314         if (!abbrev_beam_type_i_)
315           {
316             reqs.push (new Beam_req);
317           }
318         else
319           {
320             Abbreviation_beam_req* a = new Abbreviation_beam_req;
321             a->type_i_ = abbrev_beam_type_i_;
322             if (t==']')
323               abbrev_beam_type_i_ = 0;
324             reqs.push (a);
325           }
326       }
327       break;
328
329     case '>':
330     case '!':
331     case '<':
332       reqs.push (new Span_dynamic_req);
333       break;
334
335     case ')':
336     case '(':
337       {
338         reqs.push (new Slur_req);
339       }
340       break;
341     default:
342       assert (false);
343       break;
344     }
345
346   switch (t)
347     {
348     case '<':
349     case '>':
350     case '(':
351     case '[':
352       dynamic_cast<Span_req*> (reqs[0])->spantype_ = START;
353       break;
354       
355     case '!':
356     case ')':
357     case ']':
358       dynamic_cast<Span_req*> (reqs[0])->spantype_ = STOP;
359       break;
360
361     default:
362       break;
363     }
364
365   for (int i = 0; i < reqs.size (); i++)
366     if (dynamic_cast<Span_dynamic_req*> (reqs[i]))
367       {
368         Span_dynamic_req* s_l= dynamic_cast<Span_dynamic_req*> (reqs[i]);
369         s_l->dynamic_dir_ = (t == '<') ? UP:DOWN;
370       }
371
372   // ugh? don't we do this in the parser too?
373   reqs[0]->set_spot (here_input());
374   return &reqs;
375 }
376
377 void
378 My_lily_parser::add_requests (Simultaneous_music*v)
379 {
380   for (int i = 0; i < pre_reqs.size(); i++)
381     {
382       v->add_music (pre_reqs[i]);
383     }
384   pre_reqs.clear();
385   for (int i = 0; i <post_reqs.size(); i++)
386     {
387       v->add_music (post_reqs[i]);
388     }
389
390   post_reqs.clear();
391 }
392
393 Input
394 My_lily_parser::pop_spot()
395 {
396   return define_spot_array_.pop();
397 }
398
399 Input
400 My_lily_parser::here_input() const
401 {
402   Source_file * f_l= lexer_p_->source_file_l();
403   return Input (f_l, here_ch_C());
404 }
405
406 Paper_def*
407 My_lily_parser::default_paper_p ()
408 {
409   Identifier *id = lexer_p_->lookup_identifier ("$defaultpaper");
410   return id ? id->access_content_Paper_def (true) : new Paper_def ;
411 }
412
413 Midi_def*
414 My_lily_parser::default_midi_p ()
415 {
416   Identifier *id = lexer_p_->lookup_identifier ("$defaultmidi");
417   return id ? id->access_content_Midi_def (true) : new Midi_def ;
418 }
419