Doc -- compile fix
[lilypond/mpolesky.git] / lily / lily-lexer.cc
blob0adee98fec480614e90665289bb05a40897bb352
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2009 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"
22 #include <cctype>
23 #include <sstream>
24 using namespace std;
26 #include "international.hh"
27 #include "interval.hh"
28 #include "keyword.hh"
29 #include "main.hh"
30 #include "moment.hh"
31 #include "parser.hh"
32 #include "scm-hash.hh"
33 #include "source-file.hh"
34 #include "warn.hh"
35 #include "program-option.hh"
36 #include "lily-parser.hh"
38 static Keyword_ent the_key_tab[]
39 = {
40 {"accepts", ACCEPTS},
41 {"addlyrics", ADDLYRICS},
42 {"alias", ALIAS},
43 {"alternative", ALTERNATIVE},
44 {"book", BOOK},
45 {"bookpart", BOOKPART},
46 {"change", CHANGE},
47 {"chordmode", CHORDMODE},
48 {"chords", CHORDS},
49 {"consists", CONSISTS},
50 {"context", CONTEXT},
51 {"default", DEFAULT},
52 {"defaultchild", DEFAULTCHILD},
53 {"denies", DENIES},
54 {"description", DESCRIPTION},
55 {"drummode", DRUMMODE},
56 {"drums", DRUMS},
57 {"figuremode", FIGUREMODE},
58 {"figures", FIGURES},
59 {"grobdescriptions", GROBDESCRIPTIONS},
60 {"header", HEADER},
61 {"key", KEY},
62 {"layout", LAYOUT},
63 {"lyricmode", LYRICMODE},
64 {"lyrics", LYRICS},
65 {"lyricsto", LYRICSTO},
66 {"mark", MARK},
67 {"markup", MARKUP},
68 {"markuplines", MARKUPLINES},
69 {"midi", MIDI},
70 {"name", NAME},
71 {"new", NEWCONTEXT},
72 {"notemode", NOTEMODE},
73 {"objectid", OBJECTID},
74 {"once", ONCE},
75 {"override", OVERRIDE},
76 {"paper", PAPER},
77 {"partial", PARTIAL},
78 {"relative", RELATIVE},
79 {"remove", REMOVE},
80 {"repeat", REPEAT},
81 {"rest", REST},
82 {"revert", REVERT},
83 {"score", SCORE},
84 {"sequential", SEQUENTIAL},
85 {"set", SET},
86 {"simultaneous", SIMULTANEOUS},
87 {"skip", SKIP},
88 {"tempo", TEMPO},
89 {"time", TIME_T},
90 {"times", TIMES},
91 {"transpose", TRANSPOSE},
92 {"type", TYPE},
93 {"unset", UNSET},
94 {"with", WITH},
95 {0, 0}
98 Lily_lexer::Lily_lexer (Sources *sources, Lily_parser *parser)
100 parser_ = parser;
101 keytable_ = new Keyword_table (the_key_tab);
102 chordmodifier_tab_ = SCM_EOL;
103 pitchname_tab_stack_ = SCM_EOL;
104 sources_ = sources;
105 scopes_ = SCM_EOL;
106 error_level_ = 0;
107 is_main_input_ = false;
108 start_module_ = SCM_EOL;
109 chord_repetition_ = Chord_repetition ();
110 smobify_self ();
112 add_scope (ly_make_anonymous_module (false));
113 push_note_state (scm_c_make_hash_table (0));
114 chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
117 Lily_lexer::Lily_lexer (Lily_lexer const &src, Lily_parser *parser)
118 : Includable_lexer ()
120 parser_ = parser;
121 keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
122 chordmodifier_tab_ = src.chordmodifier_tab_;
123 pitchname_tab_stack_ = src.pitchname_tab_stack_;
124 sources_ = src.sources_;
125 start_module_ = SCM_EOL;
126 chord_repetition_ = src.chord_repetition_;
128 error_level_ = src.error_level_;
129 is_main_input_ = src.is_main_input_;
131 scopes_ = SCM_EOL;
133 smobify_self ();
135 SCM scopes = SCM_EOL;
136 SCM *tail = &scopes;
137 for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
139 SCM newmod = ly_make_anonymous_module (false);
140 ly_module_copy (newmod, scm_car (s));
141 *tail = scm_cons (newmod, SCM_EOL);
142 tail = SCM_CDRLOC (*tail);
145 scopes_ = scopes;
146 push_note_state (scm_c_make_hash_table (0));
149 Lily_lexer::~Lily_lexer ()
151 delete keytable_;
154 void
155 Lily_lexer::add_scope (SCM module)
157 ly_reexport_module (scm_current_module ());
158 if (!scm_is_pair (scopes_))
159 start_module_ = scm_current_module ();
161 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
162 ly_use_module (module, scm_car (s));
163 scopes_ = scm_cons (module, scopes_);
165 set_current_scope ();
167 bool
168 Lily_lexer::has_scope () const
170 return scm_is_pair (scopes_);
174 Lily_lexer::remove_scope ()
176 SCM sc = scm_car (scopes_);
177 scopes_ = scm_cdr (scopes_);
178 set_current_scope ();
179 return sc;
183 Lily_lexer::set_current_scope ()
185 SCM old = scm_current_module ();
187 if (scm_is_pair (scopes_))
188 scm_set_current_module (scm_car (scopes_));
189 else
190 scm_set_current_module (start_module_);
192 return old;
196 Lily_lexer::lookup_keyword (string s)
198 return keytable_->lookup (s.c_str ());
202 Lily_lexer::keyword_list () const
204 if (!keytable_)
205 return SCM_EOL;
207 SCM l = SCM_EOL;
208 SCM *tail = &l;
209 for (vsize i = 0; i < keytable_->table_.size (); i++)
211 *tail = scm_acons (scm_from_locale_string (keytable_->table_[i].name_),
212 scm_from_int (keytable_->table_[i].tokcode_),
213 SCM_EOL);
215 tail = SCM_CDRLOC (*tail);
218 return l;
222 Lily_lexer::lookup_identifier_symbol (SCM sym)
224 for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
226 SCM var = ly_module_lookup (scm_car (s), sym);
227 if (var != SCM_BOOL_F)
228 return scm_variable_ref (var);
231 return SCM_UNDEFINED;
235 Lily_lexer::lookup_identifier (string name)
237 return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
240 void
241 Lily_lexer::start_main_input ()
243 yy_flex_debug = get_program_option ("debug-lexer");
244 parser_->set_yydebug (get_program_option ("debug-parser"));
247 new_input (main_input_name_, sources_);
249 scm_module_define (scm_car (scopes_),
250 ly_symbol2scm ("input-file-name"),
251 ly_string2scm (main_input_name_));
254 void
255 Lily_lexer::new_input (string str, string d, Sources *ss)
257 Includable_lexer::new_input (str, d, ss);
260 void
261 Lily_lexer::new_input (string str, Sources *ss)
263 if (is_main_input_ && be_safe_global)
265 LexerError (_ ("include files are not allowed in safe mode").c_str ());
266 return;
269 Includable_lexer::new_input (str, ss);
272 void
273 Lily_lexer::set_identifier (SCM name, SCM s)
275 SCM sym = name;
276 if (scm_is_string (name))
277 sym = scm_string_to_symbol (name);
279 if (scm_is_symbol (sym))
281 if (lookup_keyword (ly_symbol2string (sym)) >= 0)
283 string symstr = ly_symbol2string (sym);
284 warning (_f ("identifier name is a keyword: `%s'", symstr.c_str ()));
287 SCM mod = scm_car (scopes_);
289 scm_module_define (mod, sym, s);
291 else
292 programming_error ("identifier is not a symbol");
295 void
296 Lily_lexer::LexerError (char const *s)
298 if (include_stack_.empty ())
299 message (_f ("error at EOF: %s", s) + "\n");
300 else
302 error_level_ |= 1;
303 Input spot (*lexloc);
304 spot.error (s);
308 char
309 Lily_lexer::escaped_char (char c) const
311 switch (c)
313 case 'n':
314 return '\n';
315 case 't':
316 return '\t';
317 case '\'':
318 case '\"':
319 case '\\':
320 return c;
322 return 0;
325 Input
326 Lily_lexer::here_input () const
328 return Input (*lexloc);
331 void
332 Lily_lexer::prepare_for_next_token ()
334 last_input_ = here_input ();
338 Since we don't create the buffer state from the bytes directly, we
339 don't know about the location of the lexer. Add this as a
340 YY_USER_ACTION */
341 void
342 Lily_lexer::add_lexed_char (int count)
344 char const *start = here_str0 ();
345 lexloc->set (get_source_file (),
346 start, start + count);
347 char_count_stack_.back () += count;
350 #include "ly-smobs.icc"
352 IMPLEMENT_SMOBS (Lily_lexer);
353 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
354 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
357 Lily_lexer::mark_smob (SCM s)
359 ASSERT_LIVE_IS_ALLOWED ();
361 Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
363 scm_gc_mark (lexer->chordmodifier_tab_);
364 if (lexer->parser_)
365 scm_gc_mark (lexer->parser_->self_scm ());
366 scm_gc_mark (lexer->pitchname_tab_stack_);
367 scm_gc_mark (lexer->start_module_);
368 return lexer->scopes_;
372 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
374 Lily_lexer *lexer = Lily_lexer::unsmob (s);
376 scm_puts ("#<Lily_lexer ", port);
377 scm_display (lexer->scopes_, port);
378 scm_puts (" >", port);
379 return 1;