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@stack.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_octave_i_ = 0;
31 textstyle_str_="roman"; // in lexer?
33 last_duration_mode_b_ = true;
37 relative_octave_mode_b_ = false;
39 last_melodic_ = new Melodic_req;
40 last_melodic_->octave_i_ = 0; // -1; // huh?
41 last_melodic_->notename_i_ = 0;
42 last_melodic_->accidental_i_ = 0;
45 My_lily_parser::~My_lily_parser()
48 delete default_header_p_;
53 My_lily_parser::clear_notenames()
55 lexer_p_->clear_notenames();
59 My_lily_parser::set_version_check (bool ig)
61 ignore_version_b_ = ig;
65 My_lily_parser::parse_file (String init, String s)
67 lexer_p_ = new My_lily_lexer;
69 lexer_p_->main_input_str_ = s;
71 *mlog << _("Parsing ... ");
73 init_parse_b_ = false;
74 set_yydebug (!monitor->silent_b ("Parser") && check_debug);
75 lexer_p_->new_input (init, source_l_);
79 if (!define_spot_array_.empty())
81 warning (_("Braces don't match."));
85 inclusion_global_array = lexer_p_->filename_str_arr_;
89 My_lily_parser::remember_spot()
91 define_spot_array_.push (here_input());
95 My_lily_parser::here_ch_C() const
97 return lexer_p_->here_ch_C();
101 My_lily_parser::parser_error (String s)
103 here_input().error (s);
105 exit (fatal_error_i_);
111 My_lily_parser::set_duration_mode (String s)
114 last_duration_mode_b_ = (s== "LAST");
118 My_lily_parser::set_octave_mode (String s)
123 relative_octave_mode_b_ = true;
125 last_melodic_ = new Melodic_req;
126 last_melodic_->octave_i_ = 0; // -1; // huh?
127 last_melodic_->notename_i_ = 0;
128 last_melodic_->accidental_i_ = 0;
131 relative_octave_mode_b_ = false;
135 My_lily_parser::set_abbrev_beam (int type_i)
137 abbrev_beam_type_i_ = type_i;
141 My_lily_parser::set_default_duration (Duration const *d)
143 last_duration_mode_b_ = false;
144 default_duration_ = *d;
149 My_lily_parser::set_last_duration (Duration const *d)
151 if (last_duration_mode_b_)
153 default_duration_ = *d;
156 sticky plet factor only within plet brackets
158 default_duration_.set_plet (1, 1);
163 My_lily_parser::notename_str (Melodic_req* melodic)
166 String str ((char)('a' + ((melodic->notename_i_ + 2) % 7)));
167 int i = melodic->accidental_i_;
177 My_lily_parser::get_melodic_req (Melodic_req* melodic, int quotes)
179 if (relative_octave_mode_b_)
181 set_nearest (melodic);
182 int d = melodic->pitch () - last_melodic_->pitch ();
184 if (quotes && (sign (d) == sign (quotes)))
185 shift -= sign (quotes);
186 if (!quotes && (abs (d) == 6))
188 String str = _("Octave ambiguity; assuming ");
193 If the distance is exactly*) half an octave, there is
194 no nearest pitch. In that case, we'll try to guess what
195 composer/ typist meant.
196 Firstly, we'll do this by comparing the 'notename distance':
198 f b' % name-distance: f g a b: 3
200 is surely a shorter notename distance than
202 f 'b % name-distance: b c d e f: 4
204 (should we give a warning at all, or can we safely assume
205 this is a positive interval up?)
207 *) It is conceivable that, musically speaking, the interval
208 with the greater pitch-distance is thought to be smaller?
212 int name_delta = melodic->notename_i_ - last_melodic_->notename_i_;
213 int name_near = abs (name_delta) % 7;
214 int name_wrap = (7 - abs (name_delta)) % 7;
215 if (name_near != name_wrap)
216 shift = name_near < name_wrap ? sign (name_delta) : -sign (name_delta);
217 else if (sign (last_melodic_->accidental_i_)
218 != sign (melodic->accidental_i_))
219 shift = last_melodic_->accidental_i_ - melodic->accidental_i_;
222 String name_str = notename_str (melodic);
223 str += shift > 0 ? name_str + "'" : "'" + name_str;
224 if (sign (d) == sign (shift))
226 melodic->warning (str);
228 melodic->octave_i_ += quotes + shift;
232 Melodic_req nearest (*melodic);
233 set_nearest (&nearest);
234 melodic->octave_i_ += quotes;
236 if (find_quarts_global_b)
238 int e = melodic->pitch () - nearest.pitch ();
241 int d = melodic->pitch () - last_melodic_->pitch ();
242 String str = _("Interval greater than quart");
243 int n = 1 + (abs (d) - 1) / 12;
244 String quote_str ('\'', n);
245 str += _(", relative: ");
246 String name_str = notename_str (melodic);
247 str += d < 0 ? quote_str + name_str : name_str + quote_str;
248 melodic->warning (str);
252 delete last_melodic_;
253 last_melodic_ = melodic->clone ()->musical ()->melodic ();
258 My_lily_parser::set_nearest (Melodic_req* melodic)
260 melodic->octave_i_ = last_melodic_->octave_i_;
261 int d = melodic->pitch () - last_melodic_->pitch ();
263 melodic->octave_i_ -= sign (d);
267 My_lily_parser::get_word_element (Text_def* tdef_p, Duration * duration_p)
269 Chord* velt_p = new Request_chord;
271 Lyric_req* lreq_p = new Lyric_req (tdef_p);
273 lreq_p->duration_ = *duration_p;
274 lreq_p->set_spot (here_input());
276 velt_p->add (lreq_p);
284 My_lily_parser::get_rest_element (String s, Duration * duration_p)
286 Chord* velt_p = new Request_chord;
287 velt_p->set_spot (here_input());
291 Skip_req * skip_p = new Skip_req;
292 skip_p->duration_ = *duration_p;
294 skip_p->set_spot (here_input());
295 velt_p->add (skip_p);
299 Rest_req * rest_req_p = new Rest_req;
300 rest_req_p->duration_ = *duration_p;
301 rest_req_p->set_spot (here_input());
303 velt_p->add (rest_req_p);
311 My_lily_parser::get_note_element (Note_req *rq, Duration * duration_p)
313 Chord*v = new Request_chord;
314 v->set_spot (here_input ());
318 // too bad parser reads (default) duration via member access,
319 // this hack will do for now..
320 if (abbrev_beam_type_i_)
322 assert (!duration_p->plet_b ());
323 duration_p->set_plet (1, 2);
325 rq->set_duration (*duration_p);
326 rq->set_spot (here_input ());
332 My_lily_parser::get_parens_request (int t)
334 Array<Request*>& reqs = *new Array<Request*>;
338 reqs.push (new Tie_req);
343 Plet_req* p = new Plet_req;
344 p->plet_i_ = plet_.type_i_;
351 if (!abbrev_beam_type_i_)
353 reqs.push (new Beam_req);
357 Abbreviation_beam_req* a = new Abbreviation_beam_req;
358 a->type_i_ = abbrev_beam_type_i_;
360 abbrev_beam_type_i_ = 0;
369 reqs.push (new Span_dynamic_req);
375 Plet_req* p = new Plet_req;
376 p->plet_i_ = plet_.type_i_;
383 reqs.push (new Slur_req);
394 reqs.top ()->span()->spantype = Span_req::START;
401 reqs[0]->span ()->spantype = Span_req::START;
404 reqs.top ()->span()->spantype = Span_req::STOP;
409 reqs[0]->span ()->spantype = Span_req::STOP;
416 for (int i = 0; i < reqs.size (); i++)
417 if (reqs[i]->musical ()->span_dynamic ())
419 Span_dynamic_req* s_l= (reqs[i]->musical ()->span_dynamic ()) ;
420 s_l->dynamic_dir_ = (t == '<') ? UP:DOWN;
423 // ugh? don't we do this in the parser too?
424 reqs[0]->set_spot (here_input());
429 My_lily_parser::add_requests (Chord*v)
431 for (int i = 0; i < pre_reqs.size(); i++)
433 v->add (pre_reqs[i]);
436 for (int i = 0; i <post_reqs.size(); i++)
438 v->add (post_reqs[i]);
444 My_lily_parser::pop_spot()
446 return define_spot_array_.pop();
450 My_lily_parser::here_input() const
452 Source_file * f_l= lexer_p_->source_file_l();
453 return Input (f_l, here_ch_C());
457 My_lily_parser::add_notename (String s, Melodic_req * m_p)
459 lexer_p_->add_notename (s, m_p);
463 My_lily_parser::default_paper_p ()
465 Identifier *id = lexer_p_->lookup_identifier ("default_paper");
466 return id ? id->paperdef () : new Paper_def ;
470 My_lily_parser::default_midi_p ()
472 Identifier *id = lexer_p_->lookup_identifier ("default_midi");
473 return id ? id->mididef () : new Midi_def ;