2 my-lily-lexer.cc -- implement My_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 "my-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 My_lily_lexer::My_lily_lexer (Sources *sources)
88 keytable_ = new Keyword_table (the_key_tab);
90 chordmodifier_tab_ = scm_make_vector (scm_int2num (1), SCM_EOL);
91 pitchname_tab_stack_ = SCM_EOL;
95 main_input_b_ = false;
97 add_scope (ly_make_anonymous_module (false));
99 push_note_state (scm_c_make_hash_table (0));
103 My_lily_lexer::My_lily_lexer (My_lily_lexer const &src)
104 : Includable_lexer ()
106 keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
107 encoding_ = src.encoding_;
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_;
115 SCM scopes = SCM_EOL;
117 for (SCM s = src.scopes_; ly_c_pair_p (s); s = ly_cdr (s))
119 SCM newmod = ly_make_anonymous_module (false);
120 ly_import_module (newmod, ly_car (s));
121 *tail = scm_cons (newmod, SCM_EOL);
122 tail = SCM_CDRLOC (*tail);
126 push_note_state (scm_c_make_hash_table (0));
129 My_lily_lexer::~My_lily_lexer ()
135 My_lily_lexer::encoding () const
142 My_lily_lexer::add_scope (SCM module)
144 ly_reexport_module (scm_current_module ());
145 scm_set_current_module (module);
146 for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
148 ly_use_module (module, ly_car (s));
150 scopes_ = scm_cons (module, scopes_);
154 My_lily_lexer::remove_scope ()
156 SCM sc = ly_car (scopes_);
157 scopes_ = ly_cdr (scopes_);
158 scm_set_current_module (ly_car (scopes_));
165 My_lily_lexer::lookup_keyword (String s)
167 return keytable_->lookup (s.to_str0 ());
171 My_lily_lexer::lookup_identifier_symbol (SCM sym)
173 for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
175 SCM var = ly_module_lookup (ly_car (s), sym);
176 if (var != SCM_BOOL_F)
177 return scm_variable_ref (var);
180 return SCM_UNDEFINED;
184 My_lily_lexer::lookup_identifier (String name)
186 return lookup_identifier_symbol (ly_symbol2scm (name.to_str0 ()));
190 My_lily_lexer::start_main_input ()
192 // yy_flex_debug = 1;
193 new_input (main_input_name_, sources_);
195 /* Do not allow \include in --safe-mode */
196 allow_includes_b_ = allow_includes_b_ && !safe_global_b;
198 scm_module_define (ly_car (scopes_),
199 ly_symbol2scm ("input-file-name"),
200 scm_makfrom0str (main_input_name_.to_str0 ()));
204 My_lily_lexer::set_identifier (SCM name, SCM s)
207 if (ly_c_string_p (name))
208 sym = scm_string_to_symbol (name);
210 if (ly_c_symbol_p (sym))
212 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
214 warning (_f ("Identifier name is a keyword: `%s'", SCM_SYMBOL_CHARS (sym)));
217 SCM mod = ly_car (scopes_);
219 scm_module_define (mod, sym, s);
223 programming_error ("Identifier is not a symbol.");
228 My_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 My_lily_lexer::escaped_char (char c) const
258 My_lily_lexer::here_input () const
260 Source_file * f= get_source_file ();
261 return Input (f, (char*)here_str0 ());
265 My_lily_lexer::prepare_for_next_token ()
267 last_input_ = here_input ();
271 My_lily_lexer::set_encoding (String s)
274 encoding_ = ly_symbol2scm (s.to_str0 ());