2 lily-lexer.cc -- implement Lily_lexer
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 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},
35 {"chordmode", CHORDMODE},
37 {"consists", CONSISTS},
40 {"defaultchild", DEFAULTCHILD},
42 {"description", DESCRIPTION},
43 {"drummode", DRUMMODE},
45 {"figuremode", FIGUREMODE},
47 {"grobdescriptions", GROBDESCRIPTIONS},
51 {"lyricmode", LYRICMODE},
53 {"lyricsto", LYRICSTO},
56 {"markuplines", MARKUPLINES},
60 {"notemode", NOTEMODE},
61 {"objectid", OBJECTID},
63 {"override", OVERRIDE},
66 {"relative", RELATIVE},
72 {"sequential", SEQUENTIAL},
74 {"simultaneous", SIMULTANEOUS},
79 {"transpose", TRANSPOSE},
86 Lily_lexer::Lily_lexer (Sources *sources, Lily_parser *parser)
89 keytable_ = new Keyword_table (the_key_tab);
90 chordmodifier_tab_ = SCM_EOL;
91 pitchname_tab_stack_ = SCM_EOL;
95 is_main_input_ = false;
96 start_module_ = SCM_EOL;
99 add_scope (ly_make_anonymous_module (false));
100 push_note_state (scm_c_make_hash_table (0));
101 chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
104 Lily_lexer::Lily_lexer (Lily_lexer const &src, Lily_parser *parser)
105 : Includable_lexer ()
108 keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
109 chordmodifier_tab_ = src.chordmodifier_tab_;
110 pitchname_tab_stack_ = src.pitchname_tab_stack_;
111 sources_ = src.sources_;
112 start_module_ = SCM_EOL;
114 error_level_ = src.error_level_;
115 is_main_input_ = src.is_main_input_;
121 SCM scopes = SCM_EOL;
123 for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
125 SCM newmod = ly_make_anonymous_module (false);
126 ly_module_copy (newmod, scm_car (s));
127 *tail = scm_cons (newmod, SCM_EOL);
128 tail = SCM_CDRLOC (*tail);
132 push_note_state (scm_c_make_hash_table (0));
135 Lily_lexer::~Lily_lexer ()
141 Lily_lexer::add_scope (SCM module)
143 ly_reexport_module (scm_current_module ());
144 if (!scm_is_pair (scopes_))
145 start_module_ = scm_current_module ();
147 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
148 ly_use_module (module, scm_car (s));
149 scopes_ = scm_cons (module, scopes_);
151 set_current_scope ();
154 Lily_lexer::has_scope () const
156 return scm_is_pair (scopes_);
160 Lily_lexer::remove_scope ()
162 SCM sc = scm_car (scopes_);
163 scopes_ = scm_cdr (scopes_);
164 set_current_scope ();
169 Lily_lexer::set_current_scope ()
171 SCM old = scm_current_module ();
173 if (scm_is_pair (scopes_))
174 scm_set_current_module (scm_car (scopes_));
176 scm_set_current_module (start_module_);
182 Lily_lexer::lookup_keyword (string s)
184 return keytable_->lookup (s.c_str ());
188 Lily_lexer::keyword_list () const
195 for (vsize i = 0; i < keytable_->table_.size (); i++)
197 *tail = scm_acons (scm_from_locale_string (keytable_->table_[i].name_),
198 scm_from_int (keytable_->table_[i].tokcode_),
201 tail = SCM_CDRLOC (*tail);
208 Lily_lexer::lookup_identifier_symbol (SCM sym)
210 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
212 SCM var = ly_module_lookup (scm_car (s), sym);
213 if (var != SCM_BOOL_F)
214 return scm_variable_ref (var);
217 return SCM_UNDEFINED;
221 Lily_lexer::lookup_identifier (string name)
223 return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
227 Lily_lexer::start_main_input ()
229 yy_flex_debug = get_program_option ("debug-lexer");
230 parser_->set_yydebug (get_program_option ("debug-parser"));
233 new_input (main_input_name_, sources_);
235 scm_module_define (scm_car (scopes_),
236 ly_symbol2scm ("input-file-name"),
237 ly_string2scm (main_input_name_));
241 Lily_lexer::new_input (string str, string d, Sources *ss)
243 Includable_lexer::new_input (str, d, ss);
247 Lily_lexer::new_input (string str, Sources *ss)
249 if (is_main_input_ && be_safe_global)
251 LexerError (_ ("include files are not allowed in safe mode").c_str ());
255 Includable_lexer::new_input (str, ss);
259 Lily_lexer::set_identifier (SCM name, SCM s)
262 if (scm_is_string (name))
263 sym = scm_string_to_symbol (name);
265 if (scm_is_symbol (sym))
267 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
269 string symstr = ly_symbol2string (sym);
270 warning (_f ("identifier name is a keyword: `%s'", symstr.c_str ()));
273 SCM mod = scm_car (scopes_);
275 scm_module_define (mod, sym, s);
278 programming_error ("identifier is not a symbol");
282 Lily_lexer::LexerError (char const *s)
284 if (include_stack_.empty ())
285 message (_f ("error at EOF: %s", s) + "\n");
289 Input spot (*lexloc);
295 Lily_lexer::escaped_char (char c) const
312 Lily_lexer::here_input () const
314 return Input (*lexloc);
318 Lily_lexer::prepare_for_next_token ()
320 last_input_ = here_input ();
324 Since we don't create the buffer state from the bytes directly, we
325 don't know about the location of the lexer. Add this as a
328 Lily_lexer::add_lexed_char (int count)
330 char const *start = here_str0 ();
331 lexloc->set (get_source_file (),
332 start, start + count);
333 char_count_stack_.back () += count;
336 #include "ly-smobs.icc"
338 IMPLEMENT_SMOBS (Lily_lexer);
339 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
340 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
343 Lily_lexer::mark_smob (SCM s)
345 ASSERT_LIVE_IS_ALLOWED ();
347 Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
349 scm_gc_mark (lexer->chordmodifier_tab_);
351 scm_gc_mark (lexer->parser_->self_scm ());
352 scm_gc_mark (lexer->pitchname_tab_stack_);
353 scm_gc_mark (lexer->start_module_);
354 return lexer->scopes_;
358 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
360 Lily_lexer *lexer = Lily_lexer::unsmob (s);
362 scm_puts ("#<Lily_lexer ", port);
363 scm_display (lexer->scopes_, port);
364 scm_puts (" >", port);