]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser.cc
Merge http://git.sv.gnu.org/r/lilypond
[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--2007 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 "main.hh"
19 #include "output-def.hh"
20 #include "paper-book.hh"
21 #include "parser.hh"
22 #include "score.hh"
23 #include "sources.hh"
24 #include "text-metrics.hh"
25 #include "warn.hh"
26
27 #include "ly-smobs.icc"
28
29 Lily_parser::Lily_parser (Sources *sources)
30 {
31   lexer_ = 0;
32   sources_ = sources;
33   default_duration_ = Duration (2, 0);
34   error_level_ = 0;
35
36   smobify_self ();
37
38   lexer_ = new Lily_lexer (sources_, this);
39   lexer_->unprotect ();
40 }
41
42 Lily_parser::Lily_parser (Lily_parser const &src)
43 {
44   lexer_ = 0;
45   sources_ = src.sources_;
46   default_duration_ = src.default_duration_;
47   error_level_ = src.error_level_;
48   output_basename_ = src.output_basename_;
49
50   smobify_self ();
51   if (src.lexer_)
52     {
53       lexer_ = new Lily_lexer (*src.lexer_, this);
54     }
55   
56   lexer_->unprotect ();
57 }
58
59 Lily_parser::~Lily_parser ()
60 {
61 }
62
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
100   set_yydebug (0);
101
102   lexer_->new_input (init, sources_);
103
104   File_name f (name);
105   string s = global_path.find (f.base_ + ".twy");
106   s = gulp_file_to_string (s, false, -1);
107   scm_eval_string (ly_string2scm (s));
108
109   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
110      OUT_FILE (unless IN_FILE redefines output file name).  */
111
112   SCM mod = lexer_->set_current_scope ();
113   do_yyparse ();
114
115   /*
116     Don't mix cyclic pointers with weak tables.
117   */
118   lexer_->set_identifier (ly_symbol2scm ("parser"),
119                           SCM_EOL);
120   ly_reexport_module (scm_current_module ());
121
122   scm_set_current_module (mod);
123
124   if (!define_spots_.empty ())
125     {
126       define_spots_.back ().warning (_ ("braces do not match"));
127       error_level_ = 1;
128     }
129
130   error_level_ = error_level_ | lexer_->error_level_;
131   clear ();
132 }
133
134 void
135 Lily_parser::parse_string (string ly_code)
136 {
137   // TODO: use $parser 
138   lexer_->set_identifier (ly_symbol2scm ("parser"),
139                           self_scm ());
140
141   lexer_->main_input_name_ = "<string>";
142   lexer_->is_main_input_ = true; 
143   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
144
145   SCM mod = lexer_->set_current_scope ();
146   do_yyparse ();
147   scm_set_current_module (mod);
148
149   if (!define_spots_.empty ())
150     {
151       if (define_spots_.empty ()
152           && !error_level_)
153         programming_error ("define_spots_ don't match, but error_level_ not set.");
154     }
155
156   error_level_ = error_level_ | lexer_->error_level_;
157 }
158
159 void
160 Lily_parser::clear ()
161 {
162   if (lexer_)
163     {
164       while (lexer_->has_scope ())
165         lexer_->remove_scope ();
166     }
167
168   lexer_ = 0;  
169 }
170
171 char const *
172 Lily_parser::here_str0 () const
173 {
174   return lexer_->here_str0 ();
175 }
176
177 void
178 Lily_parser::parser_error (string s)
179 {
180   lexer_->here_input ().error (_ (s.c_str ()));
181   error_level_ = 1;
182 }
183
184 void
185 Lily_parser::parser_error (Input const &i, string s)
186 {
187   i.error (s);
188   error_level_ = 1;
189 }
190
191
192
193 IMPLEMENT_SMOBS (Lily_parser);
194 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
195 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
196
197
198 /****************************************************************
199   OUTPUT-DEF 
200  ****************************************************************/
201
202 Output_def *
203 get_layout (Lily_parser *parser)
204 {
205   SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
206   Output_def *layout = unsmob_output_def (id);
207   layout = layout ? layout->clone () : new Output_def;
208   layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
209     
210   return layout;
211 }
212
213 Output_def *
214 get_midi (Lily_parser *parser)
215 {
216   SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
217   Output_def *layout = unsmob_output_def (id);
218   layout = layout ? layout->clone () : new Output_def;
219   layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
220   return layout;
221 }
222
223 Output_def *
224 get_paper (Lily_parser *parser)
225 {
226   SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
227   Output_def *layout = unsmob_output_def (id);
228
229   layout = layout ? dynamic_cast<Output_def *> (layout->clone ()) : new Output_def;
230   layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
231   return layout;
232 }
233
234 SCM
235 get_header (Lily_parser *parser)
236 {
237   SCM id = parser->lexer_->lookup_identifier ("$defaultheader");
238   if (!ly_is_module (id))
239     id = parser->make_scope ();
240   else
241     {
242       SCM nid = parser->make_scope ();
243       ly_module_copy (nid, id);
244       id = nid;
245     }
246   
247   return id;
248 }
249
250 SCM 
251 Lily_parser::make_scope () const
252 {
253   return ly_make_anonymous_module (be_safe_global);
254 }