]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-lexer.cc
Merge branch 'master' of git+ssh://jneem@git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / lily / lily-lexer.cc
1 /*
2   lily-lexer.cc -- implement Lily_lexer
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "lily-lexer.hh"
10
11 #include <cctype>
12 #include <sstream>
13 using namespace std;
14
15 #include "international.hh"
16 #include "interval.hh"
17 #include "keyword.hh"
18 #include "main.hh"
19 #include "moment.hh"
20 #include "parser.hh"
21 #include "scm-hash.hh"
22 #include "source-file.hh"
23 #include "warn.hh"
24 #include "program-option.hh"
25 #include "lily-parser.hh"
26
27 static Keyword_ent the_key_tab[]
28 = {
29   {"accepts", ACCEPTS},
30   {"addlyrics", ADDLYRICS},
31   {"alias", ALIAS},
32   {"alternative", ALTERNATIVE},
33   {"book", BOOK},
34   {"change", CHANGE},
35   {"chordmode", CHORDMODE},
36   {"chords", CHORDS},
37   {"consists", CONSISTS},
38   {"context", CONTEXT},
39   {"default", DEFAULT},
40   {"defaultchild", DEFAULTCHILD},
41   {"denies", DENIES},
42   {"description", DESCRIPTION},
43   {"drummode", DRUMMODE},
44   {"drums", DRUMS},
45   {"figuremode", FIGUREMODE},
46   {"figures", FIGURES},
47   {"grobdescriptions", GROBDESCRIPTIONS},
48   {"header", HEADER},
49   {"key", KEY},
50   {"layout", LAYOUT},
51   {"lyricmode", LYRICMODE},
52   {"lyrics", LYRICS},
53   {"lyricsto", LYRICSTO},
54   {"mark", MARK},
55   {"markup", MARKUP},
56   {"midi", MIDI},
57   {"name", NAME},
58   {"new", NEWCONTEXT},
59   {"notemode", NOTEMODE},
60   {"objectid", OBJECTID},
61   {"once", ONCE},
62   {"override", OVERRIDE},
63   {"paper", PAPER},
64   {"partial", PARTIAL},
65   {"relative", RELATIVE},
66   {"remove", REMOVE},
67   {"repeat", REPEAT},
68   {"rest", REST},
69   {"revert", REVERT},
70   {"score", SCORE},
71   {"sequential", SEQUENTIAL},
72   {"set", SET},
73   {"simultaneous", SIMULTANEOUS},
74   {"skip", SKIP},
75   {"tempo", TEMPO},
76   {"time", TIME_T},
77   {"times", TIMES},
78   {"transpose", TRANSPOSE},
79   {"type", TYPE},
80   {"unset", UNSET},
81   {"with", WITH},
82   {0, 0}
83 };
84
85 Lily_lexer::Lily_lexer (Sources *sources, Lily_parser *parser)
86 {
87   parser_ = parser;
88   keytable_ = new Keyword_table (the_key_tab);
89   chordmodifier_tab_ = SCM_EOL;
90   pitchname_tab_stack_ = SCM_EOL;
91   sources_ = sources;
92   scopes_ = SCM_EOL;
93   error_level_ = 0;
94   is_main_input_ = false;
95   start_module_ = SCM_EOL;
96   smobify_self ();
97
98   add_scope (ly_make_anonymous_module (false));
99   push_note_state (scm_c_make_hash_table (0));
100   chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
101 }
102
103 Lily_lexer::Lily_lexer (Lily_lexer const &src, Lily_parser *parser)
104   : Includable_lexer ()
105 {
106   parser_ = parser; 
107   keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
108   chordmodifier_tab_ = src.chordmodifier_tab_;
109   pitchname_tab_stack_ = src.pitchname_tab_stack_;
110   sources_ = src.sources_;
111   start_module_ = SCM_EOL;
112
113   error_level_ = src.error_level_;
114   is_main_input_ = src.is_main_input_;
115
116   scopes_ = SCM_EOL;
117
118   smobify_self ();
119
120   SCM scopes = SCM_EOL;
121   SCM *tail = &scopes;
122   for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
123     {
124       SCM newmod = ly_make_anonymous_module (false);
125       ly_module_copy (newmod, scm_car (s));
126       *tail = scm_cons (newmod, SCM_EOL);
127       tail = SCM_CDRLOC (*tail);
128     }
129
130   scopes_ = scopes;
131   push_note_state (scm_c_make_hash_table (0));
132 }
133
134 Lily_lexer::~Lily_lexer ()
135 {
136   delete keytable_;
137 }
138
139 void
140 Lily_lexer::add_scope (SCM module)
141 {
142   ly_reexport_module (scm_current_module ());
143   if (!scm_is_pair (scopes_))
144     start_module_ = scm_current_module ();
145
146   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
147     ly_use_module (module, scm_car (s));
148   scopes_ = scm_cons (module, scopes_);
149
150   set_current_scope ();
151 }
152 bool
153 Lily_lexer::has_scope () const
154 {
155   return scm_is_pair (scopes_);
156 }
157
158 SCM
159 Lily_lexer::remove_scope ()
160 {
161   SCM sc = scm_car (scopes_);
162   scopes_ = scm_cdr (scopes_);
163   set_current_scope ();
164   return sc;
165 }
166
167 SCM
168 Lily_lexer::set_current_scope ()
169 {
170   SCM old = scm_current_module ();
171
172   if (scm_is_pair (scopes_))
173     scm_set_current_module (scm_car (scopes_));
174   else
175     scm_set_current_module (start_module_);
176
177   return old;
178 }
179
180 int
181 Lily_lexer::lookup_keyword (string s)
182 {
183   return keytable_->lookup (s.c_str ());
184 }
185
186 SCM
187 Lily_lexer::keyword_list () const
188 {
189   if (!keytable_)
190     return SCM_EOL;
191   
192   SCM l = SCM_EOL;
193   SCM *tail = &l;
194   for (vsize i = 0; i < keytable_->table_.size (); i++)
195     {
196       *tail = scm_acons (scm_from_locale_string (keytable_->table_[i].name_),
197                          scm_from_int (keytable_->table_[i].tokcode_),
198                          SCM_EOL);
199
200       tail = SCM_CDRLOC(*tail);
201     }
202
203   return l;
204 }
205
206 SCM
207 Lily_lexer::lookup_identifier_symbol (SCM sym)
208 {
209   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
210     {
211       SCM var = ly_module_lookup (scm_car (s), sym);
212       if (var != SCM_BOOL_F)
213         return scm_variable_ref (var);
214     }
215
216   return SCM_UNDEFINED;
217 }
218
219 SCM
220 Lily_lexer::lookup_identifier (string name)
221 {
222   return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
223 }
224
225 void
226 Lily_lexer::start_main_input ()
227 {
228   yy_flex_debug = get_program_option ("debug-lexer");
229   parser_->set_yydebug (get_program_option ("debug-parser"));
230
231   
232   new_input (main_input_name_, sources_);
233
234   scm_module_define (scm_car (scopes_),
235                      ly_symbol2scm ("input-file-name"),
236                      ly_string2scm (main_input_name_));
237 }
238
239 void
240 Lily_lexer::new_input (string str, string d, Sources *ss)
241 {
242   Includable_lexer::new_input (str, d, ss);
243 }
244
245 void
246 Lily_lexer::new_input (string str, Sources *ss)
247 {
248   if (is_main_input_ && be_safe_global)
249     {
250       LexerError (_ ("include files are not allowed in safe mode").c_str ());
251       return;
252     }
253
254   Includable_lexer::new_input (str, ss);
255 }
256
257 void
258 Lily_lexer::set_identifier (SCM name, SCM s)
259 {
260   SCM sym = name;
261   if (scm_is_string (name))
262     sym = scm_string_to_symbol (name);
263
264   if (scm_is_symbol (sym))
265     {
266       if (lookup_keyword (ly_symbol2string (sym)) >= 0)
267         {
268           string symstr = ly_symbol2string (sym);
269           warning (_f ("identifier name is a keyword: `%s'", symstr.c_str ()));
270         }
271
272       SCM mod = scm_car (scopes_);
273
274       scm_module_define (mod, sym, s);
275     }
276   else
277     programming_error ("identifier is not a symbol");
278 }
279
280 void
281 Lily_lexer::LexerError (char const *s)
282 {
283   if (include_stack_.empty ())
284     message (_f ("error at EOF: %s", s) + "\n");
285   else
286     {
287       error_level_ |= 1;
288       Input spot (*lexloc);
289       spot.error (s);
290     }
291 }
292
293 char
294 Lily_lexer::escaped_char (char c) const
295 {
296   switch (c)
297     {
298     case 'n':
299       return '\n';
300     case 't':
301       return '\t';
302     case '\'':
303     case '\"':
304     case '\\':
305       return c;
306     }
307   return 0;
308 }
309
310 Input
311 Lily_lexer::here_input () const
312 {
313   return Input (*lexloc);
314 }
315
316 void
317 Lily_lexer::prepare_for_next_token ()
318 {
319   last_input_ = here_input ();
320 }
321
322 /**
323    Since we don't create the buffer state from the bytes directly, we
324    don't know about the location of the lexer. Add this as a
325    YY_USER_ACTION */
326 void
327 Lily_lexer::add_lexed_char (int count)
328 {
329   char const *start = here_str0 ();
330   lexloc->set (get_source_file (),
331                start, start + count);
332   char_count_stack_.back () += count;
333 }
334
335 #include "ly-smobs.icc"
336
337 IMPLEMENT_SMOBS (Lily_lexer);
338 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
339 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
340
341 SCM
342 Lily_lexer::mark_smob (SCM s)
343 {
344   ASSERT_LIVE_IS_ALLOWED();
345   
346   Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
347
348   scm_gc_mark (lexer->chordmodifier_tab_);
349   if (lexer->parser_)
350     scm_gc_mark (lexer->parser_->self_scm ());
351   scm_gc_mark (lexer->pitchname_tab_stack_);
352   scm_gc_mark (lexer->start_module_);
353   return lexer->scopes_;
354 }
355
356 int
357 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
358 {
359   Lily_lexer *lexer = Lily_lexer::unsmob (s);
360
361   scm_puts ("#<Lily_lexer ", port);
362   scm_display (lexer->scopes_, port);
363   scm_puts (" >", port);
364   return 1;
365 }