Restore MIDI output for DrumStaff.
[lilypond/mpolesky.git] / lily / lily-parser.cc
blobeb59e7bda331038f4c7d1ad7f695ae64831f39a3
1 /*
2 lily-parser.cc -- implement Lily_parser
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
8 */
10 #include "lily-parser.hh"
12 #include "book.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"
18 #include "ly-module.hh"
19 #include "main.hh"
20 #include "output-def.hh"
21 #include "paper-book.hh"
22 #include "parser.hh"
23 #include "score.hh"
24 #include "sources.hh"
25 #include "warn.hh"
26 #include "program-option.hh"
28 #include "ly-smobs.icc"
30 Lily_parser::Lily_parser (Sources *sources)
32 lexer_ = 0;
33 sources_ = sources;
34 default_duration_ = Duration (2, 0);
35 error_level_ = 0;
37 smobify_self ();
39 lexer_ = new Lily_lexer (sources_, this);
40 lexer_->unprotect ();
43 Lily_parser::Lily_parser (Lily_parser const &src)
45 lexer_ = 0;
46 sources_ = src.sources_;
47 default_duration_ = src.default_duration_;
48 error_level_ = src.error_level_;
49 output_basename_ = src.output_basename_;
51 smobify_self ();
52 if (src.lexer_)
54 lexer_ = new Lily_lexer (*src.lexer_, this);
57 lexer_->unprotect ();
60 Lily_parser::~Lily_parser ()
65 SCM
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;
72 int
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);
77 if (parser->lexer_)
78 scm_display (parser->lexer_->self_scm (), port);
79 else
80 scm_puts ("(no lexer yet)", port);
81 scm_puts (" >", port);
82 return 1;
85 /* Process one .ly file, or book. */
86 void
87 Lily_parser::parse_file (string init, string name, string out_name)
89 // TODO: use $parser
90 lexer_->set_identifier (ly_symbol2scm ("parser"), self_scm ());
91 output_basename_ = out_name;
93 lexer_->main_input_name_ = name;
95 message (_ ("Parsing..."));
97 set_yydebug (0);
99 lexer_->new_input (init, sources_);
101 File_name f (name);
102 string s = global_path.find (f.base_ + ".twy");
103 s = gulp_file_to_string (s, false, -1);
104 scm_eval_string (ly_string2scm (s));
106 /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
107 OUT_FILE (unless IN_FILE redefines output file name). */
109 SCM mod = lexer_->set_current_scope ();
110 do_yyparse ();
113 Don't mix cyclic pointers with weak tables.
115 lexer_->set_identifier (ly_symbol2scm ("parser"),
116 SCM_EOL);
117 ly_reexport_module (scm_current_module ());
119 scm_set_current_module (mod);
121 if (!define_spots_.empty ())
123 define_spots_.back ().warning (_ ("braces do not match"));
124 error_level_ = 1;
127 error_level_ = error_level_ | lexer_->error_level_;
128 clear ();
131 void
132 Lily_parser::parse_string (string ly_code)
134 // TODO: use $parser
135 lexer_->set_identifier (ly_symbol2scm ("parser"),
136 self_scm ());
138 lexer_->main_input_name_ = "<string>";
139 lexer_->is_main_input_ = true;
140 lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
142 SCM mod = lexer_->set_current_scope ();
143 do_yyparse ();
144 scm_set_current_module (mod);
146 if (!define_spots_.empty ())
148 if (define_spots_.empty ()
149 && !error_level_)
150 programming_error ("define_spots_ don't match, but error_level_ not set.");
153 error_level_ = error_level_ | lexer_->error_level_;
156 void
157 Lily_parser::clear ()
159 if (lexer_)
161 while (lexer_->has_scope ())
162 lexer_->remove_scope ();
165 lexer_ = 0;
168 char const *
169 Lily_parser::here_str0 () const
171 return lexer_->here_str0 ();
174 void
175 Lily_parser::parser_error (string s)
177 lexer_->here_input ().error (_ (s.c_str ()));
178 error_level_ = 1;
181 void
182 Lily_parser::parser_error (Input const &i, string s)
184 i.error (s);
185 error_level_ = 1;
190 IMPLEMENT_SMOBS (Lily_parser);
191 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
192 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
195 /****************************************************************
196 OUTPUT-DEF
197 ****************************************************************/
199 Output_def *
200 get_layout (Lily_parser *parser)
202 SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
203 Output_def *layout = unsmob_output_def (id);
204 layout = layout ? layout->clone () : new Output_def;
205 layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
207 return layout;
210 Output_def *
211 get_midi (Lily_parser *parser)
213 SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
214 Output_def *layout = unsmob_output_def (id);
215 layout = layout ? layout->clone () : new Output_def;
216 layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
217 return layout;
220 /* Return a copy of the top of $papers stack, or $defaultpaper if the
221 * stack is empty */
222 Output_def *
223 get_paper (Lily_parser *parser)
225 SCM papers = parser->lexer_->lookup_identifier ("$papers");
226 Output_def *layout = ((papers == SCM_UNDEFINED) || scm_is_null (papers)) ?
227 0 : unsmob_output_def (scm_car (papers));
228 SCM default_paper = parser->lexer_->lookup_identifier ("$defaultpaper");
229 layout = layout ? layout : unsmob_output_def (default_paper);
231 layout = layout ? dynamic_cast<Output_def *> (layout->clone ()) : new Output_def;
232 layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
233 return layout;
236 /* Initialize (reset) the $papers stack */
237 void
238 init_papers (Lily_parser *parser)
240 parser->lexer_->set_identifier (ly_symbol2scm ("$papers"), SCM_EOL);
243 /* Push a paper on top of $papers stack */
244 void
245 push_paper (Lily_parser *parser, Output_def *paper)
247 parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
248 scm_cons (paper->self_scm (),
249 parser->lexer_->lookup_identifier ("$papers")));
252 /* Pop a paper from $papers stack */
253 void
254 pop_paper (Lily_parser *parser)
256 if (! scm_is_null (parser->lexer_->lookup_identifier ("$papers")))
257 parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
258 scm_cdr (parser->lexer_->lookup_identifier ("$papers")));
261 /* Change the paper on top of $papers stack */
262 void
263 set_paper (Lily_parser *parser, Output_def *paper)
265 scm_set_car_x (parser->lexer_->lookup_identifier ("$papers"), paper->self_scm ());
269 get_header (Lily_parser *parser)
271 SCM id = parser->lexer_->lookup_identifier ("$defaultheader");
272 if (!ly_is_module (id))
273 id = parser->make_scope ();
274 else
276 SCM nid = parser->make_scope ();
277 ly_module_copy (nid, id);
278 id = nid;
281 return id;
284 SCM
285 Lily_parser::make_scope () const
287 SCM module = ly_make_anonymous_module (be_safe_global);
288 return module;