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