2 my-lily-parser.cc -- implement My_lily_parser
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
10 #include "my-lily-parser.hh"
11 #include "my-lily-lexer.hh"
14 #include "music-list.hh"
15 #include "musical-request.hh"
16 #include "command-request.hh"
19 #include "file-results.hh"
20 #include "midi-def.hh"
21 #include "paper-def.hh"
22 #include "identifier.hh"
24 My_lily_parser::My_lily_parser (Sources * source_l)
29 abbrev_beam_type_i_ = 0;
30 default_duration_.durlog_i_ = 2;
31 default_abbrev_i_ = 0;
38 My_lily_parser::~My_lily_parser()
41 delete default_header_p_;
47 My_lily_parser::set_version_check (bool ig)
49 ignore_version_b_ = ig;
53 My_lily_parser::parse_file (String init, String s)
55 lexer_p_ = new My_lily_lexer;
57 lexer_p_->main_input_str_ = s;
59 *mlog << _ ("Parsing...");
61 init_parse_b_ = false;
62 set_yydebug (!monitor->silent_b ("Parser") && check_debug);
63 lexer_p_->new_input (init, source_l_);
66 if (!define_spot_array_.empty())
68 warning (_ ("braces don't match"));
72 inclusion_global_array = lexer_p_->filename_str_arr_;
76 My_lily_parser::remember_spot()
78 define_spot_array_.push (here_input());
82 My_lily_parser::here_ch_C() const
84 return lexer_p_->here_ch_C();
88 My_lily_parser::parser_error (String s)
90 here_input().error (s);
92 exit (fatal_error_i_);
98 My_lily_parser::set_abbrev_beam (int type_i)
100 abbrev_beam_type_i_ = type_i;
105 My_lily_parser::set_last_duration (Duration const *d)
107 default_duration_ = *d;
111 but keep sticky plet factor within plet brackets
113 default_duration_.plet_ = plet_;
118 My_lily_parser::get_word_element (String s, Duration * duration_p)
120 Simultaneous_music* velt_p = new Request_chord;
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());
127 velt_p->add_music (lreq_p);
135 My_lily_parser::get_rest_element (String s, Duration * duration_p)
137 Simultaneous_music* velt_p = new Request_chord;
138 velt_p->set_spot (here_input());
142 Skip_req * skip_p = new Skip_req;
143 skip_p->duration_ = *duration_p;
145 skip_p->set_spot (here_input());
146 velt_p->add_music (skip_p);
150 Rest_req * rest_req_p = new Rest_req;
151 rest_req_p->duration_ = *duration_p;
152 rest_req_p->set_spot (here_input());
154 velt_p->add_music (rest_req_p);
162 My_lily_parser::get_chord (Musical_pitch tonic, Array<Musical_pitch>* add_arr_p, Array<Musical_pitch>* sub_arr_p, Duration d)
164 Simultaneous_music*v = new Request_chord;
165 v->set_spot (here_input ());
167 Note_req* n = new Note_req;
172 for (int i = 0; i < add_arr_p->size (); i++)
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)
182 add_arr_p->sort (Musical_pitch::compare);
183 for (int i = 0; i < sub_arr_p->size (); i++)
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)
193 sub_arr_p->sort (Musical_pitch::compare);
195 Musical_pitch third (2);
196 Musical_pitch mthird (2, -1);
197 Musical_pitch missing;
199 missing.transpose (third);
204 p.transpose (mthird);
207 must have minimum at 5 (3 is added automatically as missing)
209 if (!add_arr_p->size ())
211 else if ((add_arr_p->top () < p) && (add_arr_p->top ().notename_i_ != p.notename_i_))
213 add_arr_p->sort (Musical_pitch::compare);
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
227 if first addition is 4, assume sus4 and don't add third implicitely
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);
238 for (int i = 0; i < add_arr_p->size (); i++)
240 Musical_pitch p = (*add_arr_p)[i];
244 if (p.notename_i_ != missing.notename_i_)
246 if ((missing.notename_i_ - tonic.notename_i_ + 7) % 7 == 6)
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++);
254 add_arr_p->insert (missing, i++);
256 missing.transpose (triads[(missing.notename_i_ - tonic.notename_i_ + 7) % 7]);
263 add all that aren't subtracted
265 for (int i = 0; i < add_arr_p->size (); i++)
267 Musical_pitch p = (*add_arr_p)[i];
268 Note_req* n = new Note_req;
271 for (int j = 0; j < sub_arr_p->size (); j++)
273 if (p == (*sub_arr_p)[j])
284 v->set_spot (here_input ());
289 My_lily_parser::get_note_element (Note_req *rq, Duration * duration_p)
291 Simultaneous_music*v = new Request_chord;
292 v->set_spot (here_input ());
296 // too bad parser reads (default) duration via member access,
297 // this hack will do for now..
298 if (abbrev_beam_type_i_)
300 assert (!duration_p->plet_b ());
301 duration_p->set_plet (1, 2);
303 rq->duration_ = *duration_p;
304 rq->set_spot (here_input ());
314 My_lily_parser::get_parens_request (int t)
316 Array<Request*>& reqs = *new Array<Request*>;
320 reqs.push (new Tie_req);
325 Plet_req* p = new Plet_req;
326 p->plet_i_ = plet_.type_i_;
333 if (!abbrev_beam_type_i_)
335 reqs.push (new Beam_req);
339 Abbreviation_beam_req* a = new Abbreviation_beam_req;
340 a->type_i_ = abbrev_beam_type_i_;
342 abbrev_beam_type_i_ = 0;
351 reqs.push (new Span_dynamic_req);
357 Plet_req* p = new Plet_req;
358 p->plet_i_ = plet_.type_i_;
365 reqs.push (new Slur_req);
376 dynamic_cast<Span_req*> (reqs.top ())->spantype = Span_req::START;
383 dynamic_cast<Span_req*> (reqs.top ())->spantype = Span_req::START;
386 dynamic_cast<Span_req*> (reqs.top ())->spantype = Span_req::STOP;
391 dynamic_cast<Span_req*> (reqs[0])->spantype = Span_req::STOP;
398 for (int i = 0; i < reqs.size (); i++)
399 if (dynamic_cast<Span_dynamic_req*> (reqs[i]))
401 Span_dynamic_req* s_l= dynamic_cast<Span_dynamic_req*> (reqs[i]);
402 s_l->dynamic_dir_ = (t == '<') ? UP:DOWN;
405 // ugh? don't we do this in the parser too?
406 reqs[0]->set_spot (here_input());
411 My_lily_parser::add_requests (Simultaneous_music*v)
413 for (int i = 0; i < pre_reqs.size(); i++)
415 v->add_music (pre_reqs[i]);
418 for (int i = 0; i <post_reqs.size(); i++)
420 v->add_music (post_reqs[i]);
427 My_lily_parser::pop_spot()
429 return define_spot_array_.pop();
433 My_lily_parser::here_input() const
435 Source_file * f_l= lexer_p_->source_file_l();
436 return Input (f_l, here_ch_C());
440 My_lily_parser::default_paper_p ()
442 Identifier *id = lexer_p_->lookup_identifier ("$defaultpaper");
443 return id ? id->access_content_Paper_def (true) : new Paper_def ;
447 My_lily_parser::default_midi_p ()
449 Identifier *id = lexer_p_->lookup_identifier ("$defaultmidi");
450 return id ? id->access_content_Midi_def (true) : new Midi_def ;