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