Bump version.
[lilypond.git] / lily / lily-lexer.cc
blob7b77d58922b6816c55607a1d30c9c0f622ae87d1
1 /*
2 lily-lexer.cc -- implement Lily_lexer
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 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 {"change", CHANGE},
35 {"chordmode", CHORDMODE},
36 {"chords", CHORDS},
37 {"consists", CONSISTS},
38 {"context", CONTEXT},
39 {"default", DEFAULT},
40 {"defaultchild", DEFAULTCHILD},
41 {"denies", DENIES},
42 {"description", DESCRIPTION},
43 {"drummode", DRUMMODE},
44 {"drums", DRUMS},
45 {"figuremode", FIGUREMODE},
46 {"figures", FIGURES},
47 {"grobdescriptions", GROBDESCRIPTIONS},
48 {"header", HEADER},
49 {"key", KEY},
50 {"layout", LAYOUT},
51 {"lyricmode", LYRICMODE},
52 {"lyrics", LYRICS},
53 {"lyricsto", LYRICSTO},
54 {"mark", MARK},
55 {"markup", MARKUP},
56 {"markuplines", MARKUPLINES},
57 {"midi", MIDI},
58 {"name", NAME},
59 {"new", NEWCONTEXT},
60 {"notemode", NOTEMODE},
61 {"objectid", OBJECTID},
62 {"once", ONCE},
63 {"override", OVERRIDE},
64 {"paper", PAPER},
65 {"partial", PARTIAL},
66 {"relative", RELATIVE},
67 {"remove", REMOVE},
68 {"repeat", REPEAT},
69 {"rest", REST},
70 {"revert", REVERT},
71 {"score", SCORE},
72 {"sequential", SEQUENTIAL},
73 {"set", SET},
74 {"simultaneous", SIMULTANEOUS},
75 {"skip", SKIP},
76 {"tempo", TEMPO},
77 {"time", TIME_T},
78 {"times", TIMES},
79 {"transpose", TRANSPOSE},
80 {"type", TYPE},
81 {"unset", UNSET},
82 {"with", WITH},
83 {0, 0}
86 Lily_lexer::Lily_lexer (Sources *sources, Lily_parser *parser)
88 parser_ = parser;
89 keytable_ = new Keyword_table (the_key_tab);
90 chordmodifier_tab_ = SCM_EOL;
91 pitchname_tab_stack_ = SCM_EOL;
92 sources_ = sources;
93 scopes_ = SCM_EOL;
94 error_level_ = 0;
95 is_main_input_ = false;
96 start_module_ = SCM_EOL;
97 smobify_self ();
99 add_scope (ly_make_anonymous_module (false));
100 push_note_state (scm_c_make_hash_table (0));
101 chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
104 Lily_lexer::Lily_lexer (Lily_lexer const &src, Lily_parser *parser)
105 : Includable_lexer ()
107 parser_ = parser;
108 keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
109 chordmodifier_tab_ = src.chordmodifier_tab_;
110 pitchname_tab_stack_ = src.pitchname_tab_stack_;
111 sources_ = src.sources_;
112 start_module_ = SCM_EOL;
114 error_level_ = src.error_level_;
115 is_main_input_ = src.is_main_input_;
117 scopes_ = SCM_EOL;
119 smobify_self ();
121 SCM scopes = SCM_EOL;
122 SCM *tail = &scopes;
123 for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
125 SCM newmod = ly_make_anonymous_module (false);
126 ly_module_copy (newmod, scm_car (s));
127 *tail = scm_cons (newmod, SCM_EOL);
128 tail = SCM_CDRLOC (*tail);
131 scopes_ = scopes;
132 push_note_state (scm_c_make_hash_table (0));
135 Lily_lexer::~Lily_lexer ()
137 delete keytable_;
140 void
141 Lily_lexer::add_scope (SCM module)
143 ly_reexport_module (scm_current_module ());
144 if (!scm_is_pair (scopes_))
145 start_module_ = scm_current_module ();
147 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
148 ly_use_module (module, scm_car (s));
149 scopes_ = scm_cons (module, scopes_);
151 set_current_scope ();
153 bool
154 Lily_lexer::has_scope () const
156 return scm_is_pair (scopes_);
160 Lily_lexer::remove_scope ()
162 SCM sc = scm_car (scopes_);
163 scopes_ = scm_cdr (scopes_);
164 set_current_scope ();
165 return sc;
169 Lily_lexer::set_current_scope ()
171 SCM old = scm_current_module ();
173 if (scm_is_pair (scopes_))
174 scm_set_current_module (scm_car (scopes_));
175 else
176 scm_set_current_module (start_module_);
178 return old;
182 Lily_lexer::lookup_keyword (string s)
184 return keytable_->lookup (s.c_str ());
188 Lily_lexer::keyword_list () const
190 if (!keytable_)
191 return SCM_EOL;
193 SCM l = SCM_EOL;
194 SCM *tail = &l;
195 for (vsize i = 0; i < keytable_->table_.size (); i++)
197 *tail = scm_acons (scm_from_locale_string (keytable_->table_[i].name_),
198 scm_from_int (keytable_->table_[i].tokcode_),
199 SCM_EOL);
201 tail = SCM_CDRLOC (*tail);
204 return l;
208 Lily_lexer::lookup_identifier_symbol (SCM sym)
210 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
212 SCM var = ly_module_lookup (scm_car (s), sym);
213 if (var != SCM_BOOL_F)
214 return scm_variable_ref (var);
217 return SCM_UNDEFINED;
221 Lily_lexer::lookup_identifier (string name)
223 return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
226 void
227 Lily_lexer::start_main_input ()
229 yy_flex_debug = get_program_option ("debug-lexer");
230 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_));
240 void
241 Lily_lexer::new_input (string str, string d, Sources *ss)
243 Includable_lexer::new_input (str, d, ss);
246 void
247 Lily_lexer::new_input (string str, Sources *ss)
249 if (is_main_input_ && be_safe_global)
251 LexerError (_ ("include files are not allowed in safe mode").c_str ());
252 return;
255 Includable_lexer::new_input (str, ss);
258 void
259 Lily_lexer::set_identifier (SCM name, SCM s)
261 SCM sym = name;
262 if (scm_is_string (name))
263 sym = scm_string_to_symbol (name);
265 if (scm_is_symbol (sym))
267 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
269 string symstr = ly_symbol2string (sym);
270 warning (_f ("identifier name is a keyword: `%s'", symstr.c_str ()));
273 SCM mod = scm_car (scopes_);
275 scm_module_define (mod, sym, s);
277 else
278 programming_error ("identifier is not a symbol");
281 void
282 Lily_lexer::LexerError (char const *s)
284 if (include_stack_.empty ())
285 message (_f ("error at EOF: %s", s) + "\n");
286 else
288 error_level_ |= 1;
289 Input spot (*lexloc);
290 spot.error (s);
294 char
295 Lily_lexer::escaped_char (char c) const
297 switch (c)
299 case 'n':
300 return '\n';
301 case 't':
302 return '\t';
303 case '\'':
304 case '\"':
305 case '\\':
306 return c;
308 return 0;
311 Input
312 Lily_lexer::here_input () const
314 return Input (*lexloc);
317 void
318 Lily_lexer::prepare_for_next_token ()
320 last_input_ = here_input ();
324 Since we don't create the buffer state from the bytes directly, we
325 don't know about the location of the lexer. Add this as a
326 YY_USER_ACTION */
327 void
328 Lily_lexer::add_lexed_char (int count)
330 char const *start = here_str0 ();
331 lexloc->set (get_source_file (),
332 start, start + count);
333 char_count_stack_.back () += count;
336 #include "ly-smobs.icc"
338 IMPLEMENT_SMOBS (Lily_lexer);
339 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
340 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
343 Lily_lexer::mark_smob (SCM s)
345 ASSERT_LIVE_IS_ALLOWED ();
347 Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
349 scm_gc_mark (lexer->chordmodifier_tab_);
350 if (lexer->parser_)
351 scm_gc_mark (lexer->parser_->self_scm ());
352 scm_gc_mark (lexer->pitchname_tab_stack_);
353 scm_gc_mark (lexer->start_module_);
354 return lexer->scopes_;
358 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
360 Lily_lexer *lexer = Lily_lexer::unsmob (s);
362 scm_puts ("#<Lily_lexer ", port);
363 scm_display (lexer->scopes_, port);
364 scm_puts (" >", port);
365 return 1;