]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-parser.cc
patch::: 0.1.12.jcn2: kliener pats
[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   print_declarations();
94
95   init_parse_b_ = false;
96   set_debug();
97   lexer_p_->new_input (s , source_l_);
98   do_yyparse();
99   print_declarations();
100
101   
102   if (!define_spot_array_.empty())
103     {
104       warning ("Braces don't match.");
105       error_level_i_ = 1;
106     }
107 }
108
109 void
110 My_lily_parser::remember_spot()
111 {
112   define_spot_array_.push (here_input());
113 }
114
115 char const * 
116 My_lily_parser::here_ch_C() const
117 {
118   return lexer_p_->here_ch_C();
119 }
120
121 void
122 My_lily_parser::parser_error (String s)
123 {
124   here_input().error (s);
125   if (fatal_error_i_)
126     exit (fatal_error_i_);
127   error_level_i_ = 1;
128 }
129
130 void
131 My_lily_parser::set_duration_mode (String s)
132 {
133   s = s.upper_str();
134   last_duration_mode_b_ = (s== "LAST");
135 }
136
137 void
138 My_lily_parser::set_abbrev_beam (int type_i)
139 {
140   abbrev_beam_type_i_ = type_i;
141 }
142
143 void
144 My_lily_parser::set_last_abbrev (int type_i)
145 {
146   default_abbrev_type_i_ = type_i;
147 }
148
149 void
150 My_lily_parser::set_default_duration (Duration const *d)
151 {
152   last_duration_mode_b_ = false;
153   default_duration_ = *d;
154   set_last_abbrev (0);
155 }
156
157
158 void
159 My_lily_parser::set_last_duration (Duration const *d)
160 {
161   if (last_duration_mode_b_)
162     default_duration_ = *d;
163   set_last_abbrev (0);
164 }
165
166
167 Chord*
168 My_lily_parser::get_word_element (Text_def* tdef_p, Duration * duration_p)
169 {
170   Chord* velt_p = new Request_chord;
171   
172   Lyric_req* lreq_p = new Lyric_req (tdef_p);
173
174   lreq_p->duration_ = *duration_p;
175   lreq_p->set_spot (here_input());
176
177   velt_p->add (lreq_p);
178
179   delete  duration_p;
180   return velt_p;
181 }
182
183 Chord *
184 My_lily_parser::get_rest_element (String s,  Duration * duration_p)
185 {    
186   Chord* velt_p = new Request_chord;
187   velt_p->set_spot (here_input());
188
189   if (s=="s")
190     { /* Space */
191     Skip_req * skip_p = new Skip_req;
192     skip_p->duration_ = *duration_p;
193
194     skip_p->set_spot (here_input());
195     velt_p->add (skip_p);
196     }
197   else 
198     {
199       Rest_req * rest_req_p = new Rest_req;
200       rest_req_p->duration_ = *duration_p;
201       rest_req_p->set_spot (here_input());
202         
203       velt_p->add (rest_req_p);
204     }
205
206   delete duration_p;
207   return velt_p;
208 }
209
210 Chord *
211 My_lily_parser::get_note_element (Note_req *rq, Duration * duration_p)
212 {
213   Chord*v = new Request_chord;
214   v->set_spot (here_input());
215
216   v->add (rq);
217   
218   // too bad parser reads (default) duration via member access,
219   // this hack will do for now..
220   if (abbrev_beam_type_i_)
221     {
222       assert (!duration_p->plet_b ());
223       duration_p->set_plet (1, 2);
224     }
225   rq->set_duration (*duration_p);
226   rq->set_spot (here_input());
227   delete duration_p ;
228   return v;
229 }
230
231 Request*
232 My_lily_parser::get_parens_request (char c)
233 {
234   Request* req_p=0;
235   switch (c) 
236     {
237
238     case '~':
239       req_p = new Tie_req;
240       break;
241     case '[':
242     case ']':
243       {
244         if (!abbrev_beam_type_i_)
245           {
246             Beam_req*b = new Beam_req;
247             int p_i=plet_.type_i_ ; // ugh . Should junk?
248             if (p_i!= 1)
249               b->nplet = p_i;
250             req_p = b;
251           }
252         else
253           {
254             Abbreviation_beam_req* a = new Abbreviation_beam_req;
255             a->type_i_ = abbrev_beam_type_i_;
256             if (c==']')
257               abbrev_beam_type_i_ = 0;
258             req_p = a;
259           }
260       }
261     break;
262
263     case '>':
264     case '!':
265     case '<':
266       req_p = new Span_dynamic_req;
267       break;
268   
269     case ')':
270     case '(':
271       req_p = new Slur_req;
272       break;
273     default:
274       assert (false);
275       break;
276     }
277   
278   switch (c) 
279     {
280     case '<':
281     case '>':
282     case '(':
283     case '[':
284       req_p->span()->spantype = Span_req::START;
285       break;
286     case '!':
287     case ')':
288     case ']':
289       req_p->span()->spantype = Span_req::STOP;
290       break;
291         
292     default:
293       break;
294     }
295
296   if (req_p->musical()->span_dynamic ()) 
297     {
298       Span_dynamic_req* s_l= (req_p->musical()->span_dynamic ()) ;
299       s_l->dynamic_dir_ = (c == '<') ? UP:DOWN;
300     }
301
302   req_p->set_spot (here_input());
303   return req_p;
304 }
305
306 void
307 My_lily_parser::add_requests (Chord*v)
308 {
309   for (int i = 0; i < pre_reqs.size(); i++) 
310     {
311       v->add (pre_reqs[i]);
312     }
313   pre_reqs.clear();
314   for (int i = 0; i <post_reqs.size(); i++) 
315     {
316       v->add (post_reqs[i]);
317     }
318   post_reqs.clear();
319 }
320
321 Input
322 My_lily_parser::pop_spot()
323 {
324   return define_spot_array_.pop();
325 }
326
327 Input
328 My_lily_parser::here_input() const
329 {
330   Source_file * f_l= lexer_p_->source_file_l();
331   return Input (f_l, here_ch_C());
332 }
333
334 void
335 My_lily_parser::add_notename (String s, Melodic_req * m_p)
336 {
337   lexer_p_->add_notename (s, m_p);
338 }