]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser.cc
a339e8a8f5d7c6585b98c3ef034a28b43224a1dc
[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     lexer_ = new Lily_lexer (*src.lexer_);
54
55   lexer_->unprotect ();
56 }
57
58 Lily_parser::~Lily_parser ()
59 {
60 }
61
62
63 SCM
64 Lily_parser::mark_smob (SCM s)
65 {
66   Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
67   return (parser->lexer_) ? parser->lexer_->self_scm () : SCM_EOL;
68 }
69
70 int
71 Lily_parser::print_smob (SCM s, SCM port, scm_print_state*)
72 {
73   scm_puts ("#<Lily_parser ", port);
74   Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
75   if (parser->lexer_)
76     scm_display (parser->lexer_->self_scm (), port);
77   else
78     scm_puts ("(no lexer yet)", port);
79   scm_puts (" >", port);
80   return 1;
81 }
82
83 /* Process one .ly file, or book.  */
84 void
85 Lily_parser::parse_file (string init, string name, string out_name)
86 {
87   if (output_backend_global == "tex")
88     try_load_text_metrics (out_name);
89
90   // TODO: use $parser 
91   lexer_->set_identifier (ly_symbol2scm ("parser"),
92                           self_scm ());
93   output_basename_ = out_name;
94
95   lexer_->main_input_name_ = name;
96
97   message (_ ("Parsing..."));
98
99   set_yydebug (0);
100
101   lexer_->new_input (init, sources_);
102
103   File_name f (name);
104   string s = global_path.find (f.base_ + ".twy");
105   s = gulp_file_to_string (s, false, -1);
106   scm_eval_string (scm_makfrom0str (s.c_str ()));
107
108   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
109      OUT_FILE (unless IN_FILE redefines output file name).  */
110
111   SCM mod = lexer_->set_current_scope ();
112   do_yyparse ();
113
114   /*
115     Don't mix cyclic pointers with weak tables.
116   */
117   lexer_->set_identifier (ly_symbol2scm ("parser"),
118                           SCM_EOL);
119   ly_reexport_module (scm_current_module ());
120
121   scm_set_current_module (mod);
122
123   if (!define_spots_.empty ())
124     {
125       define_spots_.back ().warning (_ ("braces don't match"));
126       error_level_ = 1;
127     }
128
129   error_level_ = error_level_ | lexer_->error_level_;
130   clear ();
131 }
132
133 void
134 Lily_parser::parse_string (string ly_code)
135 {
136   // TODO: use $parser 
137   lexer_->set_identifier (ly_symbol2scm ("parser"),
138                           self_scm ());
139
140   lexer_->main_input_name_ = "<string>";
141   lexer_->is_main_input_ = true;
142
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   layout->parser_ = parser;
210     
211   return layout;
212 }
213
214 Output_def *
215 get_midi (Lily_parser *parser)
216 {
217   SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
218   Output_def *layout = unsmob_output_def (id);
219   layout = layout ? layout->clone () : new Output_def;
220   layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
221   layout->parser_ = parser;
222   return layout;
223 }
224
225 Output_def *
226 get_paper (Lily_parser *parser)
227 {
228   SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
229   Output_def *layout = unsmob_output_def (id);
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   layout->parser_ = parser;
234   return layout;
235 }
236
237 SCM
238 get_header (Lily_parser *parser)
239 {
240   SCM id = parser->lexer_->lookup_identifier ("$defaultheader");
241   if (!ly_is_module (id))
242     id = ly_make_anonymous_module (be_safe_global);
243   else
244     {
245       SCM nid = ly_make_anonymous_module (false);
246       ly_module_copy(nid,id);
247       id = nid;
248     }
249   
250   return id;
251 }