]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-lexer.cc
374eed4ec812dc51ab33a66994b7a70fc168f164
[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--2007 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   smobify_self ();
99
100   add_scope (ly_make_anonymous_module (false));
101   push_note_state (scm_c_make_hash_table (0));
102   chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
103 }
104
105 Lily_lexer::Lily_lexer (Lily_lexer const &src, Lily_parser *parser)
106   : Includable_lexer ()
107 {
108   parser_ = parser; 
109   keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
110   chordmodifier_tab_ = src.chordmodifier_tab_;
111   pitchname_tab_stack_ = src.pitchname_tab_stack_;
112   sources_ = src.sources_;
113   start_module_ = SCM_EOL;
114
115   error_level_ = src.error_level_;
116   is_main_input_ = src.is_main_input_;
117
118   scopes_ = SCM_EOL;
119
120   smobify_self ();
121
122   SCM scopes = SCM_EOL;
123   SCM *tail = &scopes;
124   for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
125     {
126       SCM newmod = ly_make_anonymous_module (false);
127       ly_module_copy (newmod, scm_car (s));
128       *tail = scm_cons (newmod, SCM_EOL);
129       tail = SCM_CDRLOC (*tail);
130     }
131
132   scopes_ = scopes;
133   push_note_state (scm_c_make_hash_table (0));
134 }
135
136 Lily_lexer::~Lily_lexer ()
137 {
138   delete keytable_;
139 }
140
141 void
142 Lily_lexer::add_scope (SCM module)
143 {
144   ly_reexport_module (scm_current_module ());
145   if (!scm_is_pair (scopes_))
146     start_module_ = scm_current_module ();
147
148   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
149     ly_use_module (module, scm_car (s));
150   scopes_ = scm_cons (module, scopes_);
151
152   set_current_scope ();
153 }
154 bool
155 Lily_lexer::has_scope () const
156 {
157   return scm_is_pair (scopes_);
158 }
159
160 SCM
161 Lily_lexer::remove_scope ()
162 {
163   SCM sc = scm_car (scopes_);
164   scopes_ = scm_cdr (scopes_);
165   set_current_scope ();
166   return sc;
167 }
168
169 SCM
170 Lily_lexer::set_current_scope ()
171 {
172   SCM old = scm_current_module ();
173
174   if (scm_is_pair (scopes_))
175     scm_set_current_module (scm_car (scopes_));
176   else
177     scm_set_current_module (start_module_);
178
179   return old;
180 }
181
182 int
183 Lily_lexer::lookup_keyword (string s)
184 {
185   return keytable_->lookup (s.c_str ());
186 }
187
188 SCM
189 Lily_lexer::keyword_list () const
190 {
191   if (!keytable_)
192     return SCM_EOL;
193   
194   SCM l = SCM_EOL;
195   SCM *tail = &l;
196   for (vsize i = 0; i < keytable_->table_.size (); i++)
197     {
198       *tail = scm_acons (scm_from_locale_string (keytable_->table_[i].name_),
199                          scm_from_int (keytable_->table_[i].tokcode_),
200                          SCM_EOL);
201
202       tail = SCM_CDRLOC (*tail);
203     }
204
205   return l;
206 }
207
208 SCM
209 Lily_lexer::lookup_identifier_symbol (SCM sym)
210 {
211   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
212     {
213       SCM var = ly_module_lookup (scm_car (s), sym);
214       if (var != SCM_BOOL_F)
215         return scm_variable_ref (var);
216     }
217
218   return SCM_UNDEFINED;
219 }
220
221 SCM
222 Lily_lexer::lookup_identifier (string name)
223 {
224   return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
225 }
226
227 void
228 Lily_lexer::start_main_input ()
229 {
230   yy_flex_debug = get_program_option ("debug-lexer");
231   parser_->set_yydebug (get_program_option ("debug-parser"));
232
233   
234   new_input (main_input_name_, sources_);
235
236   scm_module_define (scm_car (scopes_),
237                      ly_symbol2scm ("input-file-name"),
238                      ly_string2scm (main_input_name_));
239 }
240
241 void
242 Lily_lexer::new_input (string str, string d, Sources *ss)
243 {
244   Includable_lexer::new_input (str, d, ss);
245 }
246
247 void
248 Lily_lexer::new_input (string str, Sources *ss)
249 {
250   if (is_main_input_ && be_safe_global)
251     {
252       LexerError (_ ("include files are not allowed in safe mode").c_str ());
253       return;
254     }
255
256   Includable_lexer::new_input (str, ss);
257 }
258
259 void
260 Lily_lexer::set_identifier (SCM name, SCM s)
261 {
262   SCM sym = name;
263   if (scm_is_string (name))
264     sym = scm_string_to_symbol (name);
265
266   if (scm_is_symbol (sym))
267     {
268       if (lookup_keyword (ly_symbol2string (sym)) >= 0)
269         {
270           string symstr = ly_symbol2string (sym);
271           warning (_f ("identifier name is a keyword: `%s'", symstr.c_str ()));
272         }
273
274       SCM mod = scm_car (scopes_);
275
276       scm_module_define (mod, sym, s);
277     }
278   else
279     programming_error ("identifier is not a symbol");
280 }
281
282 void
283 Lily_lexer::LexerError (char const *s)
284 {
285   if (include_stack_.empty ())
286     message (_f ("error at EOF: %s", s) + "\n");
287   else
288     {
289       error_level_ |= 1;
290       Input spot (*lexloc);
291       spot.error (s);
292     }
293 }
294
295 char
296 Lily_lexer::escaped_char (char c) const
297 {
298   switch (c)
299     {
300     case 'n':
301       return '\n';
302     case 't':
303       return '\t';
304     case '\'':
305     case '\"':
306     case '\\':
307       return c;
308     }
309   return 0;
310 }
311
312 Input
313 Lily_lexer::here_input () const
314 {
315   return Input (*lexloc);
316 }
317
318 void
319 Lily_lexer::prepare_for_next_token ()
320 {
321   last_input_ = here_input ();
322 }
323
324 /**
325    Since we don't create the buffer state from the bytes directly, we
326    don't know about the location of the lexer. Add this as a
327    YY_USER_ACTION */
328 void
329 Lily_lexer::add_lexed_char (int count)
330 {
331   char const *start = here_str0 ();
332   lexloc->set (get_source_file (),
333                start, start + count);
334   char_count_stack_.back () += count;
335 }
336
337 #include "ly-smobs.icc"
338
339 IMPLEMENT_SMOBS (Lily_lexer);
340 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
341 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
342
343 SCM
344 Lily_lexer::mark_smob (SCM s)
345 {
346   ASSERT_LIVE_IS_ALLOWED ();
347   
348   Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
349
350   scm_gc_mark (lexer->chordmodifier_tab_);
351   if (lexer->parser_)
352     scm_gc_mark (lexer->parser_->self_scm ());
353   scm_gc_mark (lexer->pitchname_tab_stack_);
354   scm_gc_mark (lexer->start_module_);
355   return lexer->scopes_;
356 }
357
358 int
359 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
360 {
361   Lily_lexer *lexer = Lily_lexer::unsmob (s);
362
363   scm_puts ("#<Lily_lexer ", port);
364   scm_display (lexer->scopes_, port);
365   scm_puts (" >", port);
366   return 1;
367 }