]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-parser.cc
eb5d2f0d82c25cacedad75f8b05dd11698b1b440
[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 void
21 My_lily_parser::clear_notenames()
22 {
23   lexer_p_->clear_notenames();
24 }
25 void
26 My_lily_parser::set_version_check (bool ig)
27 {
28   ignore_version_b_ = ig;
29 }
30 void
31 My_lily_parser::set_debug()
32 {
33 #ifndef NPRINT
34   String s = "";
35   if (init_parse_b_) 
36         s = "Init";
37   set_yydebug (!monitor->silence (s+"Parser") && check_debug);
38   lexer_p_->set_debug (!monitor->silence (s+"Lexer") && check_debug);
39 #endif
40 }
41
42 void
43 My_lily_parser::print_declarations()
44 {
45 #ifndef NPRINT
46   String s = "";
47   
48   if (init_parse_b_) 
49         s = "Init";
50   if (!monitor->silence (s+"Declarations") && check_debug) 
51     {
52         lexer_p_->print_declarations (init_parse_b_);
53     }
54 #endif   
55 }
56
57 void
58 My_lily_parser::parse_file (String init, String s)
59 {
60   lexer_p_ = new My_lily_lexer;
61   init_str_ = init;
62   
63   *mlog << "Parsing ... ";
64   
65   init_parse_b_ = true;
66   set_debug();
67   lexer_p_->new_input (init, source_l_);
68   do_yyparse();
69   print_declarations();
70
71   init_parse_b_ = false;
72     set_debug();
73   lexer_p_->new_input (s , source_l_);
74   do_yyparse();
75   print_declarations();
76
77   
78   if (!define_spot_array_.empty())
79         warning ("Braces don't match.");
80 }
81
82 My_lily_parser::~My_lily_parser()
83 {
84   delete lexer_p_;
85   delete default_header_p_;
86 }
87   
88 void
89 My_lily_parser::remember_spot()
90 {
91   define_spot_array_.push (here_input());
92 }
93
94 char const * 
95 My_lily_parser::here_ch_C()const
96 {
97   return lexer_p_->here_ch_C();
98 }
99
100 void
101 My_lily_parser::parser_error (String s)
102 {
103   here_input().error (s);
104   if ( fatal_error_i_)
105         exit (fatal_error_i_);
106   error_level_i_ = 1;
107 }
108
109 void
110 My_lily_parser::set_duration_mode (String s)
111 {
112   s = s.upper_str();
113   last_duration_mode_b_ = (s== "LAST");
114 }
115
116 void
117 My_lily_parser::set_default_duration (Duration const *d)
118 {
119   last_duration_mode_b_ = false;
120   default_duration_ = *d;
121 }
122
123
124 void
125 My_lily_parser::set_last_duration (Duration const *d)
126 {
127   if (last_duration_mode_b_)
128         default_duration_ = *d;
129 }
130
131
132 Chord*
133 My_lily_parser::get_word_element (Text_def* tdef_p, Duration * duration_p)
134 {
135   Chord* velt_p = new Request_chord;
136   
137   Lyric_req* lreq_p = new Lyric_req (tdef_p);
138
139   lreq_p->duration_ = *duration_p;
140   lreq_p->set_spot (here_input());
141
142   velt_p->add (lreq_p);
143
144   delete  duration_p;
145   return velt_p;
146 }
147
148 Chord *
149 My_lily_parser::get_rest_element (String s,  Duration * duration_p)
150 {    
151   Chord* velt_p = new Request_chord;
152   velt_p->set_spot (here_input());
153
154   if (s=="s") { /* Space */
155         Skip_req * skip_p = new Skip_req;
156         skip_p->duration_ = *duration_p;
157
158         skip_p->set_spot (here_input());
159         velt_p->add (skip_p);
160     }
161   else 
162     {
163         Rest_req * rest_req_p = new Rest_req;
164         rest_req_p->duration_ = *duration_p;
165         rest_req_p->set_spot (here_input());
166         
167         velt_p->add (rest_req_p);
168     }
169   Stem_req * stem_p = new Stem_req;
170   stem_p->duration_ = *duration_p;
171   stem_p->set_spot ( here_input());
172   velt_p->add (stem_p);
173
174   delete duration_p;
175   return velt_p;
176 }
177
178 Chord *
179 My_lily_parser::get_note_element (Note_req *rq, Duration * duration_p)
180 {
181   Chord*v = new Request_chord;
182   v->set_spot (here_input());
183
184   v->add (rq);
185   
186   if (duration_p->durlog_i_ >= 1) 
187     {
188         Stem_req * stem_req_p = new Stem_req();
189         stem_req_p->duration_ = *duration_p;
190         
191         stem_req_p->set_spot (here_input());
192         v->add (stem_req_p);
193     }
194
195   rq->set_duration (*duration_p);
196   rq->set_spot (here_input());
197   delete duration_p ;
198   return v;
199 }
200
201 Request*
202 My_lily_parser::get_parens_request (char c)
203 {
204   Request* req_p=0;
205   switch (c) 
206     {
207
208   case '~':
209         req_p = new Tie_req;
210         break;
211   case '[':
212   case ']':
213   {
214         Beam_req*b = new Beam_req;
215         int p_i=plet_.type_i_ ; // ugh . Should junk?
216         if (p_i!= 1)
217             b->nplet = p_i;
218         req_p = b;
219     }
220   break;
221
222   case '>':
223   case '!':
224   case '<':
225         req_p = new Span_dynamic_req;
226         break;
227   
228   case ')':
229   case '(':
230         req_p = new Slur_req;
231         break;
232   default:
233         assert (false);
234         break;
235     }
236   
237   switch (c) 
238     {
239   case '<':
240   case '>':
241   case '(':
242   case '[':
243         req_p->span()->spantype = Span_req::START;
244         break;
245   case '!':
246   case ')':
247   case ']':
248         req_p->span()->spantype = Span_req::STOP;
249         break;
250         
251   default:
252         break;
253     }
254
255    if (req_p->musical()->span_dynamic ()) 
256    {
257         Span_dynamic_req* s_l= (req_p->musical()->span_dynamic ()) ;
258         s_l->dynamic_dir_i_ = (c == '<') ? 1:-1;
259     }
260
261   req_p->set_spot (here_input());
262   return req_p;
263 }
264
265 My_lily_parser::My_lily_parser (Sources * source_l)
266 {
267   first_b_ = true;
268   source_l_ = source_l;
269   lexer_p_ = 0;
270   default_duration_.durlog_i_ = 2;
271   default_octave_i_ = 0;
272   textstyle_str_="roman";               // in lexer?
273   error_level_i_ = 0;
274   last_duration_mode_b_ = true;
275   fatal_error_i_ = 0;
276   default_header_p_ =0;
277 }
278
279 void
280 My_lily_parser::add_requests (Chord*v)
281 {
282   for (int i = 0; i < pre_reqs.size(); i++) 
283     {
284         v->add (pre_reqs[i]);
285     }
286   pre_reqs.clear();
287   for (int i = 0; i <post_reqs.size(); i++) 
288     {
289         v->add (post_reqs[i]);
290     }
291   post_reqs.clear();
292 }
293
294 Input
295 My_lily_parser::pop_spot()
296 {
297   return define_spot_array_.pop();
298 }
299
300 Input
301 My_lily_parser::here_input()const
302 {
303   Source_file * f_l= lexer_p_->source_file_l();
304   return Input (f_l, here_ch_C());
305 }
306
307 void
308 My_lily_parser::add_notename (String s, Melodic_req * m_p)
309 {
310   lexer_p_->add_notename (s, m_p);
311 }