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},
37 {"chordmode", CHORDMODE},
39 {"consists", CONSISTS},
43 {"drummode", DRUMMODE},
45 {"description", DESCRIPTION},
47 {"figuremode",FIGUREMODE},
48 {"grobdescriptions", GROBDESCRIPTIONS},
52 {"lyricmode", LYRICMODE},
53 {"lyricsto", LYRICSTO},
60 {"notemode", NOTEMODE},
63 {"override", OVERRIDE},
67 {"relative", RELATIVE},
73 {"sequential", SEQUENTIAL},
75 {"simultaneous", SIMULTANEOUS},
81 {"transpose", TRANSPOSE},
82 {"transposition", TRANSPOSITION},
90 Lily_lexer::Lily_lexer (Sources *sources)
92 keytable_ = new Keyword_table (the_key_tab);
94 chordmodifier_tab_ = SCM_EOL;
95 pitchname_tab_stack_ = SCM_EOL;
99 main_input_b_ = false;
103 add_scope (ly_make_anonymous_module (false));
104 push_note_state (scm_c_make_hash_table (0));
105 chordmodifier_tab_ = scm_make_vector (scm_int2num (1), SCM_EOL);
108 Lily_lexer::Lily_lexer (Lily_lexer const &src)
109 : Includable_lexer ()
111 keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
112 encoding_ = src.encoding_;
113 chordmodifier_tab_ = src.chordmodifier_tab_;
114 pitchname_tab_stack_ = src.pitchname_tab_stack_;
115 sources_ = src.sources_;
117 error_level_ = src.error_level_;
118 main_input_b_ = src.main_input_b_;
124 SCM scopes = SCM_EOL;
126 for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
128 SCM newmod = ly_make_anonymous_module (false);
129 ly_import_module (newmod, scm_car (s));
130 *tail = scm_cons (newmod, SCM_EOL);
131 tail = SCM_CDRLOC (*tail);
135 push_note_state (scm_c_make_hash_table (0));
138 Lily_lexer::~Lily_lexer ()
144 Lily_lexer::encoding () const
151 Lily_lexer::add_scope (SCM module)
153 ly_reexport_module (scm_current_module ());
154 scm_set_current_module (module);
155 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
157 ly_use_module (module, scm_car (s));
159 scopes_ = scm_cons (module, scopes_);
163 Lily_lexer::remove_scope ()
165 SCM sc = scm_car (scopes_);
166 scopes_ = scm_cdr (scopes_);
167 scm_set_current_module (scm_car (scopes_));
174 Lily_lexer::lookup_keyword (String s)
176 return keytable_->lookup (s.to_str0 ());
180 Lily_lexer::lookup_identifier_symbol (SCM sym)
182 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
184 SCM var = ly_module_lookup (scm_car (s), sym);
185 if (var != SCM_BOOL_F)
186 return scm_variable_ref (var);
189 return SCM_UNDEFINED;
193 Lily_lexer::lookup_identifier (String name)
195 return lookup_identifier_symbol (ly_symbol2scm (name.to_str0 ()));
199 Lily_lexer::start_main_input ()
201 // yy_flex_debug = 1;
202 new_input (main_input_name_, sources_);
204 /* Do not allow \include in --safe-mode */
205 allow_includes_b_ = allow_includes_b_ && !safe_global_b;
207 scm_module_define (scm_car (scopes_),
208 ly_symbol2scm ("input-file-name"),
209 scm_makfrom0str (main_input_name_.to_str0 ()));
213 Lily_lexer::set_identifier (SCM name, SCM s)
216 if (scm_is_string (name))
217 sym = scm_string_to_symbol (name);
219 if (scm_is_symbol (sym))
221 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
223 String symstr = ly_symbol2string (sym);
224 warning (_f ("Identifier name is a keyword: `%s'", symstr.to_str0()));
227 SCM mod = scm_car (scopes_);
229 scm_module_define (mod, sym, s);
233 programming_error ("Identifier is not a symbol.");
238 Lily_lexer::LexerError (char const *s)
240 if (include_stack_.is_empty ())
241 progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
245 Input spot (get_source_file (), here_str0 ());
251 Lily_lexer::escaped_char (char c) const
268 Lily_lexer::here_input () const
270 Source_file * f= get_source_file ();
271 return Input (f, (char*)here_str0 ());
275 Lily_lexer::prepare_for_next_token ()
277 last_input_ = here_input ();
281 Lily_lexer::set_encoding (String s)
284 encoding_ = ly_symbol2scm (s.to_str0 ());
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->scopes_);
303 return lexer->encoding_;
307 Lily_lexer::print_smob (SCM, SCM port, scm_print_state*)
309 scm_puts ("#<Lily_lexer ", port);
310 scm_puts (" >", port);