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