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