2 lily-lexer.cc -- implement Lily_lexer
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
12 #include "lily-proto.hh"
13 #include "scm-hash.hh"
14 #include "interval.hh"
15 #include "lily-guile.hh"
18 #include "lily-lexer.hh"
20 #include "source-file.hh"
24 #include "ly-module.hh"
27 static Keyword_ent the_key_tab[] = {
29 {"addquote", ADDQUOTE},
30 {"addlyrics", ADDLYRICS},
32 {"alternative", ALTERNATIVE},
35 {"bookpaper", BOOKPAPER},
38 {"chordmode", CHORDMODE},
40 {"consists", CONSISTS},
44 {"drummode", DRUMMODE},
46 {"description", DESCRIPTION},
48 {"figuremode",FIGUREMODE},
49 {"grobdescriptions", GROBDESCRIPTIONS},
52 {"lyricmode", LYRICMODE},
53 {"lyricsto", LYRICSTO},
59 {"notemode", NOTEMODE},
62 {"override", OVERRIDE},
66 {"relative", RELATIVE},
72 {"sequential", SEQUENTIAL},
74 {"simultaneous", SIMULTANEOUS},
80 {"transpose", TRANSPOSE},
81 {"transposition", TRANSPOSITION},
89 Lily_lexer::Lily_lexer (Sources *sources)
91 keytable_ = new Keyword_table (the_key_tab);
93 chordmodifier_tab_ = SCM_EOL;
94 pitchname_tab_stack_ = SCM_EOL;
98 main_input_b_ = false;
102 add_scope (ly_make_anonymous_module (false));
103 push_note_state (scm_c_make_hash_table (0));
104 chordmodifier_tab_ = scm_make_vector (scm_int2num (1), SCM_EOL);
107 Lily_lexer::Lily_lexer (Lily_lexer const &src)
108 : Includable_lexer ()
110 keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
111 encoding_ = src.encoding_;
112 chordmodifier_tab_ = src.chordmodifier_tab_;
113 pitchname_tab_stack_ = src.pitchname_tab_stack_;
114 sources_ = src.sources_;
116 error_level_ = src.error_level_;
117 main_input_b_ = src.main_input_b_;
123 SCM scopes = SCM_EOL;
125 for (SCM s = src.scopes_; ly_c_pair_p (s); s = ly_cdr (s))
127 SCM newmod = ly_make_anonymous_module (false);
128 ly_import_module (newmod, ly_car (s));
129 *tail = scm_cons (newmod, SCM_EOL);
130 tail = SCM_CDRLOC (*tail);
134 push_note_state (scm_c_make_hash_table (0));
137 Lily_lexer::~Lily_lexer ()
143 Lily_lexer::encoding () const
150 Lily_lexer::add_scope (SCM module)
152 ly_reexport_module (scm_current_module ());
153 scm_set_current_module (module);
154 for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
156 ly_use_module (module, ly_car (s));
158 scopes_ = scm_cons (module, scopes_);
162 Lily_lexer::remove_scope ()
164 SCM sc = ly_car (scopes_);
165 scopes_ = ly_cdr (scopes_);
166 scm_set_current_module (ly_car (scopes_));
173 Lily_lexer::lookup_keyword (String s)
175 return keytable_->lookup (s.to_str0 ());
179 Lily_lexer::lookup_identifier_symbol (SCM sym)
181 for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
183 SCM var = ly_module_lookup (ly_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.to_str0 ()));
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_ && !safe_global_b;
206 scm_module_define (ly_car (scopes_),
207 ly_symbol2scm ("input-file-name"),
208 scm_makfrom0str (main_input_name_.to_str0 ()));
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.to_str0()));
226 SCM mod = ly_car (scopes_);
228 scm_module_define (mod, sym, s);
232 programming_error ("Identifier is not a symbol.");
237 Lily_lexer::LexerError (char const *s)
239 if (include_stack_.is_empty ())
240 progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
244 Input spot (get_source_file (), here_str0 ());
250 Lily_lexer::escaped_char (char c) const
267 Lily_lexer::here_input () const
269 Source_file * f= get_source_file ();
270 return Input (f, (char*)here_str0 ());
274 Lily_lexer::prepare_for_next_token ()
276 last_input_ = here_input ();
280 Lily_lexer::set_encoding (String s)
283 encoding_ = ly_symbol2scm (s.to_str0 ());
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*) ly_cdr (s);
299 scm_gc_mark (lexer->chordmodifier_tab_);
300 scm_gc_mark (lexer->pitchname_tab_stack_);
301 scm_gc_mark (lexer->scopes_);
302 return lexer->encoding_;
306 Lily_lexer::print_smob (SCM, SCM port, scm_print_state*)
308 scm_puts ("#<Lily_lexer ", port);
309 scm_puts (" >", port);