]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser.cc
Merge remote branch 'origin/master' into release/unstable
[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
41 Lily_parser::Lily_parser (Sources *sources)
42 {
43   lexer_ = 0;
44   sources_ = sources;
45   default_duration_ = Duration (2, 0);
46   default_tremolo_type_ = 8;
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   default_tremolo_type_ = src.default_tremolo_type_;
62   error_level_ = 0;
63   output_basename_ = src.output_basename_;
64   closures_ = closures;
65
66   smobify_self ();
67   if (src.lexer_)
68     {
69       lexer_ = new Lily_lexer (*src.lexer_, this, location);
70       lexer_->unprotect ();
71     }
72 }
73
74 Lily_parser::~Lily_parser ()
75 {
76 }
77
78 SCM
79 Lily_parser::mark_smob ()
80 {
81   scm_gc_mark (closures_);
82   return (lexer_) ? lexer_->self_scm () : SCM_EOL;
83 }
84
85 int
86 Lily_parser::print_smob (SCM port, scm_print_state *)
87 {
88   scm_puts ("#<Lily_parser ", port);
89   if (lexer_)
90     scm_display (lexer_->self_scm (), port);
91   else
92     scm_puts ("(no lexer yet)", port);
93   scm_puts (" >", port);
94   return 1;
95 }
96
97 /* Process one .ly file, or book.  */
98 void
99 Lily_parser::parse_file (const string &init, const string &name, const string &out_name)
100 {
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
122     {
123       do_yyparse ();
124     }
125   while (!lexer_->is_clean ());
126
127   /*
128     Don't mix cyclic pointers with weak tables.
129   */
130   lexer_->set_identifier (ly_symbol2scm ("parser"),
131                           SCM_EOL);
132   ly_reexport_module (scm_current_module ());
133
134   scm_set_current_module (mod);
135
136   error_level_ = error_level_ | lexer_->error_level_;
137   clear ();
138 }
139
140 void
141 Lily_parser::parse_string (const string &ly_code)
142 {
143   lexer_->main_input_name_ = "<string>";
144   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
145
146   SCM mod = lexer_->set_current_scope ();
147   SCM parser = lexer_->lookup_identifier_symbol (ly_symbol2scm ("parser"));
148   lexer_->set_identifier (ly_symbol2scm ("parser"), self_scm ());
149   do_yyparse ();
150   lexer_->set_identifier (ly_symbol2scm ("parser"), parser);
151   scm_set_current_module (mod);
152
153   error_level_ = error_level_ | lexer_->error_level_;
154 }
155
156 SCM
157 Lily_parser::parse_string_expression (const string &ly_code, const string &filename,
158                                       int line)
159 {
160   lexer_->main_input_name_ = filename;
161   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
162   if (line)
163     {
164       lexer_->get_source_file ()->set_line (0, line);
165     }
166   SCM mod = lexer_->set_current_scope ();
167   SCM parser = lexer_->lookup_identifier_symbol (ly_symbol2scm ("parser"));
168   lexer_->set_identifier (ly_symbol2scm ("parser"), self_scm ());
169   lexer_->push_extra_token (Input (), EMBEDDED_LILY);
170   SCM result = do_yyparse ();
171
172   lexer_->set_identifier (ly_symbol2scm ("parser"), parser);
173   scm_set_current_module (mod);
174
175   error_level_ = error_level_ | lexer_->error_level_;
176   return result;
177 }
178
179 void
180 Lily_parser::include_string (const string &ly_code)
181 {
182   lexer_->new_input ("<included string>", ly_code, sources_);
183 }
184
185 void
186 Lily_parser::clear ()
187 {
188   if (lexer_)
189     {
190       while (lexer_->has_scope ())
191         lexer_->remove_scope ();
192     }
193
194   lexer_ = 0;
195 }
196
197 void
198 Lily_parser::parser_error (const string &s)
199 {
200   lexer_->here_input ().error (_ (s.c_str ()));
201   error_level_ = 1;
202 }
203
204 void
205 Lily_parser::parser_error (Input const &i, const string &s)
206 {
207   i.error (s);
208   error_level_ = 1;
209 }
210
211 const char Lily_parser::type_p_name_[] = "ly:lily-parser?";
212
213 /****************************************************************
214   OUTPUT-DEF
215  ****************************************************************/
216
217 Output_def *
218 get_layout (Lily_parser *parser)
219 {
220   SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
221   Output_def *layout = Output_def::unsmob (id);
222   layout = layout ? layout->clone () : new Output_def;
223   layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
224
225   return layout;
226 }
227
228 Output_def *
229 get_midi (Lily_parser *parser)
230 {
231   SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
232   Output_def *layout = Output_def::unsmob (id);
233   layout = layout ? layout->clone () : new Output_def;
234   layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
235   return layout;
236 }
237
238 /* Return a copy of the top of $papers stack, or $defaultpaper if the
239  * stack is empty */
240 Output_def *
241 get_paper (Lily_parser *parser)
242 {
243   SCM papers = parser->lexer_->lookup_identifier ("$papers");
244   Output_def *layout = ((papers == SCM_UNDEFINED) || scm_is_null (papers))
245                        ? 0 : Output_def::unsmob (scm_car (papers));
246   SCM default_paper = parser->lexer_->lookup_identifier ("$defaultpaper");
247   layout = layout ? layout : Output_def::unsmob (default_paper);
248
249   layout = layout ? dynamic_cast<Output_def *> (layout->clone ()) : new Output_def;
250   layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
251   return layout;
252 }
253
254 /* Initialize (reset) the $papers stack */
255 void
256 init_papers (Lily_parser *parser)
257 {
258   parser->lexer_->set_identifier (ly_symbol2scm ("$papers"), SCM_EOL);
259 }
260
261 /* Push a paper on top of $papers stack */
262 void
263 push_paper (Lily_parser *parser, Output_def *paper)
264 {
265   parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
266                                   scm_cons (paper->self_scm (),
267                                             parser->lexer_->lookup_identifier ("$papers")));
268 }
269
270 /* Pop a paper from $papers stack */
271 void
272 pop_paper (Lily_parser *parser)
273 {
274   if (scm_is_pair (parser->lexer_->lookup_identifier ("$papers")))
275     parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
276                                     scm_cdr (parser->lexer_->lookup_identifier ("$papers")));
277 }
278
279 /* Change the paper on top of $papers stack */
280 void
281 set_paper (Lily_parser *parser, Output_def *paper)
282 {
283   scm_set_car_x (parser->lexer_->lookup_identifier ("$papers"), paper->self_scm ());
284 }
285
286 SCM
287 get_header (Lily_parser *parser)
288 {
289   SCM id = parser->lexer_->lookup_identifier ("$defaultheader");
290   if (!ly_is_module (id))
291     id = parser->make_scope ();
292   else
293     {
294       SCM nid = parser->make_scope ();
295       ly_module_copy (nid, id);
296       id = nid;
297     }
298
299   return id;
300 }
301
302 SCM
303 Lily_parser::make_scope () const
304 {
305   SCM module = ly_make_module (be_safe_global);
306   return module;
307 }