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