2 lily-lexer.cc -- implement Lily_lexer
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "lily-lexer.hh"
14 #include "scm-hash.hh"
15 #include "interval.hh"
19 #include "source-file.hh"
23 static Keyword_ent the_key_tab[]
26 {"addlyrics", ADDLYRICS},
27 {"addquote", ADDQUOTE},
29 {"alternative", ALTERNATIVE},
33 {"chordmode", CHORDMODE},
36 {"consists", CONSISTS},
39 {"defaultchild", DEFAULTCHILD},
41 {"description", DESCRIPTION},
42 {"drummode", DRUMMODE},
44 {"figuremode", FIGUREMODE},
46 {"grobdescriptions", GROBDESCRIPTIONS},
50 {"lyricmode", LYRICMODE},
52 {"lyricsto", LYRICSTO},
58 {"notemode", NOTEMODE},
59 {"objectid", OBJECTID},
62 {"override", OVERRIDE},
65 {"relative", RELATIVE},
71 {"sequential", SEQUENTIAL},
73 {"simultaneous", SIMULTANEOUS},
79 {"transpose", TRANSPOSE},
80 {"transposition", TRANSPOSITION},
87 Lily_lexer::Lily_lexer (Sources *sources)
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_int2num (1), SCM_EOL);
104 Lily_lexer::Lily_lexer (Lily_lexer const &src)
105 : Includable_lexer ()
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;
113 error_level_ = src.error_level_;
114 is_main_input_ = src.is_main_input_;
120 SCM scopes = SCM_EOL;
122 for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
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);
131 push_note_state (scm_c_make_hash_table (0));
134 Lily_lexer::~Lily_lexer ()
140 Lily_lexer::add_scope (SCM module)
142 ly_reexport_module (scm_current_module ());
143 if (!scm_is_pair (scopes_))
144 start_module_ = scm_current_module ();
146 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
148 ly_use_module (module, scm_car (s));
150 scopes_ = scm_cons (module, scopes_);
152 set_current_scope ();
157 Lily_lexer::remove_scope ()
159 SCM sc = scm_car (scopes_);
160 scopes_ = scm_cdr (scopes_);
161 set_current_scope ();
166 Lily_lexer::set_current_scope ()
168 SCM old = scm_current_module ();
170 if (scm_is_pair (scopes_))
171 scm_set_current_module (scm_car (scopes_));
173 scm_set_current_module (start_module_);
179 Lily_lexer::lookup_keyword (String s)
181 return keytable_->lookup (s.to_str0 ());
185 Lily_lexer::lookup_identifier_symbol (SCM sym)
187 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
189 SCM var = ly_module_lookup (scm_car (s), sym);
190 if (var != SCM_BOOL_F)
191 return scm_variable_ref (var);
194 return SCM_UNDEFINED;
198 Lily_lexer::lookup_identifier (String name)
200 return lookup_identifier_symbol (ly_symbol2scm (name.to_str0 ()));
204 Lily_lexer::start_main_input ()
206 // yy_flex_debug = 1;
207 new_input (main_input_name_, sources_);
209 /* Do not allow \include in --safe-mode */
210 allow_includes_b_ = allow_includes_b_ && !be_safe_global;
212 scm_module_define (scm_car (scopes_),
213 ly_symbol2scm ("input-file-name"),
214 scm_makfrom0str (main_input_name_.to_str0 ()));
218 Lily_lexer::set_identifier (SCM name, SCM s)
221 if (scm_is_string (name))
222 sym = scm_string_to_symbol (name);
224 if (scm_is_symbol (sym))
226 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
228 String symstr = ly_symbol2string (sym);
229 warning (_f ("identifier name is a keyword: `%s'", symstr.to_str0 ()));
232 SCM mod = scm_car (scopes_);
234 scm_module_define (mod, sym, s);
237 programming_error ("identifier is not a symbol");
241 Lily_lexer::LexerError (char const *s)
243 if (include_stack_.is_empty ())
244 message (_f ("error at EOF: %s", s) + "\n");
248 Input spot (*lexloc);
254 Lily_lexer::escaped_char (char c) const
271 Lily_lexer::here_input () const
273 return Input(*lexloc);
277 Lily_lexer::prepare_for_next_token ()
279 last_input_ = here_input ();
283 Since we don't create the buffer state from the bytes directly, we
284 don't know about the location of the lexer. Add this as a
287 Lily_lexer::add_lexed_char (int count)
289 char const * start = here_str0 ();
290 lexloc->set (get_source_file (),
291 start, start + count);
292 char_count_stack_.top () += count;
295 #include "ly-smobs.icc"
297 IMPLEMENT_SMOBS (Lily_lexer);
298 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
299 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
302 Lily_lexer::mark_smob (SCM s)
304 Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
306 scm_gc_mark (lexer->chordmodifier_tab_);
307 scm_gc_mark (lexer->pitchname_tab_stack_);
308 scm_gc_mark (lexer->start_module_);
309 return lexer->scopes_;
313 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
315 Lily_lexer *lexer = Lily_lexer::unsmob (s);
317 scm_puts ("#<Lily_lexer ", port);
318 scm_display (lexer->scopes_, port);
319 scm_puts (" >", port);