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>
9 #include "my-lily-parser.hh"
10 #include "my-lily-lexer.hh"
13 #include "music-list.hh"
14 #include "musical-request.hh"
15 #include "command-request.hh"
18 #include "file-results.hh"
19 #include "midi-def.hh"
20 #include "paper-def.hh"
21 #include "identifier.hh"
23 My_lily_parser::My_lily_parser (Sources * source_l)
28 abbrev_beam_type_i_ = 0;
29 default_duration_.durlog_i_ = 2;
30 default_abbrev_i_ = 0;
37 My_lily_parser::~My_lily_parser()
40 delete default_header_p_;
46 My_lily_parser::set_version_check (bool ig)
48 ignore_version_b_ = ig;
52 My_lily_parser::parse_file (String init, String s)
54 lexer_p_ = new My_lily_lexer;
56 lexer_p_->main_input_str_ = s;
58 *mlog << _ ("Parsing...");
60 init_parse_b_ = false;
61 set_yydebug (!monitor->silent_b ("Parser") && check_debug);
62 lexer_p_->new_input (init, source_l_);
65 if (!define_spot_array_.empty())
67 warning (_ ("braces don't match"));
71 inclusion_global_array = lexer_p_->filename_str_arr_;
75 My_lily_parser::remember_spot()
77 define_spot_array_.push (here_input());
81 My_lily_parser::here_ch_C() const
83 return lexer_p_->here_ch_C();
87 My_lily_parser::parser_error (String s)
89 here_input().error (s);
91 exit (fatal_error_i_);
97 My_lily_parser::set_abbrev_beam (int type_i)
99 abbrev_beam_type_i_ = type_i;
104 My_lily_parser::set_last_duration (Duration const *d)
106 default_duration_ = *d;
110 but keep sticky plet factor within plet brackets
112 default_duration_.plet_ = plet_;
117 My_lily_parser::get_word_element (String s, Duration * duration_p)
119 Simultaneous_music* velt_p = new Request_chord;
121 Lyric_req* lreq_p = new Lyric_req;
122 lreq_p ->text_str_ = s;
123 lreq_p->duration_ = *duration_p;
124 lreq_p->set_spot (here_input());
126 velt_p->add_music (lreq_p);
134 My_lily_parser::get_rest_element (String s, Duration * duration_p)
136 Simultaneous_music* velt_p = new Request_chord;
137 velt_p->set_spot (here_input());
141 Skip_req * skip_p = new Skip_req;
142 skip_p->duration_ = *duration_p;
144 skip_p->set_spot (here_input());
145 velt_p->add_music (skip_p);
149 Rest_req * rest_req_p = new Rest_req;
150 rest_req_p->duration_ = *duration_p;
151 rest_req_p->set_spot (here_input());
153 velt_p->add_music (rest_req_p);
161 My_lily_parser::get_chord (Musical_pitch tonic, Array<Musical_pitch>* add_arr_p, Array<Musical_pitch>* sub_arr_p, Duration d)
163 Simultaneous_music*v = new Request_chord;
164 v->set_spot (here_input ());
166 Note_req* n = new Note_req;
171 for (int i = 0; i < add_arr_p->size (); i++)
173 Musical_pitch p = tonic;
174 p.transpose ((*add_arr_p)[i]);
177 add_arr_p->sort (Musical_pitch::compare);
178 for (int i = 0; i < sub_arr_p->size (); i++)
180 Musical_pitch p = tonic;
181 p.transpose ((*sub_arr_p)[i]);
184 sub_arr_p->sort (Musical_pitch::compare);
187 third.notename_i_ = 2;
189 Musical_pitch mthird;
190 mthird.notename_i_ = 2;
191 mthird.accidental_i_ = -1;
193 Musical_pitch missing;
195 missing.transpose (third);
200 p.transpose (mthird);
203 must have minimum at 5 (3 is added automatically as missing)
205 if (!add_arr_p->size ()
206 || ((add_arr_p->size () == 1) &&
207 ((add_arr_p->top ().notename_i_ != p.notename_i_)
208 || (add_arr_p->top () < p))))
211 Array<Musical_pitch> triads;
212 triads.push (third); // c e
213 triads.push (mthird); // d f
214 triads.push (mthird); // e g
215 triads.push (third); // f a
216 triads.push (third); // g b
217 triads.push (mthird); // a c
218 triads.push (mthird); // b d
223 for (int i = 0; i < add_arr_p->size (); i++)
225 Musical_pitch p = (*add_arr_p)[i];
226 if ((p > missing) && (p.notename_i_ != missing.notename_i_))
227 while ((p > missing) && (p.notename_i_ != missing.notename_i_))
229 add_arr_p->insert (missing, i++);
230 missing.transpose (triads[(missing.notename_i_ - tonic.notename_i_ + 8) % 8]);
237 add all that aren't subtracted
239 for (int i = 0; i < add_arr_p->size (); i++)
241 Musical_pitch p = (*add_arr_p)[i];
242 Note_req* n = new Note_req;
245 for (int j = 0; j < sub_arr_p->size (); j++)
247 if (p == (*sub_arr_p)[j])
258 v->set_spot (here_input ());
263 My_lily_parser::get_note_element (Note_req *rq, Duration * duration_p)
265 Simultaneous_music*v = new Request_chord;
266 v->set_spot (here_input ());
270 // too bad parser reads (default) duration via member access,
271 // this hack will do for now..
272 if (abbrev_beam_type_i_)
274 assert (!duration_p->plet_b ());
275 duration_p->set_plet (1, 2);
277 rq->duration_ = *duration_p;
278 rq->set_spot (here_input ());
288 My_lily_parser::get_parens_request (int t)
290 Array<Request*>& reqs = *new Array<Request*>;
294 reqs.push (new Tie_req);
299 Plet_req* p = new Plet_req;
300 p->plet_i_ = plet_.type_i_;
307 if (!abbrev_beam_type_i_)
309 reqs.push (new Beam_req);
313 Abbreviation_beam_req* a = new Abbreviation_beam_req;
314 a->type_i_ = abbrev_beam_type_i_;
316 abbrev_beam_type_i_ = 0;
325 reqs.push (new Span_dynamic_req);
331 Plet_req* p = new Plet_req;
332 p->plet_i_ = plet_.type_i_;
339 reqs.push (new Slur_req);
350 dynamic_cast<Span_req*> (reqs.top ())->spantype = Span_req::START;
357 dynamic_cast<Span_req*> (reqs.top ())->spantype = Span_req::START;
360 dynamic_cast<Span_req*> (reqs.top ())->spantype = Span_req::STOP;
365 dynamic_cast<Span_req*> (reqs[0])->spantype = Span_req::STOP;
372 for (int i = 0; i < reqs.size (); i++)
373 if (dynamic_cast<Span_dynamic_req*> (reqs[i]))
375 Span_dynamic_req* s_l= dynamic_cast<Span_dynamic_req*> (reqs[i]);
376 s_l->dynamic_dir_ = (t == '<') ? UP:DOWN;
379 // ugh? don't we do this in the parser too?
380 reqs[0]->set_spot (here_input());
385 My_lily_parser::add_requests (Simultaneous_music*v)
387 for (int i = 0; i < pre_reqs.size(); i++)
389 v->add_music (pre_reqs[i]);
392 for (int i = 0; i <post_reqs.size(); i++)
394 v->add_music (post_reqs[i]);
401 My_lily_parser::pop_spot()
403 return define_spot_array_.pop();
407 My_lily_parser::here_input() const
409 Source_file * f_l= lexer_p_->source_file_l();
410 return Input (f_l, here_ch_C());
414 My_lily_parser::add_notename (String s, Musical_pitch p)
416 lexer_p_->add_notename (s, p);
421 My_lily_parser::default_paper_p ()
423 Identifier *id = lexer_p_->lookup_identifier ("$defaultpaper");
424 return id ? id->access_content_Paper_def (true) : new Paper_def ;
428 My_lily_parser::default_midi_p ()
430 Identifier *id = lexer_p_->lookup_identifier ("$defaultmidi");
431 return id ? id->access_content_Midi_def (true) : new Midi_def ;