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