]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser.cc
Merge branch 'master' into lilypond/translation
[lilypond.git] / lily / lily-parser.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2010 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
76 SCM
77 Lily_parser::mark_smob (SCM s)
78 {
79   Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
80   return (parser->lexer_) ? parser->lexer_->self_scm () : SCM_EOL;
81 }
82
83 int
84 Lily_parser::print_smob (SCM s, SCM port, scm_print_state*)
85 {
86   scm_puts ("#<Lily_parser ", port);
87   Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
88   if (parser->lexer_)
89     scm_display (parser->lexer_->self_scm (), port);
90   else
91     scm_puts ("(no lexer yet)", port);
92   scm_puts (" >", port);
93   return 1;
94 }
95
96 /* Process one .ly file, or book.  */
97 void
98 Lily_parser::parse_file (string init, string name, string out_name)
99 {
100   // TODO: use $parser
101   lexer_->set_identifier (ly_symbol2scm ("parser"), self_scm ());
102   output_basename_ = out_name;
103
104   lexer_->main_input_name_ = name;
105
106   message (_ ("Parsing..."));
107
108   set_yydebug (0);
109
110   lexer_->new_input (init, sources_);
111
112   File_name f (name);
113   string s = global_path.find (f.base_ + ".twy");
114   s = gulp_file_to_string (s, false, -1);
115   scm_eval_string (ly_string2scm (s));
116
117   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
118      OUT_FILE (unless IN_FILE redefines output file name).  */
119
120   SCM mod = lexer_->set_current_scope ();
121   do_yyparse ();
122
123   /*
124     Don't mix cyclic pointers with weak tables.
125   */
126   lexer_->set_identifier (ly_symbol2scm ("parser"),
127                           SCM_EOL);
128   ly_reexport_module (scm_current_module ());
129
130   scm_set_current_module (mod);
131
132   if (!define_spots_.empty ())
133     {
134       define_spots_.back ().warning (_ ("braces do not match"));
135       error_level_ = 1;
136     }
137
138   error_level_ = error_level_ | lexer_->error_level_;
139   clear ();
140 }
141
142 void
143 Lily_parser::parse_string (string ly_code)
144 {
145   // TODO: use $parser
146   lexer_->set_identifier (ly_symbol2scm ("parser"),
147                           self_scm ());
148
149   lexer_->main_input_name_ = "<string>";
150   lexer_->is_main_input_ = true;
151   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
152
153   SCM mod = lexer_->set_current_scope ();
154   do_yyparse ();
155   scm_set_current_module (mod);
156
157   if (!define_spots_.empty ())
158     {
159       if (define_spots_.empty ()
160           && !error_level_)
161         programming_error ("define_spots_ don't match, but error_level_ not set.");
162     }
163
164   error_level_ = error_level_ | lexer_->error_level_;
165 }
166
167 void
168 Lily_parser::include_string (string ly_code)
169 {
170   lexer_->add_string_include (ly_code);
171 }
172
173 void
174 Lily_parser::clear ()
175 {
176   if (lexer_)
177     {
178       while (lexer_->has_scope ())
179         lexer_->remove_scope ();
180     }
181
182   lexer_ = 0;
183 }
184
185 char const *
186 Lily_parser::here_str0 () const
187 {
188   return lexer_->here_str0 ();
189 }
190
191 void
192 Lily_parser::parser_error (string s)
193 {
194   lexer_->here_input ().error (_ (s.c_str ()));
195   error_level_ = 1;
196 }
197
198 void
199 Lily_parser::parser_error (Input const &i, string s)
200 {
201   i.error (s);
202   error_level_ = 1;
203 }
204
205
206
207 IMPLEMENT_SMOBS (Lily_parser);
208 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
209 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
210
211
212 /****************************************************************
213   OUTPUT-DEF
214  ****************************************************************/
215
216 Output_def *
217 get_layout (Lily_parser *parser)
218 {
219   SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
220   Output_def *layout = unsmob_output_def (id);
221   layout = layout ? layout->clone () : new Output_def;
222   layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
223
224   return layout;
225 }
226
227 Output_def *
228 get_midi (Lily_parser *parser)
229 {
230   SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
231   Output_def *layout = unsmob_output_def (id);
232   layout = layout ? layout->clone () : new Output_def;
233   layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
234   return layout;
235 }
236
237 /* Return a copy of the top of $papers stack, or $defaultpaper if the
238  * stack is empty */
239 Output_def *
240 get_paper (Lily_parser *parser)
241 {
242   SCM papers = parser->lexer_->lookup_identifier ("$papers");
243   Output_def *layout = ((papers == SCM_UNDEFINED) || scm_is_null (papers)) ?
244     0 : unsmob_output_def (scm_car (papers));
245   SCM default_paper = parser->lexer_->lookup_identifier ("$defaultpaper");
246   layout = layout ? layout : unsmob_output_def (default_paper);
247
248   layout = layout ? dynamic_cast<Output_def *> (layout->clone ()) : new Output_def;
249   layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
250   return layout;
251 }
252
253 /* Initialize (reset) the $papers stack */
254 void
255 init_papers (Lily_parser *parser)
256 {
257   parser->lexer_->set_identifier (ly_symbol2scm ("$papers"), SCM_EOL);
258 }
259
260 /* Push a paper on top of $papers stack */
261 void
262 push_paper (Lily_parser *parser, Output_def *paper)
263 {
264   parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
265                                   scm_cons (paper->self_scm (),
266                                             parser->lexer_->lookup_identifier ("$papers")));
267 }
268
269 /* Pop a paper from $papers stack */
270 void
271 pop_paper (Lily_parser *parser)
272 {
273   if (scm_is_pair (parser->lexer_->lookup_identifier ("$papers")))
274     parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
275                                     scm_cdr (parser->lexer_->lookup_identifier ("$papers")));
276 }
277
278 /* Change the paper on top of $papers stack */
279 void
280 set_paper (Lily_parser *parser, Output_def *paper)
281 {
282   scm_set_car_x (parser->lexer_->lookup_identifier ("$papers"), paper->self_scm ());
283 }
284
285 SCM
286 get_header (Lily_parser *parser)
287 {
288   SCM id = parser->lexer_->lookup_identifier ("$defaultheader");
289   if (!ly_is_module (id))
290     id = parser->make_scope ();
291   else
292     {
293       SCM nid = parser->make_scope ();
294       ly_module_copy (nid, id);
295       id = nid;
296     }
297
298   return id;
299 }
300
301 SCM
302 Lily_parser::make_scope () const
303 {
304   SCM module = ly_make_module (be_safe_global);
305   return module;
306 }