]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-parser.cc
release: 0.1.59
[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 Han-Wen Nienhuys <hanwen@stack.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 "header.hh"
18
19
20 My_lily_parser::My_lily_parser (Sources * source_l)
21 {
22   first_b_ = true;
23   source_l_ = source_l;
24   lexer_p_ = 0;
25   abbrev_beam_type_i_ = 0;
26   default_duration_.durlog_i_ = 2;
27   default_octave_i_ = 0;
28   textstyle_str_="roman";               // in lexer?
29   error_level_i_ = 0;
30   last_duration_mode_b_ = true;
31   fatal_error_i_ = 0;
32   default_header_p_ =0;
33 }
34
35 My_lily_parser::~My_lily_parser()
36 {
37   delete lexer_p_;
38   delete default_header_p_;
39 }
40
41
42 void
43 My_lily_parser::clear_notenames()
44 {
45   lexer_p_->clear_notenames();
46 }
47
48 void
49 My_lily_parser::set_version_check (bool ig)
50 {
51   ignore_version_b_ = ig;
52 }
53 void
54 My_lily_parser::set_debug()
55 {
56 #ifndef NPRINT
57   String s = "";
58   if (init_parse_b_)
59     s = "Init";
60   set_yydebug (!monitor->silent_b (s+"Parser") && check_debug);
61   lexer_p_->set_debug (!monitor->silent_b (s+"Lexer") && check_debug);
62 #endif
63 }
64
65 void
66 My_lily_parser::print_declarations()
67 {
68 #ifndef NPRINT
69   String s = "";
70
71   if (init_parse_b_)
72     s = "Init";
73   if (!monitor->silent_b (s+"Declarations") && check_debug)
74     {
75       lexer_p_->print_declarations (init_parse_b_);
76     }
77 #endif
78 }
79
80 void
81 My_lily_parser::parse_file (String init, String s)
82 {
83   lexer_p_ = new My_lily_lexer;
84   init_str_ = init;
85
86   *mlog << _("Parsing ... ");
87
88   init_parse_b_ = true;
89   set_debug();
90   lexer_p_->new_input (init, source_l_);
91   do_yyparse();
92
93   if (error_level_i_)
94     {
95       error (_("Found errors in init files"));
96     }
97   print_declarations();
98
99   init_parse_b_ = false;
100   set_debug();
101   lexer_p_->new_input (s , source_l_);
102   do_yyparse();
103   print_declarations();
104
105
106   if (!define_spot_array_.empty())
107     {
108       warning (_("Braces don't match."));
109       error_level_i_ = 1;
110     }
111 }
112
113 void
114 My_lily_parser::remember_spot()
115 {
116   define_spot_array_.push (here_input());
117 }
118
119 char const *
120 My_lily_parser::here_ch_C() const
121 {
122   return lexer_p_->here_ch_C();
123 }
124
125 void
126 My_lily_parser::parser_error (String s)
127 {
128   here_input().error (s);
129   if (fatal_error_i_)
130     exit (fatal_error_i_);
131   error_level_i_ = 1;
132   exit_status_i_ = 1;
133 }
134
135 void
136 My_lily_parser::set_duration_mode (String s)
137 {
138   s = s.upper_str();
139   last_duration_mode_b_ = (s== "LAST");
140 }
141
142 void
143 My_lily_parser::set_abbrev_beam (int type_i)
144 {
145   abbrev_beam_type_i_ = type_i;
146 }
147
148 void
149 My_lily_parser::set_default_duration (Duration const *d)
150 {
151   last_duration_mode_b_ = false;
152   default_duration_ = *d;
153 }
154
155
156 void
157 My_lily_parser::set_last_duration (Duration const *d)
158 {
159   if (last_duration_mode_b_)
160     default_duration_ = *d;
161 }
162
163
164 Chord*
165 My_lily_parser::get_word_element (Text_def* tdef_p, Duration * duration_p)
166 {
167   Chord* velt_p = new Request_chord;
168
169   Lyric_req* lreq_p = new Lyric_req (tdef_p);
170
171   lreq_p->duration_ = *duration_p;
172   lreq_p->set_spot (here_input());
173
174   velt_p->add (lreq_p);
175
176   delete  duration_p;
177   return velt_p;
178 }
179
180 Chord *
181 My_lily_parser::get_rest_element (String s,  Duration * duration_p)
182 {
183   Chord* velt_p = new Request_chord;
184   velt_p->set_spot (here_input());
185
186   if (s=="s")
187     { /* Space */
188       Skip_req * skip_p = new Skip_req;
189       skip_p->duration_ = *duration_p;
190
191       skip_p->set_spot (here_input());
192       velt_p->add (skip_p);
193     }
194   else if ((duration_p->plet_.type_i_ == 1) && (duration_p->plet_.iso_i_ > 1))
195     {
196       Multi_measure_rest_req* m = new Multi_measure_rest_req;
197       plet_.iso_i_ = 1;
198       default_duration_.plet_.iso_i_ = 1;
199       m->duration_ = *duration_p;
200       m->set_spot (here_input());
201       velt_p->add (m);
202     }
203   else
204     {
205       Rest_req * rest_req_p = new Rest_req;
206       rest_req_p->duration_ = *duration_p;
207       rest_req_p->set_spot (here_input());
208
209       velt_p->add (rest_req_p);
210     }
211
212   delete duration_p;
213   return velt_p;
214 }
215
216 Chord *
217 My_lily_parser::get_note_element (Note_req *rq, Duration * duration_p)
218 {
219   Chord*v = new Request_chord;
220   v->set_spot (here_input ());
221
222   v->add (rq);
223
224   // too bad parser reads (default) duration via member access,
225   // this hack will do for now..
226   if (abbrev_beam_type_i_)
227     {
228       assert (!duration_p->plet_b ());
229       duration_p->set_plet (1, 2);
230     }
231   rq->set_duration (*duration_p);
232   rq->set_spot (here_input ());
233   delete duration_p ;
234   return v;
235 }
236
237 Array<Request*>*
238 My_lily_parser::get_parens_request (int t)
239 {
240   Array<Request*>& reqs = *new Array<Request*>;
241   switch (t)
242     {
243     case '~':
244       reqs.push (new Tie_req);
245       break;
246     case BEAMPLET:
247     case MAEBTELP:
248       {
249         Plet_req* p = new Plet_req;
250         p->plet_i_ = plet_.type_i_;
251         reqs.push (p);
252       }
253       /* fall through */
254     case '[':
255     case ']':
256       {
257         if (!abbrev_beam_type_i_)
258           {
259             reqs.push (new Beam_req);
260           }
261         else
262           {
263             Abbreviation_beam_req* a = new Abbreviation_beam_req;
264             a->type_i_ = abbrev_beam_type_i_;
265             if (t==']')
266               abbrev_beam_type_i_ = 0;
267             reqs.push (a);
268           }
269       }
270       break;
271
272     case '>':
273     case '!':
274     case '<':
275       reqs.push (new Span_dynamic_req);
276       break;
277
278     case PLET:  
279     case TELP:
280       {
281         Plet_req* p = new Plet_req;
282         p->plet_i_ = plet_.type_i_;
283         reqs.push (p);
284       }
285       break;
286     case ')':
287     case '(':
288       {
289         reqs.push (new Slur_req);
290       }
291       break;
292     default:
293       assert (false);
294       break;
295     }
296
297   switch (t)
298     {
299     case BEAMPLET:
300       reqs.top ()->span()->spantype = Span_req::START;
301       /* fall through */
302     case '<':
303     case '>':
304     case '(':
305     case '[':
306     case PLET:
307       reqs[0]->span ()->spantype = Span_req::START;
308       break;
309     case MAEBTELP:
310       reqs.top ()->span()->spantype = Span_req::STOP;
311       /* fall through */
312     case '!':
313     case ')':
314     case ']':
315       reqs[0]->span ()->spantype = Span_req::STOP;
316       break;
317
318     default:
319       break;
320     }
321
322   for (int i = 0; i < reqs.size (); i++)
323     if (reqs[i]->musical ()->span_dynamic ())
324       {
325         Span_dynamic_req* s_l= (reqs[i]->musical ()->span_dynamic ()) ;
326         s_l->dynamic_dir_ = (t == '<') ? UP:DOWN;
327       }
328
329   // ugh? don't we do this in the parser too?
330   reqs[0]->set_spot (here_input());
331   return &reqs;
332 }
333
334 void
335 My_lily_parser::add_requests (Chord*v)
336 {
337   for (int i = 0; i < pre_reqs.size(); i++)
338     {
339       v->add (pre_reqs[i]);
340     }
341   pre_reqs.clear();
342   for (int i = 0; i <post_reqs.size(); i++)
343     {
344       v->add (post_reqs[i]);
345     }
346   post_reqs.clear();
347 }
348
349 Input
350 My_lily_parser::pop_spot()
351 {
352   return define_spot_array_.pop();
353 }
354
355 Input
356 My_lily_parser::here_input() const
357 {
358   Source_file * f_l= lexer_p_->source_file_l();
359   return Input (f_l, here_ch_C());
360 }
361
362 void
363 My_lily_parser::add_notename (String s, Melodic_req * m_p)
364 {
365   lexer_p_->add_notename (s, m_p);
366 }