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