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