]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-parser.cc
release: 0.1.11
[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")
155     { /* Space */
156     Skip_req * skip_p = new Skip_req;
157     skip_p->duration_ = *duration_p;
158
159     skip_p->set_spot (here_input());
160     velt_p->add (skip_p);
161     }
162   else 
163     {
164       Rest_req * rest_req_p = new Rest_req;
165       rest_req_p->duration_ = *duration_p;
166       rest_req_p->set_spot (here_input());
167         
168       velt_p->add (rest_req_p);
169     }
170
171   delete duration_p;
172   return velt_p;
173 }
174
175 Chord *
176 My_lily_parser::get_note_element (Note_req *rq, Duration * duration_p)
177 {
178   Chord*v = new Request_chord;
179   v->set_spot (here_input());
180
181   v->add (rq);
182   
183   rq->set_duration (*duration_p);
184   rq->set_spot (here_input());
185   delete duration_p ;
186   return v;
187 }
188
189 Request*
190 My_lily_parser::get_parens_request (char c)
191 {
192   Request* req_p=0;
193   switch (c) 
194     {
195
196     case '~':
197       req_p = new Tie_req;
198       break;
199     case '[':
200     case ']':
201       {
202         Beam_req*b = new Beam_req;
203         int p_i=plet_.type_i_ ; // ugh . Should junk?
204         if (p_i!= 1)
205           b->nplet = p_i;
206         req_p = b;
207       }
208     break;
209
210     case '>':
211     case '!':
212     case '<':
213       req_p = new Span_dynamic_req;
214       break;
215   
216     case ')':
217     case '(':
218       req_p = new Slur_req;
219       break;
220     default:
221       assert (false);
222       break;
223     }
224   
225   switch (c) 
226     {
227     case '<':
228     case '>':
229     case '(':
230     case '[':
231       req_p->span()->spantype = Span_req::START;
232       break;
233     case '!':
234     case ')':
235     case ']':
236       req_p->span()->spantype = Span_req::STOP;
237       break;
238         
239     default:
240       break;
241     }
242
243   if (req_p->musical()->span_dynamic ()) 
244     {
245       Span_dynamic_req* s_l= (req_p->musical()->span_dynamic ()) ;
246       s_l->dynamic_dir_ = (c == '<') ? UP:DOWN;
247     }
248
249   req_p->set_spot (here_input());
250   return req_p;
251 }
252
253 My_lily_parser::My_lily_parser (Sources * source_l)
254 {
255   first_b_ = true;
256   source_l_ = source_l;
257   lexer_p_ = 0;
258   default_duration_.durlog_i_ = 2;
259   default_octave_i_ = 0;
260   textstyle_str_="roman";               // in lexer?
261   error_level_i_ = 0;
262   last_duration_mode_b_ = true;
263   fatal_error_i_ = 0;
264   default_header_p_ =0;
265 }
266
267 void
268 My_lily_parser::add_requests (Chord*v)
269 {
270   for (int i = 0; i < pre_reqs.size(); i++) 
271     {
272       v->add (pre_reqs[i]);
273     }
274   pre_reqs.clear();
275   for (int i = 0; i <post_reqs.size(); i++) 
276     {
277       v->add (post_reqs[i]);
278     }
279   post_reqs.clear();
280 }
281
282 Input
283 My_lily_parser::pop_spot()
284 {
285   return define_spot_array_.pop();
286 }
287
288 Input
289 My_lily_parser::here_input() const
290 {
291   Source_file * f_l= lexer_p_->source_file_l();
292   return Input (f_l, here_ch_C());
293 }
294
295 void
296 My_lily_parser::add_notename (String s, Melodic_req * m_p)
297 {
298   lexer_p_->add_notename (s, m_p);
299 }