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[] = {
28 {"acciaccatura", ACCIACCATURA},
30 {"addlyrics", ADDLYRICS},
31 {"addquote", ADDQUOTE},
33 {"alternative", ALTERNATIVE},
35 {"applycontext", APPLYCONTEXT},
36 {"applyoutput", APPLYOUTPUT},
37 {"appoggiatura", APPOGGIATURA},
38 {"autochange", AUTOCHANGE},
45 {"consists", CONSISTS},
46 {"consistsend", CONSISTSEND},
51 {"description", DESCRIPTION},
54 {"grobdescriptions", GROBDESCRIPTIONS},
58 {"lyricsto", LYRICSTO},
64 {"newlyrics", NEWLYRICS},
68 {"override", OVERRIDE},
70 {"partcombine", PARTCOMBINE},
73 {"relative", RELATIVE},
79 {"sequential", SEQUENTIAL},
81 {"simultaneous", SIMULTANEOUS},
87 {"transpose", TRANSPOSE},
88 {"transposition", TRANSPOSITION},
96 My_lily_lexer::My_lily_lexer (Sources *sources)
99 keytable_ = new Keyword_table (the_key_tab);
101 chordmodifier_tab_ = scm_make_vector (scm_int2num (1), SCM_EOL);
102 pitchname_tab_stack_ = SCM_EOL;
106 main_input_b_ = false;
108 add_scope (ly_make_anonymous_module ());
111 My_lily_lexer::My_lily_lexer (My_lily_lexer const &src)
112 : Includable_lexer ()
114 keytable_ = src.keytable_;
115 encoding_ = src.encoding_;
116 chordmodifier_tab_ = src.chordmodifier_tab_;
117 pitchname_tab_stack_ = src.pitchname_tab_stack_;
118 sources_ = src.sources_;
119 scopes_ = src.scopes_;
120 error_level_ = src.error_level_;
121 // main_input_b_ = false;
122 main_input_b_ = src.main_input_b_;
126 My_lily_lexer::encoding () const
132 My_lily_lexer::add_scope (SCM module)
134 ly_reexport_module (scm_current_module ());
135 scm_set_current_module (module);
136 for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
138 /* UGH. how to do this more neatly? */
140 = scm_list_3 (ly_symbol2scm ("module-use!"),
142 scm_list_2 (ly_symbol2scm ("module-public-interface"),
144 scm_primitive_eval (expr);
146 scopes_ = scm_cons (module, scopes_);
150 My_lily_lexer::remove_scope ()
152 SCM sc = ly_car (scopes_);
153 scopes_ = ly_cdr (scopes_);
154 scm_set_current_module (ly_car (scopes_));
161 My_lily_lexer::lookup_keyword (String s)
163 return keytable_->lookup (s.to_str0 ());
167 My_lily_lexer::lookup_identifier (String name)
169 SCM sym = ly_symbol2scm (name.to_str0 ());
170 for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
172 SCM var = ly_module_lookup (ly_car (s), sym);
173 if (var != SCM_BOOL_F)
174 return scm_variable_ref (var);
177 return SCM_UNDEFINED;
181 My_lily_lexer::start_main_input ()
183 // yy_flex_debug = 1;
184 new_input (main_input_name_, sources_);
185 /* Do not allow \include in --safe-mode */
186 allow_includes_b_ = allow_includes_b_ && ! safe_global_b;
188 scm_module_define (ly_car (scopes_),
189 ly_symbol2scm ("input-file-name"),
190 scm_makfrom0str (main_input_name_.to_str0 ()));
194 My_lily_lexer::set_identifier (SCM name, SCM s)
196 assert (ly_c_string_p (name));
198 if (lookup_keyword (ly_scm2string (name)) >= 0)
200 warning (_f ("Identifier name is a keyword: `%s'", SCM_STRING_CHARS (name)));
203 SCM sym = scm_string_to_symbol (name);
204 SCM mod = ly_car (scopes_);
206 scm_module_define (mod, sym, s);
209 My_lily_lexer::~My_lily_lexer ()
217 My_lily_lexer::LexerError (char const *s)
219 if (include_stack_.is_empty ())
220 progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
224 Input spot (get_source_file (), here_str0 ());
230 My_lily_lexer::escaped_char (char c) const
248 My_lily_lexer::here_input () const
250 Source_file * f= get_source_file ();
251 return Input (f, (char*)here_str0 ());
255 My_lily_lexer::prepare_for_next_token ()
257 last_input_ = here_input ();
261 My_lily_lexer::set_encoding (String s)
264 encoding_ = ly_symbol2scm (s.to_str0 ());