]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-lexer.cc
Doc-de: fixing linkage
[lilypond.git] / lily / lily-lexer.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 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   {"grobdescriptions", GROBDESCRIPTIONS},
62   {"header", HEADER},
63   {"key", KEY},
64   {"layout", LAYOUT},
65   {"lyricmode", LYRICMODE},
66   {"lyrics", LYRICS},
67   {"lyricsto", LYRICSTO},
68   {"mark", MARK},
69   {"markup", MARKUP},
70   {"markuplines", MARKUPLINES},
71   {"midi", MIDI},
72   {"name", NAME},
73   {"new", NEWCONTEXT},
74   {"notemode", NOTEMODE},
75   {"once", ONCE},
76   {"override", OVERRIDE},
77   {"paper", PAPER},
78   {"partial", PARTIAL},
79   {"relative", RELATIVE},
80   {"remove", REMOVE},
81   {"repeat", REPEAT},
82   {"rest", REST},
83   {"revert", REVERT},
84   {"score", SCORE},
85   {"sequential", SEQUENTIAL},
86   {"set", SET},
87   {"simultaneous", SIMULTANEOUS},
88   {"skip", SKIP},
89   {"tempo", TEMPO},
90   {"time", TIME_T},
91   {"times", TIMES},
92   {"transpose", TRANSPOSE},
93   {"type", TYPE},
94   {"unset", UNSET},
95   {"with", WITH},
96   {0, 0}
97 };
98
99 Lily_lexer::Lily_lexer (Sources *sources, Lily_parser *parser)
100 {
101   parser_ = parser;
102   keytable_ = new Keyword_table (the_key_tab);
103   chordmodifier_tab_ = SCM_EOL;
104   pitchname_tab_stack_ = SCM_EOL;
105   sources_ = sources;
106   scopes_ = SCM_EOL;
107   error_level_ = 0;
108   is_main_input_ = false;
109   start_module_ = SCM_EOL;
110   chord_repetition_ = Chord_repetition ();
111   smobify_self ();
112
113   add_scope (ly_make_module (false));
114   push_note_state (scm_c_make_hash_table (0));
115   chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
116 }
117
118 Lily_lexer::Lily_lexer (Lily_lexer const &src, Lily_parser *parser)
119   : Includable_lexer ()
120 {
121   parser_ = parser;
122   keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
123   chordmodifier_tab_ = src.chordmodifier_tab_;
124   pitchname_tab_stack_ = src.pitchname_tab_stack_;
125   sources_ = src.sources_;
126   start_module_ = SCM_EOL;
127   chord_repetition_ = src.chord_repetition_;
128
129   error_level_ = src.error_level_;
130   is_main_input_ = src.is_main_input_;
131
132   scopes_ = SCM_EOL;
133
134   smobify_self ();
135
136   SCM scopes = SCM_EOL;
137   SCM *tail = &scopes;
138   for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
139     {
140       SCM newmod = ly_make_module (false);
141       ly_module_copy (newmod, scm_car (s));
142       *tail = scm_cons (newmod, SCM_EOL);
143       tail = SCM_CDRLOC (*tail);
144     }
145
146   scopes_ = scopes;
147   push_note_state (scm_c_make_hash_table (0));
148 }
149
150 Lily_lexer::~Lily_lexer ()
151 {
152   delete keytable_;
153 }
154
155 void
156 Lily_lexer::add_scope (SCM module)
157 {
158   ly_reexport_module (scm_current_module ());
159   if (!scm_is_pair (scopes_))
160     start_module_ = scm_current_module ();
161
162   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
163     ly_use_module (module, scm_car (s));
164   scopes_ = scm_cons (module, scopes_);
165
166   set_current_scope ();
167 }
168 bool
169 Lily_lexer::has_scope () const
170 {
171   return scm_is_pair (scopes_);
172 }
173
174 SCM
175 Lily_lexer::remove_scope ()
176 {
177   SCM sc = scm_car (scopes_);
178   scopes_ = scm_cdr (scopes_);
179   set_current_scope ();
180   return sc;
181 }
182
183 SCM
184 Lily_lexer::set_current_scope ()
185 {
186   SCM old = scm_current_module ();
187
188   if (scm_is_pair (scopes_))
189     scm_set_current_module (scm_car (scopes_));
190   else
191     scm_set_current_module (start_module_);
192
193   return old;
194 }
195
196 int
197 Lily_lexer::lookup_keyword (string s)
198 {
199   return keytable_->lookup (s.c_str ());
200 }
201
202 SCM
203 Lily_lexer::keyword_list () const
204 {
205   if (!keytable_)
206     return SCM_EOL;
207
208   SCM l = SCM_EOL;
209   SCM *tail = &l;
210   for (vsize i = 0; i < keytable_->table_.size (); i++)
211     {
212       *tail = scm_acons (scm_from_locale_string (keytable_->table_[i].name_),
213                          scm_from_int (keytable_->table_[i].tokcode_),
214                          SCM_EOL);
215
216       tail = SCM_CDRLOC (*tail);
217     }
218
219   return l;
220 }
221
222 SCM
223 Lily_lexer::lookup_identifier_symbol (SCM sym)
224 {
225   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
226     {
227       SCM var = ly_module_lookup (scm_car (s), sym);
228       if (var != SCM_BOOL_F)
229         return scm_variable_ref (var);
230     }
231
232   return SCM_UNDEFINED;
233 }
234
235 SCM
236 Lily_lexer::lookup_identifier (string name)
237 {
238   return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
239 }
240
241 void
242 Lily_lexer::start_main_input ()
243 {
244   yy_flex_debug = get_program_option ("debug-lexer");
245   parser_->set_yydebug (get_program_option ("debug-parser"));
246
247   new_input (main_input_name_, sources_);
248
249   scm_module_define (scm_car (scopes_),
250                      ly_symbol2scm ("input-file-name"),
251                      ly_string2scm (main_input_name_));
252 }
253
254 void
255 Lily_lexer::new_input (string str, string d, Sources *ss)
256 {
257   Includable_lexer::new_input (str, d, ss);
258 }
259
260 void
261 Lily_lexer::new_input (string str, Sources *ss)
262 {
263   if (is_main_input_ && be_safe_global)
264     {
265       LexerError (_ ("include files are not allowed in safe mode").c_str ());
266       return;
267     }
268
269   Includable_lexer::new_input (str, ss);
270 }
271
272 // PATH is either a single symbol (or string) or a list of symbols
273 // giving the path to a nested property.  A symbol is treated the same
274 // as a list of length 1.
275 void
276 Lily_lexer::set_identifier (SCM path, SCM val)
277 {
278   SCM sym = path;
279   if (scm_is_string (path))
280     sym = scm_string_to_symbol (path);
281   else if (scm_is_pair (path))
282     {
283       sym = scm_car (path);
284       path = scm_cdr (path);
285     }
286
287   if (scm_is_symbol (sym))
288     {
289       if (lookup_keyword (ly_symbol2string (sym)) >= 0)
290         {
291           string symstr = ly_symbol2string (sym);
292           warning (_f ("identifier name is a keyword: `%s'", symstr.c_str ()));
293         }
294
295       SCM mod = scm_car (scopes_);
296
297       if (scm_is_pair (path))
298         {
299           SCM prev = ly_module_lookup (mod, sym);
300           if (prev != SCM_BOOL_F)
301             val = nested_property_alist (scm_variable_ref (prev), path, val);
302         }
303       scm_module_define (mod, sym, val);
304     }
305   else
306     programming_error ("identifier is not a symbol");
307 }
308
309 void
310 Lily_lexer::LexerError (char const *s)
311 {
312   if (include_stack_.empty ())
313     message (_f ("error at EOF: %s", s) + "\n");
314   else
315     {
316       error_level_ |= 1;
317       Input spot (*lexloc_);
318       spot.error (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 void
346 Lily_lexer::prepare_for_next_token ()
347 {
348   last_input_ = here_input ();
349 }
350
351 /**
352    Since we don't create the buffer state from the bytes directly, we
353    don't know about the location of the lexer. Add this as a
354    YY_USER_ACTION */
355 void
356 Lily_lexer::add_lexed_char (int count)
357 {
358   char const *start = here_str0 ();
359   lexloc_->set (get_source_file (),
360                 start, start + count);
361   char_count_stack_.back () += count;
362 }
363
364 #include "ly-smobs.icc"
365
366 IMPLEMENT_SMOBS (Lily_lexer);
367 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
368 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
369
370 SCM
371 Lily_lexer::mark_smob (SCM s)
372 {
373   ASSERT_LIVE_IS_ALLOWED ();
374
375   Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
376
377   scm_gc_mark (lexer->chordmodifier_tab_);
378   if (lexer->parser_)
379     scm_gc_mark (lexer->parser_->self_scm ());
380   scm_gc_mark (lexer->pitchname_tab_stack_);
381   scm_gc_mark (lexer->start_module_);
382   return lexer->scopes_;
383 }
384
385 int
386 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state *)
387 {
388   Lily_lexer *lexer = Lily_lexer::unsmob (s);
389
390   scm_puts ("#<Lily_lexer ", port);
391   scm_display (lexer->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 }