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