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>
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"
18 #include "ly-module.hh"
20 #include "output-def.hh"
21 #include "paper-book.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
)
90 lexer_
->set_identifier (ly_symbol2scm ("parser"), self_scm ());
91 output_basename_
= out_name
;
93 lexer_
->main_input_name_
= name
;
95 message (_ ("Parsing..."));
99 lexer_
->new_input (init
, sources_
);
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 ();
113 Don't mix cyclic pointers with weak tables.
115 lexer_
->set_identifier (ly_symbol2scm ("parser"),
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"));
127 error_level_
= error_level_
| lexer_
->error_level_
;
132 Lily_parser::parse_string (string ly_code
)
135 lexer_
->set_identifier (ly_symbol2scm ("parser"),
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 ();
144 scm_set_current_module (mod
);
146 if (!define_spots_
.empty ())
148 if (define_spots_
.empty ()
150 programming_error ("define_spots_ don't match, but error_level_ not set.");
153 error_level_
= error_level_
| lexer_
->error_level_
;
157 Lily_parser::clear ()
161 while (lexer_
->has_scope ())
162 lexer_
->remove_scope ();
169 Lily_parser::here_str0 () const
171 return lexer_
->here_str0 ();
175 Lily_parser::parser_error (string s
)
177 lexer_
->here_input ().error (_ (s
.c_str ()));
182 Lily_parser::parser_error (Input
const &i
, string s
)
190 IMPLEMENT_SMOBS (Lily_parser
);
191 IMPLEMENT_TYPE_P (Lily_parser
, "ly:lily-parser?");
192 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser
);
195 /****************************************************************
197 ****************************************************************/
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
);
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
);
220 /* Return a copy of the top of $papers stack, or $defaultpaper if the
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
);
236 /* Initialize (reset) the $papers stack */
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 */
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 */
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 */
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 ();
276 SCM nid
= parser
->make_scope ();
277 ly_module_copy (nid
, id
);
285 Lily_parser::make_scope () const
287 SCM module
= ly_make_anonymous_module (be_safe_global
);