2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2012 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"
39 static Keyword_ent the_key_tab[]
43 {"addlyrics", ADDLYRICS},
45 {"alternative", ALTERNATIVE},
47 {"bookpart", BOOKPART},
49 {"chordmode", CHORDMODE},
51 {"consists", CONSISTS},
54 {"defaultchild", DEFAULTCHILD},
56 {"description", DESCRIPTION},
57 {"drummode", DRUMMODE},
59 {"figuremode", FIGUREMODE},
63 {"lyricmode", LYRICMODE},
65 {"lyricsto", LYRICSTO},
67 {"markuplist", MARKUPLIST},
71 {"notemode", NOTEMODE},
72 {"override", OVERRIDE},
79 {"sequential", SEQUENTIAL},
81 {"simultaneous", SIMULTANEOUS},
89 Lily_lexer::Lily_lexer (Sources *sources, Lily_parser *parser)
92 keytable_ = new Keyword_table (the_key_tab);
93 chordmodifier_tab_ = SCM_EOL;
94 pitchname_tab_stack_ = SCM_EOL;
98 is_main_input_ = false;
99 main_input_level_ = 0;
100 start_module_ = SCM_EOL;
101 extra_tokens_ = SCM_EOL;
104 add_scope (ly_make_module (false));
105 push_note_state (SCM_EOL);
106 chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
109 Lily_lexer::Lily_lexer (Lily_lexer const &src, Lily_parser *parser)
110 : Includable_lexer ()
113 keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
114 chordmodifier_tab_ = src.chordmodifier_tab_;
115 pitchname_tab_stack_ = src.pitchname_tab_stack_;
116 sources_ = src.sources_;
117 scopes_ = src.scopes_;
118 start_module_ = SCM_EOL;
121 is_main_input_ = src.is_main_input_;
122 main_input_level_ = 0;
124 extra_tokens_ = SCM_EOL;
128 push_note_state (SCM_EOL);
131 Lily_lexer::~Lily_lexer ()
137 Lily_lexer::add_scope (SCM module)
139 ly_reexport_module (scm_current_module ());
140 if (!scm_is_pair (scopes_))
141 start_module_ = scm_current_module ();
143 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
144 ly_use_module (module, scm_car (s));
145 scopes_ = scm_cons (module, scopes_);
147 set_current_scope ();
150 Lily_lexer::has_scope () const
152 return scm_is_pair (scopes_);
156 Lily_lexer::remove_scope ()
158 SCM sc = scm_car (scopes_);
159 scopes_ = scm_cdr (scopes_);
160 set_current_scope ();
165 Lily_lexer::set_current_scope ()
167 SCM old = scm_current_module ();
169 if (scm_is_pair (scopes_))
170 scm_set_current_module (scm_car (scopes_));
172 scm_set_current_module (start_module_);
178 Lily_lexer::lookup_keyword (string s)
180 return keytable_->lookup (s.c_str ());
184 Lily_lexer::keyword_list () const
191 for (vsize i = 0; i < keytable_->table_.size (); i++)
193 *tail = scm_acons (scm_from_locale_string (keytable_->table_[i].name_),
194 scm_from_int (keytable_->table_[i].tokcode_),
197 tail = SCM_CDRLOC (*tail);
204 Lily_lexer::lookup_identifier_symbol (SCM sym)
206 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
208 SCM var = ly_module_lookup (scm_car (s), sym);
209 if (var != SCM_BOOL_F)
210 return scm_variable_ref (var);
213 return SCM_UNDEFINED;
217 Lily_lexer::lookup_identifier (string name)
219 return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
223 Lily_lexer::start_main_input ()
225 yy_flex_debug = get_program_option ("debug-lexer");
226 parser_->set_yydebug (get_program_option ("debug-parser"));
228 new_input (main_input_name_, sources_);
230 scm_module_define (scm_car (scopes_),
231 ly_symbol2scm ("input-file-name"),
232 ly_string2scm (main_input_name_));
236 Lily_lexer::new_input (string str, string d, Sources *ss)
238 Includable_lexer::new_input (str, d, ss);
242 Lily_lexer::new_input (string str, Sources *ss)
244 if (is_main_input_ && be_safe_global)
246 LexerError (_ ("include files are not allowed in safe mode").c_str ());
250 Includable_lexer::new_input (str, ss);
253 // PATH is either a single symbol (or string) or a list of symbols
254 // giving the path to a nested property. A symbol is treated the same
255 // as a list of length 1.
257 Lily_lexer::set_identifier (SCM path, SCM val)
260 if (scm_is_string (path))
261 sym = scm_string_to_symbol (path);
262 else if (scm_is_pair (path))
264 sym = scm_car (path);
265 path = scm_cdr (path);
268 if (scm_is_symbol (sym))
270 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
272 string symstr = ly_symbol2string (sym);
273 warning (_f ("identifier name is a keyword: `%s'", symstr.c_str ()));
276 SCM mod = scm_car (scopes_);
278 if (scm_is_pair (path))
280 SCM prev = ly_module_lookup (mod, sym);
281 if (prev != SCM_BOOL_F)
282 val = nested_property_alist (scm_variable_ref (prev), path, val);
284 scm_module_define (mod, sym, val);
287 programming_error ("identifier is not a symbol");
291 Lily_lexer::LexerError (char const *s)
293 if (include_stack_.empty ())
294 non_fatal_error (s, _f ("%s:EOF", s));
298 Input spot (*lexloc_);
304 Lily_lexer::LexerWarning (char const *s)
306 if (include_stack_.empty ())
307 warning (s, _f ("%s:EOF", s));
310 Input spot (*lexloc_);
316 Lily_lexer::escaped_char (char c) const
333 Lily_lexer::here_input () const
335 return Input (*lexloc_);
339 Lily_lexer::prepare_for_next_token ()
341 last_input_ = here_input ();
345 Since we don't create the buffer state from the bytes directly, we
346 don't know about the location of the lexer. Add this as a
349 Lily_lexer::add_lexed_char (int count)
351 char const *start = here_str0 ();
352 lexloc_->set (get_source_file (),
353 start, start + count);
354 char_count_stack_.back () += count;
357 #include "ly-smobs.icc"
359 IMPLEMENT_SMOBS (Lily_lexer);
360 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
361 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
364 Lily_lexer::mark_smob (SCM s)
366 ASSERT_LIVE_IS_ALLOWED (s);
368 Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
370 scm_gc_mark (lexer->chordmodifier_tab_);
372 scm_gc_mark (lexer->parser_->self_scm ());
373 scm_gc_mark (lexer->pitchname_tab_stack_);
374 scm_gc_mark (lexer->start_module_);
375 scm_gc_mark (lexer->extra_tokens_);
376 return lexer->scopes_;
380 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state *)
382 Lily_lexer *lexer = Lily_lexer::unsmob (s);
384 scm_puts ("#<Lily_lexer ", port);
385 scm_display (lexer->scopes_, port);
386 scm_puts (" >", port);
391 Lily_lexer::is_clean () const
393 return include_stack_.empty ();