2 lily-parser.cc -- implement Lily_parser
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
10 #include "lily-parser.hh"
13 #include "file-name.hh"
14 #include "file-path.hh"
15 #include "international.hh"
16 #include "lily-lexer.hh"
17 #include "lily-version.hh"
19 #include "output-def.hh"
20 #include "paper-book.hh"
24 #include "text-metrics.hh"
26 #include "program-option.hh"
28 #include "ly-smobs.icc"
30 Lily_parser::Lily_parser (Sources *sources)
34 default_duration_ = Duration (2, 0);
39 lexer_ = new Lily_lexer (sources_, this);
43 Lily_parser::Lily_parser (Lily_parser const &src)
46 sources_ = src.sources_;
47 default_duration_ = src.default_duration_;
48 error_level_ = src.error_level_;
49 output_basename_ = src.output_basename_;
54 lexer_ = new Lily_lexer (*src.lexer_, this);
60 Lily_parser::~Lily_parser ()
66 Lily_parser::mark_smob (SCM s)
68 Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
69 return (parser->lexer_) ? parser->lexer_->self_scm () : SCM_EOL;
73 Lily_parser::print_smob (SCM s, SCM port, scm_print_state*)
75 scm_puts ("#<Lily_parser ", port);
76 Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
78 scm_display (parser->lexer_->self_scm (), port);
80 scm_puts ("(no lexer yet)", port);
81 scm_puts (" >", port);
85 /* Process one .ly file, or book. */
87 Lily_parser::parse_file (string init, string name, string out_name)
89 if (get_output_backend_name () == "tex")
90 try_load_text_metrics (out_name);
93 lexer_->set_identifier (ly_symbol2scm ("parser"), self_scm ());
94 output_basename_ = out_name;
96 lexer_->main_input_name_ = name;
98 message (_ ("Parsing..."));
102 lexer_->new_input (init, sources_);
105 string s = global_path.find (f.base_ + ".twy");
106 s = gulp_file_to_string (s, false, -1);
107 scm_eval_string (ly_string2scm (s));
109 /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
110 OUT_FILE (unless IN_FILE redefines output file name). */
112 SCM mod = lexer_->set_current_scope ();
116 Don't mix cyclic pointers with weak tables.
118 lexer_->set_identifier (ly_symbol2scm ("parser"),
120 ly_reexport_module (scm_current_module ());
122 scm_set_current_module (mod);
124 if (!define_spots_.empty ())
126 define_spots_.back ().warning (_ ("braces do not match"));
130 error_level_ = error_level_ | lexer_->error_level_;
135 Lily_parser::parse_string (string ly_code)
138 lexer_->set_identifier (ly_symbol2scm ("parser"),
141 lexer_->main_input_name_ = "<string>";
142 lexer_->is_main_input_ = true;
143 lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
145 SCM mod = lexer_->set_current_scope ();
147 scm_set_current_module (mod);
149 if (!define_spots_.empty ())
151 if (define_spots_.empty ()
153 programming_error ("define_spots_ don't match, but error_level_ not set.");
156 error_level_ = error_level_ | lexer_->error_level_;
160 Lily_parser::clear ()
164 while (lexer_->has_scope ())
165 lexer_->remove_scope ();
172 Lily_parser::here_str0 () const
174 return lexer_->here_str0 ();
178 Lily_parser::parser_error (string s)
180 lexer_->here_input ().error (_ (s.c_str ()));
185 Lily_parser::parser_error (Input const &i, string s)
193 IMPLEMENT_SMOBS (Lily_parser);
194 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
195 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
198 /****************************************************************
200 ****************************************************************/
203 get_layout (Lily_parser *parser)
205 SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
206 Output_def *layout = unsmob_output_def (id);
207 layout = layout ? layout->clone () : new Output_def;
208 layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
214 get_midi (Lily_parser *parser)
216 SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
217 Output_def *layout = unsmob_output_def (id);
218 layout = layout ? layout->clone () : new Output_def;
219 layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
224 get_paper (Lily_parser *parser)
226 SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
227 Output_def *layout = unsmob_output_def (id);
229 layout = layout ? dynamic_cast<Output_def *> (layout->clone ()) : new Output_def;
230 layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
235 get_header (Lily_parser *parser)
237 SCM id = parser->lexer_->lookup_identifier ("$defaultheader");
238 if (!ly_is_module (id))
239 id = parser->make_scope ();
242 SCM nid = parser->make_scope ();
243 ly_module_copy (nid, id);
251 Lily_parser::make_scope () const
253 return ly_make_anonymous_module (be_safe_global);