]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-lexer.cc
Issue 4464/2: include hygiene: break cycles, remove duplicate includes etc.
[lilypond.git] / lily / lily-lexer.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 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 #include "ly-module.hh"
39
40 static Keyword_ent the_key_tab[]
41 =
42 {
43   {"accepts", ACCEPTS},
44   {"addlyrics", ADDLYRICS},
45   {"alias", ALIAS},
46   {"alternative", ALTERNATIVE},
47   {"book", BOOK},
48   {"bookpart", BOOKPART},
49   {"change", CHANGE},
50   {"chordmode", CHORDMODE},
51   {"chords", CHORDS},
52   {"consists", CONSISTS},
53   {"context", CONTEXT},
54   {"default", DEFAULT},
55   {"defaultchild", DEFAULTCHILD},
56   {"denies", DENIES},
57   {"description", DESCRIPTION},
58   {"drummode", DRUMMODE},
59   {"drums", DRUMS},
60   {"figuremode", FIGUREMODE},
61   {"figures", FIGURES},
62   {"header", HEADER},
63   {"layout", LAYOUT},
64   {"lyricmode", LYRICMODE},
65   {"lyrics", LYRICS},
66   {"lyricsto", LYRICSTO},
67   {"markup", MARKUP},
68   {"markuplist", MARKUPLIST},
69   {"midi", MIDI},
70   {"name", NAME},
71   {"new", NEWCONTEXT},
72   {"notemode", NOTEMODE},
73   {"override", OVERRIDE},
74   {"paper", PAPER},
75   {"remove", REMOVE},
76   {"repeat", REPEAT},
77   {"rest", REST},
78   {"revert", REVERT},
79   {"score", SCORE},
80   {"sequential", SEQUENTIAL},
81   {"set", SET},
82   {"simultaneous", SIMULTANEOUS},
83   {"tempo", TEMPO},
84   {"type", TYPE},
85   {"unset", UNSET},
86   {"with", WITH},
87   {0, 0}
88 };
89
90 Lily_lexer::Lily_lexer (Sources *sources, Lily_parser *parser)
91 {
92   parser_ = parser;
93   keytable_ = new Keyword_table (the_key_tab);
94   chordmodifier_tab_ = SCM_EOL;
95   pitchname_tab_stack_ = SCM_EOL;
96   sources_ = sources;
97   scopes_ = SCM_EOL;
98   error_level_ = 0;
99   is_main_input_ = false;
100   main_input_level_ = 0;
101   start_module_ = SCM_EOL;
102   extra_tokens_ = SCM_EOL;
103   smobify_self ();
104
105   add_scope (ly_make_module (false));
106   push_note_state (SCM_EOL);
107   chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
108 }
109
110 Lily_lexer::Lily_lexer (Lily_lexer const &src, Lily_parser *parser,
111                         SCM override_input)
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   scopes_ = src.scopes_;
120   start_module_ = SCM_EOL;
121
122   error_level_ = 0;
123   is_main_input_ = src.is_main_input_;
124   main_input_level_ = 0;
125
126   extra_tokens_ = SCM_EOL;
127   if (unsmob<Input> (override_input))
128     override_input_ = *unsmob<Input> (override_input);
129
130   smobify_self ();
131
132   push_note_state (SCM_EOL);
133 }
134
135 Lily_lexer::~Lily_lexer ()
136 {
137   delete keytable_;
138 }
139
140 void
141 Lily_lexer::add_scope (SCM module)
142 {
143   ly_reexport_module (scm_current_module ());
144   if (!scm_is_pair (scopes_))
145     start_module_ = scm_current_module ();
146
147   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
148     ly_use_module (module, scm_car (s));
149   scopes_ = scm_cons (module, scopes_);
150
151   set_current_scope ();
152 }
153 bool
154 Lily_lexer::has_scope () const
155 {
156   return scm_is_pair (scopes_);
157 }
158
159 SCM
160 Lily_lexer::remove_scope ()
161 {
162   SCM sc = scm_car (scopes_);
163   scopes_ = scm_cdr (scopes_);
164   set_current_scope ();
165   return sc;
166 }
167
168 SCM
169 Lily_lexer::set_current_scope ()
170 {
171   SCM old = scm_current_module ();
172
173   if (scm_is_pair (scopes_))
174     scm_set_current_module (scm_car (scopes_));
175   else
176     scm_set_current_module (start_module_);
177
178   return old;
179 }
180
181 int
182 Lily_lexer::lookup_keyword (const string &s)
183 {
184   return keytable_->lookup (s.c_str ());
185 }
186
187 SCM
188 Lily_lexer::keyword_list () const
189 {
190   if (!keytable_)
191     return SCM_EOL;
192
193   SCM l = SCM_EOL;
194   SCM *tail = &l;
195   for (vsize i = 0; i < keytable_->table_.size (); i++)
196     {
197       *tail = scm_acons (scm_from_utf8_string (keytable_->table_[i].name_),
198                          scm_from_int (keytable_->table_[i].tokcode_),
199                          SCM_EOL);
200
201       tail = SCM_CDRLOC (*tail);
202     }
203
204   return l;
205 }
206
207 SCM
208 Lily_lexer::lookup_identifier_symbol (SCM sym)
209 {
210   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
211     {
212       SCM var = ly_module_lookup (scm_car (s), sym);
213       if (scm_is_true (var))
214         return scm_variable_ref (var);
215     }
216
217   return SCM_UNDEFINED;
218 }
219
220 SCM
221 Lily_lexer::lookup_identifier (const string &name)
222 {
223   return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
224 }
225
226 void
227 Lily_lexer::start_main_input ()
228 {
229   yy_flex_debug = get_program_option ("debug-lexer");
230   parser_->set_yydebug (get_program_option ("debug-parser"));
231
232   new_input (main_input_name_, sources_);
233
234   scm_module_define (scm_car (scopes_),
235                      ly_symbol2scm ("input-file-name"),
236                      ly_string2scm (main_input_name_));
237 }
238
239 void
240 Lily_lexer::new_input (const string &str, string d, Sources *ss)
241 {
242   Includable_lexer::new_input (str, d, ss);
243 }
244
245 void
246 Lily_lexer::new_input (const string &str, Sources *ss)
247 {
248   if (is_main_input_ && be_safe_global)
249     {
250       LexerError (_ ("include files are not allowed in safe mode").c_str ());
251       return;
252     }
253
254   Includable_lexer::new_input (str, ss);
255 }
256
257 // PATH is either a single symbol (or string) or a list of symbols
258 // giving the path to a nested property.  A symbol is treated the same
259 // as a list of length 1.
260 void
261 Lily_lexer::set_identifier (SCM path, SCM val)
262 {
263   SCM sym = path;
264   if (scm_is_string (path))
265     sym = scm_string_to_symbol (path);
266   else if (scm_is_pair (path))
267     {
268       sym = scm_car (path);
269       path = scm_cdr (path);
270     }
271
272   if (scm_is_symbol (sym))
273     {
274       if (lookup_keyword (ly_symbol2string (sym)) >= 0)
275         {
276           string symstr = ly_symbol2string (sym);
277           warning (_f ("identifier name is a keyword: `%s'", symstr.c_str ()));
278         }
279
280       SCM mod = scm_car (scopes_);
281
282       if (scm_is_pair (path))
283         {
284           SCM prev = ly_module_lookup (mod, sym);
285           if (scm_is_true (prev))
286             val = nested_property_alist (scm_variable_ref (prev), path, val);
287         }
288       scm_module_define (mod, sym, val);
289     }
290   else
291     programming_error ("identifier is not a symbol");
292 }
293
294 void
295 Lily_lexer::LexerError (char const *s)
296 {
297   if (include_stack_.empty ())
298     non_fatal_error (s, _f ("%s:EOF", s));
299   else
300     {
301       error_level_ |= 1;
302       Input spot (*lexloc_);
303       spot.non_fatal_error (s);
304     }
305 }
306
307 void
308 Lily_lexer::LexerWarning (char const *s)
309 {
310   if (include_stack_.empty ())
311     warning (s, _f ("%s:EOF", s));
312   else
313     {
314       Input spot (*lexloc_);
315       spot.warning (s);
316     }
317 }
318
319 char
320 Lily_lexer::escaped_char (char c) const
321 {
322   switch (c)
323     {
324     case 'n':
325       return '\n';
326     case 't':
327       return '\t';
328     case '\'':
329     case '\"':
330     case '\\':
331       return c;
332     }
333   return 0;
334 }
335
336 Input
337 Lily_lexer::here_input () const
338 {
339   return Input (*lexloc_);
340 }
341
342 Input const &
343 Lily_lexer::override_input (Input const &in) const
344 {
345   return override_input_.get_source_file ()
346     ? override_input_ : in;
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
369 const char Lily_lexer::type_p_name_[] = "ly:lily-lexer?";
370
371 SCM
372 Lily_lexer::mark_smob () const
373 {
374   ASSERT_LIVE_IS_ALLOWED (self_scm ());
375
376   scm_gc_mark (chordmodifier_tab_);
377   if (parser_)
378     scm_gc_mark (parser_->self_scm ());
379   scm_gc_mark (pitchname_tab_stack_);
380   scm_gc_mark (start_module_);
381   scm_gc_mark (extra_tokens_);
382   return scopes_;
383 }
384
385 int
386 Lily_lexer::print_smob (SCM port, scm_print_state *) const
387 {
388   scm_puts ("#<Lily_lexer ", port);
389   scm_display (scopes_, port);
390   scm_puts (" >", port);
391   return 1;
392 }
393
394 bool
395 Lily_lexer::is_clean () const
396 {
397   return include_stack_.empty ();
398 }