]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser.cc
Remove tex and texstr backends (part 1).
[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--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include "lily-parser.hh"
11
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"
27
28 #include "ly-smobs.icc"
29
30 Lily_parser::Lily_parser (Sources *sources)
31 {
32   lexer_ = 0;
33   sources_ = sources;
34   default_duration_ = Duration (2, 0);
35   error_level_ = 0;
36
37   smobify_self ();
38
39   lexer_ = new Lily_lexer (sources_, this);
40   lexer_->unprotect ();
41 }
42
43 Lily_parser::Lily_parser (Lily_parser const &src)
44 {
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_;
50
51   smobify_self ();
52   if (src.lexer_)
53     {
54       lexer_ = new Lily_lexer (*src.lexer_, this);
55     }
56   
57   lexer_->unprotect ();
58 }
59
60 Lily_parser::~Lily_parser ()
61 {
62 }
63
64
65 SCM
66 Lily_parser::mark_smob (SCM s)
67 {
68   Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
69   return (parser->lexer_) ? parser->lexer_->self_scm () : SCM_EOL;
70 }
71
72 int
73 Lily_parser::print_smob (SCM s, SCM port, scm_print_state*)
74 {
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;
83 }
84
85 /* Process one .ly file, or book.  */
86 void
87 Lily_parser::parse_file (string init, string name, string out_name)
88 {
89   // TODO: use $parser 
90   lexer_->set_identifier (ly_symbol2scm ("parser"), self_scm ());
91   output_basename_ = out_name;
92
93   lexer_->main_input_name_ = name;
94
95   message (_ ("Parsing..."));
96
97   set_yydebug (0);
98
99   lexer_->new_input (init, sources_);
100
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));
105
106   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
107      OUT_FILE (unless IN_FILE redefines output file name).  */
108
109   SCM mod = lexer_->set_current_scope ();
110   do_yyparse ();
111
112   /*
113     Don't mix cyclic pointers with weak tables.
114   */
115   lexer_->set_identifier (ly_symbol2scm ("parser"),
116                           SCM_EOL);
117   ly_reexport_module (scm_current_module ());
118
119   scm_set_current_module (mod);
120
121   if (!define_spots_.empty ())
122     {
123       define_spots_.back ().warning (_ ("braces do not match"));
124       error_level_ = 1;
125     }
126
127   error_level_ = error_level_ | lexer_->error_level_;
128   clear ();
129 }
130
131 void
132 Lily_parser::parse_string (string ly_code)
133 {
134   // TODO: use $parser 
135   lexer_->set_identifier (ly_symbol2scm ("parser"),
136                           self_scm ());
137
138   lexer_->main_input_name_ = "<string>";
139   lexer_->is_main_input_ = true; 
140   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
141
142   SCM mod = lexer_->set_current_scope ();
143   do_yyparse ();
144   scm_set_current_module (mod);
145
146   if (!define_spots_.empty ())
147     {
148       if (define_spots_.empty ()
149           && !error_level_)
150         programming_error ("define_spots_ don't match, but error_level_ not set.");
151     }
152
153   error_level_ = error_level_ | lexer_->error_level_;
154 }
155
156 void
157 Lily_parser::clear ()
158 {
159   if (lexer_)
160     {
161       while (lexer_->has_scope ())
162         lexer_->remove_scope ();
163     }
164
165   lexer_ = 0;  
166 }
167
168 char const *
169 Lily_parser::here_str0 () const
170 {
171   return lexer_->here_str0 ();
172 }
173
174 void
175 Lily_parser::parser_error (string s)
176 {
177   lexer_->here_input ().error (_ (s.c_str ()));
178   error_level_ = 1;
179 }
180
181 void
182 Lily_parser::parser_error (Input const &i, string s)
183 {
184   i.error (s);
185   error_level_ = 1;
186 }
187
188
189
190 IMPLEMENT_SMOBS (Lily_parser);
191 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
192 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
193
194
195 /****************************************************************
196   OUTPUT-DEF 
197  ****************************************************************/
198
199 Output_def *
200 get_layout (Lily_parser *parser)
201 {
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);
206     
207   return layout;
208 }
209
210 Output_def *
211 get_midi (Lily_parser *parser)
212 {
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;
218 }
219
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)
224 {
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);
230
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;
234 }
235
236 /* Initialize (reset) the $papers stack */
237 void
238 init_papers (Lily_parser *parser)
239 {
240   parser->lexer_->set_identifier (ly_symbol2scm ("$papers"), SCM_EOL);
241 }
242
243 /* Push a paper on top of $papers stack */
244 void
245 push_paper (Lily_parser *parser, Output_def *paper)
246 {
247   parser->lexer_->set_identifier (ly_symbol2scm ("$papers"),
248                                   scm_cons (paper->self_scm (),
249                                             parser->lexer_->lookup_identifier ("$papers")));
250 }
251
252 /* Pop a paper from $papers stack */
253 void
254 pop_paper (Lily_parser *parser)
255 {
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")));
259 }
260
261 /* Change the paper on top of $papers stack */
262 void
263 set_paper (Lily_parser *parser, Output_def *paper)
264 {
265   scm_set_car_x (parser->lexer_->lookup_identifier ("$papers"), paper->self_scm ());
266 }
267
268 SCM
269 get_header (Lily_parser *parser)
270 {
271   SCM id = parser->lexer_->lookup_identifier ("$defaultheader");
272   if (!ly_is_module (id))
273     id = parser->make_scope ();
274   else
275     {
276       SCM nid = parser->make_scope ();
277       ly_module_copy (nid, id);
278       id = nid;
279     }
280   
281   return id;
282 }
283
284 SCM 
285 Lily_parser::make_scope () const
286 {
287   SCM module = ly_make_anonymous_module (be_safe_global);
288   return module;    
289 }