Use scalar instead of embedded_scm for context mod overrides.
[lilypond/mpolesky.git] / lily / lily-lexer.cc
blob27c3bfef3bec056b0310fde28abe8ac07244909e
1 /*
2 lily-lexer.cc -- implement Lily_lexer
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "lily-lexer.hh"
11 #include <cctype>
12 #include <sstream>
13 using namespace std;
15 #include "international.hh"
16 #include "interval.hh"
17 #include "keyword.hh"
18 #include "main.hh"
19 #include "moment.hh"
20 #include "parser.hh"
21 #include "scm-hash.hh"
22 #include "source-file.hh"
23 #include "warn.hh"
24 #include "program-option.hh"
25 #include "lily-parser.hh"
27 static Keyword_ent the_key_tab[]
28 = {
29 {"accepts", ACCEPTS},
30 {"addlyrics", ADDLYRICS},
31 {"alias", ALIAS},
32 {"alternative", ALTERNATIVE},
33 {"book", BOOK},
34 {"bookpart", BOOKPART},
35 {"change", CHANGE},
36 {"chordmode", CHORDMODE},
37 {"chords", CHORDS},
38 {"consists", CONSISTS},
39 {"context", CONTEXT},
40 {"default", DEFAULT},
41 {"defaultchild", DEFAULTCHILD},
42 {"denies", DENIES},
43 {"description", DESCRIPTION},
44 {"drummode", DRUMMODE},
45 {"drums", DRUMS},
46 {"figuremode", FIGUREMODE},
47 {"figures", FIGURES},
48 {"grobdescriptions", GROBDESCRIPTIONS},
49 {"header", HEADER},
50 {"key", KEY},
51 {"layout", LAYOUT},
52 {"lyricmode", LYRICMODE},
53 {"lyrics", LYRICS},
54 {"lyricsto", LYRICSTO},
55 {"mark", MARK},
56 {"markup", MARKUP},
57 {"markuplines", MARKUPLINES},
58 {"midi", MIDI},
59 {"name", NAME},
60 {"new", NEWCONTEXT},
61 {"notemode", NOTEMODE},
62 {"objectid", OBJECTID},
63 {"once", ONCE},
64 {"override", OVERRIDE},
65 {"paper", PAPER},
66 {"partial", PARTIAL},
67 {"relative", RELATIVE},
68 {"remove", REMOVE},
69 {"repeat", REPEAT},
70 {"rest", REST},
71 {"revert", REVERT},
72 {"score", SCORE},
73 {"sequential", SEQUENTIAL},
74 {"set", SET},
75 {"simultaneous", SIMULTANEOUS},
76 {"skip", SKIP},
77 {"tempo", TEMPO},
78 {"time", TIME_T},
79 {"times", TIMES},
80 {"transpose", TRANSPOSE},
81 {"type", TYPE},
82 {"unset", UNSET},
83 {"with", WITH},
84 {0, 0}
87 Lily_lexer::Lily_lexer (Sources *sources, Lily_parser *parser)
89 parser_ = parser;
90 keytable_ = new Keyword_table (the_key_tab);
91 chordmodifier_tab_ = SCM_EOL;
92 pitchname_tab_stack_ = SCM_EOL;
93 sources_ = sources;
94 scopes_ = SCM_EOL;
95 error_level_ = 0;
96 is_main_input_ = false;
97 start_module_ = SCM_EOL;
98 chord_repetition_ = Chord_repetition ();
99 smobify_self ();
101 add_scope (ly_make_anonymous_module (false));
102 push_note_state (scm_c_make_hash_table (0));
103 chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
106 Lily_lexer::Lily_lexer (Lily_lexer const &src, Lily_parser *parser)
107 : Includable_lexer ()
109 parser_ = parser;
110 keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
111 chordmodifier_tab_ = src.chordmodifier_tab_;
112 pitchname_tab_stack_ = src.pitchname_tab_stack_;
113 sources_ = src.sources_;
114 start_module_ = SCM_EOL;
115 chord_repetition_ = Chord_repetition ();
117 error_level_ = src.error_level_;
118 is_main_input_ = src.is_main_input_;
120 scopes_ = SCM_EOL;
122 smobify_self ();
124 SCM scopes = SCM_EOL;
125 SCM *tail = &scopes;
126 for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
128 SCM newmod = ly_make_anonymous_module (false);
129 ly_module_copy (newmod, scm_car (s));
130 *tail = scm_cons (newmod, SCM_EOL);
131 tail = SCM_CDRLOC (*tail);
134 scopes_ = scopes;
135 push_note_state (scm_c_make_hash_table (0));
138 Lily_lexer::~Lily_lexer ()
140 delete keytable_;
143 void
144 Lily_lexer::add_scope (SCM module)
146 ly_reexport_module (scm_current_module ());
147 if (!scm_is_pair (scopes_))
148 start_module_ = scm_current_module ();
150 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
151 ly_use_module (module, scm_car (s));
152 scopes_ = scm_cons (module, scopes_);
154 set_current_scope ();
156 bool
157 Lily_lexer::has_scope () const
159 return scm_is_pair (scopes_);
163 Lily_lexer::remove_scope ()
165 SCM sc = scm_car (scopes_);
166 scopes_ = scm_cdr (scopes_);
167 set_current_scope ();
168 return sc;
172 Lily_lexer::set_current_scope ()
174 SCM old = scm_current_module ();
176 if (scm_is_pair (scopes_))
177 scm_set_current_module (scm_car (scopes_));
178 else
179 scm_set_current_module (start_module_);
181 return old;
185 Lily_lexer::lookup_keyword (string s)
187 return keytable_->lookup (s.c_str ());
191 Lily_lexer::keyword_list () const
193 if (!keytable_)
194 return SCM_EOL;
196 SCM l = SCM_EOL;
197 SCM *tail = &l;
198 for (vsize i = 0; i < keytable_->table_.size (); i++)
200 *tail = scm_acons (scm_from_locale_string (keytable_->table_[i].name_),
201 scm_from_int (keytable_->table_[i].tokcode_),
202 SCM_EOL);
204 tail = SCM_CDRLOC (*tail);
207 return l;
211 Lily_lexer::lookup_identifier_symbol (SCM sym)
213 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
215 SCM var = ly_module_lookup (scm_car (s), sym);
216 if (var != SCM_BOOL_F)
217 return scm_variable_ref (var);
220 return SCM_UNDEFINED;
224 Lily_lexer::lookup_identifier (string name)
226 return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
229 void
230 Lily_lexer::start_main_input ()
232 yy_flex_debug = get_program_option ("debug-lexer");
233 parser_->set_yydebug (get_program_option ("debug-parser"));
236 new_input (main_input_name_, sources_);
238 scm_module_define (scm_car (scopes_),
239 ly_symbol2scm ("input-file-name"),
240 ly_string2scm (main_input_name_));
243 void
244 Lily_lexer::new_input (string str, string d, Sources *ss)
246 Includable_lexer::new_input (str, d, ss);
249 void
250 Lily_lexer::new_input (string str, Sources *ss)
252 if (is_main_input_ && be_safe_global)
254 LexerError (_ ("include files are not allowed in safe mode").c_str ());
255 return;
258 Includable_lexer::new_input (str, ss);
261 void
262 Lily_lexer::set_identifier (SCM name, SCM s)
264 SCM sym = name;
265 if (scm_is_string (name))
266 sym = scm_string_to_symbol (name);
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 scm_module_define (mod, sym, s);
280 else
281 programming_error ("identifier is not a symbol");
284 void
285 Lily_lexer::LexerError (char const *s)
287 if (include_stack_.empty ())
288 message (_f ("error at EOF: %s", s) + "\n");
289 else
291 error_level_ |= 1;
292 Input spot (*lexloc);
293 spot.error (s);
297 char
298 Lily_lexer::escaped_char (char c) const
300 switch (c)
302 case 'n':
303 return '\n';
304 case 't':
305 return '\t';
306 case '\'':
307 case '\"':
308 case '\\':
309 return c;
311 return 0;
314 Input
315 Lily_lexer::here_input () const
317 return Input (*lexloc);
320 void
321 Lily_lexer::prepare_for_next_token ()
323 last_input_ = here_input ();
327 Since we don't create the buffer state from the bytes directly, we
328 don't know about the location of the lexer. Add this as a
329 YY_USER_ACTION */
330 void
331 Lily_lexer::add_lexed_char (int count)
333 char const *start = here_str0 ();
334 lexloc->set (get_source_file (),
335 start, start + count);
336 char_count_stack_.back () += count;
339 #include "ly-smobs.icc"
341 IMPLEMENT_SMOBS (Lily_lexer);
342 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
343 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
346 Lily_lexer::mark_smob (SCM s)
348 ASSERT_LIVE_IS_ALLOWED ();
350 Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
352 scm_gc_mark (lexer->chordmodifier_tab_);
353 if (lexer->parser_)
354 scm_gc_mark (lexer->parser_->self_scm ());
355 scm_gc_mark (lexer->pitchname_tab_stack_);
356 scm_gc_mark (lexer->start_module_);
357 return lexer->scopes_;
361 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
363 Lily_lexer *lexer = Lily_lexer::unsmob (s);
365 scm_puts ("#<Lily_lexer ", port);
366 scm_display (lexer->scopes_, port);
367 scm_puts (" >", port);
368 return 1;