]> 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--2005 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 /* 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     {
89       try_load_text_metrics (out_name);
90     }
91
92   lexer_ = new Lily_lexer (sources_);
93   scm_gc_unprotect_object (lexer_->self_scm ());
94   // TODO: use $parser 
95   lexer_->set_identifier (ly_symbol2scm ("parser"),
96                           self_scm ());
97   output_basename_ = out_name;
98
99   lexer_->main_input_name_ = name;
100
101   message (_ ("Parsing..."));
102   //  progress_indication ("\n");
103
104   set_yydebug (0);
105
106   lexer_->new_input (init, sources_);
107
108   File_name f (name);
109   String s = global_path.find (f.base_ + ".twy");
110   s = gulp_file_to_string (s, false);
111   scm_eval_string (scm_makfrom0str (s.to_str0 ()));
112
113   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
114      OUT_FILE (unless IN_FILE redefines output file name).  */
115   do_yyparse ();
116
117   if (!define_spots_.is_empty ())
118     {
119       define_spots_.top ().warning (_ ("braces don't match"));
120       error_level_ = 1;
121     }
122
123   error_level_ = error_level_ | lexer_->error_level_;
124   lexer_ = 0;
125 }
126
127 void
128 Lily_parser::parse_string (String ly_code)
129 {
130   Lily_lexer *parent = lexer_;
131   lexer_ = (parent == 0 ? new Lily_lexer (sources_)
132             : new Lily_lexer (*parent));
133   scm_gc_unprotect_object (lexer_->self_scm ());
134
135   SCM oldmod = scm_current_module ();
136   scm_set_current_module (scm_car (lexer_->scopes_));
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   set_yydebug (0);
146   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
147   do_yyparse ();
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   scm_set_current_module (oldmod);
159   lexer_ = 0;
160 }
161
162 char const *
163 Lily_parser::here_str0 () const
164 {
165   return lexer_->here_str0 ();
166 }
167
168 void
169 Lily_parser::parser_error (String s)
170 {
171   /* FIXME: cannot otherwise internationalize this bison warning.  */
172   (void) _i ("syntax error, unexpected ");
173   lexer_->here_input ().error (_ (s.to_str0 ()));
174   error_level_ = 1;
175 }
176
177 void
178 Lily_parser::parser_error (Input const &i, String s)
179 {
180   i.error (s);
181   error_level_ = 1;
182 }
183
184 /****************************************************************/
185
186 Output_def *
187 get_layout (Lily_parser *parser)
188 {
189   SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
190   Output_def *layout = unsmob_output_def (id);
191   layout = layout ? layout->clone () : new Output_def;
192   layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
193
194   return layout;
195 }
196
197 Output_def *
198 get_midi (Lily_parser *parser)
199 {
200   SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
201   Output_def *layout = unsmob_output_def (id);
202   layout = layout ? layout->clone () : new Output_def;
203   layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
204   return layout;
205 }
206
207 Output_def *
208 get_paper (Lily_parser *parser)
209 {
210   SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
211   Output_def *layout = unsmob_output_def (id);
212
213   layout = layout ? dynamic_cast<Output_def *> (layout->clone ()) : new Output_def;
214   layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
215   return layout;
216 }
217