lilypond-0.1.57
[lilypond.git] / lily / my-lily-parser.cc
blobaaaf7201b2c5a1fdc39f4e25ff3621a7ab1bdef1
1 /*
2 my-lily-parser.cc -- implement My_lily_parser
4 source file of the GNU LilyPond music typesetter
6 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
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"
20 My_lily_parser::My_lily_parser (Sources * source_l)
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;
35 My_lily_parser::~My_lily_parser()
37 delete lexer_p_;
38 delete default_header_p_;
42 void
43 My_lily_parser::clear_notenames()
45 lexer_p_->clear_notenames();
48 void
49 My_lily_parser::set_version_check (bool ig)
51 ignore_version_b_ = ig;
53 void
54 My_lily_parser::set_debug()
56 #ifndef NPRINT
57 String s = "";
58 if (init_parse_b_)
59 s = "Init";
60 set_yydebug (!monitor->silent_b (s+"Parser") && check_debug);
61 lexer_p_->set_debug (!monitor->silent_b (s+"Lexer") && check_debug);
62 #endif
65 void
66 My_lily_parser::print_declarations()
68 #ifndef NPRINT
69 String s = "";
71 if (init_parse_b_)
72 s = "Init";
73 if (!monitor->silent_b (s+"Declarations") && check_debug)
75 lexer_p_->print_declarations (init_parse_b_);
77 #endif
80 void
81 My_lily_parser::parse_file (String init, String s)
83 lexer_p_ = new My_lily_lexer;
84 init_str_ = init;
86 *mlog << _("Parsing ... ");
88 init_parse_b_ = true;
89 set_debug();
90 lexer_p_->new_input (init, source_l_);
91 do_yyparse();
93 if (error_level_i_)
95 error (_("Found errors in init files"));
97 print_declarations();
99 init_parse_b_ = false;
100 set_debug();
101 lexer_p_->new_input (s , source_l_);
102 do_yyparse();
103 print_declarations();
106 if (!define_spot_array_.empty())
108 warning (_("Braces don't match."));
109 error_level_i_ = 1;
113 void
114 My_lily_parser::remember_spot()
116 define_spot_array_.push (here_input());
119 char const *
120 My_lily_parser::here_ch_C() const
122 return lexer_p_->here_ch_C();
125 void
126 My_lily_parser::parser_error (String s)
128 here_input().error (s);
129 if (fatal_error_i_)
130 exit (fatal_error_i_);
131 error_level_i_ = 1;
132 exit_status_i_ = 1;
135 void
136 My_lily_parser::set_duration_mode (String s)
138 s = s.upper_str();
139 last_duration_mode_b_ = (s== "LAST");
142 void
143 My_lily_parser::set_abbrev_beam (int type_i)
145 abbrev_beam_type_i_ = type_i;
148 void
149 My_lily_parser::set_default_duration (Duration const *d)
151 last_duration_mode_b_ = false;
152 default_duration_ = *d;
156 void
157 My_lily_parser::set_last_duration (Duration const *d)
159 if (last_duration_mode_b_)
160 default_duration_ = *d;
164 Chord*
165 My_lily_parser::get_word_element (Text_def* tdef_p, Duration * duration_p)
167 Chord* velt_p = new Request_chord;
169 Lyric_req* lreq_p = new Lyric_req (tdef_p);
171 lreq_p->duration_ = *duration_p;
172 lreq_p->set_spot (here_input());
174 velt_p->add (lreq_p);
176 delete duration_p;
177 return velt_p;
180 Chord *
181 My_lily_parser::get_rest_element (String s, Duration * duration_p)
183 Chord* velt_p = new Request_chord;
184 velt_p->set_spot (here_input());
186 if (s=="s")
187 { /* Space */
188 Skip_req * skip_p = new Skip_req;
189 skip_p->duration_ = *duration_p;
191 skip_p->set_spot (here_input());
192 velt_p->add (skip_p);
194 else
196 Rest_req * rest_req_p = new Rest_req;
197 rest_req_p->duration_ = *duration_p;
198 rest_req_p->set_spot (here_input());
200 velt_p->add (rest_req_p);
203 delete duration_p;
204 return velt_p;
207 Chord *
208 My_lily_parser::get_note_element (Note_req *rq, Duration * duration_p)
210 Chord*v = new Request_chord;
211 v->set_spot (here_input ());
213 v->add (rq);
215 // too bad parser reads (default) duration via member access,
216 // this hack will do for now..
217 if (abbrev_beam_type_i_)
219 assert (!duration_p->plet_b ());
220 duration_p->set_plet (1, 2);
222 rq->set_duration (*duration_p);
223 rq->set_spot (here_input ());
224 delete duration_p ;
225 return v;
228 Array<Request*>*
229 My_lily_parser::get_parens_request (int t)
231 Array<Request*>& reqs = *new Array<Request*>;
232 switch (t)
234 case '~':
235 reqs.push (new Tie_req);
236 break;
237 case BEAMPLET:
238 case MAEBTELP:
240 Plet_req* p = new Plet_req;
241 p->plet_i_ = plet_.type_i_;
242 reqs.push (p);
244 /* fall through */
245 case '[':
246 case ']':
248 if (!abbrev_beam_type_i_)
250 reqs.push (new Beam_req);
252 else
254 Abbreviation_beam_req* a = new Abbreviation_beam_req;
255 a->type_i_ = abbrev_beam_type_i_;
256 if (t==']')
257 abbrev_beam_type_i_ = 0;
258 reqs.push (a);
261 break;
263 case '>':
264 case '!':
265 case '<':
266 reqs.push (new Span_dynamic_req);
267 break;
269 case PLET:
270 case TELP:
272 Plet_req* p = new Plet_req;
273 p->plet_i_ = plet_.type_i_;
274 reqs.push (p);
276 break;
277 case ')':
278 case '(':
280 reqs.push (new Slur_req);
282 break;
283 default:
284 assert (false);
285 break;
288 switch (t)
290 case BEAMPLET:
291 reqs.top ()->span()->spantype = Span_req::START;
292 /* fall through */
293 case '<':
294 case '>':
295 case '(':
296 case '[':
297 case PLET:
298 reqs[0]->span ()->spantype = Span_req::START;
299 break;
300 case MAEBTELP:
301 reqs.top ()->span()->spantype = Span_req::STOP;
302 /* fall through */
303 case '!':
304 case ')':
305 case ']':
306 reqs[0]->span ()->spantype = Span_req::STOP;
307 break;
309 default:
310 break;
313 for (int i = 0; i < reqs.size (); i++)
314 if (reqs[i]->musical ()->span_dynamic ())
316 Span_dynamic_req* s_l= (reqs[i]->musical ()->span_dynamic ()) ;
317 s_l->dynamic_dir_ = (t == '<') ? UP:DOWN;
320 // ugh? don't we do this in the parser too?
321 reqs[0]->set_spot (here_input());
322 return &reqs;
325 void
326 My_lily_parser::add_requests (Chord*v)
328 for (int i = 0; i < pre_reqs.size(); i++)
330 v->add (pre_reqs[i]);
332 pre_reqs.clear();
333 for (int i = 0; i <post_reqs.size(); i++)
335 v->add (post_reqs[i]);
337 post_reqs.clear();
340 Input
341 My_lily_parser::pop_spot()
343 return define_spot_array_.pop();
346 Input
347 My_lily_parser::here_input() const
349 Source_file * f_l= lexer_p_->source_file_l();
350 return Input (f_l, here_ch_C());
353 void
354 My_lily_parser::add_notename (String s, Melodic_req * m_p)
356 lexer_p_->add_notename (s, m_p);