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