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