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"
22 #include "ly-module.hh"
24 static Keyword_ent the_key_tab[] = {
26 {"addquote", ADDQUOTE},
27 {"addlyrics", ADDLYRICS},
29 {"alternative", ALTERNATIVE},
34 {"chordmode", CHORDMODE},
36 {"consists", CONSISTS},
40 {"drummode", DRUMMODE},
42 {"description", DESCRIPTION},
44 {"figuremode",FIGUREMODE},
45 {"grobdescriptions", GROBDESCRIPTIONS},
49 {"lyricmode", LYRICMODE},
50 {"lyricsto", LYRICSTO},
57 {"objectid", OBJECTID},
58 {"notemode", NOTEMODE},
61 {"override", OVERRIDE},
64 {"relative", RELATIVE},
70 {"sequential", SEQUENTIAL},
72 {"simultaneous", SIMULTANEOUS},
78 {"transpose", TRANSPOSE},
79 {"transposition", TRANSPOSITION},
87 Lily_lexer::Lily_lexer (Sources *sources)
89 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 chordmodifier_tab_ = src.chordmodifier_tab_;
109 pitchname_tab_stack_ = src.pitchname_tab_stack_;
110 sources_ = src.sources_;
112 error_level_ = src.error_level_;
113 main_input_b_ = src.main_input_b_;
119 SCM scopes = SCM_EOL;
121 for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
123 SCM newmod = ly_make_anonymous_module (false);
124 ly_module_copy (newmod, scm_car (s));
125 *tail = scm_cons (newmod, SCM_EOL);
126 tail = SCM_CDRLOC (*tail);
130 push_note_state (scm_c_make_hash_table (0));
133 Lily_lexer::~Lily_lexer ()
141 Lily_lexer::add_scope (SCM module)
143 ly_reexport_module (scm_current_module ());
144 scm_set_current_module (module);
145 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
147 ly_use_module (module, scm_car (s));
149 scopes_ = scm_cons (module, scopes_);
153 Lily_lexer::remove_scope ()
155 SCM sc = scm_car (scopes_);
156 scopes_ = scm_cdr (scopes_);
157 scm_set_current_module (scm_car (scopes_));
164 Lily_lexer::lookup_keyword (String s)
166 return keytable_->lookup (s.to_str0 ());
170 Lily_lexer::lookup_identifier_symbol (SCM sym)
172 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
174 SCM var = ly_module_lookup (scm_car (s), sym);
175 if (var != SCM_BOOL_F)
176 return scm_variable_ref (var);
179 return SCM_UNDEFINED;
183 Lily_lexer::lookup_identifier (String name)
185 return lookup_identifier_symbol (ly_symbol2scm (name.to_str0 ()));
189 Lily_lexer::start_main_input ()
191 // yy_flex_debug = 1;
192 new_input (main_input_name_, sources_);
194 /* Do not allow \include in --safe-mode */
195 allow_includes_b_ = allow_includes_b_ && !safe_global_b;
197 scm_module_define (scm_car (scopes_),
198 ly_symbol2scm ("input-file-name"),
199 scm_makfrom0str (main_input_name_.to_str0 ()));
203 Lily_lexer::set_identifier (SCM name, SCM s)
206 if (scm_is_string (name))
207 sym = scm_string_to_symbol (name);
209 if (scm_is_symbol (sym))
211 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
213 String symstr = ly_symbol2string (sym);
214 warning (_f ("Identifier name is a keyword: `%s'", symstr.to_str0()));
217 SCM mod = scm_car (scopes_);
219 scm_module_define (mod, sym, s);
223 programming_error ("Identifier is not a symbol.");
228 Lily_lexer::LexerError (char const *s)
230 if (include_stack_.is_empty ())
231 progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
235 Input spot (get_source_file (), here_str0 ());
241 Lily_lexer::escaped_char (char c) const
258 Lily_lexer::here_input () const
260 Source_file * f = get_source_file ();
261 return Input (f, (char*)here_str0 ());
265 Lily_lexer::prepare_for_next_token ()
267 last_input_ = here_input ();
270 #include "ly-smobs.icc"
272 IMPLEMENT_SMOBS (Lily_lexer);
273 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
274 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
277 Lily_lexer::mark_smob (SCM s)
279 Lily_lexer *lexer = (Lily_lexer*) SCM_CELL_WORD_1 (s);
281 scm_gc_mark (lexer->chordmodifier_tab_);
282 scm_gc_mark (lexer->pitchname_tab_stack_);
283 return lexer->scopes_;
287 Lily_lexer::print_smob (SCM, SCM port, scm_print_state*)
289 scm_puts ("#<Lily_lexer ", port);
290 scm_puts (" >", port);