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},
34 {"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},
59 {"notemode", NOTEMODE},
60 {"objectid", OBJECTID},
63 {"override", OVERRIDE},
66 {"relative", RELATIVE},
72 {"sequential", SEQUENTIAL},
74 {"simultaneous", SIMULTANEOUS},
80 {"transpose", TRANSPOSE},
81 {"transposition", TRANSPOSITION},
88 Lily_lexer::Lily_lexer (Sources *sources)
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)
106 : 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 ();
155 Lily_lexer::remove_scope ()
157 SCM sc = scm_car (scopes_);
158 scopes_ = scm_cdr (scopes_);
159 set_current_scope ();
164 Lily_lexer::set_current_scope ()
166 SCM old = scm_current_module ();
168 if (scm_is_pair (scopes_))
169 scm_set_current_module (scm_car (scopes_));
171 scm_set_current_module (start_module_);
177 Lily_lexer::lookup_keyword (String s)
179 return keytable_->lookup (s.to_str0 ());
183 Lily_lexer::lookup_identifier_symbol (SCM sym)
185 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
187 SCM var = ly_module_lookup (scm_car (s), sym);
188 if (var != SCM_BOOL_F)
189 return scm_variable_ref (var);
192 return SCM_UNDEFINED;
196 Lily_lexer::lookup_identifier (String name)
198 return lookup_identifier_symbol (ly_symbol2scm (name.to_str0 ()));
202 Lily_lexer::start_main_input ()
204 // yy_flex_debug = 1;
205 new_input (main_input_name_, sources_);
207 /* Do not allow \include in --safe-mode */
208 allow_includes_b_ = allow_includes_b_ && !be_safe_global;
210 scm_module_define (scm_car (scopes_),
211 ly_symbol2scm ("input-file-name"),
212 scm_makfrom0str (main_input_name_.to_str0 ()));
216 Lily_lexer::set_identifier (SCM name, SCM s)
219 if (scm_is_string (name))
220 sym = scm_string_to_symbol (name);
222 if (scm_is_symbol (sym))
224 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
226 String symstr = ly_symbol2string (sym);
227 warning (_f ("identifier name is a keyword: `%s'", symstr.to_str0 ()));
230 SCM mod = scm_car (scopes_);
232 scm_module_define (mod, sym, s);
235 programming_error ("identifier is not a symbol");
239 Lily_lexer::LexerError (char const *s)
241 if (include_stack_.is_empty ())
242 message (_f ("error at EOF: %s", s) + "\n");
246 Input spot (*lexloc);
252 Lily_lexer::escaped_char (char c) const
269 Lily_lexer::here_input () const
271 return Input (*lexloc);
275 Lily_lexer::prepare_for_next_token ()
277 last_input_ = here_input ();
281 Since we don't create the buffer state from the bytes directly, we
282 don't know about the location of the lexer. Add this as a
285 Lily_lexer::add_lexed_char (int count)
287 char const *start = here_str0 ();
288 lexloc->set (get_source_file (),
289 start, start + count);
290 char_count_stack_.top () += count;
293 #include "ly-smobs.icc"
295 IMPLEMENT_SMOBS (Lily_lexer);
296 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
297 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
300 Lily_lexer::mark_smob (SCM s)
302 Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
304 scm_gc_mark (lexer->chordmodifier_tab_);
305 scm_gc_mark (lexer->pitchname_tab_stack_);
306 scm_gc_mark (lexer->start_module_);
307 return lexer->scopes_;
311 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
313 Lily_lexer *lexer = Lily_lexer::unsmob (s);
315 scm_puts ("#<Lily_lexer ", port);
316 scm_display (lexer->scopes_, port);
317 scm_puts (" >", port);