]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-parser.cc
release: 0.0.56
[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     
144     Stem_req * stem_p = new Stem_req;
145     stem_p->duration_ = *duration_p;
146     stem_p->set_spot ( here_input ());
147     velt_p->add(stem_p);
148     delete duration_p;
149     return velt_p;
150 }
151
152 Voice_element *
153 My_lily_parser::get_note_element(Note_req *rq, Duration * duration_p )
154 {
155     Voice_element*v = new Voice_element;
156     v->set_spot( here_input());
157
158     v->add(rq);
159     
160     if (duration_p->type_i_ >= 2) {
161         Stem_req * stem_req_p = new Stem_req();
162         stem_req_p->duration_ = *duration_p;
163         
164         stem_req_p->set_spot( here_input());
165         v->add(stem_req_p);
166     }
167
168     rq->set_duration(*duration_p);
169     rq->set_spot( here_input());
170     delete duration_p ;
171     return v;
172 }
173
174 Request*
175 My_lily_parser::get_parens_request(char c)
176 {
177     Request* req_p=0;
178     switch (c) {
179
180     case '[':
181     case ']':
182     {
183         Beam_req*b = new Beam_req;
184         int p_i=default_duration_.plet_.type_i_ ; // ugh . Should junk?
185         if (p_i!= 1)
186             b->nplet = p_i;
187         req_p = b;
188     }
189     break;
190
191     case '>':
192     case '!':
193     case '<':
194         req_p = new Span_dynamic_req;
195         break;
196     
197     case ')':
198     case '(':
199         req_p = new Slur_req;
200         break;
201     default:
202         assert(false);
203         break;
204     }
205     
206     switch (c) {
207     case '<':
208     case '>':
209     case '(':
210     case '[':
211         req_p->span()->spantype = Span_req::START;
212         break;
213     case '!':
214     case ')':
215     case ']':
216         req_p->span()->spantype = Span_req::STOP;
217         break;
218         
219     default:
220         break;
221     }
222
223    if (req_p->musical()->span_dynamic()) {
224         Span_dynamic_req* s_l= (req_p->musical()->span_dynamic()) ;
225         s_l->dynamic_dir_i_ = (c == '<') ? 1:-1;
226     }
227
228     req_p->set_spot( here_input());
229     return req_p;
230 }
231
232 My_lily_parser::My_lily_parser(Sources * source_l)
233 {
234     first_b_ = true;
235     source_l_ = source_l;
236     lexer_p_ = 0;
237     default_duration_.type_i_ = 4;
238     default_octave_i_ = 0;
239     textstyle_str_="roman";             // in lexer?
240     error_level_i_ = 0;
241     last_duration_mode = false;
242     fatal_error_i_ = 0;
243 }
244
245 void
246 My_lily_parser::add_requests(Voice_element*v)
247 {
248     for (int i = 0; i < pre_reqs.size(); i++) {
249         v->add(pre_reqs[i]);
250     }
251     pre_reqs.set_size(0);
252     for (int i = 0; i <post_reqs.size(); i++) {
253         v->add(post_reqs[i]);
254     }
255     post_reqs.set_size(0);
256 }
257
258 Input
259 My_lily_parser::pop_spot()
260 {
261     return define_spot_array_.pop();
262 }
263
264 Input
265 My_lily_parser::here_input()const
266 {
267     Source_file * f_l= lexer_p_->source_file_l();
268     return Input(f_l, here_ch_C());
269 }
270
271 void
272 My_lily_parser::add_notename(String s, Melodic_req * m_p)
273 {
274     lexer_p_->add_notename(s, m_p);
275 }