]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-parser.cc
release: 0.1.12
[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     {
80       warning ("Braces don't match.");
81       error_level_i_ = 1;
82     }
83 }
84
85 My_lily_parser::~My_lily_parser()
86 {
87   delete lexer_p_;
88   delete default_header_p_;
89 }
90   
91 void
92 My_lily_parser::remember_spot()
93 {
94   define_spot_array_.push (here_input());
95 }
96
97 char const * 
98 My_lily_parser::here_ch_C() const
99 {
100   return lexer_p_->here_ch_C();
101 }
102
103 void
104 My_lily_parser::parser_error (String s)
105 {
106   here_input().error (s);
107   if (fatal_error_i_)
108     exit (fatal_error_i_);
109   error_level_i_ = 1;
110 }
111
112 void
113 My_lily_parser::set_duration_mode (String s)
114 {
115   s = s.upper_str();
116   last_duration_mode_b_ = (s== "LAST");
117 }
118
119 void
120 My_lily_parser::set_default_duration (Duration const *d)
121 {
122   last_duration_mode_b_ = false;
123   default_duration_ = *d;
124 }
125
126
127 void
128 My_lily_parser::set_last_duration (Duration const *d)
129 {
130   if (last_duration_mode_b_)
131     default_duration_ = *d;
132 }
133
134
135 Chord*
136 My_lily_parser::get_word_element (Text_def* tdef_p, Duration * duration_p)
137 {
138   Chord* velt_p = new Request_chord;
139   
140   Lyric_req* lreq_p = new Lyric_req (tdef_p);
141
142   lreq_p->duration_ = *duration_p;
143   lreq_p->set_spot (here_input());
144
145   velt_p->add (lreq_p);
146
147   delete  duration_p;
148   return velt_p;
149 }
150
151 Chord *
152 My_lily_parser::get_rest_element (String s,  Duration * duration_p)
153 {    
154   Chord* velt_p = new Request_chord;
155   velt_p->set_spot (here_input());
156
157   if (s=="s")
158     { /* Space */
159     Skip_req * skip_p = new Skip_req;
160     skip_p->duration_ = *duration_p;
161
162     skip_p->set_spot (here_input());
163     velt_p->add (skip_p);
164     }
165   else 
166     {
167       Rest_req * rest_req_p = new Rest_req;
168       rest_req_p->duration_ = *duration_p;
169       rest_req_p->set_spot (here_input());
170         
171       velt_p->add (rest_req_p);
172     }
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   rq->set_duration (*duration_p);
187   rq->set_spot (here_input());
188   delete duration_p ;
189   return v;
190 }
191
192 Request*
193 My_lily_parser::get_parens_request (char c)
194 {
195   Request* req_p=0;
196   switch (c) 
197     {
198
199     case '~':
200       req_p = new Tie_req;
201       break;
202     case '[':
203     case ']':
204       {
205         Beam_req*b = new Beam_req;
206         int p_i=plet_.type_i_ ; // ugh . Should junk?
207         if (p_i!= 1)
208           b->nplet = p_i;
209         req_p = b;
210       }
211     break;
212
213     case '>':
214     case '!':
215     case '<':
216       req_p = new Span_dynamic_req;
217       break;
218   
219     case ')':
220     case '(':
221       req_p = new Slur_req;
222       break;
223     default:
224       assert (false);
225       break;
226     }
227   
228   switch (c) 
229     {
230     case '<':
231     case '>':
232     case '(':
233     case '[':
234       req_p->span()->spantype = Span_req::START;
235       break;
236     case '!':
237     case ')':
238     case ']':
239       req_p->span()->spantype = Span_req::STOP;
240       break;
241         
242     default:
243       break;
244     }
245
246   if (req_p->musical()->span_dynamic ()) 
247     {
248       Span_dynamic_req* s_l= (req_p->musical()->span_dynamic ()) ;
249       s_l->dynamic_dir_ = (c == '<') ? UP:DOWN;
250     }
251
252   req_p->set_spot (here_input());
253   return req_p;
254 }
255
256 My_lily_parser::My_lily_parser (Sources * source_l)
257 {
258   first_b_ = true;
259   source_l_ = source_l;
260   lexer_p_ = 0;
261   default_duration_.durlog_i_ = 2;
262   default_octave_i_ = 0;
263   textstyle_str_="roman";               // in lexer?
264   error_level_i_ = 0;
265   last_duration_mode_b_ = true;
266   fatal_error_i_ = 0;
267   default_header_p_ =0;
268 }
269
270 void
271 My_lily_parser::add_requests (Chord*v)
272 {
273   for (int i = 0; i < pre_reqs.size(); i++) 
274     {
275       v->add (pre_reqs[i]);
276     }
277   pre_reqs.clear();
278   for (int i = 0; i <post_reqs.size(); i++) 
279     {
280       v->add (post_reqs[i]);
281     }
282   post_reqs.clear();
283 }
284
285 Input
286 My_lily_parser::pop_spot()
287 {
288   return define_spot_array_.pop();
289 }
290
291 Input
292 My_lily_parser::here_input() const
293 {
294   Source_file * f_l= lexer_p_->source_file_l();
295   return Input (f_l, here_ch_C());
296 }
297
298 void
299 My_lily_parser::add_notename (String s, Melodic_req * m_p)
300 {
301   lexer_p_->add_notename (s, m_p);
302 }