2 lily-lexer.cc -- implement Lily_lexer
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "lily-lexer.hh"
15 #include "international.hh"
16 #include "interval.hh"
21 #include "scm-hash.hh"
22 #include "source-file.hh"
24 #include "program-option.hh"
25 #include "lily-parser.hh"
27 static Keyword_ent the_key_tab[]
30 {"addlyrics", ADDLYRICS},
32 {"alternative", ALTERNATIVE},
34 {"bookpart", BOOKPART},
36 {"chordmode", CHORDMODE},
38 {"consists", CONSISTS},
41 {"defaultchild", DEFAULTCHILD},
43 {"description", DESCRIPTION},
44 {"drummode", DRUMMODE},
46 {"figuremode", FIGUREMODE},
48 {"grobdescriptions", GROBDESCRIPTIONS},
52 {"lyricmode", LYRICMODE},
54 {"lyricsto", LYRICSTO},
57 {"markuplines", MARKUPLINES},
61 {"notemode", NOTEMODE},
62 {"objectid", OBJECTID},
64 {"override", OVERRIDE},
67 {"relative", RELATIVE},
73 {"sequential", SEQUENTIAL},
75 {"simultaneous", SIMULTANEOUS},
80 {"transpose", TRANSPOSE},
87 Lily_lexer::Lily_lexer (Sources *sources, Lily_parser *parser)
90 keytable_ = new Keyword_table (the_key_tab);
91 chordmodifier_tab_ = SCM_EOL;
92 pitchname_tab_stack_ = SCM_EOL;
96 is_main_input_ = false;
97 start_module_ = SCM_EOL;
100 add_scope (ly_make_anonymous_module (false));
101 push_note_state (scm_c_make_hash_table (0));
102 chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
105 Lily_lexer::Lily_lexer (Lily_lexer const &src, Lily_parser *parser)
106 : Includable_lexer ()
109 keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
110 chordmodifier_tab_ = src.chordmodifier_tab_;
111 pitchname_tab_stack_ = src.pitchname_tab_stack_;
112 sources_ = src.sources_;
113 start_module_ = SCM_EOL;
115 error_level_ = src.error_level_;
116 is_main_input_ = src.is_main_input_;
122 SCM scopes = SCM_EOL;
124 for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
126 SCM newmod = ly_make_anonymous_module (false);
127 ly_module_copy (newmod, scm_car (s));
128 *tail = scm_cons (newmod, SCM_EOL);
129 tail = SCM_CDRLOC (*tail);
133 push_note_state (scm_c_make_hash_table (0));
136 Lily_lexer::~Lily_lexer ()
142 Lily_lexer::add_scope (SCM module)
144 ly_reexport_module (scm_current_module ());
145 if (!scm_is_pair (scopes_))
146 start_module_ = scm_current_module ();
148 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
149 ly_use_module (module, scm_car (s));
150 scopes_ = scm_cons (module, scopes_);
152 set_current_scope ();
155 Lily_lexer::has_scope () const
157 return scm_is_pair (scopes_);
161 Lily_lexer::remove_scope ()
163 SCM sc = scm_car (scopes_);
164 scopes_ = scm_cdr (scopes_);
165 set_current_scope ();
170 Lily_lexer::set_current_scope ()
172 SCM old = scm_current_module ();
174 if (scm_is_pair (scopes_))
175 scm_set_current_module (scm_car (scopes_));
177 scm_set_current_module (start_module_);
183 Lily_lexer::lookup_keyword (string s)
185 return keytable_->lookup (s.c_str ());
189 Lily_lexer::keyword_list () const
196 for (vsize i = 0; i < keytable_->table_.size (); i++)
198 *tail = scm_acons (scm_from_locale_string (keytable_->table_[i].name_),
199 scm_from_int (keytable_->table_[i].tokcode_),
202 tail = SCM_CDRLOC (*tail);
209 Lily_lexer::lookup_identifier_symbol (SCM sym)
211 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
213 SCM var = ly_module_lookup (scm_car (s), sym);
214 if (var != SCM_BOOL_F)
215 return scm_variable_ref (var);
218 return SCM_UNDEFINED;
222 Lily_lexer::lookup_identifier (string name)
224 return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
228 Lily_lexer::start_main_input ()
230 yy_flex_debug = get_program_option ("debug-lexer");
231 parser_->set_yydebug (get_program_option ("debug-parser"));
234 new_input (main_input_name_, sources_);
236 scm_module_define (scm_car (scopes_),
237 ly_symbol2scm ("input-file-name"),
238 ly_string2scm (main_input_name_));
242 Lily_lexer::new_input (string str, string d, Sources *ss)
244 Includable_lexer::new_input (str, d, ss);
248 Lily_lexer::new_input (string str, Sources *ss)
250 if (is_main_input_ && be_safe_global)
252 LexerError (_ ("include files are not allowed in safe mode").c_str ());
256 Includable_lexer::new_input (str, ss);
260 Lily_lexer::set_identifier (SCM name, SCM s)
263 if (scm_is_string (name))
264 sym = scm_string_to_symbol (name);
266 if (scm_is_symbol (sym))
268 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
270 string symstr = ly_symbol2string (sym);
271 warning (_f ("identifier name is a keyword: `%s'", symstr.c_str ()));
274 SCM mod = scm_car (scopes_);
276 scm_module_define (mod, sym, s);
279 programming_error ("identifier is not a symbol");
283 Lily_lexer::LexerError (char const *s)
285 if (include_stack_.empty ())
286 message (_f ("error at EOF: %s", s) + "\n");
290 Input spot (*lexloc);
296 Lily_lexer::escaped_char (char c) const
313 Lily_lexer::here_input () const
315 return Input (*lexloc);
319 Lily_lexer::prepare_for_next_token ()
321 last_input_ = here_input ();
325 Since we don't create the buffer state from the bytes directly, we
326 don't know about the location of the lexer. Add this as a
329 Lily_lexer::add_lexed_char (int count)
331 char const *start = here_str0 ();
332 lexloc->set (get_source_file (),
333 start, start + count);
334 char_count_stack_.back () += count;
337 #include "ly-smobs.icc"
339 IMPLEMENT_SMOBS (Lily_lexer);
340 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
341 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
344 Lily_lexer::mark_smob (SCM s)
346 ASSERT_LIVE_IS_ALLOWED ();
348 Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
350 scm_gc_mark (lexer->chordmodifier_tab_);
352 scm_gc_mark (lexer->parser_->self_scm ());
353 scm_gc_mark (lexer->pitchname_tab_stack_);
354 scm_gc_mark (lexer->start_module_);
355 return lexer->scopes_;
359 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
361 Lily_lexer *lexer = Lily_lexer::unsmob (s);
363 scm_puts ("#<Lily_lexer ", port);
364 scm_display (lexer->scopes_, port);
365 scm_puts (" >", port);