]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser.cc
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond into...
[lilypond.git] / lily / lily-parser.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5   Jan Nieuwenhuizen <janneke@gnu.org>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "lily-parser.hh"
22
23 #include "book.hh"
24 #include "file-name.hh"
25 #include "file-path.hh"
26 #include "international.hh"
27 #include "lily-lexer.hh"
28 #include "lily-version.hh"
29 #include "ly-module.hh"
30 #include "main.hh"
31 #include "output-def.hh"
32 #include "paper-book.hh"
33 #include "parser.hh"
34 #include "score.hh"
35 #include "sources.hh"
36 #include "warn.hh"
37 #include "program-option.hh"
38
39 #include "ly-smobs.icc"
40
41 Lily_parser::Lily_parser (Sources *sources)
42 {
43   lexer_ = 0;
44   sources_ = sources;
45   default_duration_ = Duration (2, 0);
46   error_level_ = 0;
47
48   smobify_self ();
49
50   lexer_ = new Lily_lexer (sources_, this);
51   lexer_->unprotect ();
52 }
53
54 Lily_parser::Lily_parser (Lily_parser const &src)
55 {
56   lexer_ = 0;
57   sources_ = src.sources_;
58   default_duration_ = src.default_duration_;
59   error_level_ = src.error_level_;
60   output_basename_ = src.output_basename_;
61
62   smobify_self ();
63   if (src.lexer_)
64     {
65       lexer_ = new Lily_lexer (*src.lexer_, this);
66     }
67
68   lexer_->unprotect ();
69 }
70
71 Lily_parser::~Lily_parser ()
72 {
73 }
74
75 SCM
76 Lily_parser::mark_smob (SCM s)
77 {
78   Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
79   return (parser->lexer_) ? parser->lexer_->self_scm () : SCM_EOL;
80 }
81
82 int
83 Lily_parser::print_smob (SCM s, SCM port, scm_print_state *)
84 {
85   scm_puts ("#<Lily_parser ", port);
86   Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
87   if (parser->lexer_)
88     scm_display (parser->lexer_->self_scm (), port);
89   else
90     scm_puts ("(no lexer yet)", port);
91   scm_puts (" >", port);
92   return 1;
93 }
94
95 /* Process one .ly file, or book.  */
96 void
97 Lily_parser::parse_file (string init, string name, string out_name)
98 {
99   // TODO: use $parser
100   lexer_->set_identifier (ly_symbol2scm ("parser"), self_scm ());
101   output_basename_ = out_name;
102
103   lexer_->main_input_name_ = name;
104
105   message (_ ("Parsing..."));
106
107   set_yydebug (0);
108
109   lexer_->new_input (init, sources_);
110
111   File_name f (name);
112   string s = global_path.find (f.base_ + ".twy");
113   s = gulp_file_to_string (s, false, -1);
114   scm_eval_string (ly_string2scm (s));
115
116   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
117      OUT_FILE (unless IN_FILE redefines output file name).  */
118
119   SCM mod = lexer_->set_current_scope ();
120   do_yyparse ();
121
122   /*
123     Don't mix cyclic pointers with weak tables.
124   */
125   lexer_->set_identifier (ly_symbol2scm ("parser"),
126                           SCM_EOL);
127   ly_reexport_module (scm_current_module ());
128
129   scm_set_current_module (mod);
130
131   if (!define_spots_.empty ())
132     {
133       define_spots_.back ().warning (_ ("braces do not match"));
134       error_level_ = 1;
135     }
136
137   error_level_ = error_level_ | lexer_->error_level_;
138   clear ();
139 }
140
141 void
142 Lily_parser::parse_string (string ly_code)
143 {
144   // TODO: use $parser
145   lexer_->set_identifier (ly_symbol2scm ("parser"),
146                           self_scm ());
147
148   lexer_->main_input_name_ = "<string>";
149   lexer_->is_main_input_ = true;
150   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
151
152   SCM mod = lexer_->set_current_scope ();
153   do_yyparse ();
154   scm_set_current_module (mod);
155
156   if (!define_spots_.empty ())
157     {
158       if (define_spots_.empty ()
159           && !error_level_)
160         programming_error ("define_spots_ don't match, but error_level_ not set.");
161     }
162
163   error_level_ = error_level_ | lexer_->error_level_;
164 }
165
166 SCM
167 Lily_parser::parse_string_expression (string ly_code)
168 {
169   // TODO: use $parser
170   lexer_->set_identifier (ly_symbol2scm ("parser"),
171                           self_scm ());
172
173   lexer_->main_input_name_ = "<string>";
174   lexer_->is_main_input_ = true;
175   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
176
177   SCM mod = lexer_->set_current_scope ();
178   lexer_->push_embedded_token ();
179   do_yyparse ();
180   SCM result = lexer_->lookup_identifier_symbol (ly_symbol2scm ("$parseStringResult"));
181   // $parseStringResult is set in the grammar rule for embedded_lilypond
182   
183   scm_set_current_module (mod);
184
185   if (!define_spots_.empty ())
186     {
187       if (define_spots_.empty ()
188           && !error_level_)
189         programming_error ("define_spots_ don't match, but error_level_ not set.");
190     }
191
192   error_level_ = error_level_ | lexer_->error_level_;
193   return result;
194 }
195
196 void
197 Lily_parser::include_string (string ly_code)
198 {
199   lexer_->add_string_include (ly_code);
200 }
201
202 void
203 Lily_parser::clear ()
204 {
205   if (lexer_)
206     {
207       while (lexer_->has_scope ())
208         lexer_->remove_scope ();
209     }
210
211   lexer_ = 0;
212 }
213
214 char const *
215 Lily_parser::here_str0 () const
216 {
217   return lexer_->here_str0 ();
218 }
219
220 void
221 Lily_parser::parser_error (string s)
222 {
223   lexer_->here_input ().error (_ (s.c_str ()));
224   error_level_ = 1;
225 }
226
227 void
228 Lily_parser::parser_error (Input const &i, string s)
229 {
230   i.error (s);
231   error_level_ = 1;
232 }
233
234 IMPLEMENT_SMOBS (Lily_parser);
235 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
236 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
237
238 /****************************************************************
239   OUTPUT-DEF
240  ****************************************************************/
241
242 Output_def *
243 get_layout (Lily_parser *parser)
244 {
245   SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
246   Output_def *layout = unsmob_output_def (id);
247   layout = layout ? layout->clone () : new Output_def;
248   layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
249
250   return layout;
251 }
252
253 Output_def *
254 get_midi (Lily_parser *parser)
255 {
256   SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
257   Output_def *layout = unsmob_output_def (id);
258   layout = layout ? layout->clone () : new Output_def;
259   layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
260   return layout;
261 }
262
263 /* Return a copy of the top of $papers stack, or $defaultpaper if the
264  * stack is empty */
265 Output_def *
266 get_paper (Lily_parser *parser)
267 {
268   SCM papers = parser->lexer_->lookup_identifier ("$papers");
269   Output_def *layout = ((papers == SCM_UNDEFINED) || scm_is_null (papers))
270                        ? 0 : unsmob_output_def (scm_car (papers));
271   SCM default_paper = parser->lexer_->lookup_identifier ("$defaultpaper");
272   layout = layout ? layout : unsmob_output_def (default_paper);
273
274   layout = layout ? dynamic_cast<Output_def *> (layout->clone ()) : new Output_def;
275   layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
276   return layout;
277 }
278
279 /* Initialize (reset) the $papers stack */
280 void
281 init_papers (Lily_parser *parser)
282 {
283   parser->lexer_->set_identifier (ly_symbol2scm ("$papers"), SCM_EOL);
284 }
285
286 /* Push a paper on top of $papers stack */
287 void
288 push_paper (Lily_parser *parser, Output_def *paper)
289 {
290   parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
291                                   scm_cons (paper->self_scm (),
292                                             parser->lexer_->lookup_identifier ("$papers")));
293 }
294
295 /* Pop a paper from $papers stack */
296 void
297 pop_paper (Lily_parser *parser)
298 {
299   if (scm_is_pair (parser->lexer_->lookup_identifier ("$papers")))
300     parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
301                                     scm_cdr (parser->lexer_->lookup_identifier ("$papers")));
302 }
303
304 /* Change the paper on top of $papers stack */
305 void
306 set_paper (Lily_parser *parser, Output_def *paper)
307 {
308   scm_set_car_x (parser->lexer_->lookup_identifier ("$papers"), paper->self_scm ());
309 }
310
311 SCM
312 get_header (Lily_parser *parser)
313 {
314   SCM id = parser->lexer_->lookup_identifier ("$defaultheader");
315   if (!ly_is_module (id))
316     id = parser->make_scope ();
317   else
318     {
319       SCM nid = parser->make_scope ();
320       ly_module_copy (nid, id);
321       id = nid;
322     }
323
324   return id;
325 }
326
327 SCM
328 Lily_parser::make_scope () const
329 {
330   SCM module = ly_make_module (be_safe_global);
331   return module;
332 }