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