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