]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-parser.cc
release: 0.0.53
[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::do_init_file()
54 {
55     init_parse_b_ = true;
56     set_debug();
57     lexer_p_->new_input(init_str_, source_l_);
58 }
59
60 void
61 My_lily_parser::parse_file(String init, String s)
62 {
63     *mlog << "Parsing ... ";
64     lexer_p_ = new My_lily_lexer;
65     init_str_ = init;
66     
67     lexer_p_->new_input(s, source_l_);
68     do_yyparse();
69     print_declarations();
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     first_b_ = true;
231     source_l_ = source_l;
232     lexer_p_ = 0;
233     default_duration_.type_i_ = 4;
234     default_octave_i_ = 0;
235     textstyle_str_="roman";             // in lexer?
236     error_level_i_ = 0;
237     last_duration_mode = false;
238     fatal_error_i_ = 0;
239 }
240
241 void
242 My_lily_parser::add_requests(Voice_element*v)
243 {
244     for (int i = 0; i < pre_reqs.size(); i++) {
245         v->add(pre_reqs[i]);
246     }
247     pre_reqs.set_size(0);
248     for (int i = 0; i <post_reqs.size(); i++) {
249         v->add(post_reqs[i]);
250     }
251     post_reqs.set_size(0);
252 }
253
254 Input
255 My_lily_parser::pop_spot()
256 {
257     return define_spot_array_.pop();
258 }
259
260 Input
261 My_lily_parser::here_input()const
262 {
263     Source_file * f_l= lexer_p_->source_file_l();
264     return Input(f_l, here_ch_C());
265 }
266
267 void
268 My_lily_parser::add_notename(String s, Melodic_req * m_p)
269 {
270     lexer_p_->add_notename(s, m_p);
271 }