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