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