]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-parser.cc
release: 1.1.0
[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 Array<Request*>*
181 My_lily_parser::get_parens_request (int t)
182 {
183   Array<Request*>& reqs = *new Array<Request*>;
184   switch (t)
185     {
186     case '~':
187       reqs.push (new Tie_req);
188       break;
189     case BEAMPLET:
190     case MAEBTELP:
191       {
192         Plet_req* p = new Plet_req;
193         p->plet_i_ = plet_.type_i_;
194         reqs.push (p);
195       }
196       /* fall through */
197     case '[':
198     case ']':
199       {
200         if (!abbrev_beam_type_i_)
201           {
202             reqs.push (new Beam_req);
203           }
204         else
205           {
206             Abbreviation_beam_req* a = new Abbreviation_beam_req;
207             a->type_i_ = abbrev_beam_type_i_;
208             if (t==']')
209               abbrev_beam_type_i_ = 0;
210             reqs.push (a);
211           }
212       }
213       break;
214
215     case '>':
216     case '!':
217     case '<':
218       reqs.push (new Span_dynamic_req);
219       break;
220
221     case PLET:  
222     case TELP:
223       {
224         Plet_req* p = new Plet_req;
225         p->plet_i_ = plet_.type_i_;
226         reqs.push (p);
227       }
228       break;
229     case ')':
230     case '(':
231       {
232         reqs.push (new Slur_req);
233       }
234       break;
235     default:
236       assert (false);
237       break;
238     }
239
240   switch (t)
241     {
242     case BEAMPLET:
243       dynamic_cast<Span_req*> (reqs.top ())->spantype = Span_req::START;
244       /* fall through */
245     case '<':
246     case '>':
247     case '(':
248     case '[':
249     case PLET:
250       dynamic_cast<Span_req*> (reqs.top ())->spantype = Span_req::START;
251       break;
252     case MAEBTELP:
253       dynamic_cast<Span_req*> (reqs.top ())->spantype = Span_req::STOP;
254       /* fall through */
255     case '!':
256     case ')':
257     case ']':
258       dynamic_cast<Span_req*> (reqs[0])->spantype = Span_req::STOP;
259       break;
260
261     default:
262       break;
263     }
264
265   for (int i = 0; i < reqs.size (); i++)
266     if (dynamic_cast<Span_dynamic_req*> (reqs[i]))
267       {
268         Span_dynamic_req* s_l= dynamic_cast<Span_dynamic_req*> (reqs[i]);
269         s_l->dynamic_dir_ = (t == '<') ? UP:DOWN;
270       }
271
272   // ugh? don't we do this in the parser too?
273   reqs[0]->set_spot (here_input());
274   return &reqs;
275 }
276
277 void
278 My_lily_parser::add_requests (Simultaneous_music*v)
279 {
280   for (int i = 0; i < pre_reqs.size(); i++)
281     {
282       v->add_music (pre_reqs[i]);
283     }
284   pre_reqs.clear();
285   for (int i = 0; i <post_reqs.size(); i++)
286     {
287       v->add_music (post_reqs[i]);
288     }
289
290   post_reqs.clear();
291 }
292
293 Input
294 My_lily_parser::pop_spot()
295 {
296   return define_spot_array_.pop();
297 }
298
299 Input
300 My_lily_parser::here_input() const
301 {
302   Source_file * f_l= lexer_p_->source_file_l();
303   return Input (f_l, here_ch_C());
304 }
305
306 void
307 My_lily_parser::add_notename (String s, Musical_pitch p)
308 {
309   lexer_p_->add_notename (s, p);
310
311 }
312
313 Paper_def*
314 My_lily_parser::default_paper_p ()
315 {
316   Identifier *id = lexer_p_->lookup_identifier ("$defaultpaper");
317   return id ? id->access_Paper_def () : new Paper_def ;
318 }
319
320 Midi_def*
321 My_lily_parser::default_midi_p ()
322 {
323   Identifier *id = lexer_p_->lookup_identifier ("$defaultmidi");
324   return id ? id->access_Midi_def () : new Midi_def ;
325 }
326