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},
39 {"consistsend", CONSISTSEND},
44 {"description", DESCRIPTION},
46 {"grobdescriptions", GROBDESCRIPTIONS},
50 {"lyricsto", LYRICSTO},
56 {"newlyrics", NEWLYRICS},
60 {"override", OVERRIDE},
64 {"relative", RELATIVE},
70 {"sequential", SEQUENTIAL},
72 {"simultaneous", SIMULTANEOUS},
78 {"transpose", TRANSPOSE},
79 {"transposition", TRANSPOSITION},
87 My_lily_lexer::My_lily_lexer (Sources *sources)
90 keytable_ = new Keyword_table (the_key_tab);
92 chordmodifier_tab_ = scm_make_vector (scm_int2num (1), SCM_EOL);
93 pitchname_tab_stack_ = SCM_EOL;
97 main_input_b_ = false;
99 add_scope (ly_make_anonymous_module (false));
102 My_lily_lexer::My_lily_lexer (My_lily_lexer const &src)
103 : Includable_lexer ()
105 keytable_ = new Keyword_table (*src.keytable_);
106 encoding_ = src.encoding_;
107 chordmodifier_tab_ = src.chordmodifier_tab_;
108 pitchname_tab_stack_ = src.pitchname_tab_stack_;
109 sources_ = src.sources_;
110 scopes_ = src.scopes_;
111 error_level_ = src.error_level_;
112 main_input_b_ = src.main_input_b_;
115 My_lily_lexer::~My_lily_lexer ()
123 My_lily_lexer::encoding () const
130 My_lily_lexer::add_scope (SCM module)
132 ly_reexport_module (scm_current_module ());
133 scm_set_current_module (module);
134 for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
136 ly_use_module (module, ly_car (s));
138 scopes_ = scm_cons (module, scopes_);
142 My_lily_lexer::remove_scope ()
144 SCM sc = ly_car (scopes_);
145 scopes_ = ly_cdr (scopes_);
146 scm_set_current_module (ly_car (scopes_));
153 My_lily_lexer::lookup_keyword (String s)
155 return keytable_->lookup (s.to_str0 ());
159 My_lily_lexer::lookup_identifier_symbol (SCM sym)
161 for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
163 SCM var = ly_module_lookup (ly_car (s), sym);
164 if (var != SCM_BOOL_F)
165 return scm_variable_ref (var);
168 return SCM_UNDEFINED;
172 My_lily_lexer::lookup_identifier (String name)
174 return lookup_identifier_symbol ( ly_symbol2scm (name.to_str0 ()));
178 My_lily_lexer::start_main_input ()
180 // yy_flex_debug = 1;
181 new_input (main_input_name_, sources_);
182 /* Do not allow \include in --safe-mode */
183 allow_includes_b_ = allow_includes_b_ && ! safe_global_b;
185 scm_module_define (ly_car (scopes_),
186 ly_symbol2scm ("input-file-name"),
187 scm_makfrom0str (main_input_name_.to_str0 ()));
191 My_lily_lexer::set_identifier (SCM name, SCM s)
194 if (ly_c_string_p (name))
195 sym = scm_string_to_symbol (name);
197 if (ly_c_symbol_p (sym))
199 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
201 warning (_f ("Identifier name is a keyword: `%s'", SCM_SYMBOL_CHARS (sym)));
204 SCM mod = ly_car (scopes_);
206 scm_module_define (mod, sym, s);
211 programming_error ("Identifier is not a symbol.");
218 My_lily_lexer::LexerError (char const *s)
220 if (include_stack_.is_empty ())
221 progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
225 Input spot (get_source_file (), here_str0 ());
231 My_lily_lexer::escaped_char (char c) const
249 My_lily_lexer::here_input () const
251 Source_file * f= get_source_file ();
252 return Input (f, (char*)here_str0 ());
256 My_lily_lexer::prepare_for_next_token ()
258 last_input_ = here_input ();
262 My_lily_lexer::set_encoding (String s)
265 encoding_ = ly_symbol2scm (s.to_str0 ());