2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2010 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
[]
42 {"addlyrics", ADDLYRICS
},
44 {"alternative", ALTERNATIVE
},
46 {"bookpart", BOOKPART
},
48 {"chordmode", CHORDMODE
},
50 {"consists", CONSISTS
},
53 {"defaultchild", DEFAULTCHILD
},
55 {"description", DESCRIPTION
},
56 {"drummode", DRUMMODE
},
58 {"figuremode", FIGUREMODE
},
60 {"grobdescriptions", GROBDESCRIPTIONS
},
64 {"lyricmode", LYRICMODE
},
66 {"lyricsto", LYRICSTO
},
69 {"markuplines", MARKUPLINES
},
73 {"notemode", NOTEMODE
},
74 {"objectid", OBJECTID
},
76 {"override", OVERRIDE
},
79 {"relative", RELATIVE
},
85 {"sequential", SEQUENTIAL
},
87 {"simultaneous", SIMULTANEOUS
},
92 {"transpose", TRANSPOSE
},
99 Lily_lexer::Lily_lexer (Sources
*sources
, Lily_parser
*parser
)
102 keytable_
= new Keyword_table (the_key_tab
);
103 chordmodifier_tab_
= SCM_EOL
;
104 pitchname_tab_stack_
= SCM_EOL
;
108 is_main_input_
= false;
109 start_module_
= SCM_EOL
;
110 chord_repetition_
= Chord_repetition ();
113 add_scope (ly_make_anonymous_module (false));
114 push_note_state (scm_c_make_hash_table (0));
115 chordmodifier_tab_
= scm_make_vector (scm_from_int (1), SCM_EOL
);
118 Lily_lexer::Lily_lexer (Lily_lexer
const &src
, Lily_parser
*parser
)
119 : Includable_lexer ()
122 keytable_
= (src
.keytable_
) ? new Keyword_table (*src
.keytable_
) : 0;
123 chordmodifier_tab_
= src
.chordmodifier_tab_
;
124 pitchname_tab_stack_
= src
.pitchname_tab_stack_
;
125 sources_
= src
.sources_
;
126 start_module_
= SCM_EOL
;
127 chord_repetition_
= src
.chord_repetition_
;
129 error_level_
= src
.error_level_
;
130 is_main_input_
= src
.is_main_input_
;
136 SCM scopes
= SCM_EOL
;
138 for (SCM s
= src
.scopes_
; scm_is_pair (s
); s
= scm_cdr (s
))
140 SCM newmod
= ly_make_anonymous_module (false);
141 ly_module_copy (newmod
, scm_car (s
));
142 *tail
= scm_cons (newmod
, SCM_EOL
);
143 tail
= SCM_CDRLOC (*tail
);
147 push_note_state (scm_c_make_hash_table (0));
150 Lily_lexer::~Lily_lexer ()
156 Lily_lexer::add_scope (SCM module
)
158 ly_reexport_module (scm_current_module ());
159 if (!scm_is_pair (scopes_
))
160 start_module_
= scm_current_module ();
162 for (SCM s
= scopes_
; scm_is_pair (s
); s
= scm_cdr (s
))
163 ly_use_module (module
, scm_car (s
));
164 scopes_
= scm_cons (module
, scopes_
);
166 set_current_scope ();
169 Lily_lexer::has_scope () const
171 return scm_is_pair (scopes_
);
175 Lily_lexer::remove_scope ()
177 SCM sc
= scm_car (scopes_
);
178 scopes_
= scm_cdr (scopes_
);
179 set_current_scope ();
184 Lily_lexer::set_current_scope ()
186 SCM old
= scm_current_module ();
188 if (scm_is_pair (scopes_
))
189 scm_set_current_module (scm_car (scopes_
));
191 scm_set_current_module (start_module_
);
197 Lily_lexer::lookup_keyword (string s
)
199 return keytable_
->lookup (s
.c_str ());
203 Lily_lexer::keyword_list () const
210 for (vsize i
= 0; i
< keytable_
->table_
.size (); i
++)
212 *tail
= scm_acons (scm_from_locale_string (keytable_
->table_
[i
].name_
),
213 scm_from_int (keytable_
->table_
[i
].tokcode_
),
216 tail
= SCM_CDRLOC (*tail
);
223 Lily_lexer::lookup_identifier_symbol (SCM sym
)
225 for (SCM s
= scopes_
; scm_is_pair (s
); s
= scm_cdr (s
))
227 SCM var
= ly_module_lookup (scm_car (s
), sym
);
228 if (var
!= SCM_BOOL_F
)
229 return scm_variable_ref (var
);
232 return SCM_UNDEFINED
;
236 Lily_lexer::lookup_identifier (string name
)
238 return lookup_identifier_symbol (ly_symbol2scm (name
.c_str ()));
242 Lily_lexer::start_main_input ()
244 yy_flex_debug
= get_program_option ("debug-lexer");
245 parser_
->set_yydebug (get_program_option ("debug-parser"));
248 new_input (main_input_name_
, sources_
);
250 scm_module_define (scm_car (scopes_
),
251 ly_symbol2scm ("input-file-name"),
252 ly_string2scm (main_input_name_
));
256 Lily_lexer::new_input (string str
, string d
, Sources
*ss
)
258 Includable_lexer::new_input (str
, d
, ss
);
262 Lily_lexer::new_input (string str
, Sources
*ss
)
264 if (is_main_input_
&& be_safe_global
)
266 LexerError (_ ("include files are not allowed in safe mode").c_str ());
270 Includable_lexer::new_input (str
, ss
);
273 // PATH is either a single symbol (or string) or a list of symbols
274 // giving the path to a nested property. A symbol is treated the same
275 // as a list of length 1.
277 Lily_lexer::set_identifier (SCM path
, SCM val
)
280 if (scm_is_string (path
))
281 sym
= scm_string_to_symbol (path
);
282 else if (scm_is_pair (path
))
284 sym
= scm_car (path
);
285 path
= scm_cdr (path
);
288 if (scm_is_symbol (sym
))
290 if (lookup_keyword (ly_symbol2string (sym
)) >= 0)
292 string symstr
= ly_symbol2string (sym
);
293 warning (_f ("identifier name is a keyword: `%s'", symstr
.c_str ()));
296 SCM mod
= scm_car (scopes_
);
298 if (scm_is_pair (path
))
300 SCM prev
= scm_module_lookup (mod
, sym
);
301 if (prev
!= SCM_UNDEFINED
)
302 val
= nested_property_alist (prev
, path
, val
);
304 scm_module_define (mod
, sym
, val
);
307 programming_error ("identifier is not a symbol");
311 Lily_lexer::LexerError (char const *s
)
313 if (include_stack_
.empty ())
314 message (_f ("error at EOF: %s", s
) + "\n");
318 Input
spot (*lexloc_
);
324 Lily_lexer::escaped_char (char c
) const
341 Lily_lexer::here_input () const
343 return Input (*lexloc_
);
347 Lily_lexer::prepare_for_next_token ()
349 last_input_
= here_input ();
353 Since we don't create the buffer state from the bytes directly, we
354 don't know about the location of the lexer. Add this as a
357 Lily_lexer::add_lexed_char (int count
)
359 char const *start
= here_str0 ();
360 lexloc_
->set (get_source_file (),
361 start
, start
+ count
);
362 char_count_stack_
.back () += count
;
365 #include "ly-smobs.icc"
367 IMPLEMENT_SMOBS (Lily_lexer
);
368 IMPLEMENT_TYPE_P (Lily_lexer
, "ly:lily-lexer?");
369 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer
);
372 Lily_lexer::mark_smob (SCM s
)
374 ASSERT_LIVE_IS_ALLOWED ();
376 Lily_lexer
*lexer
= (Lily_lexer
*) SCM_CELL_WORD_1 (s
);
378 scm_gc_mark (lexer
->chordmodifier_tab_
);
380 scm_gc_mark (lexer
->parser_
->self_scm ());
381 scm_gc_mark (lexer
->pitchname_tab_stack_
);
382 scm_gc_mark (lexer
->start_module_
);
383 return lexer
->scopes_
;
387 Lily_lexer::print_smob (SCM s
, SCM port
, scm_print_state
*)
389 Lily_lexer
*lexer
= Lily_lexer::unsmob (s
);
391 scm_puts ("#<Lily_lexer ", port
);
392 scm_display (lexer
->scopes_
, port
);
393 scm_puts (" >", port
);