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},
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},
60 {"override", OVERRIDE},
63 {"relative", RELATIVE},
69 {"sequential", SEQUENTIAL},
71 {"simultaneous", SIMULTANEOUS},
76 {"transpose", TRANSPOSE},
83 Lily_lexer::Lily_lexer (Sources *sources)
85 keytable_ = new Keyword_table (the_key_tab);
86 chordmodifier_tab_ = SCM_EOL;
87 pitchname_tab_stack_ = SCM_EOL;
91 is_main_input_ = false;
92 start_module_ = SCM_EOL;
95 add_scope (ly_make_anonymous_module (false));
96 push_note_state (scm_c_make_hash_table (0));
97 chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
100 Lily_lexer::Lily_lexer (Lily_lexer const &src)
101 : Includable_lexer ()
103 keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
104 chordmodifier_tab_ = src.chordmodifier_tab_;
105 pitchname_tab_stack_ = src.pitchname_tab_stack_;
106 sources_ = src.sources_;
107 start_module_ = SCM_EOL;
109 error_level_ = src.error_level_;
110 is_main_input_ = src.is_main_input_;
116 SCM scopes = SCM_EOL;
118 for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
120 SCM newmod = ly_make_anonymous_module (false);
121 ly_module_copy (newmod, scm_car (s));
122 *tail = scm_cons (newmod, SCM_EOL);
123 tail = SCM_CDRLOC (*tail);
127 push_note_state (scm_c_make_hash_table (0));
130 Lily_lexer::~Lily_lexer ()
136 Lily_lexer::add_scope (SCM module)
138 ly_reexport_module (scm_current_module ());
139 if (!scm_is_pair (scopes_))
140 start_module_ = scm_current_module ();
142 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
143 ly_use_module (module, scm_car (s));
144 scopes_ = scm_cons (module, scopes_);
146 set_current_scope ();
150 Lily_lexer::remove_scope ()
152 SCM sc = scm_car (scopes_);
153 scopes_ = scm_cdr (scopes_);
154 set_current_scope ();
159 Lily_lexer::set_current_scope ()
161 SCM old = scm_current_module ();
163 if (scm_is_pair (scopes_))
164 scm_set_current_module (scm_car (scopes_));
166 scm_set_current_module (start_module_);
172 Lily_lexer::lookup_keyword (string s)
174 return keytable_->lookup (s.c_str ());
178 Lily_lexer::lookup_identifier_symbol (SCM sym)
180 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
182 SCM var = ly_module_lookup (scm_car (s), sym);
183 if (var != SCM_BOOL_F)
184 return scm_variable_ref (var);
187 return SCM_UNDEFINED;
191 Lily_lexer::lookup_identifier (string name)
193 return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
197 Lily_lexer::start_main_input ()
199 // yy_flex_debug = 1;
200 new_input (main_input_name_, sources_);
202 /* Do not allow \include in --safe-mode */
203 allow_includes_b_ = allow_includes_b_ && !be_safe_global;
205 scm_module_define (scm_car (scopes_),
206 ly_symbol2scm ("input-file-name"),
207 scm_makfrom0str (main_input_name_.c_str ()));
211 Lily_lexer::set_identifier (SCM name, SCM s)
214 if (scm_is_string (name))
215 sym = scm_string_to_symbol (name);
217 if (scm_is_symbol (sym))
219 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
221 string symstr = ly_symbol2string (sym);
222 warning (_f ("identifier name is a keyword: `%s'", symstr.c_str ()));
225 SCM mod = scm_car (scopes_);
227 scm_module_define (mod, sym, s);
230 programming_error ("identifier is not a symbol");
234 Lily_lexer::LexerError (char const *s)
236 if (include_stack_.empty ())
237 message (_f ("error at EOF: %s", s) + "\n");
241 Input spot (*lexloc);
247 Lily_lexer::escaped_char (char c) const
264 Lily_lexer::here_input () const
266 return Input (*lexloc);
270 Lily_lexer::prepare_for_next_token ()
272 last_input_ = here_input ();
276 Since we don't create the buffer state from the bytes directly, we
277 don't know about the location of the lexer. Add this as a
280 Lily_lexer::add_lexed_char (int count)
282 char const *start = here_str0 ();
283 lexloc->set (get_source_file (),
284 start, start + count);
285 char_count_stack_.back () += count;
288 #include "ly-smobs.icc"
290 IMPLEMENT_SMOBS (Lily_lexer);
291 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
292 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
295 Lily_lexer::mark_smob (SCM s)
297 Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
299 scm_gc_mark (lexer->chordmodifier_tab_);
300 scm_gc_mark (lexer->pitchname_tab_stack_);
301 scm_gc_mark (lexer->start_module_);
302 return lexer->scopes_;
306 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
308 Lily_lexer *lexer = Lily_lexer::unsmob (s);
310 scm_puts ("#<Lily_lexer ", port);
311 scm_display (lexer->scopes_, port);
312 scm_puts (" >", port);