2 lily-lexer.cc -- implement Lily_lexer
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "lily-lexer.hh"
15 #include "scm-hash.hh"
16 #include "interval.hh"
20 #include "source-file.hh"
24 static Keyword_ent the_key_tab[]
27 {"addlyrics", ADDLYRICS},
28 {"addquote", ADDQUOTE},
30 {"alternative", ALTERNATIVE},
33 {"chordmode", CHORDMODE},
35 {"consists", CONSISTS},
38 {"defaultchild", DEFAULTCHILD},
40 {"description", DESCRIPTION},
41 {"drummode", DRUMMODE},
43 {"figuremode", FIGUREMODE},
45 {"grobdescriptions", GROBDESCRIPTIONS},
49 {"lyricmode", LYRICMODE},
51 {"lyricsto", LYRICSTO},
57 {"notemode", NOTEMODE},
58 {"objectid", OBJECTID},
61 {"override", OVERRIDE},
64 {"relative", RELATIVE},
70 {"sequential", SEQUENTIAL},
72 {"simultaneous", SIMULTANEOUS},
77 {"transpose", TRANSPOSE},
78 {"transposition", TRANSPOSITION},
85 Lily_lexer::Lily_lexer (Sources *sources)
87 keytable_ = new Keyword_table (the_key_tab);
88 chordmodifier_tab_ = SCM_EOL;
89 pitchname_tab_stack_ = SCM_EOL;
93 is_main_input_ = false;
94 start_module_ = SCM_EOL;
97 add_scope (ly_make_anonymous_module (false));
98 push_note_state (scm_c_make_hash_table (0));
99 chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
102 Lily_lexer::Lily_lexer (Lily_lexer const &src)
103 : Includable_lexer ()
105 keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
106 chordmodifier_tab_ = src.chordmodifier_tab_;
107 pitchname_tab_stack_ = src.pitchname_tab_stack_;
108 sources_ = src.sources_;
109 start_module_ = SCM_EOL;
111 error_level_ = src.error_level_;
112 is_main_input_ = src.is_main_input_;
118 SCM scopes = SCM_EOL;
120 for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
122 SCM newmod = ly_make_anonymous_module (false);
123 ly_module_copy (newmod, scm_car (s));
124 *tail = scm_cons (newmod, SCM_EOL);
125 tail = SCM_CDRLOC (*tail);
129 push_note_state (scm_c_make_hash_table (0));
132 Lily_lexer::~Lily_lexer ()
138 Lily_lexer::add_scope (SCM module)
140 ly_reexport_module (scm_current_module ());
141 if (!scm_is_pair (scopes_))
142 start_module_ = scm_current_module ();
144 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
145 ly_use_module (module, scm_car (s));
146 scopes_ = scm_cons (module, scopes_);
148 set_current_scope ();
152 Lily_lexer::remove_scope ()
154 SCM sc = scm_car (scopes_);
155 scopes_ = scm_cdr (scopes_);
156 set_current_scope ();
161 Lily_lexer::set_current_scope ()
163 SCM old = scm_current_module ();
165 if (scm_is_pair (scopes_))
166 scm_set_current_module (scm_car (scopes_));
168 scm_set_current_module (start_module_);
174 Lily_lexer::lookup_keyword (String s)
176 return keytable_->lookup (s.to_str0 ());
180 Lily_lexer::lookup_identifier_symbol (SCM sym)
182 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
184 SCM var = ly_module_lookup (scm_car (s), sym);
185 if (var != SCM_BOOL_F)
186 return scm_variable_ref (var);
189 return SCM_UNDEFINED;
193 Lily_lexer::lookup_identifier (String name)
195 return lookup_identifier_symbol (ly_symbol2scm (name.to_str0 ()));
199 Lily_lexer::start_main_input ()
201 // yy_flex_debug = 1;
202 new_input (main_input_name_, sources_);
204 /* Do not allow \include in --safe-mode */
205 allow_includes_b_ = allow_includes_b_ && !be_safe_global;
207 scm_module_define (scm_car (scopes_),
208 ly_symbol2scm ("input-file-name"),
209 scm_makfrom0str (main_input_name_.to_str0 ()));
213 Lily_lexer::set_identifier (SCM name, SCM s)
216 if (scm_is_string (name))
217 sym = scm_string_to_symbol (name);
219 if (scm_is_symbol (sym))
221 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
223 String symstr = ly_symbol2string (sym);
224 warning (_f ("identifier name is a keyword: `%s'", symstr.to_str0 ()));
227 SCM mod = scm_car (scopes_);
229 scm_module_define (mod, sym, s);
232 programming_error ("identifier is not a symbol");
236 Lily_lexer::LexerError (char const *s)
238 if (include_stack_.is_empty ())
239 message (_f ("error at EOF: %s", s) + "\n");
243 Input spot (*lexloc);
249 Lily_lexer::escaped_char (char c) const
266 Lily_lexer::here_input () const
268 return Input (*lexloc);
272 Lily_lexer::prepare_for_next_token ()
274 last_input_ = here_input ();
278 Since we don't create the buffer state from the bytes directly, we
279 don't know about the location of the lexer. Add this as a
282 Lily_lexer::add_lexed_char (int count)
284 char const *start = here_str0 ();
285 lexloc->set (get_source_file (),
286 start, start + count);
287 char_count_stack_.top () += count;
290 #include "ly-smobs.icc"
292 IMPLEMENT_SMOBS (Lily_lexer);
293 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
294 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
297 Lily_lexer::mark_smob (SCM s)
299 Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
301 scm_gc_mark (lexer->chordmodifier_tab_);
302 scm_gc_mark (lexer->pitchname_tab_stack_);
303 scm_gc_mark (lexer->start_module_);
304 return lexer->scopes_;
308 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
310 Lily_lexer *lexer = Lily_lexer::unsmob (s);
312 scm_puts ("#<Lily_lexer ", port);
313 scm_display (lexer->scopes_, port);
314 scm_puts (" >", port);