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