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);
91 chordmodifier_tab_ = SCM_EOL;
92 pitchname_tab_stack_ = SCM_EOL;
96 main_input_b_ = false;
100 add_scope (ly_make_anonymous_module (false));
101 push_note_state (scm_c_make_hash_table (0));
102 chordmodifier_tab_ = scm_make_vector (scm_int2num (1), SCM_EOL);
105 Lily_lexer::Lily_lexer (Lily_lexer const &src)
106 : Includable_lexer ()
108 keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
109 encoding_ = src.encoding_;
110 chordmodifier_tab_ = src.chordmodifier_tab_;
111 pitchname_tab_stack_ = src.pitchname_tab_stack_;
112 sources_ = src.sources_;
114 error_level_ = src.error_level_;
115 main_input_b_ = src.main_input_b_;
121 SCM scopes = SCM_EOL;
123 for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
125 SCM newmod = ly_make_anonymous_module (false);
126 ly_module_copy (newmod, scm_car (s));
127 *tail = scm_cons (newmod, SCM_EOL);
128 tail = SCM_CDRLOC (*tail);
132 push_note_state (scm_c_make_hash_table (0));
135 Lily_lexer::~Lily_lexer ()
141 Lily_lexer::encoding () const
148 Lily_lexer::add_scope (SCM module)
150 ly_reexport_module (scm_current_module ());
151 scm_set_current_module (module);
152 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
154 ly_use_module (module, scm_car (s));
156 scopes_ = scm_cons (module, scopes_);
160 Lily_lexer::remove_scope ()
162 SCM sc = scm_car (scopes_);
163 scopes_ = scm_cdr (scopes_);
164 scm_set_current_module (scm_car (scopes_));
171 Lily_lexer::lookup_keyword (String s)
173 return keytable_->lookup (s.to_str0 ());
177 Lily_lexer::lookup_identifier_symbol (SCM sym)
179 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
181 SCM var = ly_module_lookup (scm_car (s), sym);
182 if (var != SCM_BOOL_F)
183 return scm_variable_ref (var);
186 return SCM_UNDEFINED;
190 Lily_lexer::lookup_identifier (String name)
192 return lookup_identifier_symbol (ly_symbol2scm (name.to_str0 ()));
196 Lily_lexer::start_main_input ()
198 // yy_flex_debug = 1;
199 new_input (main_input_name_, sources_);
201 /* Do not allow \include in --safe-mode */
202 allow_includes_b_ = allow_includes_b_ && !safe_global_b;
204 scm_module_define (scm_car (scopes_),
205 ly_symbol2scm ("input-file-name"),
206 scm_makfrom0str (main_input_name_.to_str0 ()));
210 Lily_lexer::set_identifier (SCM name, SCM s)
213 if (scm_is_string (name))
214 sym = scm_string_to_symbol (name);
216 if (scm_is_symbol (sym))
218 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
220 String symstr = ly_symbol2string (sym);
221 warning (_f ("Identifier name is a keyword: `%s'", symstr.to_str0()));
224 SCM mod = scm_car (scopes_);
226 scm_module_define (mod, sym, s);
230 programming_error ("Identifier is not a symbol.");
235 Lily_lexer::LexerError (char const *s)
237 if (include_stack_.is_empty ())
238 progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
242 Input spot (get_source_file (), here_str0 ());
248 Lily_lexer::escaped_char (char c) const
265 Lily_lexer::here_input () const
267 Source_file * f = get_source_file ();
268 return Input (f, (char*)here_str0 ());
272 Lily_lexer::prepare_for_next_token ()
274 last_input_ = here_input ();
278 Lily_lexer::set_encoding (String s)
281 encoding_ = ly_symbol2scm (s.to_str0 ());
286 #include "ly-smobs.icc"
288 IMPLEMENT_SMOBS (Lily_lexer);
289 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
290 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
293 Lily_lexer::mark_smob (SCM s)
295 Lily_lexer *lexer = (Lily_lexer*) SCM_CELL_WORD_1 (s);
297 scm_gc_mark (lexer->chordmodifier_tab_);
298 scm_gc_mark (lexer->pitchname_tab_stack_);
299 scm_gc_mark (lexer->scopes_);
300 return lexer->encoding_;
304 Lily_lexer::print_smob (SCM, SCM port, scm_print_state*)
306 scm_puts ("#<Lily_lexer ", port);
307 scm_puts (" >", port);