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