2 lily-parser.cc -- implement Lily_parser
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
10 #include "lily-parser.hh"
11 #include "text-metrics.hh"
13 #include "lilypond-key.hh"
14 #include "lily-version.hh"
15 #include "ly-module.hh"
17 #include "lily-lexer.hh"
18 #include "output-def.hh"
19 #include "paper-book.hh"
22 #include "file-name.hh"
23 #include "file-path.hh"
27 Lily_parser::Lily_parser (Sources *sources)
33 default_duration_ = Duration (2, 0);
35 last_beam_start_ = SCM_EOL;
40 Lily_parser::Lily_parser (Lily_parser const &src)
42 book_count_ = src.book_count_;
43 score_count_ = src.score_count_;
45 sources_ = src.sources_;
46 default_duration_ = src.default_duration_;
47 error_level_ = src.error_level_;
48 last_beam_start_ = src.last_beam_start_;
52 lexer_ = new Lily_lexer (*src.lexer_);
54 scm_gc_unprotect_object (lexer_->self_scm ());
57 Lily_parser::~Lily_parser ()
61 #include "ly-smobs.icc"
63 IMPLEMENT_SMOBS (Lily_parser);
64 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
65 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
68 Lily_parser::mark_smob (SCM s)
70 Lily_parser *parser = (Lily_parser*) SCM_CELL_WORD_1 (s);
71 return (parser->lexer_) ? parser->lexer_->self_scm () : SCM_EOL;
75 Lily_parser::print_smob (SCM s, SCM port, scm_print_state*)
77 scm_puts ("#<my_lily_parser ", port);
78 Lily_parser *parser = (Lily_parser*) SCM_CELL_WORD_1 (s);
80 scm_puts (" >", port);
85 /* Process one .ly file, or book. */
87 Lily_parser::parse_file (String init, String name, String out_name)
89 if (output_backend_global == "tex")
91 try_load_text_metrics (out_name);
94 lexer_ = new Lily_lexer (sources_);
95 scm_gc_unprotect_object (lexer_->self_scm ());
97 lexer_->set_identifier (ly_symbol2scm ("parser"),
99 output_basename_ = out_name;
101 lexer_->main_input_name_ = name;
103 progress_indication (_ ("Parsing..."));
104 progress_indication ("\n");
108 lexer_->new_input (init, sources_);
111 String s = global_path.find (f.base_ + ".twy");
112 s = gulp_file_to_string (s, false);
113 scm_eval_string (scm_makfrom0str (s.to_str0 ()));
115 /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
116 OUT_FILE (unless IN_FILE redefines output file name). */
119 if (!define_spots_.is_empty ())
121 define_spots_.top ().warning (_ ("Braces don't match"));
125 error_level_ = error_level_ | lexer_->error_level_;
130 Lily_parser::parse_string (String ly_code)
132 Lily_lexer * parent = lexer_;
133 lexer_ = (parent == 0 ? new Lily_lexer (sources_)
134 : new Lily_lexer (*parent));
135 scm_gc_unprotect_object (lexer_->self_scm ());
138 SCM oldmod = scm_current_module ();
139 scm_set_current_module (scm_car (lexer_->scopes_));
142 lexer_->set_identifier (ly_symbol2scm ("parser"),
145 lexer_->main_input_name_ = "<string>";
146 lexer_->main_input_b_ = true;
149 lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
152 if (!define_spots_.is_empty ())
154 if (define_spots_.is_empty()
156 programming_error ("define_spots_ don't match, but error_level_ not set.");
159 error_level_ = error_level_ | lexer_->error_level_;
161 scm_set_current_module (oldmod);
166 Lily_parser::push_spot ()
168 define_spots_.push (here_input ());
172 Lily_parser::here_str0 () const
174 return lexer_->here_str0 ();
178 Lily_parser::parser_error (String s)
180 here_input ().error (s);
185 Lily_parser::pop_spot ()
187 return define_spots_.pop ();
191 Lily_parser::here_input () const
194 Parsing looks ahead , so we really want the previous location of the
195 lexer, not lexer_->here_input ().
199 Actually, that gets very icky when there are white space, because
200 the line-numbers are all wrong. Let's try the character before
201 the current token. That gets the right result for note/duration
202 stuff, but doesn't mess up for errors in the 1st token of the
205 Input hi (lexer_->here_input ());
207 char const * bla = hi.defined_str0_;
208 if (hi.line_number () > 1
209 || hi.column_number () > 1)
212 return Input (hi.source_file_, bla);
216 /****************************************************************/
220 get_layout (Lily_parser *parser)
222 SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
223 Output_def *layout = unsmob_output_def (id);
224 layout = layout ? layout->clone () : new Output_def;
225 layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
232 get_midi (Lily_parser *parser)
234 SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
235 Output_def *layout = unsmob_output_def (id);
236 layout = layout ? layout->clone () : new Output_def;
237 layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
243 get_paper (Lily_parser *parser)
245 SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
246 Output_def *layout = unsmob_output_def (id);
248 layout = layout ? dynamic_cast<Output_def*> (layout->clone ()) : new Output_def;
249 layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);