]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-lexer.cc
parser.yy et al: Turn \transpose into a 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   {"key", KEY},
64   {"layout", LAYOUT},
65   {"lyricmode", LYRICMODE},
66   {"lyrics", LYRICS},
67   {"lyricsto", LYRICSTO},
68   {"mark", MARK},
69   {"markup", MARKUP},
70   {"markuplines", MARKUPLINES},
71   {"midi", MIDI},
72   {"name", NAME},
73   {"new", NEWCONTEXT},
74   {"notemode", NOTEMODE},
75   {"once", ONCE},
76   {"override", OVERRIDE},
77   {"paper", PAPER},
78   {"partial", PARTIAL},
79   {"relative", RELATIVE},
80   {"remove", REMOVE},
81   {"repeat", REPEAT},
82   {"rest", REST},
83   {"revert", REVERT},
84   {"score", SCORE},
85   {"sequential", SEQUENTIAL},
86   {"set", SET},
87   {"simultaneous", SIMULTANEOUS},
88   {"skip", SKIP},
89   {"tempo", TEMPO},
90   {"time", TIME_T},
91   {"times", TIMES},
92   {"type", TYPE},
93   {"unset", UNSET},
94   {"with", WITH},
95   {0, 0}
96 };
97
98 Lily_lexer::Lily_lexer (Sources *sources, Lily_parser *parser)
99 {
100   parser_ = parser;
101   keytable_ = new Keyword_table (the_key_tab);
102   chordmodifier_tab_ = SCM_EOL;
103   pitchname_tab_stack_ = SCM_EOL;
104   sources_ = sources;
105   scopes_ = SCM_EOL;
106   error_level_ = 0;
107   is_main_input_ = false;
108   start_module_ = SCM_EOL;
109   chord_repetition_ = Chord_repetition ();
110   smobify_self ();
111
112   add_scope (ly_make_module (false));
113   push_note_state (scm_c_make_hash_table (0));
114   chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
115 }
116
117 Lily_lexer::Lily_lexer (Lily_lexer const &src, Lily_parser *parser)
118   : Includable_lexer ()
119 {
120   parser_ = parser;
121   keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
122   chordmodifier_tab_ = src.chordmodifier_tab_;
123   pitchname_tab_stack_ = src.pitchname_tab_stack_;
124   sources_ = src.sources_;
125   start_module_ = SCM_EOL;
126   chord_repetition_ = src.chord_repetition_;
127
128   error_level_ = src.error_level_;
129   is_main_input_ = src.is_main_input_;
130
131   scopes_ = SCM_EOL;
132
133   smobify_self ();
134
135   SCM scopes = SCM_EOL;
136   SCM *tail = &scopes;
137   for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
138     {
139       SCM newmod = ly_make_module (false);
140       ly_module_copy (newmod, scm_car (s));
141       *tail = scm_cons (newmod, SCM_EOL);
142       tail = SCM_CDRLOC (*tail);
143     }
144
145   scopes_ = scopes;
146   push_note_state (scm_c_make_hash_table (0));
147 }
148
149 Lily_lexer::~Lily_lexer ()
150 {
151   delete keytable_;
152 }
153
154 void
155 Lily_lexer::add_scope (SCM module)
156 {
157   ly_reexport_module (scm_current_module ());
158   if (!scm_is_pair (scopes_))
159     start_module_ = scm_current_module ();
160
161   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
162     ly_use_module (module, scm_car (s));
163   scopes_ = scm_cons (module, scopes_);
164
165   set_current_scope ();
166 }
167 bool
168 Lily_lexer::has_scope () const
169 {
170   return scm_is_pair (scopes_);
171 }
172
173 SCM
174 Lily_lexer::remove_scope ()
175 {
176   SCM sc = scm_car (scopes_);
177   scopes_ = scm_cdr (scopes_);
178   set_current_scope ();
179   return sc;
180 }
181
182 SCM
183 Lily_lexer::set_current_scope ()
184 {
185   SCM old = scm_current_module ();
186
187   if (scm_is_pair (scopes_))
188     scm_set_current_module (scm_car (scopes_));
189   else
190     scm_set_current_module (start_module_);
191
192   return old;
193 }
194
195 int
196 Lily_lexer::lookup_keyword (string s)
197 {
198   return keytable_->lookup (s.c_str ());
199 }
200
201 SCM
202 Lily_lexer::keyword_list () const
203 {
204   if (!keytable_)
205     return SCM_EOL;
206
207   SCM l = SCM_EOL;
208   SCM *tail = &l;
209   for (vsize i = 0; i < keytable_->table_.size (); i++)
210     {
211       *tail = scm_acons (scm_from_locale_string (keytable_->table_[i].name_),
212                          scm_from_int (keytable_->table_[i].tokcode_),
213                          SCM_EOL);
214
215       tail = SCM_CDRLOC (*tail);
216     }
217
218   return l;
219 }
220
221 SCM
222 Lily_lexer::lookup_identifier_symbol (SCM sym)
223 {
224   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
225     {
226       SCM var = ly_module_lookup (scm_car (s), sym);
227       if (var != SCM_BOOL_F)
228         return scm_variable_ref (var);
229     }
230
231   return SCM_UNDEFINED;
232 }
233
234 SCM
235 Lily_lexer::lookup_identifier (string name)
236 {
237   return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
238 }
239
240 void
241 Lily_lexer::start_main_input ()
242 {
243   yy_flex_debug = get_program_option ("debug-lexer");
244   parser_->set_yydebug (get_program_option ("debug-parser"));
245
246   new_input (main_input_name_, sources_);
247
248   scm_module_define (scm_car (scopes_),
249                      ly_symbol2scm ("input-file-name"),
250                      ly_string2scm (main_input_name_));
251 }
252
253 void
254 Lily_lexer::new_input (string str, string d, Sources *ss)
255 {
256   Includable_lexer::new_input (str, d, ss);
257 }
258
259 void
260 Lily_lexer::new_input (string str, Sources *ss)
261 {
262   if (is_main_input_ && be_safe_global)
263     {
264       LexerError (_ ("include files are not allowed in safe mode").c_str ());
265       return;
266     }
267
268   Includable_lexer::new_input (str, ss);
269 }
270
271 // PATH is either a single symbol (or string) or a list of symbols
272 // giving the path to a nested property.  A symbol is treated the same
273 // as a list of length 1.
274 void
275 Lily_lexer::set_identifier (SCM path, SCM val)
276 {
277   SCM sym = path;
278   if (scm_is_string (path))
279     sym = scm_string_to_symbol (path);
280   else if (scm_is_pair (path))
281     {
282       sym = scm_car (path);
283       path = scm_cdr (path);
284     }
285
286   if (scm_is_symbol (sym))
287     {
288       if (lookup_keyword (ly_symbol2string (sym)) >= 0)
289         {
290           string symstr = ly_symbol2string (sym);
291           warning (_f ("identifier name is a keyword: `%s'", symstr.c_str ()));
292         }
293
294       SCM mod = scm_car (scopes_);
295
296       if (scm_is_pair (path))
297         {
298           SCM prev = ly_module_lookup (mod, sym);
299           if (prev != SCM_BOOL_F)
300             val = nested_property_alist (scm_variable_ref (prev), path, val);
301         }
302       scm_module_define (mod, sym, val);
303     }
304   else
305     programming_error ("identifier is not a symbol");
306 }
307
308 void
309 Lily_lexer::LexerError (char const *s)
310 {
311   if (include_stack_.empty ())
312     non_fatal_error (s, _f ("%s:EOF", s));
313   else
314     {
315       error_level_ |= 1;
316       Input spot (*lexloc_);
317       spot.error (s);
318     }
319 }
320
321 void
322 Lily_lexer::LexerWarning (char const *s)
323 {
324   if (include_stack_.empty ())
325     warning (s, _f ("%s:EOF", s));
326   else
327     {
328       Input spot (*lexloc_);
329       spot.warning (s);
330     }
331 }
332
333 char
334 Lily_lexer::escaped_char (char c) const
335 {
336   switch (c)
337     {
338     case 'n':
339       return '\n';
340     case 't':
341       return '\t';
342     case '\'':
343     case '\"':
344     case '\\':
345       return c;
346     }
347   return 0;
348 }
349
350 Input
351 Lily_lexer::here_input () const
352 {
353   return Input (*lexloc_);
354 }
355
356 void
357 Lily_lexer::prepare_for_next_token ()
358 {
359   last_input_ = here_input ();
360 }
361
362 /**
363    Since we don't create the buffer state from the bytes directly, we
364    don't know about the location of the lexer. Add this as a
365    YY_USER_ACTION */
366 void
367 Lily_lexer::add_lexed_char (int count)
368 {
369   char const *start = here_str0 ();
370   lexloc_->set (get_source_file (),
371                 start, start + count);
372   char_count_stack_.back () += count;
373 }
374
375 #include "ly-smobs.icc"
376
377 IMPLEMENT_SMOBS (Lily_lexer);
378 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
379 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
380
381 SCM
382 Lily_lexer::mark_smob (SCM s)
383 {
384   ASSERT_LIVE_IS_ALLOWED ();
385
386   Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
387
388   scm_gc_mark (lexer->chordmodifier_tab_);
389   if (lexer->parser_)
390     scm_gc_mark (lexer->parser_->self_scm ());
391   scm_gc_mark (lexer->pitchname_tab_stack_);
392   scm_gc_mark (lexer->start_module_);
393   return lexer->scopes_;
394 }
395
396 int
397 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state *)
398 {
399   Lily_lexer *lexer = Lily_lexer::unsmob (s);
400
401   scm_puts ("#<Lily_lexer ", port);
402   scm_display (lexer->scopes_, port);
403   scm_puts (" >", port);
404   return 1;
405 }
406
407 bool
408 Lily_lexer::is_clean () const
409 {
410   return include_stack_.empty ();
411 }