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