]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser.cc
* lily/lily-parser.cc (Lily_parser): also copy output_basename_
[lilypond.git] / lily / lily-parser.cc
1 /*
2   lily-parser.cc -- implement Lily_parser
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include "lily-parser.hh"
11 #include "text-metrics.hh"
12 #include "book.hh"
13 #include "lilypond-key.hh"
14 #include "lily-version.hh"
15 #include "main.hh"
16 #include "lily-lexer.hh"
17 #include "output-def.hh"
18 #include "paper-book.hh"
19 #include "parser.hh"
20 #include "score.hh"
21 #include "file-name.hh"
22 #include "file-path.hh"
23 #include "source.hh"
24 #include "warn.hh"
25
26 Lily_parser::Lily_parser (Sources *sources)
27 {
28   lexer_ = 0;
29   sources_ = sources;
30   default_duration_ = Duration (2, 0);
31   error_level_ = 0;
32
33   smobify_self ();
34
35   lexer_ = new Lily_lexer (sources_);
36   lexer_->unprotect ();
37 }
38
39 Lily_parser::Lily_parser (Lily_parser const &src)
40 {
41   lexer_ = 0;
42   sources_ = src.sources_;
43   default_duration_ = src.default_duration_;
44   error_level_ = src.error_level_;
45   output_basename_ = src.output_basename_;
46
47   smobify_self ();
48   if (src.lexer_)
49     lexer_ = new Lily_lexer (*src.lexer_);
50
51   lexer_->unprotect ();
52 }
53
54 Lily_parser::~Lily_parser ()
55 {
56 }
57
58 #include "ly-smobs.icc"
59
60 IMPLEMENT_SMOBS (Lily_parser);
61 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
62 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
63
64 SCM
65 Lily_parser::mark_smob (SCM s)
66 {
67   Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
68   return (parser->lexer_) ? parser->lexer_->self_scm () : SCM_EOL;
69 }
70
71 int
72 Lily_parser::print_smob (SCM s, SCM port, scm_print_state*)
73 {
74   scm_puts ("#<Lily_parser ", port);
75   Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
76   if (parser->lexer_)
77     scm_display (parser->lexer_->self_scm (), port);
78   else
79     scm_puts ("(no lexer yet)", port);
80   scm_puts (" >", port);
81   return 1;
82 }
83
84 /* Process one .ly file, or book.  */
85 void
86 Lily_parser::parse_file (String init, String name, String out_name)
87 {
88   if (output_backend_global == "tex")
89     try_load_text_metrics (out_name);
90
91   // TODO: use $parser 
92   lexer_->set_identifier (ly_symbol2scm ("parser"),
93                           self_scm ());
94   output_basename_ = out_name;
95
96   lexer_->main_input_name_ = name;
97
98   message (_ ("Parsing..."));
99   //  progress_indication ("\n");
100
101   set_yydebug (0);
102
103   lexer_->new_input (init, sources_);
104
105   File_name f (name);
106   String s = global_path.find (f.base_ + ".twy");
107   s = gulp_file_to_string (s, false, -1);
108   scm_eval_string (scm_makfrom0str (s.to_str0 ()));
109
110   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
111      OUT_FILE (unless IN_FILE redefines output file name).  */
112
113   SCM mod = lexer_->set_current_scope ();
114   do_yyparse ();
115
116   /*
117     Don't mix cyclic pointers with weak tables.
118   */
119   lexer_->set_identifier (ly_symbol2scm ("parser"),
120                           SCM_EOL);
121   ly_reexport_module (scm_current_module ());
122
123   scm_set_current_module (mod);
124
125   if (!define_spots_.is_empty ())
126     {
127       define_spots_.top ().warning (_ ("braces don't match"));
128       error_level_ = 1;
129     }
130
131   error_level_ = error_level_ | lexer_->error_level_;
132   lexer_ = 0;
133 }
134
135 void
136 Lily_parser::parse_string (String ly_code)
137 {
138   // TODO: use $parser 
139   lexer_->set_identifier (ly_symbol2scm ("parser"),
140                           self_scm ());
141
142   lexer_->main_input_name_ = "<string>";
143   lexer_->is_main_input_ = true;
144
145   set_yydebug (0);
146   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
147
148   SCM mod = lexer_->set_current_scope ();
149   do_yyparse ();
150   scm_set_current_module (mod);
151
152   if (!define_spots_.is_empty ())
153     {
154       if (define_spots_.is_empty ()
155           && !error_level_)
156         programming_error ("define_spots_ don't match, but error_level_ not set.");
157     }
158
159   error_level_ = error_level_ | lexer_->error_level_;
160 }
161
162 char const *
163 Lily_parser::here_str0 () const
164 {
165   return lexer_->here_str0 ();
166 }
167
168 void
169 Lily_parser::parser_error (String s)
170 {
171   lexer_->here_input ().error (_ (s.to_str0 ()));
172   error_level_ = 1;
173 }
174
175 void
176 Lily_parser::parser_error (Input const &i, String s)
177 {
178   i.error (s);
179   error_level_ = 1;
180 }
181
182 /****************************************************************/
183
184 Output_def *
185 get_layout (Lily_parser *parser)
186 {
187   SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
188   Output_def *layout = unsmob_output_def (id);
189   layout = layout ? layout->clone () : new Output_def;
190   layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
191
192   return layout;
193 }
194
195 Output_def *
196 get_midi (Lily_parser *parser)
197 {
198   SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
199   Output_def *layout = unsmob_output_def (id);
200   layout = layout ? layout->clone () : new Output_def;
201   layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
202   return layout;
203 }
204
205 Output_def *
206 get_paper (Lily_parser *parser)
207 {
208   SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
209   Output_def *layout = unsmob_output_def (id);
210
211   layout = layout ? dynamic_cast<Output_def *> (layout->clone ()) : new Output_def;
212   layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
213   return layout;
214 }
215
216 SCM
217 get_header (Lily_parser *parser)
218 {
219   SCM id = parser->lexer_->lookup_identifier ("$defaultheader");
220   if (!ly_is_module (id))
221     id = ly_make_anonymous_module (be_safe_global);
222   else
223     {
224       SCM nid = ly_make_anonymous_module (false);
225       ly_module_copy(nid,id);
226       id = nid;
227     }
228   
229   return id;
230 }