]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-parser.cc
release: 0.0.71pre
[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         lexer_p_->print_declarations(init_parse_b_);
50     }
51 #endif   
52 }
53
54 void
55 My_lily_parser::parse_file(String init, String s)
56 {
57     lexer_p_ = new My_lily_lexer;
58     init_str_ = init;
59     
60     *mlog << "Parsing ... ";
61     
62     init_parse_b_ = true;
63     lexer_p_->new_input( init, source_l_);
64     do_yyparse();
65     print_declarations();
66
67     init_parse_b_ = false;
68     lexer_p_->new_input( s , source_l_);
69     do_yyparse();
70     print_declarations();
71
72     
73     if(!define_spot_array_.empty())
74         warning("Braces don't match.");
75 }
76
77 My_lily_parser::~My_lily_parser()
78 {
79     delete lexer_p_;
80 }
81     
82 void
83 My_lily_parser::remember_spot()
84 {
85     define_spot_array_.push(here_input());
86 }
87
88 char const * 
89 My_lily_parser::here_ch_C()const
90 {
91     return lexer_p_->here_ch_C();
92 }
93
94 void
95 My_lily_parser::parser_error(String s)
96 {
97     here_input().error(s);
98     if ( fatal_error_i_ )
99         exit( fatal_error_i_ );
100     error_level_i_ = 1;
101 }
102
103 void
104 My_lily_parser::set_duration_mode(String s)
105 {
106     s = s.upper_str();
107     last_duration_mode_b_ = (s== "LAST");
108 }
109
110 void
111 My_lily_parser::set_default_duration(Duration const *d)
112 {
113     last_duration_mode_b_ = false;
114     default_duration_ = *d;
115 }
116
117
118 void
119 My_lily_parser::set_last_duration(Duration const *d)
120 {
121     if (last_duration_mode_b_)
122         default_duration_ = *d;
123 }
124
125
126 Chord*
127 My_lily_parser::get_word_element(Text_def* tdef_p, Duration * duration_p)
128 {
129     Chord* velt_p = new Voice_element;
130     
131     Lyric_req* lreq_p = new Lyric_req(tdef_p);
132
133     lreq_p->duration_ = *duration_p;
134     lreq_p->set_spot( here_input());
135
136     velt_p->add(lreq_p);
137
138     delete  duration_p;
139     return velt_p;
140 }
141
142 Chord *
143 My_lily_parser::get_rest_element(String s,  Duration * duration_p )
144 {    
145     Chord* velt_p = new Voice_element;
146     velt_p->set_spot( here_input());
147
148     if (s=="s") { /* Space */
149         Skip_req * skip_p = new Skip_req;
150         skip_p->duration_ = *duration_p;
151
152         skip_p->set_spot( here_input());
153         velt_p->add(skip_p);
154     }
155     else {
156         Rest_req * rest_req_p = new Rest_req;
157         rest_req_p->duration_ = *duration_p;
158         rest_req_p->set_spot( here_input());
159         
160         velt_p->add(rest_req_p);
161     }
162     Stem_req * stem_p = new Stem_req;
163     stem_p->duration_ = *duration_p;
164     stem_p->set_spot ( here_input ());
165     velt_p->add(stem_p);
166
167     delete duration_p;
168     return velt_p;
169 }
170
171 Chord *
172 My_lily_parser::get_note_element(Note_req *rq, Duration * duration_p )
173 {
174     Chord*v = new Voice_element;
175     v->set_spot( here_input());
176
177     v->add(rq);
178     
179     if (duration_p->type_i_ >= 2) {
180         Stem_req * stem_req_p = new Stem_req();
181         stem_req_p->duration_ = *duration_p;
182         
183         stem_req_p->set_spot( here_input());
184         v->add(stem_req_p);
185     }
186
187     rq->set_duration(*duration_p);
188     rq->set_spot( here_input());
189     delete duration_p ;
190     return v;
191 }
192
193 Request*
194 My_lily_parser::get_parens_request(char c)
195 {
196     Request* req_p=0;
197     switch (c) {
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=default_duration_.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     case '<':
230     case '>':
231     case '(':
232     case '[':
233         req_p->span()->spantype = Span_req::START;
234         break;
235     case '!':
236     case ')':
237     case ']':
238         req_p->span()->spantype = Span_req::STOP;
239         break;
240         
241     default:
242         break;
243     }
244
245    if (req_p->musical()->span_dynamic()) {
246         Span_dynamic_req* s_l= (req_p->musical()->span_dynamic()) ;
247         s_l->dynamic_dir_i_ = (c == '<') ? 1:-1;
248     }
249
250     req_p->set_spot( here_input());
251     return req_p;
252 }
253
254 My_lily_parser::My_lily_parser(Sources * source_l)
255 {
256     first_b_ = true;
257     source_l_ = source_l;
258     lexer_p_ = 0;
259     default_duration_.type_i_ = 4;
260     default_octave_i_ = 0;
261     textstyle_str_="roman";             // in lexer?
262     error_level_i_ = 0;
263     last_duration_mode_b_ = true;
264     fatal_error_i_ = 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         v->add(pre_reqs[i]);
272     }
273     pre_reqs.set_size(0);
274     for (int i = 0; i <post_reqs.size(); i++) {
275         v->add(post_reqs[i]);
276     }
277     post_reqs.set_size(0);
278 }
279
280 Input
281 My_lily_parser::pop_spot()
282 {
283     return define_spot_array_.pop();
284 }
285
286 Input
287 My_lily_parser::here_input()const
288 {
289     Source_file * f_l= lexer_p_->source_file_l();
290     return Input(f_l, here_ch_C());
291 }
292
293 void
294 My_lily_parser::add_notename(String s, Melodic_req * m_p)
295 {
296     lexer_p_->add_notename(s, m_p);
297 }