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>
9 #include "lily-lexer.hh"
14 #include "scm-hash.hh"
15 #include "interval.hh"
19 #include "source-file.hh"
23 static Keyword_ent the_key_tab[] = {
25 {"addquote", ADDQUOTE},
26 {"addlyrics", ADDLYRICS},
28 {"alternative", ALTERNATIVE},
33 {"chordmode", CHORDMODE},
35 {"consists", CONSISTS},
39 {"drummode", DRUMMODE},
41 {"description", DESCRIPTION},
43 {"figuremode",FIGUREMODE},
44 {"grobdescriptions", GROBDESCRIPTIONS},
48 {"lyricmode", LYRICMODE},
49 {"lyricsto", LYRICSTO},
56 {"objectid", OBJECTID},
57 {"notemode", NOTEMODE},
60 {"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);
89 chordmodifier_tab_ = SCM_EOL;
90 pitchname_tab_stack_ = SCM_EOL;
94 main_input_b_ = false;
98 add_scope (ly_make_anonymous_module (false));
99 push_note_state (scm_c_make_hash_table (0));
100 chordmodifier_tab_ = scm_make_vector (scm_int2num (1), SCM_EOL);
103 Lily_lexer::Lily_lexer (Lily_lexer const &src)
104 : Includable_lexer ()
106 keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
107 chordmodifier_tab_ = src.chordmodifier_tab_;
108 pitchname_tab_stack_ = src.pitchname_tab_stack_;
109 sources_ = src.sources_;
111 error_level_ = src.error_level_;
112 main_input_b_ = src.main_input_b_;
118 SCM scopes = SCM_EOL;
120 for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
122 SCM newmod = ly_make_anonymous_module (false);
123 ly_module_copy (newmod, scm_car (s));
124 *tail = scm_cons (newmod, SCM_EOL);
125 tail = SCM_CDRLOC (*tail);
129 push_note_state (scm_c_make_hash_table (0));
132 Lily_lexer::~Lily_lexer ()
140 Lily_lexer::add_scope (SCM module)
142 ly_reexport_module (scm_current_module ());
143 scm_set_current_module (module);
144 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
146 ly_use_module (module, scm_car (s));
148 scopes_ = scm_cons (module, scopes_);
152 Lily_lexer::remove_scope ()
154 SCM sc = scm_car (scopes_);
155 scopes_ = scm_cdr (scopes_);
156 scm_set_current_module (scm_car (scopes_));
163 Lily_lexer::lookup_keyword (String s)
165 return keytable_->lookup (s.to_str0 ());
169 Lily_lexer::lookup_identifier_symbol (SCM sym)
171 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
173 SCM var = ly_module_lookup (scm_car (s), sym);
174 if (var != SCM_BOOL_F)
175 return scm_variable_ref (var);
178 return SCM_UNDEFINED;
182 Lily_lexer::lookup_identifier (String name)
184 return lookup_identifier_symbol (ly_symbol2scm (name.to_str0 ()));
188 Lily_lexer::start_main_input ()
190 // yy_flex_debug = 1;
191 new_input (main_input_name_, sources_);
193 /* Do not allow \include in --safe-mode */
194 allow_includes_b_ = allow_includes_b_ && !be_safe_global;
196 scm_module_define (scm_car (scopes_),
197 ly_symbol2scm ("input-file-name"),
198 scm_makfrom0str (main_input_name_.to_str0 ()));
202 Lily_lexer::set_identifier (SCM name, SCM s)
205 if (scm_is_string (name))
206 sym = scm_string_to_symbol (name);
208 if (scm_is_symbol (sym))
210 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
212 String symstr = ly_symbol2string (sym);
213 warning (_f ("Identifier name is a keyword: `%s'", symstr.to_str0()));
216 SCM mod = scm_car (scopes_);
218 scm_module_define (mod, sym, s);
222 programming_error ("Identifier is not a symbol.");
227 Lily_lexer::LexerError (char const *s)
229 if (include_stack_.is_empty ())
230 progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
234 Input spot (get_source_file (), here_str0 ());
240 Lily_lexer::escaped_char (char c) const
257 Lily_lexer::here_input () const
259 Source_file * f = get_source_file ();
260 return Input (f, (char*)here_str0 ());
264 Lily_lexer::prepare_for_next_token ()
266 last_input_ = here_input ();
269 #include "ly-smobs.icc"
271 IMPLEMENT_SMOBS (Lily_lexer);
272 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
273 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
276 Lily_lexer::mark_smob (SCM s)
278 Lily_lexer *lexer = (Lily_lexer*) SCM_CELL_WORD_1 (s);
280 scm_gc_mark (lexer->chordmodifier_tab_);
281 scm_gc_mark (lexer->pitchname_tab_stack_);
282 return lexer->scopes_;
286 Lily_lexer::print_smob (SCM, SCM port, scm_print_state*)
288 scm_puts ("#<Lily_lexer ", port);
289 scm_puts (" >", port);