]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser.cc
*** empty log message ***
[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--2004 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 "ly-module.hh"
16 #include "main.hh"
17 #include "lily-lexer.hh"
18 #include "output-def.hh"
19 #include "paper-book.hh"
20 #include "parser.hh"
21 #include "score.hh"
22 #include "file-name.hh"
23 #include "file-path.hh"
24 #include "source.hh"
25 #include "warn.hh"
26
27 Lily_parser::Lily_parser (Sources *sources)
28 {
29   book_count_ = 0;
30   score_count_ = 0;
31   lexer_ = 0;
32   sources_ = sources;
33   default_duration_ = Duration (2, 0);
34   error_level_ = 0;
35   last_beam_start_ = SCM_EOL;
36
37   smobify_self ();
38 }
39
40 Lily_parser::Lily_parser (Lily_parser const &src)
41 {
42   book_count_ = src.book_count_;
43   score_count_ = src.score_count_;
44   lexer_ = 0;
45   sources_ = src.sources_;
46   default_duration_ = src.default_duration_;
47   error_level_ = src.error_level_;
48   last_beam_start_ = src.last_beam_start_;
49
50   smobify_self ();
51   if (src.lexer_)
52     lexer_ = new Lily_lexer (*src.lexer_);
53
54   scm_gc_unprotect_object (lexer_->self_scm ());
55 }
56
57 Lily_parser::~Lily_parser ()
58 {
59 }
60
61 #include "ly-smobs.icc"
62
63 IMPLEMENT_SMOBS (Lily_parser);
64 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
65 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
66
67 SCM
68 Lily_parser::mark_smob (SCM s)
69 {
70   Lily_parser *parser = (Lily_parser*) SCM_CELL_WORD_1 (s);
71   return (parser->lexer_) ? parser->lexer_->self_scm () : SCM_EOL;
72 }
73
74 int
75 Lily_parser::print_smob (SCM s, SCM port, scm_print_state*)
76 {
77   scm_puts ("#<my_lily_parser ", port);
78   Lily_parser *parser = (Lily_parser*) SCM_CELL_WORD_1 (s);
79   (void) parser;
80   scm_puts (" >", port);
81   return 1;
82 }
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     {
91       try_load_text_metrics (out_name); 
92     }
93   
94   lexer_ = new Lily_lexer (sources_);
95   scm_gc_unprotect_object (lexer_->self_scm ());
96   // TODO: use $parser 
97   lexer_->set_identifier (ly_symbol2scm ("parser"),
98                           self_scm ());
99   output_basename_ = out_name;
100   
101   lexer_->main_input_name_ = name;
102
103   progress_indication (_ ("Parsing..."));
104   progress_indication ("\n");
105
106   set_yydebug (0);
107
108   lexer_->new_input (init, sources_);
109
110   File_name f (name);
111   String s = global_path.find (f.base_ + ".twy");
112   s = gulp_file_to_string (s, false);
113   scm_eval_string (scm_makfrom0str (s.to_str0 ()));
114
115   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
116      OUT_FILE (unless IN_FILE redefines output file name).  */
117   do_yyparse ();
118
119   if (!define_spots_.is_empty ())
120     {
121       define_spots_.top ().warning (_ ("Braces don't match"));
122       error_level_ = 1;
123     }
124
125   error_level_ = error_level_ | lexer_->error_level_;
126   lexer_ = 0;
127 }
128
129 void
130 Lily_parser::parse_string (String ly_code)
131 {
132   Lily_lexer * parent = lexer_;
133   lexer_ = (parent == 0 ? new Lily_lexer (sources_)
134             : new Lily_lexer (*parent));
135   scm_gc_unprotect_object (lexer_->self_scm ());
136
137
138   SCM oldmod = scm_current_module ();
139   scm_set_current_module (scm_car (lexer_->scopes_));
140   
141   // TODO: use $parser 
142   lexer_->set_identifier (ly_symbol2scm ("parser"),
143                           self_scm ());
144   
145   lexer_->main_input_name_ = "<string>";
146   lexer_->main_input_b_ = true;
147
148   set_yydebug (0);
149   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
150   do_yyparse ();
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   scm_set_current_module (oldmod);
162   lexer_ = 0;
163 }
164
165 void
166 Lily_parser::push_spot ()
167 {
168   define_spots_.push (here_input ());
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   here_input ().error (s);
181   error_level_ = 1;
182 }
183
184 Input
185 Lily_parser::pop_spot ()
186 {
187   return define_spots_.pop ();
188 }
189
190 Input
191 Lily_parser::here_input () const
192 {
193   /*
194     Parsing looks ahead , so we really want the previous location of the
195     lexer, not lexer_->here_input ().
196   */
197   
198   /*
199     Actually, that gets very icky when there are white space, because
200     the line-numbers are all wrong.  Let's try the character before
201     the current token. That gets the right result for note/duration
202     stuff, but doesn't mess up for errors in the 1st token of the
203     line.
204   */
205   Input hi (lexer_->here_input ());
206
207   char const * bla = hi.defined_str0_;
208   if (hi.line_number () > 1
209       || hi.column_number () > 1)
210     bla --;
211   
212   return Input (hi.source_file_, bla);
213 }
214
215
216 /****************************************************************/
217
218
219 Output_def*
220 get_layout (Lily_parser *parser)
221 {
222   SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
223   Output_def *layout = unsmob_output_def (id);
224   layout = layout ? layout->clone () : new Output_def;
225   layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
226
227   return layout;
228 }
229
230
231 Output_def*
232 get_midi (Lily_parser *parser)
233 {
234   SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
235   Output_def *layout = unsmob_output_def (id);
236   layout = layout ? layout->clone () : new Output_def;
237   layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
238   return layout;
239 }
240
241
242 Output_def*
243 get_paper (Lily_parser *parser)
244 {
245   SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
246   Output_def *layout = unsmob_output_def (id);
247
248   layout = layout ? dynamic_cast<Output_def*> (layout->clone ()) : new Output_def;
249   layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
250   return layout;
251 }
252