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"
25 static Keyword_ent the_key_tab[]
28 {"addlyrics", ADDLYRICS},
29 {"addquote", ADDQUOTE},
31 {"alternative", ALTERNATIVE},
34 {"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},
78 {"transpose", TRANSPOSE},
79 {"transposition", TRANSPOSITION},
86 Lily_lexer::Lily_lexer (Sources *sources)
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)
104 : Includable_lexer ()
106 keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
107 chordmodifier_tab_ = src.chordmodifier_tab_;
108 pitchname_tab_stack_ = src.pitchname_tab_stack_;
109 sources_ = src.sources_;
110 start_module_ = SCM_EOL;
112 error_level_ = src.error_level_;
113 is_main_input_ = src.is_main_input_;
119 SCM scopes = SCM_EOL;
121 for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
123 SCM newmod = ly_make_anonymous_module (false);
124 ly_module_copy (newmod, scm_car (s));
125 *tail = scm_cons (newmod, SCM_EOL);
126 tail = SCM_CDRLOC (*tail);
130 push_note_state (scm_c_make_hash_table (0));
133 Lily_lexer::~Lily_lexer ()
139 Lily_lexer::add_scope (SCM module)
141 ly_reexport_module (scm_current_module ());
142 if (!scm_is_pair (scopes_))
143 start_module_ = scm_current_module ();
145 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
146 ly_use_module (module, scm_car (s));
147 scopes_ = scm_cons (module, scopes_);
149 set_current_scope ();
153 Lily_lexer::remove_scope ()
155 SCM sc = scm_car (scopes_);
156 scopes_ = scm_cdr (scopes_);
157 set_current_scope ();
162 Lily_lexer::set_current_scope ()
164 SCM old = scm_current_module ();
166 if (scm_is_pair (scopes_))
167 scm_set_current_module (scm_car (scopes_));
169 scm_set_current_module (start_module_);
175 Lily_lexer::lookup_keyword (std::string s)
177 return keytable_->lookup (s.c_str ());
181 Lily_lexer::lookup_identifier_symbol (SCM sym)
183 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
185 SCM var = ly_module_lookup (scm_car (s), sym);
186 if (var != SCM_BOOL_F)
187 return scm_variable_ref (var);
190 return SCM_UNDEFINED;
194 Lily_lexer::lookup_identifier (std::string name)
196 return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
200 Lily_lexer::start_main_input ()
202 // yy_flex_debug = 1;
203 new_input (main_input_name_, sources_);
205 /* Do not allow \include in --safe-mode */
206 allow_includes_b_ = allow_includes_b_ && !be_safe_global;
208 scm_module_define (scm_car (scopes_),
209 ly_symbol2scm ("input-file-name"),
210 scm_makfrom0str (main_input_name_.c_str ()));
214 Lily_lexer::set_identifier (SCM name, SCM s)
217 if (scm_is_string (name))
218 sym = scm_string_to_symbol (name);
220 if (scm_is_symbol (sym))
222 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
224 std::string symstr = ly_symbol2string (sym);
225 warning (_f ("identifier name is a keyword: `%s'", symstr.c_str ()));
228 SCM mod = scm_car (scopes_);
230 scm_module_define (mod, sym, s);
233 programming_error ("identifier is not a symbol");
237 Lily_lexer::LexerError (char const *s)
239 if (include_stack_.empty ())
240 message (_f ("error at EOF: %s", s) + "\n");
244 Input spot (*lexloc);
250 Lily_lexer::escaped_char (char c) const
267 Lily_lexer::here_input () const
269 return Input (*lexloc);
273 Lily_lexer::prepare_for_next_token ()
275 last_input_ = here_input ();
279 Since we don't create the buffer state from the bytes directly, we
280 don't know about the location of the lexer. Add this as a
283 Lily_lexer::add_lexed_char (int count)
285 char const *start = here_str0 ();
286 lexloc->set (get_source_file (),
287 start, start + count);
288 char_count_stack_.back () += count;
291 #include "ly-smobs.icc"
293 IMPLEMENT_SMOBS (Lily_lexer);
294 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
295 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
298 Lily_lexer::mark_smob (SCM s)
300 Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
302 scm_gc_mark (lexer->chordmodifier_tab_);
303 scm_gc_mark (lexer->pitchname_tab_stack_);
304 scm_gc_mark (lexer->start_module_);
305 return lexer->scopes_;
309 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
311 Lily_lexer *lexer = Lily_lexer::unsmob (s);
313 scm_puts ("#<Lily_lexer ", port);
314 scm_display (lexer->scopes_, port);
315 scm_puts (" >", port);