2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "lily-lexer.hh"
25 #include "context.hh" // for nested_property_alist
26 #include "international.hh"
27 #include "interval.hh"
32 #include "scm-hash.hh"
33 #include "source-file.hh"
35 #include "program-option.hh"
36 #include "lily-parser.hh"
37 #include "ly-module.hh"
41 static Keyword_ent the_key_tab[]
45 {"addlyrics", ADDLYRICS},
47 {"alternative", ALTERNATIVE},
49 {"bookpart", BOOKPART},
51 {"chordmode", CHORDMODE},
53 {"consists", CONSISTS},
56 {"defaultchild", DEFAULTCHILD},
58 {"description", DESCRIPTION},
59 {"drummode", DRUMMODE},
62 {"figuremode", FIGUREMODE},
66 {"lyricmode", LYRICMODE},
68 {"lyricsto", LYRICSTO},
70 {"markuplist", MARKUPLIST},
74 {"notemode", NOTEMODE},
75 {"override", OVERRIDE},
82 {"sequential", SEQUENTIAL},
84 {"simultaneous", SIMULTANEOUS},
92 Lily_lexer::Lily_lexer (Sources *sources, Lily_parser *parser)
95 keytable_ = new Keyword_table (the_key_tab);
96 chordmodifier_tab_ = SCM_EOL;
97 pitchname_tab_stack_ = SCM_EOL;
101 is_main_input_ = false;
102 main_input_level_ = 0;
103 start_module_ = SCM_EOL;
104 extra_tokens_ = SCM_EOL;
107 add_scope (ly_make_module (false));
108 push_note_state (SCM_EOL);
109 chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
112 Lily_lexer::Lily_lexer (Lily_lexer const &src, Lily_parser *parser,
114 : Includable_lexer ()
117 keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
118 chordmodifier_tab_ = src.chordmodifier_tab_;
119 pitchname_tab_stack_ = src.pitchname_tab_stack_;
120 sources_ = src.sources_;
121 scopes_ = src.scopes_;
122 start_module_ = SCM_EOL;
125 is_main_input_ = src.is_main_input_;
126 main_input_level_ = 0;
128 extra_tokens_ = SCM_EOL;
129 if (unsmob<Input> (override_input))
130 override_input_ = *unsmob<Input> (override_input);
134 push_note_state (SCM_EOL);
137 Lily_lexer::~Lily_lexer ()
143 Lily_lexer::add_scope (SCM module)
145 ly_reexport_module (scm_current_module ());
146 if (!scm_is_pair (scopes_))
147 start_module_ = scm_current_module ();
149 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
150 ly_use_module (module, scm_car (s));
151 scopes_ = scm_cons (module, scopes_);
153 set_current_scope ();
156 Lily_lexer::has_scope () const
158 return scm_is_pair (scopes_);
162 Lily_lexer::remove_scope ()
164 SCM sc = scm_car (scopes_);
165 scopes_ = scm_cdr (scopes_);
166 set_current_scope ();
171 Lily_lexer::set_current_scope ()
173 SCM old = scm_current_module ();
175 if (scm_is_pair (scopes_))
176 scm_set_current_module (scm_car (scopes_));
178 scm_set_current_module (start_module_);
184 Lily_lexer::lookup_keyword (const string &s)
186 return keytable_->lookup (s.c_str ());
190 Lily_lexer::keyword_list () const
197 for (vsize i = 0; i < keytable_->table_.size (); i++)
199 *tail = scm_acons (scm_from_utf8_string (keytable_->table_[i].name_),
200 scm_from_int (keytable_->table_[i].tokcode_),
203 tail = SCM_CDRLOC (*tail);
210 Lily_lexer::lookup_identifier_symbol (SCM sym)
212 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
214 SCM var = ly_module_lookup (scm_car (s), sym);
215 if (scm_is_true (var))
216 return scm_variable_ref (var);
219 return SCM_UNDEFINED;
223 Lily_lexer::lookup_identifier (const string &name)
225 return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
229 Lily_lexer::start_main_input ()
231 yy_flex_debug = get_program_option ("debug-lexer");
232 parser_->set_yydebug (get_program_option ("debug-parser"));
234 new_input (main_input_name_, sources_);
236 scm_module_define (scm_car (scopes_),
237 ly_symbol2scm ("input-file-name"),
238 ly_string2scm (main_input_name_));
242 Lily_lexer::new_input (const string &str, string d, Sources *ss)
244 Includable_lexer::new_input (str, d, ss);
248 Lily_lexer::new_input (const string &str, Sources *ss)
250 if (is_main_input_ && be_safe_global)
252 LexerError (_ ("include files are not allowed in safe mode").c_str ());
256 Includable_lexer::new_input (str, ss);
259 // PATH is either a single symbol (or string) or a list of symbols
260 // giving the path to a nested property. A symbol is treated the same
261 // as a list of length 1.
263 Lily_lexer::set_identifier (SCM path, SCM val)
266 if (scm_is_string (path))
267 sym = scm_string_to_symbol (path);
268 else if (scm_is_pair (path))
270 sym = scm_car (path);
271 path = scm_cdr (path);
274 if (scm_is_symbol (sym))
276 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
278 string symstr = ly_symbol2string (sym);
279 warning (_f ("identifier name is a keyword: `%s'", symstr.c_str ()));
282 SCM mod = scm_car (scopes_);
284 if (scm_is_pair (path))
286 SCM prev = ly_module_lookup (mod, sym);
287 if (scm_is_true (prev))
288 val = nested_property_alist (scm_variable_ref (prev), path, val);
290 scm_module_define (mod, sym, val);
293 programming_error ("identifier is not a symbol");
297 Lily_lexer::LexerError (char const *s)
299 if (include_stack_.empty ())
300 non_fatal_error (s, _f ("%s:EOF", s));
304 Input spot (*lexloc_);
305 spot.non_fatal_error (s);
310 Lily_lexer::LexerWarning (char const *s)
312 if (include_stack_.empty ())
313 warning (s, _f ("%s:EOF", s));
316 Input spot (*lexloc_);
322 Lily_lexer::escaped_char (char c) const
339 Lily_lexer::here_input () const
341 return Input (*lexloc_);
345 Lily_lexer::override_input (Input const &in) const
347 return override_input_.get_source_file ()
348 ? override_input_ : in;
352 Lily_lexer::prepare_for_next_token ()
354 last_input_ = here_input ();
358 Since we don't create the buffer state from the bytes directly, we
359 don't know about the location of the lexer. Add this as a
362 Lily_lexer::add_lexed_char (int count)
364 char const *start = here_str0 ();
365 lexloc_->set (get_source_file (),
366 start, start + count);
367 char_count_stack_.back () += count;
371 const char Lily_lexer::type_p_name_[] = "ly:lily-lexer?";
374 Lily_lexer::mark_smob () const
376 ASSERT_LIVE_IS_ALLOWED (self_scm ());
378 scm_gc_mark (chordmodifier_tab_);
380 scm_gc_mark (parser_->self_scm ());
381 scm_gc_mark (pitchname_tab_stack_);
382 scm_gc_mark (start_module_);
383 scm_gc_mark (extra_tokens_);
388 Lily_lexer::print_smob (SCM port, scm_print_state *) const
390 scm_puts ("#<Lily_lexer ", port);
391 scm_display (scopes_, port);
392 scm_puts (" >", port);
397 Lily_lexer::is_clean () const
399 return include_stack_.empty ();