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 default_duration_.durlog_i_ = 2;
36 My_lily_parser::~My_lily_parser()
39 delete default_header_p_;
45 My_lily_parser::set_version_check (bool ig)
47 ignore_version_b_ = ig;
51 My_lily_parser::parse_file (String init, String s)
53 lexer_p_ = new My_lily_lexer;
55 lexer_p_->main_input_str_ = s;
57 *mlog << _ ("Parsing...");
59 init_parse_b_ = false;
60 set_yydebug (!monitor->silent_b ("Parser") && check_debug);
61 lexer_p_->new_input (init, source_l_);
64 if (!define_spot_array_.empty())
66 warning (_ ("braces don't match"));
70 inclusion_global_array = lexer_p_->filename_str_arr_;
74 My_lily_parser::remember_spot()
76 define_spot_array_.push (here_input());
80 My_lily_parser::here_ch_C() const
82 return lexer_p_->here_ch_C();
86 My_lily_parser::parser_error (String s)
88 here_input().error (s);
90 exit (fatal_error_i_);
98 My_lily_parser::set_last_duration (Duration const *d)
100 default_duration_ = *d;
105 My_lily_parser::get_word_element (String s, Duration * duration_p)
107 Simultaneous_music* velt_p = new Request_chord;
109 Lyric_req* lreq_p = new Lyric_req;
110 lreq_p ->text_str_ = s;
111 lreq_p->duration_ = *duration_p;
112 lreq_p->set_spot (here_input());
114 velt_p->add_music (lreq_p);
122 My_lily_parser::get_rest_element (String s, Duration * duration_p)
124 Simultaneous_music* velt_p = new Request_chord;
125 velt_p->set_spot (here_input());
129 Skip_req * skip_p = new Skip_req;
130 skip_p->duration_ = *duration_p;
132 skip_p->set_spot (here_input());
133 velt_p->add_music (skip_p);
137 Rest_req * rest_req_p = new Rest_req;
138 rest_req_p->duration_ = *duration_p;
139 rest_req_p->set_spot (here_input());
141 velt_p->add_music (rest_req_p);
149 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)
151 Simultaneous_music*v = new Request_chord;
152 v->set_spot (here_input ());
154 for (int i = 0; i < add_arr_p->size (); i++)
156 Musical_pitch p = tonic;
157 Musical_pitch q = (*add_arr_p)[i];
158 // duh, c7 should mean <c bes>
159 if (q.notename_i_ == 6)
164 add_arr_p->sort (Musical_pitch::compare);
165 for (int i = 0; i < sub_arr_p->size (); i++)
167 Musical_pitch p = tonic;
168 Musical_pitch q = (*add_arr_p)[i];
169 // duh, c7 should mean <c bes>
170 if (q.notename_i_ == 6)
175 sub_arr_p->sort (Musical_pitch::compare);
177 Musical_pitch third (2);
178 Musical_pitch mthird (2, -1);
179 Musical_pitch missing;
181 missing.transpose (third);
186 p.transpose (mthird);
189 must have minimum at 5 (3 is added automatically as missing)
191 if (!add_arr_p->size ())
193 else if ((add_arr_p->top () < p) && (add_arr_p->top ().notename_i_ != p.notename_i_))
195 add_arr_p->sort (Musical_pitch::compare);
197 Array<Musical_pitch> triads;
198 triads.push (third); // c e
199 triads.push (mthird); // d f
200 triads.push (mthird); // e g
201 triads.push (third); // f a
202 triads.push (third); // g b
203 triads.push (mthird); // a c
204 triads.push (mthird); // b d
207 if first addition is 4, assume sus4 and don't add third implicitely
209 Musical_pitch sus (3);
210 sus.transpose (tonic);
211 if (add_arr_p->size ())
212 if ((*add_arr_p)[0] == sus)
213 missing.transpose (mthird);
218 for (int i = 0; i < add_arr_p->size (); i++)
220 Musical_pitch p = (*add_arr_p)[i];
224 if (p.notename_i_ != missing.notename_i_)
226 if ((missing.notename_i_ - tonic.notename_i_ + 7) % 7 == 6)
228 Musical_pitch special_seven = missing;
229 Musical_pitch lower (0, -1);
230 special_seven.transpose (lower);
231 add_arr_p->insert (special_seven, i++);
234 add_arr_p->insert (missing, i++);
236 missing.transpose (triads[(missing.notename_i_ - tonic.notename_i_ + 7) % 7]);
238 else if (p.notename_i_ == missing.notename_i_)
239 missing.transpose (triads[(missing.notename_i_ - tonic.notename_i_ + 7) % 7]);
247 if (!add_arr_p->size () || ((*add_arr_p)[0] != tonic))
248 add_arr_p->insert (tonic, 0);
250 Array<Musical_pitch> pitch_arr;
252 add all that aren't subtracted
254 for (int i = 0; i < add_arr_p->size (); i++)
256 Musical_pitch p = (*add_arr_p)[i];
258 for (; j < sub_arr_p->size (); j++)
259 if (p == (*sub_arr_p)[j])
261 if (j == sub_arr_p->size ())
268 for (; i < pitch_arr.size (); i++)
269 if ((pitch_arr[i].notename_i_ == inversion_p->notename_i_)
270 && (pitch_arr[i].accidental_i_ == inversion_p->accidental_i_))
272 if (i == pitch_arr.size ())
273 warning (_ ("invalid inversion pitch (not part of chord)"));
276 Array<Musical_pitch> pitches;
277 Musical_pitch last (0, 0, -5);
278 for (int j = 0; j < pitch_arr.size (); j++)
280 Musical_pitch p = pitch_arr[(j + i) % pitch_arr.size ()];
283 p.octave_i_ = last.octave_i_;
295 for (int i = 0; i < pitch_arr.size (); i++)
297 Musical_pitch p = pitch_arr[i];
298 Note_req* n = new Note_req;
304 v->set_spot (here_input ());
309 My_lily_parser::get_note_element (Note_req *rq, Duration * duration_p)
311 Simultaneous_music*v = new Request_chord;
312 v->set_spot (here_input ());
316 rq->duration_ = *duration_p;
317 rq->set_spot (here_input ());
327 My_lily_parser::get_parens_request (int t)
329 Array<Request*>& reqs = *new Array<Request*>;
333 reqs.push (new Tie_req);
340 reqs.push (new Beam_req);
347 reqs.push (new Span_dynamic_req);
353 reqs.push (new Slur_req);
367 dynamic_cast<Span_req*> (reqs[0])->spantype_ = START;
373 dynamic_cast<Span_req*> (reqs[0])->spantype_ = STOP;
380 for (int i = 0; i < reqs.size (); i++)
381 if (dynamic_cast<Span_dynamic_req*> (reqs[i]))
383 Span_dynamic_req* s_l= dynamic_cast<Span_dynamic_req*> (reqs[i]);
384 s_l->dynamic_dir_ = (t == '<') ? UP:DOWN;
387 // ugh? don't we do this in the parser too?
388 reqs[0]->set_spot (here_input());
393 My_lily_parser::add_requests (Simultaneous_music*v)
395 for (int i = 0; i < pre_reqs.size(); i++)
397 v->add_music (pre_reqs[i]);
400 for (int i = 0; i <post_reqs.size(); i++)
402 v->add_music (post_reqs[i]);
409 My_lily_parser::pop_spot()
411 return define_spot_array_.pop();
415 My_lily_parser::here_input() const
417 Source_file * f_l= lexer_p_->source_file_l();
418 return Input (f_l, here_ch_C());
422 My_lily_parser::default_paper_p ()
424 Identifier *id = lexer_p_->lookup_identifier ("$defaultpaper");
425 return id ? id->access_content_Paper_def (true) : new Paper_def ;
429 My_lily_parser::default_midi_p ()
431 Identifier *id = lexer_p_->lookup_identifier ("$defaultmidi");
432 return id ? id->access_content_Midi_def (true) : new Midi_def ;