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