2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 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"
35 #include "source-file.hh"
38 #include "program-option.hh"
41 Lily_parser::Lily_parser (Sources *sources)
45 default_duration_ = Duration (2, 0);
46 default_tremolo_type_ = 8;
52 lexer_ = new Lily_lexer (sources_, this);
56 Lily_parser::Lily_parser (Lily_parser const &src, SCM closures, SCM location)
57 : Smob<Lily_parser> ()
60 sources_ = src.sources_;
61 default_duration_ = src.default_duration_;
62 default_tremolo_type_ = src.default_tremolo_type_;
64 output_basename_ = src.output_basename_;
70 lexer_ = new Lily_lexer (*src.lexer_, this, location);
75 Lily_parser::~Lily_parser ()
80 Lily_parser::mark_smob () const
82 scm_gc_mark (closures_);
83 return (lexer_) ? lexer_->self_scm () : SCM_EOL;
87 Lily_parser::print_smob (SCM port, scm_print_state *) const
89 scm_puts ("#<Lily_parser ", port);
91 scm_display (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 (const string &init, const string &name, const string &out_name)
102 output_basename_ = out_name;
104 lexer_->main_input_name_ = name;
106 message (_ ("Parsing..."));
110 lexer_->new_input (init, sources_);
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));
117 /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
118 OUT_FILE (unless IN_FILE redefines output file name). */
120 SCM mod = lexer_->set_current_scope ();
125 while (!lexer_->is_clean ());
127 ly_reexport_module (scm_current_module ());
129 scm_set_current_module (mod);
131 error_level_ = error_level_ | lexer_->error_level_;
136 Lily_parser::parse_string (const string &ly_code)
138 lexer_->main_input_name_ = "<string>";
139 lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
141 SCM mod = lexer_->set_current_scope ();
145 scm_set_current_module (mod);
147 error_level_ = error_level_ | lexer_->error_level_;
151 Lily_parser::parse_string_expression (const string &ly_code, const string &filename,
154 lexer_->main_input_name_ = filename;
155 lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
158 lexer_->get_source_file ()->set_line (0, line);
160 SCM mod = lexer_->set_current_scope ();
162 lexer_->push_extra_token (Input (), EMBEDDED_LILY);
163 SCM result = do_yyparse ();
165 scm_set_current_module (mod);
167 error_level_ = error_level_ | lexer_->error_level_;
172 Lily_parser::include_string (const string &ly_code)
174 lexer_->new_input ("<included string>", ly_code, sources_);
178 Lily_parser::clear ()
182 while (lexer_->has_scope ())
183 lexer_->remove_scope ();
190 Lily_parser::parser_error (const string &s)
192 lexer_->here_input ().non_fatal_error (_ (s.c_str ()));
197 Lily_parser::parser_error (Input const &i, const string &s)
199 i.non_fatal_error (s);
203 const char * const Lily_parser::type_p_name_ = "ly:lily-parser?";
205 /****************************************************************
207 ****************************************************************/
210 get_layout (Lily_parser *parser)
212 SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
213 Output_def *layout = unsmob<Output_def> (id);
214 layout = layout ? layout->clone () : new Output_def;
215 layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
221 get_midi (Lily_parser *parser)
223 SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
224 Output_def *layout = unsmob<Output_def> (id);
225 layout = layout ? layout->clone () : new Output_def;
226 layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
230 /* Return a copy of the top of $papers stack, or $defaultpaper if the
233 get_paper (Lily_parser *parser)
235 SCM papers = parser->lexer_->lookup_identifier ("$papers");
236 Output_def *layout = (SCM_UNBNDP (papers) || scm_is_null (papers))
237 ? 0 : unsmob<Output_def> (scm_car (papers));
238 SCM default_paper = parser->lexer_->lookup_identifier ("$defaultpaper");
239 layout = layout ? layout : unsmob<Output_def> (default_paper);
241 layout = layout ? dynamic_cast<Output_def *> (layout->clone ()) : new Output_def;
242 layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
246 /* Initialize (reset) the $papers stack */
248 init_papers (Lily_parser *parser)
250 parser->lexer_->set_identifier (ly_symbol2scm ("$papers"), SCM_EOL);
253 /* Push a paper on top of $papers stack */
255 push_paper (Lily_parser *parser, Output_def *paper)
257 parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
258 scm_cons (paper->self_scm (),
259 parser->lexer_->lookup_identifier ("$papers")));
262 /* Pop a paper from $papers stack */
264 pop_paper (Lily_parser *parser)
266 if (scm_is_pair (parser->lexer_->lookup_identifier ("$papers")))
267 parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
268 scm_cdr (parser->lexer_->lookup_identifier ("$papers")));
271 /* Change the paper on top of $papers stack */
273 set_paper (Lily_parser *parser, Output_def *paper)
275 scm_set_car_x (parser->lexer_->lookup_identifier ("$papers"), paper->self_scm ());
279 get_header (Lily_parser *parser)
281 SCM id = parser->lexer_->lookup_identifier ("$defaultheader");
282 if (!ly_is_module (id))
283 id = parser->make_scope ();
286 SCM nid = parser->make_scope ();
287 ly_module_copy (nid, id);
295 Lily_parser::make_scope () const
297 SCM module = ly_make_module (be_safe_global);