]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-parser.cc
release: 1.1.1
[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 */
8
9 #include "my-lily-parser.hh"
10 #include "my-lily-lexer.hh"
11 #include "debug.hh"
12 #include "main.hh"
13 #include "music-list.hh"
14 #include "musical-request.hh"
15 #include "command-request.hh"
16 #include "parser.hh"
17 #include "scope.hh"
18 #include "file-results.hh"
19 #include "midi-def.hh"
20 #include "paper-def.hh"
21 #include "identifier.hh"
22
23 My_lily_parser::My_lily_parser (Sources * source_l)
24 {
25   first_b_ = true;
26   source_l_ = source_l;
27   lexer_p_ = 0;
28   abbrev_beam_type_i_ = 0;
29   default_duration_.durlog_i_ = 2;
30   default_abbrev_i_ = 0;
31   error_level_i_ = 0;
32   fatal_error_i_ = 0;
33   default_header_p_ =0;
34 }
35
36 My_lily_parser::~My_lily_parser()
37 {
38   delete lexer_p_;
39   delete default_header_p_;
40 }
41
42
43
44 void
45 My_lily_parser::set_version_check (bool ig)
46 {
47   ignore_version_b_ = ig;
48 }
49
50 void
51 My_lily_parser::parse_file (String init, String s)
52 {
53   lexer_p_ = new My_lily_lexer;
54   init_str_ = init;
55   lexer_p_->main_input_str_ = s;
56
57   *mlog << _ ("Parsing...");
58
59   init_parse_b_ = false;
60   set_yydebug (!monitor->silent_b ("Parser") && check_debug);
61   lexer_p_->new_input (init, source_l_);
62   do_yyparse ();
63
64   if (!define_spot_array_.empty())
65     {
66       warning (_ ("braces don't match"));
67       error_level_i_ = 1;
68     }
69
70   inclusion_global_array = lexer_p_->filename_str_arr_;
71 }
72
73 void
74 My_lily_parser::remember_spot()
75 {
76   define_spot_array_.push (here_input());
77 }
78
79 char const *
80 My_lily_parser::here_ch_C() const
81 {
82   return lexer_p_->here_ch_C();
83 }
84
85 void
86 My_lily_parser::parser_error (String s)
87 {
88   here_input().error (s);
89   if (fatal_error_i_)
90     exit (fatal_error_i_);
91   error_level_i_ = 1;
92   exit_status_i_ = 1;
93 }
94
95 void
96 My_lily_parser::set_abbrev_beam (int type_i)
97 {
98   abbrev_beam_type_i_ = type_i;
99 }
100
101
102 void
103 My_lily_parser::set_last_duration (Duration const *d)
104 {
105   default_duration_ = *d;
106
107   /* 
108      forget plet part,
109      but keep sticky plet factor within plet brackets
110     */  
111   default_duration_.plet_ = plet_;
112 }
113
114
115 Simultaneous_music*
116 My_lily_parser::get_word_element (String s, Duration * duration_p)
117 {
118   Simultaneous_music* velt_p = new Request_chord;
119
120   Lyric_req* lreq_p = new Lyric_req;
121   lreq_p ->text_str_ = s;
122   lreq_p->duration_ = *duration_p;
123   lreq_p->set_spot (here_input());
124
125   velt_p->add_music (lreq_p);
126
127   delete  duration_p;
128   return velt_p;
129 }
130
131
132 Simultaneous_music *
133 My_lily_parser::get_rest_element (String s,  Duration * duration_p)
134 {
135   Simultaneous_music* velt_p = new Request_chord;
136   velt_p->set_spot (here_input());
137
138   if (s=="s")
139     { /* Space */
140       Skip_req * skip_p = new Skip_req;
141       skip_p->duration_ = *duration_p;
142
143       skip_p->set_spot (here_input());
144       velt_p->add_music (skip_p);
145     }
146   else
147     {
148       Rest_req * rest_req_p = new Rest_req;
149       rest_req_p->duration_ = *duration_p;
150       rest_req_p->set_spot (here_input());
151
152       velt_p->add_music (rest_req_p);
153     }
154
155   delete duration_p;
156   return velt_p;
157 }
158
159 Simultaneous_music *
160 My_lily_parser::get_note_element (Note_req *rq, Duration * duration_p)
161 {
162   Simultaneous_music*v = new Request_chord;
163   v->set_spot (here_input ());
164
165   v->add_music (rq);
166
167   // too bad parser reads (default) duration via member access,
168   // this hack will do for now..
169   if (abbrev_beam_type_i_)
170     {
171       assert (!duration_p->plet_b ());
172       duration_p->set_plet (1, 2);
173     }
174   rq->duration_ = *duration_p;
175   rq->set_spot (here_input ());
176   delete duration_p ;
177   return v;
178 }
179
180
181 /*
182   UGH.
183  */
184 Array<Request*>*
185 My_lily_parser::get_parens_request (int t)
186 {
187   Array<Request*>& reqs = *new Array<Request*>;
188   switch (t)
189     {
190     case '~':
191       reqs.push (new Tie_req);
192       break;
193     case BEAMPLET:
194     case MAEBTELP:
195       {
196         Plet_req* p = new Plet_req;
197         p->plet_i_ = plet_.type_i_;
198         reqs.push (p);
199       }
200       /* fall through */
201     case '[':
202     case ']':
203       {
204         if (!abbrev_beam_type_i_)
205           {
206             reqs.push (new Beam_req);
207           }
208         else
209           {
210             Abbreviation_beam_req* a = new Abbreviation_beam_req;
211             a->type_i_ = abbrev_beam_type_i_;
212             if (t==']')
213               abbrev_beam_type_i_ = 0;
214             reqs.push (a);
215           }
216       }
217       break;
218
219     case '>':
220     case '!':
221     case '<':
222       reqs.push (new Span_dynamic_req);
223       break;
224
225     case PLET:  
226     case TELP:
227       {
228         Plet_req* p = new Plet_req;
229         p->plet_i_ = plet_.type_i_;
230         reqs.push (p);
231       }
232       break;
233     case ')':
234     case '(':
235       {
236         reqs.push (new Slur_req);
237       }
238       break;
239     default:
240       assert (false);
241       break;
242     }
243
244   switch (t)
245     {
246     case BEAMPLET:
247       dynamic_cast<Span_req*> (reqs.top ())->spantype = Span_req::START;
248       /* fall through */
249     case '<':
250     case '>':
251     case '(':
252     case '[':
253     case PLET:
254       dynamic_cast<Span_req*> (reqs.top ())->spantype = Span_req::START;
255       break;
256     case MAEBTELP:
257       dynamic_cast<Span_req*> (reqs.top ())->spantype = Span_req::STOP;
258       /* fall through */
259     case '!':
260     case ')':
261     case ']':
262       dynamic_cast<Span_req*> (reqs[0])->spantype = Span_req::STOP;
263       break;
264
265     default:
266       break;
267     }
268
269   for (int i = 0; i < reqs.size (); i++)
270     if (dynamic_cast<Span_dynamic_req*> (reqs[i]))
271       {
272         Span_dynamic_req* s_l= dynamic_cast<Span_dynamic_req*> (reqs[i]);
273         s_l->dynamic_dir_ = (t == '<') ? UP:DOWN;
274       }
275
276   // ugh? don't we do this in the parser too?
277   reqs[0]->set_spot (here_input());
278   return &reqs;
279 }
280
281 void
282 My_lily_parser::add_requests (Simultaneous_music*v)
283 {
284   for (int i = 0; i < pre_reqs.size(); i++)
285     {
286       v->add_music (pre_reqs[i]);
287     }
288   pre_reqs.clear();
289   for (int i = 0; i <post_reqs.size(); i++)
290     {
291       v->add_music (post_reqs[i]);
292     }
293
294   post_reqs.clear();
295 }
296
297 Input
298 My_lily_parser::pop_spot()
299 {
300   return define_spot_array_.pop();
301 }
302
303 Input
304 My_lily_parser::here_input() const
305 {
306   Source_file * f_l= lexer_p_->source_file_l();
307   return Input (f_l, here_ch_C());
308 }
309
310 void
311 My_lily_parser::add_notename (String s, Musical_pitch p)
312 {
313   lexer_p_->add_notename (s, p);
314
315 }
316
317 Paper_def*
318 My_lily_parser::default_paper_p ()
319 {
320   Identifier *id = lexer_p_->lookup_identifier ("$defaultpaper");
321   return id ? id->access_content_Paper_def (true) : new Paper_def ;
322 }
323
324 Midi_def*
325 My_lily_parser::default_midi_p ()
326 {
327   Identifier *id = lexer_p_->lookup_identifier ("$defaultmidi");
328   return id ? id->access_content_Midi_def (true) : new Midi_def ;
329 }
330