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