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"
26 #include "context.hh" // for nested_property_alist
27 #include "international.hh"
28 #include "interval.hh"
33 #include "scm-hash.hh"
34 #include "source-file.hh"
36 #include "program-option.hh"
37 #include "lily-parser.hh"
38 #include "ly-module.hh"
40 static Keyword_ent the_key_tab[]
44 {"addlyrics", ADDLYRICS},
46 {"alternative", ALTERNATIVE},
48 {"bookpart", BOOKPART},
50 {"chordmode", CHORDMODE},
52 {"consists", CONSISTS},
55 {"defaultchild", DEFAULTCHILD},
57 {"description", DESCRIPTION},
58 {"drummode", DRUMMODE},
61 {"figuremode", FIGUREMODE},
65 {"lyricmode", LYRICMODE},
67 {"lyricsto", LYRICSTO},
69 {"markuplist", MARKUPLIST},
73 {"notemode", NOTEMODE},
74 {"override", OVERRIDE},
81 {"sequential", SEQUENTIAL},
83 {"simultaneous", SIMULTANEOUS},
91 Lily_lexer::Lily_lexer (Sources *sources, Lily_parser *parser)
94 keytable_ = new Keyword_table (the_key_tab);
95 chordmodifier_tab_ = SCM_EOL;
96 pitchname_tab_stack_ = SCM_EOL;
100 is_main_input_ = false;
101 main_input_level_ = 0;
102 start_module_ = SCM_EOL;
103 extra_tokens_ = SCM_EOL;
106 add_scope (ly_make_module (false));
107 push_note_state (SCM_EOL);
108 chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
111 Lily_lexer::Lily_lexer (Lily_lexer const &src, Lily_parser *parser,
113 : Includable_lexer ()
116 keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
117 chordmodifier_tab_ = src.chordmodifier_tab_;
118 pitchname_tab_stack_ = src.pitchname_tab_stack_;
119 sources_ = src.sources_;
120 scopes_ = src.scopes_;
121 start_module_ = SCM_EOL;
124 is_main_input_ = src.is_main_input_;
125 main_input_level_ = 0;
127 extra_tokens_ = SCM_EOL;
128 if (unsmob<Input> (override_input))
129 override_input_ = *unsmob<Input> (override_input);
133 push_note_state (SCM_EOL);
136 Lily_lexer::~Lily_lexer ()
142 Lily_lexer::add_scope (SCM module)
144 ly_reexport_module (scm_current_module ());
145 if (!scm_is_pair (scopes_))
146 start_module_ = scm_current_module ();
148 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
149 ly_use_module (module, scm_car (s));
150 scopes_ = scm_cons (module, scopes_);
152 set_current_scope ();
155 Lily_lexer::has_scope () const
157 return scm_is_pair (scopes_);
161 Lily_lexer::remove_scope ()
163 SCM sc = scm_car (scopes_);
164 scopes_ = scm_cdr (scopes_);
165 set_current_scope ();
170 Lily_lexer::set_current_scope ()
172 SCM old = scm_current_module ();
174 if (scm_is_pair (scopes_))
175 scm_set_current_module (scm_car (scopes_));
177 scm_set_current_module (start_module_);
183 Lily_lexer::lookup_keyword (const string &s)
185 return keytable_->lookup (s.c_str ());
189 Lily_lexer::keyword_list () const
196 for (vsize i = 0; i < keytable_->table_.size (); i++)
198 *tail = scm_acons (scm_from_utf8_string (keytable_->table_[i].name_),
199 scm_from_int (keytable_->table_[i].tokcode_),
202 tail = SCM_CDRLOC (*tail);
209 Lily_lexer::lookup_identifier_symbol (SCM sym)
211 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
213 SCM var = ly_module_lookup (scm_car (s), sym);
214 if (scm_is_true (var))
215 return scm_variable_ref (var);
218 return SCM_UNDEFINED;
222 Lily_lexer::lookup_identifier (const string &name)
224 return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
228 Lily_lexer::start_main_input ()
230 yy_flex_debug = get_program_option ("debug-lexer");
231 parser_->set_yydebug (get_program_option ("debug-parser"));
233 new_input (main_input_name_, sources_);
235 scm_module_define (scm_car (scopes_),
236 ly_symbol2scm ("input-file-name"),
237 ly_string2scm (main_input_name_));
241 Lily_lexer::new_input (const string &str, string d, Sources *ss)
243 Includable_lexer::new_input (str, d, ss);
247 Lily_lexer::new_input (const string &str, Sources *ss)
249 if (is_main_input_ && be_safe_global)
251 LexerError (_ ("include files are not allowed in safe mode").c_str ());
255 Includable_lexer::new_input (str, ss);
258 // PATH is either a single symbol (or string) or a list of symbols
259 // giving the path to a nested property. A symbol is treated the same
260 // as a list of length 1.
262 Lily_lexer::set_identifier (SCM path, SCM val)
265 if (scm_is_string (path))
266 sym = scm_string_to_symbol (path);
267 else if (scm_is_pair (path))
269 sym = scm_car (path);
270 path = scm_cdr (path);
273 if (scm_is_symbol (sym))
275 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
277 string symstr = ly_symbol2string (sym);
278 warning (_f ("identifier name is a keyword: `%s'", symstr.c_str ()));
281 SCM mod = scm_car (scopes_);
283 if (scm_is_pair (path))
285 SCM prev = ly_module_lookup (mod, sym);
286 if (scm_is_true (prev))
287 val = nested_property_alist (scm_variable_ref (prev), path, val);
289 scm_module_define (mod, sym, val);
292 programming_error ("identifier is not a symbol");
296 Lily_lexer::LexerError (char const *s)
298 if (include_stack_.empty ())
299 non_fatal_error (s, _f ("%s:EOF", s));
303 Input spot (*lexloc_);
304 spot.non_fatal_error (s);
309 Lily_lexer::LexerWarning (char const *s)
311 if (include_stack_.empty ())
312 warning (s, _f ("%s:EOF", s));
315 Input spot (*lexloc_);
321 Lily_lexer::escaped_char (char c) const
338 Lily_lexer::here_input () const
340 return Input (*lexloc_);
344 Lily_lexer::override_input (Input const &in) const
346 return override_input_.get_source_file ()
347 ? override_input_ : in;
351 Lily_lexer::prepare_for_next_token ()
353 last_input_ = here_input ();
357 Since we don't create the buffer state from the bytes directly, we
358 don't know about the location of the lexer. Add this as a
361 Lily_lexer::add_lexed_char (int count)
363 char const *start = here_str0 ();
364 lexloc_->set (get_source_file (),
365 start, start + count);
366 char_count_stack_.back () += count;
370 const char Lily_lexer::type_p_name_[] = "ly:lily-lexer?";
373 Lily_lexer::mark_smob () const
375 ASSERT_LIVE_IS_ALLOWED (self_scm ());
377 scm_gc_mark (chordmodifier_tab_);
379 scm_gc_mark (parser_->self_scm ());
380 scm_gc_mark (pitchname_tab_stack_);
381 scm_gc_mark (start_module_);
382 scm_gc_mark (extra_tokens_);
387 Lily_lexer::print_smob (SCM port, scm_print_state *) const
389 scm_puts ("#<Lily_lexer ", port);
390 scm_display (scopes_, port);
391 scm_puts (" >", port);
396 Lily_lexer::is_clean () const
398 return include_stack_.empty ();