]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser.cc
* The grand 2005-2006 replace.
[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 #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 #include "ly-smobs.icc"
27
28 Lily_parser::Lily_parser (Sources *sources)
29 {
30   lexer_ = 0;
31   sources_ = sources;
32   default_duration_ = Duration (2, 0);
33   error_level_ = 0;
34
35   smobify_self ();
36
37   lexer_ = new Lily_lexer (sources_);
38   lexer_->unprotect ();
39 }
40
41 Lily_parser::Lily_parser (Lily_parser const &src)
42 {
43   lexer_ = 0;
44   sources_ = src.sources_;
45   default_duration_ = src.default_duration_;
46   error_level_ = src.error_level_;
47   output_basename_ = src.output_basename_;
48
49   smobify_self ();
50   if (src.lexer_)
51     lexer_ = new Lily_lexer (*src.lexer_);
52
53   lexer_->unprotect ();
54 }
55
56 Lily_parser::~Lily_parser ()
57 {
58 }
59
60
61 SCM
62 Lily_parser::mark_smob (SCM s)
63 {
64   Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
65   return (parser->lexer_) ? parser->lexer_->self_scm () : SCM_EOL;
66 }
67
68 int
69 Lily_parser::print_smob (SCM s, SCM port, scm_print_state*)
70 {
71   scm_puts ("#<Lily_parser ", port);
72   Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
73   if (parser->lexer_)
74     scm_display (parser->lexer_->self_scm (), port);
75   else
76     scm_puts ("(no lexer yet)", port);
77   scm_puts (" >", port);
78   return 1;
79 }
80
81 /* Process one .ly file, or book.  */
82 void
83 Lily_parser::parse_file (String init, String name, String out_name)
84 {
85   if (output_backend_global == "tex")
86     try_load_text_metrics (out_name);
87
88   // TODO: use $parser 
89   lexer_->set_identifier (ly_symbol2scm ("parser"),
90                           self_scm ());
91   output_basename_ = out_name;
92
93   lexer_->main_input_name_ = name;
94
95   message (_ ("Parsing..."));
96   //  progress_indication ("\n");
97
98   set_yydebug (0);
99
100   lexer_->new_input (init, sources_);
101
102   File_name f (name);
103   String s = global_path.find (f.base_ + ".twy");
104   s = gulp_file_to_string (s, false, -1);
105   scm_eval_string (scm_makfrom0str (s.to_str0 ()));
106
107   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
108      OUT_FILE (unless IN_FILE redefines output file name).  */
109
110   SCM mod = lexer_->set_current_scope ();
111   do_yyparse ();
112
113   /*
114     Don't mix cyclic pointers with weak tables.
115   */
116   lexer_->set_identifier (ly_symbol2scm ("parser"),
117                           SCM_EOL);
118   ly_reexport_module (scm_current_module ());
119
120   scm_set_current_module (mod);
121
122   if (!define_spots_.is_empty ())
123     {
124       define_spots_.top ().warning (_ ("braces don't match"));
125       error_level_ = 1;
126     }
127
128   error_level_ = error_level_ | lexer_->error_level_;
129   lexer_ = 0;
130 }
131
132 void
133 Lily_parser::parse_string (String ly_code)
134 {
135   // TODO: use $parser 
136   lexer_->set_identifier (ly_symbol2scm ("parser"),
137                           self_scm ());
138
139   lexer_->main_input_name_ = "<string>";
140   lexer_->is_main_input_ = true;
141
142   set_yydebug (0);
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_.is_empty ())
150     {
151       if (define_spots_.is_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 char const *
160 Lily_parser::here_str0 () const
161 {
162   return lexer_->here_str0 ();
163 }
164
165 void
166 Lily_parser::parser_error (String s)
167 {
168   lexer_->here_input ().error (_ (s.to_str0 ()));
169   error_level_ = 1;
170 }
171
172 void
173 Lily_parser::parser_error (Input const &i, String s)
174 {
175   i.error (s);
176   error_level_ = 1;
177 }
178
179
180
181 IMPLEMENT_SMOBS (Lily_parser);
182 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
183 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
184
185
186 /****************************************************************
187   OUTPUT-DEF 
188  ****************************************************************/
189
190 Output_def *
191 get_layout (Lily_parser *parser)
192 {
193   SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
194   Output_def *layout = unsmob_output_def (id);
195   layout = layout ? layout->clone () : new Output_def;
196   layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
197
198   return layout;
199 }
200
201 Output_def *
202 get_midi (Lily_parser *parser)
203 {
204   SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
205   Output_def *layout = unsmob_output_def (id);
206   layout = layout ? layout->clone () : new Output_def;
207   layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
208   return layout;
209 }
210
211 Output_def *
212 get_paper (Lily_parser *parser)
213 {
214   SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
215   Output_def *layout = unsmob_output_def (id);
216
217   layout = layout ? dynamic_cast<Output_def *> (layout->clone ()) : new Output_def;
218   layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
219   return layout;
220 }
221
222 SCM
223 get_header (Lily_parser *parser)
224 {
225   SCM id = parser->lexer_->lookup_identifier ("$defaultheader");
226   if (!ly_is_module (id))
227     id = ly_make_anonymous_module (be_safe_global);
228   else
229     {
230       SCM nid = ly_make_anonymous_module (false);
231       ly_module_copy(nid,id);
232       id = nid;
233     }
234   
235   return id;
236 }