]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / lily-parser.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2014 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, SCM location)
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, location);
69       lexer_->unprotect ();
70     }
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->closures_);
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 (const string &init, const string &name, const string &out_name)
101 {
102   lexer_->set_identifier (ly_symbol2scm ("parser"), self_scm ());
103   output_basename_ = out_name;
104
105   lexer_->main_input_name_ = name;
106
107   message (_ ("Parsing..."));
108
109   set_yydebug (0);
110
111   lexer_->new_input (init, sources_);
112
113   File_name f (name);
114   string s = global_path.find (f.base_ + ".twy");
115   s = gulp_file_to_string (s, false, -1);
116   scm_eval_string (ly_string2scm (s));
117
118   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
119      OUT_FILE (unless IN_FILE redefines output file name).  */
120
121   SCM mod = lexer_->set_current_scope ();
122   do
123     {
124       do_yyparse ();
125     }
126   while (!lexer_->is_clean ());
127
128   /*
129     Don't mix cyclic pointers with weak tables.
130   */
131   lexer_->set_identifier (ly_symbol2scm ("parser"),
132                           SCM_EOL);
133   ly_reexport_module (scm_current_module ());
134
135   scm_set_current_module (mod);
136
137   error_level_ = error_level_ | lexer_->error_level_;
138   clear ();
139 }
140
141 void
142 Lily_parser::parse_string (const string &ly_code)
143 {
144   lexer_->main_input_name_ = "<string>";
145   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
146
147   SCM mod = lexer_->set_current_scope ();
148   SCM parser = lexer_->lookup_identifier_symbol (ly_symbol2scm ("parser"));
149   lexer_->set_identifier (ly_symbol2scm ("parser"), self_scm ());
150   do_yyparse ();
151   lexer_->set_identifier (ly_symbol2scm ("parser"), parser);
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 (const string &ly_code, const string &filename,
159                                       int line)
160 {
161   lexer_->main_input_name_ = filename;
162   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
163   if (line)
164     {
165       lexer_->get_source_file ()->set_line (0, line);
166     }
167   SCM mod = lexer_->set_current_scope ();
168   SCM parser = lexer_->lookup_identifier_symbol (ly_symbol2scm ("parser"));
169   lexer_->set_identifier (ly_symbol2scm ("parser"), self_scm ());
170   lexer_->push_extra_token (Input (), EMBEDDED_LILY);
171   SCM result = do_yyparse ();
172
173   lexer_->set_identifier (ly_symbol2scm ("parser"), parser);
174   scm_set_current_module (mod);
175
176   error_level_ = error_level_ | lexer_->error_level_;
177   return result;
178 }
179
180 void
181 Lily_parser::include_string (const string &ly_code)
182 {
183   lexer_->new_input ("<included string>", ly_code, sources_);
184 }
185
186 void
187 Lily_parser::clear ()
188 {
189   if (lexer_)
190     {
191       while (lexer_->has_scope ())
192         lexer_->remove_scope ();
193     }
194
195   lexer_ = 0;
196 }
197
198 char const *
199 Lily_parser::here_str0 () const
200 {
201   return lexer_->here_str0 ();
202 }
203
204 void
205 Lily_parser::parser_error (const string &s)
206 {
207   lexer_->here_input ().error (_ (s.c_str ()));
208   error_level_ = 1;
209 }
210
211 void
212 Lily_parser::parser_error (Input const &i, const string &s)
213 {
214   i.error (s);
215   error_level_ = 1;
216 }
217
218 IMPLEMENT_SMOBS (Lily_parser);
219 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
220 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
221
222 /****************************************************************
223   OUTPUT-DEF
224  ****************************************************************/
225
226 Output_def *
227 get_layout (Lily_parser *parser)
228 {
229   SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
230   Output_def *layout = unsmob_output_def (id);
231   layout = layout ? layout->clone () : new Output_def;
232   layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
233
234   return layout;
235 }
236
237 Output_def *
238 get_midi (Lily_parser *parser)
239 {
240   SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
241   Output_def *layout = unsmob_output_def (id);
242   layout = layout ? layout->clone () : new Output_def;
243   layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
244   return layout;
245 }
246
247 /* Return a copy of the top of $papers stack, or $defaultpaper if the
248  * stack is empty */
249 Output_def *
250 get_paper (Lily_parser *parser)
251 {
252   SCM papers = parser->lexer_->lookup_identifier ("$papers");
253   Output_def *layout = ((papers == SCM_UNDEFINED) || scm_is_null (papers))
254                        ? 0 : unsmob_output_def (scm_car (papers));
255   SCM default_paper = parser->lexer_->lookup_identifier ("$defaultpaper");
256   layout = layout ? layout : unsmob_output_def (default_paper);
257
258   layout = layout ? dynamic_cast<Output_def *> (layout->clone ()) : new Output_def;
259   layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
260   return layout;
261 }
262
263 /* Initialize (reset) the $papers stack */
264 void
265 init_papers (Lily_parser *parser)
266 {
267   parser->lexer_->set_identifier (ly_symbol2scm ("$papers"), SCM_EOL);
268 }
269
270 /* Push a paper on top of $papers stack */
271 void
272 push_paper (Lily_parser *parser, Output_def *paper)
273 {
274   parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
275                                   scm_cons (paper->self_scm (),
276                                             parser->lexer_->lookup_identifier ("$papers")));
277 }
278
279 /* Pop a paper from $papers stack */
280 void
281 pop_paper (Lily_parser *parser)
282 {
283   if (scm_is_pair (parser->lexer_->lookup_identifier ("$papers")))
284     parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
285                                     scm_cdr (parser->lexer_->lookup_identifier ("$papers")));
286 }
287
288 /* Change the paper on top of $papers stack */
289 void
290 set_paper (Lily_parser *parser, Output_def *paper)
291 {
292   scm_set_car_x (parser->lexer_->lookup_identifier ("$papers"), paper->self_scm ());
293 }
294
295 SCM
296 get_header (Lily_parser *parser)
297 {
298   SCM id = parser->lexer_->lookup_identifier ("$defaultheader");
299   if (!ly_is_module (id))
300     id = parser->make_scope ();
301   else
302     {
303       SCM nid = parser->make_scope ();
304       ly_module_copy (nid, id);
305       id = nid;
306     }
307
308   return id;
309 }
310
311 SCM
312 Lily_parser::make_scope () const
313 {
314   SCM module = ly_make_module (be_safe_global);
315   return module;
316 }