]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-lexer.cc
Remove duplicate #include in instrument-name-engraver.cc.
[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--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "lily-lexer.hh"
10
11 #include <cctype>
12 #include <sstream>
13 using namespace std;
14
15 #include "international.hh"
16 #include "interval.hh"
17 #include "keyword.hh"
18 #include "main.hh"
19 #include "moment.hh"
20 #include "parser.hh"
21 #include "scm-hash.hh"
22 #include "source-file.hh"
23 #include "warn.hh"
24 #include "program-option.hh"
25 #include "lily-parser.hh"
26
27 static Keyword_ent the_key_tab[]
28 = {
29   {"accepts", ACCEPTS},
30   {"addlyrics", ADDLYRICS},
31   {"alias", ALIAS},
32   {"alternative", ALTERNATIVE},
33   {"book", BOOK},
34   {"bookpart", BOOKPART},
35   {"change", CHANGE},
36   {"chordmode", CHORDMODE},
37   {"chords", CHORDS},
38   {"consists", CONSISTS},
39   {"context", CONTEXT},
40   {"default", DEFAULT},
41   {"defaultchild", DEFAULTCHILD},
42   {"denies", DENIES},
43   {"description", DESCRIPTION},
44   {"drummode", DRUMMODE},
45   {"drums", DRUMS},
46   {"figuremode", FIGUREMODE},
47   {"figures", FIGURES},
48   {"grobdescriptions", GROBDESCRIPTIONS},
49   {"header", HEADER},
50   {"key", KEY},
51   {"layout", LAYOUT},
52   {"lyricmode", LYRICMODE},
53   {"lyrics", LYRICS},
54   {"lyricsto", LYRICSTO},
55   {"mark", MARK},
56   {"markup", MARKUP},
57   {"markuplines", MARKUPLINES},
58   {"midi", MIDI},
59   {"name", NAME},
60   {"new", NEWCONTEXT},
61   {"notemode", NOTEMODE},
62   {"objectid", OBJECTID},
63   {"once", ONCE},
64   {"override", OVERRIDE},
65   {"paper", PAPER},
66   {"partial", PARTIAL},
67   {"relative", RELATIVE},
68   {"remove", REMOVE},
69   {"repeat", REPEAT},
70   {"rest", REST},
71   {"revert", REVERT},
72   {"score", SCORE},
73   {"sequential", SEQUENTIAL},
74   {"set", SET},
75   {"simultaneous", SIMULTANEOUS},
76   {"skip", SKIP},
77   {"tempo", TEMPO},
78   {"time", TIME_T},
79   {"times", TIMES},
80   {"transpose", TRANSPOSE},
81   {"type", TYPE},
82   {"unset", UNSET},
83   {"with", WITH},
84   {0, 0}
85 };
86
87 Lily_lexer::Lily_lexer (Sources *sources, Lily_parser *parser)
88 {
89   parser_ = parser;
90   keytable_ = new Keyword_table (the_key_tab);
91   chordmodifier_tab_ = SCM_EOL;
92   pitchname_tab_stack_ = SCM_EOL;
93   sources_ = sources;
94   scopes_ = SCM_EOL;
95   error_level_ = 0;
96   is_main_input_ = false;
97   start_module_ = SCM_EOL;
98   chord_repetition_ = Chord_repetition ();
99   smobify_self ();
100
101   add_scope (ly_make_anonymous_module (false));
102   push_note_state (scm_c_make_hash_table (0));
103   chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
104 }
105
106 Lily_lexer::Lily_lexer (Lily_lexer const &src, Lily_parser *parser)
107   : Includable_lexer ()
108 {
109   parser_ = parser; 
110   keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
111   chordmodifier_tab_ = src.chordmodifier_tab_;
112   pitchname_tab_stack_ = src.pitchname_tab_stack_;
113   sources_ = src.sources_;
114   start_module_ = SCM_EOL;
115   chord_repetition_ = Chord_repetition ();
116
117   error_level_ = src.error_level_;
118   is_main_input_ = src.is_main_input_;
119
120   scopes_ = SCM_EOL;
121
122   smobify_self ();
123
124   SCM scopes = SCM_EOL;
125   SCM *tail = &scopes;
126   for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
127     {
128       SCM newmod = ly_make_anonymous_module (false);
129       ly_module_copy (newmod, scm_car (s));
130       *tail = scm_cons (newmod, SCM_EOL);
131       tail = SCM_CDRLOC (*tail);
132     }
133
134   scopes_ = scopes;
135   push_note_state (scm_c_make_hash_table (0));
136 }
137
138 Lily_lexer::~Lily_lexer ()
139 {
140   delete keytable_;
141 }
142
143 void
144 Lily_lexer::add_scope (SCM module)
145 {
146   ly_reexport_module (scm_current_module ());
147   if (!scm_is_pair (scopes_))
148     start_module_ = scm_current_module ();
149
150   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
151     ly_use_module (module, scm_car (s));
152   scopes_ = scm_cons (module, scopes_);
153
154   set_current_scope ();
155 }
156 bool
157 Lily_lexer::has_scope () const
158 {
159   return scm_is_pair (scopes_);
160 }
161
162 SCM
163 Lily_lexer::remove_scope ()
164 {
165   SCM sc = scm_car (scopes_);
166   scopes_ = scm_cdr (scopes_);
167   set_current_scope ();
168   return sc;
169 }
170
171 SCM
172 Lily_lexer::set_current_scope ()
173 {
174   SCM old = scm_current_module ();
175
176   if (scm_is_pair (scopes_))
177     scm_set_current_module (scm_car (scopes_));
178   else
179     scm_set_current_module (start_module_);
180
181   return old;
182 }
183
184 int
185 Lily_lexer::lookup_keyword (string s)
186 {
187   return keytable_->lookup (s.c_str ());
188 }
189
190 SCM
191 Lily_lexer::keyword_list () const
192 {
193   if (!keytable_)
194     return SCM_EOL;
195   
196   SCM l = SCM_EOL;
197   SCM *tail = &l;
198   for (vsize i = 0; i < keytable_->table_.size (); i++)
199     {
200       *tail = scm_acons (scm_from_locale_string (keytable_->table_[i].name_),
201                          scm_from_int (keytable_->table_[i].tokcode_),
202                          SCM_EOL);
203
204       tail = SCM_CDRLOC (*tail);
205     }
206
207   return l;
208 }
209
210 SCM
211 Lily_lexer::lookup_identifier_symbol (SCM sym)
212 {
213   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
214     {
215       SCM var = ly_module_lookup (scm_car (s), sym);
216       if (var != SCM_BOOL_F)
217         return scm_variable_ref (var);
218     }
219
220   return SCM_UNDEFINED;
221 }
222
223 SCM
224 Lily_lexer::lookup_identifier (string name)
225 {
226   return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
227 }
228
229 void
230 Lily_lexer::start_main_input ()
231 {
232   yy_flex_debug = get_program_option ("debug-lexer");
233   parser_->set_yydebug (get_program_option ("debug-parser"));
234
235   
236   new_input (main_input_name_, sources_);
237
238   scm_module_define (scm_car (scopes_),
239                      ly_symbol2scm ("input-file-name"),
240                      ly_string2scm (main_input_name_));
241 }
242
243 void
244 Lily_lexer::new_input (string str, string d, Sources *ss)
245 {
246   Includable_lexer::new_input (str, d, ss);
247 }
248
249 void
250 Lily_lexer::new_input (string str, Sources *ss)
251 {
252   if (is_main_input_ && be_safe_global)
253     {
254       LexerError (_ ("include files are not allowed in safe mode").c_str ());
255       return;
256     }
257
258   Includable_lexer::new_input (str, ss);
259 }
260
261 void
262 Lily_lexer::set_identifier (SCM name, SCM s)
263 {
264   SCM sym = name;
265   if (scm_is_string (name))
266     sym = scm_string_to_symbol (name);
267
268   if (scm_is_symbol (sym))
269     {
270       if (lookup_keyword (ly_symbol2string (sym)) >= 0)
271         {
272           string symstr = ly_symbol2string (sym);
273           warning (_f ("identifier name is a keyword: `%s'", symstr.c_str ()));
274         }
275
276       SCM mod = scm_car (scopes_);
277
278       scm_module_define (mod, sym, s);
279     }
280   else
281     programming_error ("identifier is not a symbol");
282 }
283
284 void
285 Lily_lexer::LexerError (char const *s)
286 {
287   if (include_stack_.empty ())
288     message (_f ("error at EOF: %s", s) + "\n");
289   else
290     {
291       error_level_ |= 1;
292       Input spot (*lexloc);
293       spot.error (s);
294     }
295 }
296
297 char
298 Lily_lexer::escaped_char (char c) const
299 {
300   switch (c)
301     {
302     case 'n':
303       return '\n';
304     case 't':
305       return '\t';
306     case '\'':
307     case '\"':
308     case '\\':
309       return c;
310     }
311   return 0;
312 }
313
314 Input
315 Lily_lexer::here_input () const
316 {
317   return Input (*lexloc);
318 }
319
320 void
321 Lily_lexer::prepare_for_next_token ()
322 {
323   last_input_ = here_input ();
324 }
325
326 /**
327    Since we don't create the buffer state from the bytes directly, we
328    don't know about the location of the lexer. Add this as a
329    YY_USER_ACTION */
330 void
331 Lily_lexer::add_lexed_char (int count)
332 {
333   char const *start = here_str0 ();
334   lexloc->set (get_source_file (),
335                start, start + count);
336   char_count_stack_.back () += count;
337 }
338
339 #include "ly-smobs.icc"
340
341 IMPLEMENT_SMOBS (Lily_lexer);
342 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
343 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
344
345 SCM
346 Lily_lexer::mark_smob (SCM s)
347 {
348   ASSERT_LIVE_IS_ALLOWED ();
349   
350   Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
351
352   scm_gc_mark (lexer->chordmodifier_tab_);
353   if (lexer->parser_)
354     scm_gc_mark (lexer->parser_->self_scm ());
355   scm_gc_mark (lexer->pitchname_tab_stack_);
356   scm_gc_mark (lexer->start_module_);
357   return lexer->scopes_;
358 }
359
360 int
361 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
362 {
363   Lily_lexer *lexer = Lily_lexer::unsmob (s);
364
365   scm_puts ("#<Lily_lexer ", port);
366   scm_display (lexer->scopes_, port);
367   scm_puts (" >", port);
368   return 1;
369 }