2 lily-parser.cc -- implement Lily_parser
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 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"
16 #include "lily-lexer.hh"
17 #include "output-def.hh"
18 #include "paper-book.hh"
21 #include "file-name.hh"
22 #include "file-path.hh"
26 Lily_parser::Lily_parser (Sources *sources)
30 default_duration_ = Duration (2, 0);
35 lexer_ = new Lily_lexer (sources_);
39 Lily_parser::Lily_parser (Lily_parser const &src)
42 sources_ = src.sources_;
43 default_duration_ = src.default_duration_;
44 error_level_ = src.error_level_;
48 lexer_ = new Lily_lexer (*src.lexer_);
53 Lily_parser::~Lily_parser ()
57 #include "ly-smobs.icc"
59 IMPLEMENT_SMOBS (Lily_parser);
60 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
61 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
64 Lily_parser::mark_smob (SCM s)
66 Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
67 return (parser->lexer_) ? parser->lexer_->self_scm () : SCM_EOL;
71 Lily_parser::print_smob (SCM s, SCM port, scm_print_state*)
73 scm_puts ("#<Lily_parser ", port);
74 Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
76 scm_display (parser->lexer_->self_scm (), port);
78 scm_puts ("(no lexer yet)", port);
79 scm_puts (" >", port);
83 /* Process one .ly file, or book. */
85 Lily_parser::parse_file (String init, String name, String out_name)
87 if (output_backend_global == "tex")
88 try_load_text_metrics (out_name);
91 lexer_->set_identifier (ly_symbol2scm ("parser"),
93 output_basename_ = out_name;
95 lexer_->main_input_name_ = name;
97 message (_ ("Parsing..."));
98 // progress_indication ("\n");
102 lexer_->new_input (init, sources_);
105 String s = global_path.find (f.base_ + ".twy");
106 s = gulp_file_to_string (s, false);
107 scm_eval_string (scm_makfrom0str (s.to_str0 ()));
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_.is_empty ())
126 define_spots_.top ().warning (_ ("braces don't 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;
145 lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
147 SCM mod = lexer_->set_current_scope ();
149 scm_set_current_module (mod);
151 if (!define_spots_.is_empty ())
153 if (define_spots_.is_empty ()
155 programming_error ("define_spots_ don't match, but error_level_ not set.");
158 error_level_ = error_level_ | lexer_->error_level_;
162 Lily_parser::here_str0 () const
164 return lexer_->here_str0 ();
168 Lily_parser::parser_error (String s)
170 lexer_->here_input ().error (_ (s.to_str0 ()));
175 Lily_parser::parser_error (Input const &i, String s)
181 /****************************************************************/
184 get_layout (Lily_parser *parser)
186 SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
187 Output_def *layout = unsmob_output_def (id);
188 layout = layout ? layout->clone () : new Output_def;
189 layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
195 get_midi (Lily_parser *parser)
197 SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
198 Output_def *layout = unsmob_output_def (id);
199 layout = layout ? layout->clone () : new Output_def;
200 layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
205 get_paper (Lily_parser *parser)
207 SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
208 Output_def *layout = unsmob_output_def (id);
210 layout = layout ? dynamic_cast<Output_def *> (layout->clone ()) : new Output_def;
211 layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
216 get_header (Lily_parser *parser)
218 SCM id = parser->lexer_->lookup_identifier ("$defaultheader");
219 if (!ly_is_module (id))
220 id = ly_make_anonymous_module (be_safe_global);
223 SCM nid = ly_make_anonymous_module (false);
224 ly_module_copy(nid,id);