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},
31 {"alternative", ALTERNATIVE},
34 {"bookpaper", BOOKPAPER},
38 {"consists", CONSISTS},
43 {"description", DESCRIPTION},
45 {"grobdescriptions", GROBDESCRIPTIONS},
49 {"lyricsto", LYRICSTO},
55 {"newlyrics", NEWLYRICS},
59 {"override", OVERRIDE},
63 {"relative", RELATIVE},
69 {"sequential", SEQUENTIAL},
71 {"simultaneous", SIMULTANEOUS},
77 {"transpose", TRANSPOSE},
78 {"transposition", TRANSPOSITION},
86 Lily_lexer::Lily_lexer (Sources *sources)
88 keytable_ = new Keyword_table (the_key_tab);
90 chordmodifier_tab_ = SCM_EOL;
91 pitchname_tab_stack_ = SCM_EOL;
95 main_input_b_ = false;
99 add_scope (ly_make_anonymous_module (false));
100 push_note_state (scm_c_make_hash_table (0));
101 chordmodifier_tab_ = scm_make_vector (scm_int2num (1), SCM_EOL);
104 Lily_lexer::Lily_lexer (Lily_lexer const &src)
105 : Includable_lexer ()
107 keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
108 encoding_ = src.encoding_;
109 chordmodifier_tab_ = src.chordmodifier_tab_;
110 pitchname_tab_stack_ = src.pitchname_tab_stack_;
111 sources_ = src.sources_;
113 error_level_ = src.error_level_;
114 main_input_b_ = src.main_input_b_;
120 SCM scopes = SCM_EOL;
122 for (SCM s = src.scopes_; ly_c_pair_p (s); s = ly_cdr (s))
124 SCM newmod = ly_make_anonymous_module (false);
125 ly_import_module (newmod, ly_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::encoding () const
147 Lily_lexer::add_scope (SCM module)
149 ly_reexport_module (scm_current_module ());
150 scm_set_current_module (module);
151 for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
153 ly_use_module (module, ly_car (s));
155 scopes_ = scm_cons (module, scopes_);
159 Lily_lexer::remove_scope ()
161 SCM sc = ly_car (scopes_);
162 scopes_ = ly_cdr (scopes_);
163 scm_set_current_module (ly_car (scopes_));
170 Lily_lexer::lookup_keyword (String s)
172 return keytable_->lookup (s.to_str0 ());
176 Lily_lexer::lookup_identifier_symbol (SCM sym)
178 for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
180 SCM var = ly_module_lookup (ly_car (s), sym);
181 if (var != SCM_BOOL_F)
182 return scm_variable_ref (var);
185 return SCM_UNDEFINED;
189 Lily_lexer::lookup_identifier (String name)
191 return lookup_identifier_symbol (ly_symbol2scm (name.to_str0 ()));
195 Lily_lexer::start_main_input ()
197 // yy_flex_debug = 1;
198 new_input (main_input_name_, sources_);
200 /* Do not allow \include in --safe-mode */
201 allow_includes_b_ = allow_includes_b_ && !safe_global_b;
203 scm_module_define (ly_car (scopes_),
204 ly_symbol2scm ("input-file-name"),
205 scm_makfrom0str (main_input_name_.to_str0 ()));
209 Lily_lexer::set_identifier (SCM name, SCM s)
212 if (ly_c_string_p (name))
213 sym = scm_string_to_symbol (name);
215 if (ly_c_symbol_p (sym))
217 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
219 warning (_f ("Identifier name is a keyword: `%s'", SCM_SYMBOL_CHARS (sym)));
222 SCM mod = ly_car (scopes_);
224 scm_module_define (mod, sym, s);
228 programming_error ("Identifier is not a symbol.");
233 Lily_lexer::LexerError (char const *s)
235 if (include_stack_.is_empty ())
236 progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
240 Input spot (get_source_file (), here_str0 ());
246 Lily_lexer::escaped_char (char c) const
263 Lily_lexer::here_input () const
265 Source_file * f= get_source_file ();
266 return Input (f, (char*)here_str0 ());
270 Lily_lexer::prepare_for_next_token ()
272 last_input_ = here_input ();
276 Lily_lexer::set_encoding (String s)
279 encoding_ = ly_symbol2scm (s.to_str0 ());
285 #include "ly-smobs.icc"
287 IMPLEMENT_SMOBS (Lily_lexer);
288 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
289 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
292 Lily_lexer::mark_smob (SCM s)
294 Lily_lexer *lexer = (Lily_lexer*) ly_cdr (s);
296 scm_gc_mark (lexer->chordmodifier_tab_);
297 scm_gc_mark (lexer->pitchname_tab_stack_);
298 scm_gc_mark (lexer->scopes_);
299 return lexer->encoding_;
303 Lily_lexer::print_smob (SCM, SCM port, scm_print_state*)
305 scm_puts ("#<Lily_lexer ", port);
306 scm_puts (" >", port);