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