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},
34 {"applycontext", APPLYCONTEXT},
35 {"applyoutput", APPLYOUTPUT},
36 {"appoggiatura", APPOGGIATURA},
37 {"autochange", AUTOCHANGE},
44 {"consists", CONSISTS},
45 {"consistsend", CONSISTSEND},
50 {"description", DESCRIPTION},
53 {"grobdescriptions", GROBDESCRIPTIONS},
57 {"lyricsto", LYRICSTO},
63 {"newlyrics", NEWLYRICS},
67 {"override", OVERRIDE},
69 {"partcombine", PARTCOMBINE},
72 {"relative", RELATIVE},
78 {"sequential", SEQUENTIAL},
80 {"simultaneous", SIMULTANEOUS},
86 {"transpose", TRANSPOSE},
87 {"transposition", TRANSPOSITION},
95 My_lily_lexer::My_lily_lexer (Sources *sources)
98 keytable_ = new Keyword_table (the_key_tab);
100 chordmodifier_tab_ = scm_make_vector (scm_int2num (1), SCM_EOL);
101 pitchname_tab_stack_ = SCM_EOL;
105 main_input_b_ = false;
107 add_scope (ly_make_anonymous_module (safe_global_b));
110 My_lily_lexer::My_lily_lexer (My_lily_lexer const &src)
111 : Includable_lexer ()
113 keytable_ = src.keytable_;
114 encoding_ = src.encoding_;
115 chordmodifier_tab_ = src.chordmodifier_tab_;
116 pitchname_tab_stack_ = src.pitchname_tab_stack_;
117 sources_ = src.sources_;
118 scopes_ = src.scopes_;
119 error_level_ = src.error_level_;
120 main_input_b_ = src.main_input_b_;
124 My_lily_lexer::encoding () const
131 My_lily_lexer::add_scope (SCM module)
133 ly_reexport_module (scm_current_module ());
134 scm_set_current_module (module);
135 for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
137 ly_use_module (module, ly_car (s));
139 scopes_ = scm_cons (module, scopes_);
143 My_lily_lexer::remove_scope ()
145 SCM sc = ly_car (scopes_);
146 scopes_ = ly_cdr (scopes_);
147 scm_set_current_module (ly_car (scopes_));
154 My_lily_lexer::lookup_keyword (String s)
156 return keytable_->lookup (s.to_str0 ());
160 My_lily_lexer::lookup_identifier (String name)
162 SCM sym = ly_symbol2scm (name.to_str0 ());
163 for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
165 SCM var = ly_module_lookup (ly_car (s), sym);
166 if (var != SCM_BOOL_F)
167 return scm_variable_ref (var);
170 return SCM_UNDEFINED;
174 My_lily_lexer::start_main_input ()
176 // yy_flex_debug = 1;
177 new_input (main_input_name_, sources_);
178 /* Do not allow \include in --safe-mode */
179 allow_includes_b_ = allow_includes_b_ && ! safe_global_b;
181 scm_module_define (ly_car (scopes_),
182 ly_symbol2scm ("input-file-name"),
183 scm_makfrom0str (main_input_name_.to_str0 ()));
187 My_lily_lexer::set_identifier (SCM name, SCM s)
189 assert (ly_c_string_p (name));
191 if (lookup_keyword (ly_scm2string (name)) >= 0)
193 warning (_f ("Identifier name is a keyword: `%s'", SCM_STRING_CHARS (name)));
196 SCM sym = scm_string_to_symbol (name);
197 SCM mod = ly_car (scopes_);
199 scm_module_define (mod, sym, s);
202 My_lily_lexer::~My_lily_lexer ()
210 My_lily_lexer::LexerError (char const *s)
212 if (include_stack_.is_empty ())
213 progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
217 Input spot (get_source_file (), here_str0 ());
223 My_lily_lexer::escaped_char (char c) const
241 My_lily_lexer::here_input () const
243 Source_file * f= get_source_file ();
244 return Input (f, (char*)here_str0 ());
248 My_lily_lexer::prepare_for_next_token ()
250 last_input_ = here_input ();
254 My_lily_lexer::set_encoding (String s)
257 encoding_ = ly_symbol2scm (s.to_str0 ());