]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-lexer.cc
* input/regression/key-signature-cancellation.ly (Module): new file.
[lilypond.git] / lily / lily-lexer.cc
1 /*
2   lily-lexer.cc -- implement Lily_lexer
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <ctype.h>
10 #include <sstream>
11
12 #include "lily-proto.hh"
13 #include "scm-hash.hh"
14 #include "interval.hh"
15 #include "lily-guile.hh"
16 #include "parser.hh"
17 #include "keyword.hh"
18 #include "lily-lexer.hh"
19 #include "warn.hh"
20 #include "source-file.hh"
21 #include "main.hh"
22 #include "input.hh"
23 #include "moment.hh"
24 #include "ly-module.hh"
25
26
27 static Keyword_ent the_key_tab[] = {
28   {"accepts", ACCEPTS},
29   {"addquote", ADDQUOTE},
30   {"addlyrics", ADDLYRICS},
31   {"alias", ALIAS},
32   {"alternative", ALTERNATIVE},
33   {"bar", BAR},
34   {"book", BOOK},
35   {"bookpaper", BOOKPAPER},
36   {"change", CHANGE},
37   {"chords", CHORDS},
38   {"clef", CLEF},
39   {"consists", CONSISTS},
40   {"context", CONTEXT},
41   {"default", DEFAULT},
42   {"denies", DENIES},
43   {"drums", DRUMS},
44   {"description", DESCRIPTION},
45   {"figures",FIGURES},
46   {"grobdescriptions", GROBDESCRIPTIONS},
47   {"header", HEADER},
48   {"key", KEY},
49   {"lyrics", LYRICS},
50   {"lyricsto", LYRICSTO},
51   {"mark", MARK},
52   {"markup", MARKUP},
53   {"midi", MIDI},
54   {"name", NAME},
55   {"new", NEWCONTEXT},
56   {"notes", NOTES},
57   {"octave", OCTAVE},
58   {"once", ONCE},
59   {"override", OVERRIDE},
60   {"paper", PAPER},
61   {"partial", PARTIAL},
62   {"quote", QUOTE},
63   {"relative", RELATIVE},
64   {"remove", REMOVE},
65   {"repeat", REPEAT},
66   {"rest", REST},
67   {"revert", REVERT},
68   {"score", SCORE},
69   {"sequential", SEQUENTIAL},
70   {"set", SET},
71   {"simultaneous", SIMULTANEOUS},
72   {"skip", SKIP},
73   {"tag", TAG},
74   {"tempo", TEMPO},
75   {"time", TIME_T},
76   {"times", TIMES},
77   {"transpose", TRANSPOSE},
78   {"transposition", TRANSPOSITION},
79   {"type", TYPE},
80   {"unset", UNSET},
81   {"with", WITH},
82   {0, 0}
83 };
84
85
86 Lily_lexer::Lily_lexer (Sources *sources)
87 {
88   keytable_ = new Keyword_table (the_key_tab);
89   encoding_ = SCM_EOL;
90   chordmodifier_tab_ = SCM_EOL;
91   pitchname_tab_stack_ = SCM_EOL; 
92   sources_ = sources;
93   scopes_ = SCM_EOL;
94   error_level_ = 0; 
95   main_input_b_ = false;
96
97   smobify_self ();
98   
99   add_scope (ly_make_anonymous_module (false));
100   push_note_state (scm_c_make_hash_table (0));
101   chordmodifier_tab_ = scm_make_vector (scm_int2num (1), SCM_EOL);
102 }
103
104 Lily_lexer::Lily_lexer (Lily_lexer const &src)
105   : Includable_lexer ()
106 {
107   keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
108   encoding_ = src.encoding_;
109   chordmodifier_tab_ = src.chordmodifier_tab_;
110   pitchname_tab_stack_ = src.pitchname_tab_stack_;
111   sources_ = src.sources_;
112   
113   error_level_ = src.error_level_; 
114   main_input_b_ = src.main_input_b_;
115
116   scopes_ = SCM_EOL;
117   
118   smobify_self ();
119   
120   SCM scopes = SCM_EOL;
121   SCM *tail = &scopes;
122   for (SCM s = src.scopes_; ly_c_pair_p (s); s = ly_cdr (s))
123     {
124       SCM newmod = ly_make_anonymous_module (false);
125       ly_import_module (newmod, ly_car (s));
126       *tail = scm_cons (newmod, SCM_EOL);
127       tail = SCM_CDRLOC (*tail);
128     }
129   
130   scopes_ =  scopes;
131   push_note_state (scm_c_make_hash_table (0));
132 }
133
134 Lily_lexer::~Lily_lexer ()
135 {
136   delete keytable_;
137 }
138
139 SCM
140 Lily_lexer::encoding () const
141 {
142   return encoding_ ;
143 }
144
145
146 void
147 Lily_lexer::add_scope (SCM module)
148 {
149   ly_reexport_module (scm_current_module ());
150   scm_set_current_module (module);
151   for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
152     {
153       ly_use_module (module, ly_car (s));
154     }
155   scopes_ = scm_cons (module, scopes_);
156 }
157
158 SCM
159 Lily_lexer::remove_scope ()
160 {
161   SCM sc = ly_car (scopes_);
162   scopes_ = ly_cdr (scopes_);
163   scm_set_current_module (ly_car (scopes_));
164
165   return sc;
166 }
167
168
169 int
170 Lily_lexer::lookup_keyword (String s)
171 {
172   return keytable_->lookup (s.to_str0 ());
173 }
174
175 SCM
176 Lily_lexer::lookup_identifier_symbol (SCM sym)
177 {
178   for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
179     {
180       SCM var = ly_module_lookup (ly_car (s), sym);
181       if (var != SCM_BOOL_F)
182         return scm_variable_ref (var);
183     }
184
185   return SCM_UNDEFINED;
186 }
187
188 SCM
189 Lily_lexer::lookup_identifier (String name)
190 {
191   return lookup_identifier_symbol (ly_symbol2scm (name.to_str0 ()));
192 }
193
194 void
195 Lily_lexer::start_main_input ()
196 {
197   // yy_flex_debug = 1;
198   new_input (main_input_name_, sources_);
199   
200   /* Do not allow \include in --safe-mode */
201   allow_includes_b_ = allow_includes_b_ && !safe_global_b;
202
203   scm_module_define (ly_car (scopes_),
204                      ly_symbol2scm ("input-file-name"),
205                      scm_makfrom0str (main_input_name_.to_str0 ()));
206 }
207
208 void
209 Lily_lexer::set_identifier (SCM name, SCM s)
210 {
211   SCM sym = name;
212   if (scm_is_string (name))
213     sym =  scm_string_to_symbol (name);
214   
215   if (scm_is_symbol (sym))
216     {
217       if (lookup_keyword (ly_symbol2string (sym)) >= 0)
218         {
219           String symstr = ly_symbol2string (sym); 
220           warning (_f ("Identifier name is a keyword: `%s'", symstr.to_str0()));
221         }
222
223       SCM mod = ly_car (scopes_);
224
225       scm_module_define (mod, sym, s);
226     }
227   else
228     {
229       programming_error ("Identifier is not a symbol.");
230     }
231 }
232
233 void
234 Lily_lexer::LexerError (char const *s)
235 {
236   if (include_stack_.is_empty ())
237     progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
238   else
239     {
240       error_level_ |= 1;
241       Input spot (get_source_file (), here_str0 ());
242       spot.error (s);
243     }
244 }
245
246 char
247 Lily_lexer::escaped_char (char c) const
248 {
249   switch (c)
250     {
251     case 'n':
252       return '\n';
253     case 't':
254       return '\t';
255     case '\'':
256     case '\"':
257     case '\\':
258       return c;
259     }
260   return 0;
261 }
262
263 Input
264 Lily_lexer::here_input () const
265 {
266   Source_file * f= get_source_file ();
267   return Input (f, (char*)here_str0 ());
268 }
269
270 void
271 Lily_lexer::prepare_for_next_token ()
272 {
273   last_input_ = here_input ();
274 }
275
276 void
277 Lily_lexer::set_encoding (String s)
278 {
279   if (s.length ())
280     encoding_ = ly_symbol2scm (s.to_str0 ());
281   else
282     encoding_ = SCM_EOL;
283 }
284
285 #include "ly-smobs.icc"
286
287 IMPLEMENT_SMOBS (Lily_lexer);
288 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
289 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
290
291 SCM
292 Lily_lexer::mark_smob (SCM s)
293 {
294   Lily_lexer *lexer = (Lily_lexer*) ly_cdr (s);
295
296   scm_gc_mark (lexer->chordmodifier_tab_);
297   scm_gc_mark (lexer->pitchname_tab_stack_);
298   scm_gc_mark (lexer->scopes_);
299   return lexer->encoding_;
300 }
301
302 int
303 Lily_lexer::print_smob (SCM, SCM port, scm_print_state*)
304 {
305   scm_puts ("#<Lily_lexer ", port);
306   scm_puts (" >", port);
307   return 1;
308 }