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);
50 lexer_ = new Lily_lexer (sources_, this);
54 Lily_parser::Lily_parser (Lily_parser const &src)
57 sources_ = src.sources_;
58 default_duration_ = src.default_duration_;
59 error_level_ = src.error_level_;
60 output_basename_ = src.output_basename_;
65 lexer_ = new Lily_lexer (*src.lexer_, this);
71 Lily_parser::~Lily_parser ()
76 Lily_parser::mark_smob (SCM s)
78 Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
79 return (parser->lexer_) ? parser->lexer_->self_scm () : SCM_EOL;
83 Lily_parser::print_smob (SCM s, SCM port, scm_print_state *)
85 scm_puts ("#<Lily_parser ", port);
86 Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
88 scm_display (parser->lexer_->self_scm (), port);
90 scm_puts ("(no lexer yet)", port);
91 scm_puts (" >", port);
95 /* Process one .ly file, or book. */
97 Lily_parser::parse_file (string init, string name, string out_name)
100 lexer_->set_identifier (ly_symbol2scm ("parser"), self_scm ());
101 output_basename_ = out_name;
103 lexer_->main_input_name_ = name;
105 message (_ ("Parsing..."));
109 lexer_->new_input (init, sources_);
112 string s = global_path.find (f.base_ + ".twy");
113 s = gulp_file_to_string (s, false, -1);
114 scm_eval_string (ly_string2scm (s));
116 /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
117 OUT_FILE (unless IN_FILE redefines output file name). */
119 SCM mod = lexer_->set_current_scope ();
123 Don't mix cyclic pointers with weak tables.
125 lexer_->set_identifier (ly_symbol2scm ("parser"),
127 ly_reexport_module (scm_current_module ());
129 scm_set_current_module (mod);
131 if (!define_spots_.empty ())
133 define_spots_.back ().warning (_ ("braces do not match"));
137 error_level_ = error_level_ | lexer_->error_level_;
142 Lily_parser::parse_string (string ly_code)
145 lexer_->set_identifier (ly_symbol2scm ("parser"),
148 lexer_->main_input_name_ = "<string>";
149 lexer_->is_main_input_ = true;
150 lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
152 SCM mod = lexer_->set_current_scope ();
154 scm_set_current_module (mod);
156 if (!define_spots_.empty ())
158 if (define_spots_.empty ()
160 programming_error ("define_spots_ don't match, but error_level_ not set.");
163 error_level_ = error_level_ | lexer_->error_level_;
167 Lily_parser::include_string (string ly_code)
169 lexer_->add_string_include (ly_code);
173 Lily_parser::clear ()
177 while (lexer_->has_scope ())
178 lexer_->remove_scope ();
185 Lily_parser::here_str0 () const
187 return lexer_->here_str0 ();
191 Lily_parser::parser_error (string s)
193 lexer_->here_input ().error (_ (s.c_str ()));
198 Lily_parser::parser_error (Input const &i, string s)
204 IMPLEMENT_SMOBS (Lily_parser);
205 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
206 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
208 /****************************************************************
210 ****************************************************************/
213 get_layout (Lily_parser *parser)
215 SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
216 Output_def *layout = unsmob_output_def (id);
217 layout = layout ? layout->clone () : new Output_def;
218 layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
224 get_midi (Lily_parser *parser)
226 SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
227 Output_def *layout = unsmob_output_def (id);
228 layout = layout ? layout->clone () : new Output_def;
229 layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
233 /* Return a copy of the top of $papers stack, or $defaultpaper if the
236 get_paper (Lily_parser *parser)
238 SCM papers = parser->lexer_->lookup_identifier ("$papers");
239 Output_def *layout = ((papers == SCM_UNDEFINED) || scm_is_null (papers))
240 ? 0 : unsmob_output_def (scm_car (papers));
241 SCM default_paper = parser->lexer_->lookup_identifier ("$defaultpaper");
242 layout = layout ? layout : unsmob_output_def (default_paper);
244 layout = layout ? dynamic_cast<Output_def *> (layout->clone ()) : new Output_def;
245 layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
249 /* Initialize (reset) the $papers stack */
251 init_papers (Lily_parser *parser)
253 parser->lexer_->set_identifier (ly_symbol2scm ("$papers"), SCM_EOL);
256 /* Push a paper on top of $papers stack */
258 push_paper (Lily_parser *parser, Output_def *paper)
260 parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
261 scm_cons (paper->self_scm (),
262 parser->lexer_->lookup_identifier ("$papers")));
265 /* Pop a paper from $papers stack */
267 pop_paper (Lily_parser *parser)
269 if (scm_is_pair (parser->lexer_->lookup_identifier ("$papers")))
270 parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
271 scm_cdr (parser->lexer_->lookup_identifier ("$papers")));
274 /* Change the paper on top of $papers stack */
276 set_paper (Lily_parser *parser, Output_def *paper)
278 scm_set_car_x (parser->lexer_->lookup_identifier ("$papers"), paper->self_scm ());
282 get_header (Lily_parser *parser)
284 SCM id = parser->lexer_->lookup_identifier ("$defaultheader");
285 if (!ly_is_module (id))
286 id = parser->make_scope ();
289 SCM nid = parser->make_scope ();
290 ly_module_copy (nid, id);
298 Lily_parser::make_scope () const
300 SCM module = ly_make_module (be_safe_global);