2 lily-lexer.cc -- implement Lily_lexer
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 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},
59 {"notemode", NOTEMODE},
60 {"objectid", OBJECTID},
62 {"override", OVERRIDE},
65 {"relative", RELATIVE},
71 {"sequential", SEQUENTIAL},
73 {"simultaneous", SIMULTANEOUS},
78 {"transpose", TRANSPOSE},
85 Lily_lexer::Lily_lexer (Sources *sources, Lily_parser *parser)
88 keytable_ = new Keyword_table (the_key_tab);
89 chordmodifier_tab_ = SCM_EOL;
90 pitchname_tab_stack_ = SCM_EOL;
94 is_main_input_ = false;
95 start_module_ = SCM_EOL;
98 add_scope (ly_make_anonymous_module (false));
99 push_note_state (scm_c_make_hash_table (0));
100 chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
103 Lily_lexer::Lily_lexer (Lily_lexer const &src, Lily_parser *parser)
104 : 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))
147 ly_use_module (module, scm_car (s));
148 scopes_ = scm_cons (module, scopes_);
150 set_current_scope ();
153 Lily_lexer::has_scope () const
155 return scm_is_pair (scopes_);
159 Lily_lexer::remove_scope ()
161 SCM sc = scm_car (scopes_);
162 scopes_ = scm_cdr (scopes_);
163 set_current_scope ();
168 Lily_lexer::set_current_scope ()
170 SCM old = scm_current_module ();
172 if (scm_is_pair (scopes_))
173 scm_set_current_module (scm_car (scopes_));
175 scm_set_current_module (start_module_);
181 Lily_lexer::lookup_keyword (string s)
183 return keytable_->lookup (s.c_str ());
187 Lily_lexer::lookup_identifier_symbol (SCM sym)
189 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
191 SCM var = ly_module_lookup (scm_car (s), sym);
192 if (var != SCM_BOOL_F)
193 return scm_variable_ref (var);
196 return SCM_UNDEFINED;
200 Lily_lexer::lookup_identifier (string name)
202 return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
206 Lily_lexer::start_main_input ()
208 yy_flex_debug = get_program_option ("debug-lexer");
209 parser_->set_yydebug (get_program_option ("debug-parser"));
212 new_input (main_input_name_, sources_);
214 /* Do not allow \include in --safe-mode */
215 allow_includes_b_ = allow_includes_b_ && !be_safe_global;
217 scm_module_define (scm_car (scopes_),
218 ly_symbol2scm ("input-file-name"),
219 scm_makfrom0str (main_input_name_.c_str ()));
223 Lily_lexer::set_identifier (SCM name, SCM s)
226 if (scm_is_string (name))
227 sym = scm_string_to_symbol (name);
229 if (scm_is_symbol (sym))
231 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
233 string symstr = ly_symbol2string (sym);
234 warning (_f ("identifier name is a keyword: `%s'", symstr.c_str ()));
237 SCM mod = scm_car (scopes_);
239 scm_module_define (mod, sym, s);
242 programming_error ("identifier is not a symbol");
246 Lily_lexer::LexerError (char const *s)
248 if (include_stack_.empty ())
249 message (_f ("error at EOF: %s", s) + "\n");
253 Input spot (*lexloc);
259 Lily_lexer::escaped_char (char c) const
276 Lily_lexer::here_input () const
278 return Input (*lexloc);
282 Lily_lexer::prepare_for_next_token ()
284 last_input_ = here_input ();
288 Since we don't create the buffer state from the bytes directly, we
289 don't know about the location of the lexer. Add this as a
292 Lily_lexer::add_lexed_char (int count)
294 char const *start = here_str0 ();
295 lexloc->set (get_source_file (),
296 start, start + count);
297 char_count_stack_.back () += count;
300 #include "ly-smobs.icc"
302 IMPLEMENT_SMOBS (Lily_lexer);
303 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
304 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
307 Lily_lexer::mark_smob (SCM s)
309 ASSERT_LIVE_IS_ALLOWED();
311 Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
313 scm_gc_mark (lexer->chordmodifier_tab_);
315 scm_gc_mark (lexer->parser_->self_scm ());
316 scm_gc_mark (lexer->pitchname_tab_stack_);
317 scm_gc_mark (lexer->start_module_);
318 return lexer->scopes_;
322 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
324 Lily_lexer *lexer = Lily_lexer::unsmob (s);
326 scm_puts ("#<Lily_lexer ", port);
327 scm_display (lexer->scopes_, port);
328 scm_puts (" >", port);