2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 Jan Nieuwenhuizen <janneke@gnu.org>
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.
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.
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/>.
21 #include "lily-parser.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"
31 #include "output-def.hh"
32 #include "paper-book.hh"
37 #include "program-option.hh"
39 #include "ly-smobs.icc"
41 Lily_parser::Lily_parser (Sources *sources)
45 default_duration_ = Duration (2, 0);
47 local_environment_ = SCM_UNDEFINED;
51 lexer_ = new Lily_lexer (sources_, this);
55 Lily_parser::Lily_parser (Lily_parser const &src, SCM env)
58 sources_ = src.sources_;
59 default_duration_ = src.default_duration_;
61 output_basename_ = src.output_basename_;
62 local_environment_ = env;
67 lexer_ = new Lily_lexer (*src.lexer_, this);
73 Lily_parser::~Lily_parser ()
78 Lily_parser::mark_smob (SCM s)
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;
86 Lily_parser::print_smob (SCM s, SCM port, scm_print_state *)
88 scm_puts ("#<Lily_parser ", port);
89 Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
91 scm_display (parser->lexer_->self_scm (), port);
93 scm_puts ("(no lexer yet)", port);
94 scm_puts (" >", port);
98 /* Process one .ly file, or book. */
100 Lily_parser::parse_file (string init, string name, string out_name)
103 lexer_->set_identifier (ly_symbol2scm ("parser"), self_scm ());
104 output_basename_ = out_name;
106 lexer_->main_input_name_ = name;
108 message (_ ("Parsing..."));
112 lexer_->new_input (init, sources_);
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));
119 /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
120 OUT_FILE (unless IN_FILE redefines output file name). */
122 SCM mod = lexer_->set_current_scope ();
126 Don't mix cyclic pointers with weak tables.
128 lexer_->set_identifier (ly_symbol2scm ("parser"),
130 ly_reexport_module (scm_current_module ());
132 scm_set_current_module (mod);
134 error_level_ = error_level_ | lexer_->error_level_;
139 Lily_parser::parse_string (string ly_code)
142 lexer_->set_identifier (ly_symbol2scm ("parser"),
145 lexer_->main_input_name_ = "<string>";
146 lexer_->is_main_input_ = true;
147 lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
149 SCM mod = lexer_->set_current_scope ();
151 scm_set_current_module (mod);
153 error_level_ = error_level_ | lexer_->error_level_;
157 Lily_parser::parse_string_expression (string ly_code)
160 lexer_->set_identifier (ly_symbol2scm ("parser"),
163 lexer_->main_input_name_ = "<string>";
164 lexer_->is_main_input_ = true;
165 lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
167 SCM mod = lexer_->set_current_scope ();
168 lexer_->push_extra_token (EMBEDDED_LILY);
170 SCM result = lexer_->lookup_identifier_symbol (ly_symbol2scm ("parseStringResult"));
171 // parseStringResult is set in the grammar rule for embedded_lilypond
173 scm_set_current_module (mod);
175 error_level_ = error_level_ | lexer_->error_level_;
180 Lily_parser::include_string (string ly_code)
182 lexer_->new_input ("<included string>", ly_code, sources_);
186 Lily_parser::clear ()
190 while (lexer_->has_scope ())
191 lexer_->remove_scope ();
198 Lily_parser::here_str0 () const
200 return lexer_->here_str0 ();
204 Lily_parser::parser_error (string s)
206 lexer_->here_input ().error (_ (s.c_str ()));
211 Lily_parser::parser_error (Input const &i, string s)
217 IMPLEMENT_SMOBS (Lily_parser);
218 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
219 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
221 /****************************************************************
223 ****************************************************************/
226 get_layout (Lily_parser *parser)
228 SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
229 Output_def *layout = unsmob_output_def (id);
230 layout = layout ? layout->clone () : new Output_def;
231 layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
237 get_midi (Lily_parser *parser)
239 SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
240 Output_def *layout = unsmob_output_def (id);
241 layout = layout ? layout->clone () : new Output_def;
242 layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
246 /* Return a copy of the top of $papers stack, or $defaultpaper if the
249 get_paper (Lily_parser *parser)
251 SCM papers = parser->lexer_->lookup_identifier ("$papers");
252 Output_def *layout = ((papers == SCM_UNDEFINED) || scm_is_null (papers))
253 ? 0 : unsmob_output_def (scm_car (papers));
254 SCM default_paper = parser->lexer_->lookup_identifier ("$defaultpaper");
255 layout = layout ? layout : unsmob_output_def (default_paper);
257 layout = layout ? dynamic_cast<Output_def *> (layout->clone ()) : new Output_def;
258 layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
262 /* Initialize (reset) the $papers stack */
264 init_papers (Lily_parser *parser)
266 parser->lexer_->set_identifier (ly_symbol2scm ("$papers"), SCM_EOL);
269 /* Push a paper on top of $papers stack */
271 push_paper (Lily_parser *parser, Output_def *paper)
273 parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
274 scm_cons (paper->self_scm (),
275 parser->lexer_->lookup_identifier ("$papers")));
278 /* Pop a paper from $papers stack */
280 pop_paper (Lily_parser *parser)
282 if (scm_is_pair (parser->lexer_->lookup_identifier ("$papers")))
283 parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
284 scm_cdr (parser->lexer_->lookup_identifier ("$papers")));
287 /* Change the paper on top of $papers stack */
289 set_paper (Lily_parser *parser, Output_def *paper)
291 scm_set_car_x (parser->lexer_->lookup_identifier ("$papers"), paper->self_scm ());
295 get_header (Lily_parser *parser)
297 SCM id = parser->lexer_->lookup_identifier ("$defaultheader");
298 if (!ly_is_module (id))
299 id = parser->make_scope ();
302 SCM nid = parser->make_scope ();
303 ly_module_copy (nid, id);
311 Lily_parser::make_scope () const
313 SCM module = ly_make_module (be_safe_global);