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},
77 {"transposition", TRANSPOSITION},
84 Lily_lexer::Lily_lexer (Sources *sources)
86 keytable_ = new Keyword_table (the_key_tab);
87 chordmodifier_tab_ = SCM_EOL;
88 pitchname_tab_stack_ = SCM_EOL;
92 is_main_input_ = false;
93 start_module_ = SCM_EOL;
96 add_scope (ly_make_anonymous_module (false));
97 push_note_state (scm_c_make_hash_table (0));
98 chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
101 Lily_lexer::Lily_lexer (Lily_lexer const &src)
102 : Includable_lexer ()
104 keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
105 chordmodifier_tab_ = src.chordmodifier_tab_;
106 pitchname_tab_stack_ = src.pitchname_tab_stack_;
107 sources_ = src.sources_;
108 start_module_ = SCM_EOL;
110 error_level_ = src.error_level_;
111 is_main_input_ = src.is_main_input_;
117 SCM scopes = SCM_EOL;
119 for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
121 SCM newmod = ly_make_anonymous_module (false);
122 ly_module_copy (newmod, scm_car (s));
123 *tail = scm_cons (newmod, SCM_EOL);
124 tail = SCM_CDRLOC (*tail);
128 push_note_state (scm_c_make_hash_table (0));
131 Lily_lexer::~Lily_lexer ()
137 Lily_lexer::add_scope (SCM module)
139 ly_reexport_module (scm_current_module ());
140 if (!scm_is_pair (scopes_))
141 start_module_ = scm_current_module ();
143 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
144 ly_use_module (module, scm_car (s));
145 scopes_ = scm_cons (module, scopes_);
147 set_current_scope ();
151 Lily_lexer::remove_scope ()
153 SCM sc = scm_car (scopes_);
154 scopes_ = scm_cdr (scopes_);
155 set_current_scope ();
160 Lily_lexer::set_current_scope ()
162 SCM old = scm_current_module ();
164 if (scm_is_pair (scopes_))
165 scm_set_current_module (scm_car (scopes_));
167 scm_set_current_module (start_module_);
173 Lily_lexer::lookup_keyword (string s)
175 return keytable_->lookup (s.c_str ());
179 Lily_lexer::lookup_identifier_symbol (SCM sym)
181 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
183 SCM var = ly_module_lookup (scm_car (s), sym);
184 if (var != SCM_BOOL_F)
185 return scm_variable_ref (var);
188 return SCM_UNDEFINED;
192 Lily_lexer::lookup_identifier (string name)
194 return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
198 Lily_lexer::start_main_input ()
200 // yy_flex_debug = 1;
201 new_input (main_input_name_, sources_);
203 /* Do not allow \include in --safe-mode */
204 allow_includes_b_ = allow_includes_b_ && !be_safe_global;
206 scm_module_define (scm_car (scopes_),
207 ly_symbol2scm ("input-file-name"),
208 scm_makfrom0str (main_input_name_.c_str ()));
212 Lily_lexer::set_identifier (SCM name, SCM s)
215 if (scm_is_string (name))
216 sym = scm_string_to_symbol (name);
218 if (scm_is_symbol (sym))
220 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
222 string symstr = ly_symbol2string (sym);
223 warning (_f ("identifier name is a keyword: `%s'", symstr.c_str ()));
226 SCM mod = scm_car (scopes_);
228 scm_module_define (mod, sym, s);
231 programming_error ("identifier is not a symbol");
235 Lily_lexer::LexerError (char const *s)
237 if (include_stack_.empty ())
238 message (_f ("error at EOF: %s", s) + "\n");
242 Input spot (*lexloc);
248 Lily_lexer::escaped_char (char c) const
265 Lily_lexer::here_input () const
267 return Input (*lexloc);
271 Lily_lexer::prepare_for_next_token ()
273 last_input_ = here_input ();
277 Since we don't create the buffer state from the bytes directly, we
278 don't know about the location of the lexer. Add this as a
281 Lily_lexer::add_lexed_char (int count)
283 char const *start = here_str0 ();
284 lexloc->set (get_source_file (),
285 start, start + count);
286 char_count_stack_.back () += count;
289 #include "ly-smobs.icc"
291 IMPLEMENT_SMOBS (Lily_lexer);
292 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
293 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
296 Lily_lexer::mark_smob (SCM s)
298 Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
300 scm_gc_mark (lexer->chordmodifier_tab_);
301 scm_gc_mark (lexer->pitchname_tab_stack_);
302 scm_gc_mark (lexer->start_module_);
303 return lexer->scopes_;
307 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
309 Lily_lexer *lexer = Lily_lexer::unsmob (s);
311 scm_puts ("#<Lily_lexer ", port);
312 scm_display (lexer->scopes_, port);
313 scm_puts (" >", port);