]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser.cc
* lily/include/lily-guile-macros.hh: don't protect exported module
[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
38 Lily_parser::Lily_parser (Lily_parser const &src)
39 {
40   book_count_ = src.book_count_;
41   score_count_ = src.score_count_;
42   lexer_ = 0;
43   sources_ = src.sources_;
44   default_duration_ = src.default_duration_;
45   error_level_ = src.error_level_;
46
47   smobify_self ();
48   if (src.lexer_)
49     lexer_ = new Lily_lexer (*src.lexer_);
50
51   scm_gc_unprotect_object (lexer_->self_scm ());
52 }
53
54 Lily_parser::~Lily_parser ()
55 {
56 }
57
58 #include "ly-smobs.icc"
59
60 IMPLEMENT_SMOBS (Lily_parser);
61 IMPLEMENT_TYPE_P (Lily_parser, "ly:lily-parser?");
62 IMPLEMENT_DEFAULT_EQUAL_P (Lily_parser);
63
64 SCM
65 Lily_parser::mark_smob (SCM s)
66 {
67   Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
68   return (parser->lexer_) ? parser->lexer_->self_scm () : SCM_EOL;
69 }
70
71 int
72 Lily_parser::print_smob (SCM s, SCM port, scm_print_state*)
73 {
74   scm_puts ("#<my_lily_parser ", port);
75   Lily_parser *parser = (Lily_parser *) SCM_CELL_WORD_1 (s);
76   (void) parser;
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     {
87       try_load_text_metrics (out_name);
88     }
89
90   lexer_ = new Lily_lexer (sources_);
91   scm_gc_unprotect_object (lexer_->self_scm ());
92   // TODO: use $parser 
93   lexer_->set_identifier (ly_symbol2scm ("parser"),
94                           self_scm ());
95   output_basename_ = out_name;
96
97   lexer_->main_input_name_ = name;
98
99   message (_ ("Parsing..."));
100   //  progress_indication ("\n");
101
102   set_yydebug (0);
103
104   lexer_->new_input (init, sources_);
105
106   File_name f (name);
107   String s = global_path.find (f.base_ + ".twy");
108   s = gulp_file_to_string (s, false);
109   scm_eval_string (scm_makfrom0str (s.to_str0 ()));
110
111   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
112      OUT_FILE (unless IN_FILE redefines output file name).  */
113   do_yyparse ();
114
115   if (!define_spots_.is_empty ())
116     {
117       define_spots_.top ().warning (_ ("braces don't match"));
118       error_level_ = 1;
119     }
120
121   error_level_ = error_level_ | lexer_->error_level_;
122   lexer_ = 0;
123 }
124
125 void
126 Lily_parser::parse_string (String ly_code)
127 {
128   Lily_lexer *parent = lexer_;
129   lexer_ = (parent == 0 ? new Lily_lexer (sources_)
130             : new Lily_lexer (*parent));
131   scm_gc_unprotect_object (lexer_->self_scm ());
132
133   SCM oldmod = scm_current_module ();
134   scm_set_current_module (scm_car (lexer_->scopes_));
135
136   // TODO: use $parser 
137   lexer_->set_identifier (ly_symbol2scm ("parser"),
138                           self_scm ());
139
140   lexer_->main_input_name_ = "<string>";
141   lexer_->is_main_input_ = true;
142
143   set_yydebug (0);
144   lexer_->new_input (lexer_->main_input_name_, ly_code, sources_);
145   do_yyparse ();
146
147   if (!define_spots_.is_empty ())
148     {
149       if (define_spots_.is_empty ()
150           && !error_level_)
151         programming_error ("define_spots_ don't match, but error_level_ not set.");
152     }
153
154   error_level_ = error_level_ | lexer_->error_level_;
155
156   scm_set_current_module (oldmod);
157   lexer_ = 0;
158 }
159
160 char const *
161 Lily_parser::here_str0 () const
162 {
163   return lexer_->here_str0 ();
164 }
165
166 void
167 Lily_parser::parser_error (String s)
168 {
169   lexer_->here_input ().error (_ (s.to_str0 ()));
170   error_level_ = 1;
171 }
172
173 void
174 Lily_parser::parser_error (Input const &i, String s)
175 {
176   i.error (s);
177   error_level_ = 1;
178 }
179
180 /****************************************************************/
181
182 Output_def *
183 get_layout (Lily_parser *parser)
184 {
185   SCM id = parser->lexer_->lookup_identifier ("$defaultlayout");
186   Output_def *layout = unsmob_output_def (id);
187   layout = layout ? layout->clone () : new Output_def;
188   layout->set_variable (ly_symbol2scm ("is-layout"), SCM_BOOL_T);
189
190   return layout;
191 }
192
193 Output_def *
194 get_midi (Lily_parser *parser)
195 {
196   SCM id = parser->lexer_->lookup_identifier ("$defaultmidi");
197   Output_def *layout = unsmob_output_def (id);
198   layout = layout ? layout->clone () : new Output_def;
199   layout->set_variable (ly_symbol2scm ("is-midi"), SCM_BOOL_T);
200   return layout;
201 }
202
203 Output_def *
204 get_paper (Lily_parser *parser)
205 {
206   SCM id = parser->lexer_->lookup_identifier ("$defaultpaper");
207   Output_def *layout = unsmob_output_def (id);
208
209   layout = layout ? dynamic_cast<Output_def *> (layout->clone ()) : new Output_def;
210   layout->set_variable (ly_symbol2scm ("is-paper"), SCM_BOOL_T);
211   return layout;
212 }
213