]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser.cc
* lily/default-actions.cc (Module): new file. default
[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
35   smobify_self ();
36
37   lexer_ = new Lily_lexer (sources_);
38   scm_gc_unprotect_object (lexer_->self_scm ());
39 }
40
41 Lily_parser::Lily_parser (Lily_parser const &src)
42 {
43   book_count_ = src.book_count_;
44   score_count_ = src.score_count_;
45   lexer_ = 0;
46   sources_ = src.sources_;
47   default_duration_ = src.default_duration_;
48   error_level_ = src.error_level_;
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 ("#<Lily_parser ", port);
78   Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
79   scm_display (parser->lexer_->self_scm (), port);
80   scm_puts (" >", port);
81   return 1;
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   // TODO: use $parser 
94   lexer_->set_identifier (ly_symbol2scm ("parser"),
95                           self_scm ());
96   output_basename_ = out_name;
97
98   lexer_->main_input_name_ = name;
99
100   message (_ ("Parsing..."));
101   //  progress_indication ("\n");
102
103   set_yydebug (0);
104
105   lexer_->new_input (init, sources_);
106
107   File_name f (name);
108   String s = global_path.find (f.base_ + ".twy");
109   s = gulp_file_to_string (s, false);
110   scm_eval_string (scm_makfrom0str (s.to_str0 ()));
111
112   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
113      OUT_FILE (unless IN_FILE redefines output file name).  */
114
115   SCM mod = lexer_->set_current_scope ();
116   do_yyparse ();
117   scm_set_current_module (mod);
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    // TODO: use $parser 
133   lexer_->set_identifier (ly_symbol2scm ("parser"),
134                           self_scm ());
135   
136   lexer_->main_input_name_ = "<string>";
137   lexer_->is_main_input_ = true;
138
139   set_yydebug (0);
140   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
141
142   SCM mod = lexer_->set_current_scope ();
143   do_yyparse ();
144   scm_set_current_module (mod);
145   
146   if (!define_spots_.is_empty ())
147     {
148       if (define_spots_.is_empty ()
149           && !error_level_)
150         programming_error ("define_spots_ don't match, but error_level_ not set.");
151     }
152
153   error_level_ = error_level_ | lexer_->error_level_;
154 }
155
156 char const *
157 Lily_parser::here_str0 () const
158 {
159   return lexer_->here_str0 ();
160 }
161
162 void
163 Lily_parser::parser_error (String s)
164 {
165   lexer_->here_input ().error (_ (s.to_str0 ()));
166   error_level_ = 1;
167 }
168
169 void
170 Lily_parser::parser_error (Input const &i, String s)
171 {
172   i.error (s);
173   error_level_ = 1;
174 }
175
176 /****************************************************************/
177
178 Output_def *
179 get_layout (Lily_parser *parser)
180 {
181   SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
182   Output_def *layout = unsmob_output_def (id);
183   layout = layout ? layout->clone () : new Output_def;
184   layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
185
186   return layout;
187 }
188
189 Output_def *
190 get_midi (Lily_parser *parser)
191 {
192   SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
193   Output_def *layout = unsmob_output_def (id);
194   layout = layout ? layout->clone () : new Output_def;
195   layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
196   return layout;
197 }
198
199 Output_def *
200 get_paper (Lily_parser *parser)
201 {
202   SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
203   Output_def *layout = unsmob_output_def (id);
204
205   layout = layout ? dynamic_cast<Output_def *> (layout->clone ()) : new Output_def;
206   layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
207   return layout;
208 }
209