]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser.cc
Merge branch 'master' of git://git.sv.gnu.org/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--2006 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 "lilypond-key.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 "source.hh"
25 #include "text-metrics.hh"
26 #include "warn.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   if (output_backend_global == "tex")
90     try_load_text_metrics (out_name);
91
92   // TODO: use $parser 
93   lexer_->set_identifier (ly_symbol2scm ("parser"),
94                           self_scm ());
95   output_basename_ = out_name;
96
97   lexer_->main_input_name_ = name;
98
99   message (_ ("Parsing..."));
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.c_str ()));
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_.empty ())
126     {
127       define_spots_.back ().warning (_ ("braces do not match"));
128       error_level_ = 1;
129     }
130
131   error_level_ = error_level_ | lexer_->error_level_;
132   clear ();
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   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
146
147   SCM mod = lexer_->set_current_scope ();
148   do_yyparse ();
149   scm_set_current_module (mod);
150
151   if (!define_spots_.empty ())
152     {
153       if (define_spots_.empty ()
154           && !error_level_)
155         programming_error ("define_spots_ don't match, but error_level_ not set.");
156     }
157
158   error_level_ = error_level_ | lexer_->error_level_;
159 }
160
161 void
162 Lily_parser::clear ()
163 {
164   if (lexer_)
165     {
166       while (lexer_->has_scope ())
167         lexer_->remove_scope ();
168     }
169
170   lexer_ = 0;  
171 }
172
173 char const *
174 Lily_parser::here_str0 () const
175 {
176   return lexer_->here_str0 ();
177 }
178
179 void
180 Lily_parser::parser_error (string s)
181 {
182   lexer_->here_input ().error (_ (s.c_str ()));
183   error_level_ = 1;
184 }
185
186 void
187 Lily_parser::parser_error (Input const &i, string s)
188 {
189   i.error (s);
190   error_level_ = 1;
191 }
192
193
194
195 IMPLEMENT_SMOBS (Lily_parser);
196 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
197 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
198
199
200 /****************************************************************
201   OUTPUT-DEF 
202  ****************************************************************/
203
204 Output_def *
205 get_layout (Lily_parser *parser)
206 {
207   SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
208   Output_def *layout = unsmob_output_def (id);
209   layout = layout ? layout->clone () : new Output_def;
210   layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
211   layout->parser_ = parser;
212     
213   return layout;
214 }
215
216 Output_def *
217 get_midi (Lily_parser *parser)
218 {
219   SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
220   Output_def *layout = unsmob_output_def (id);
221   layout = layout ? layout->clone () : new Output_def;
222   layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
223   layout->parser_ = parser;
224   return layout;
225 }
226
227 Output_def *
228 get_paper (Lily_parser *parser)
229 {
230   SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
231   Output_def *layout = unsmob_output_def (id);
232
233   layout = layout ? dynamic_cast<Output_def *> (layout->clone ()) : new Output_def;
234   layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
235   layout->parser_ = parser;
236   return layout;
237 }
238
239 SCM
240 get_header (Lily_parser *parser)
241 {
242   SCM id = parser->lexer_->lookup_identifier ("$defaultheader");
243   if (!ly_is_module (id))
244     id = ly_make_anonymous_module (be_safe_global);
245   else
246     {
247       SCM nid = ly_make_anonymous_module (false);
248       ly_module_copy(nid,id);
249       id = nid;
250     }
251   
252   return id;
253 }