]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-lexer.cc
* The grand 2005-2006 replace.
[lilypond.git] / lily / lily-lexer.cc
1 /*
2   lily-lexer.cc -- implement Lily_lexer
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "lily-lexer.hh"
10
11 #include <cctype>
12 #include <sstream>
13 using namespace std;
14
15 #include "scm-hash.hh"
16 #include "interval.hh"
17 #include "parser.hh"
18 #include "keyword.hh"
19 #include "warn.hh"
20 #include "source-file.hh"
21 #include "main.hh"
22 #include "moment.hh"
23
24 static Keyword_ent the_key_tab[]
25 = {
26   {"accepts", ACCEPTS},
27   {"addlyrics", ADDLYRICS},
28   {"addquote", ADDQUOTE},
29   {"alias", ALIAS},
30   {"alternative", ALTERNATIVE},
31   {"book", BOOK},
32   {"change", CHANGE},
33   {"chordmode", CHORDMODE},
34   {"chords", CHORDS},
35   {"consists", CONSISTS},
36   {"context", CONTEXT},
37   {"default", DEFAULT},
38   {"defaultchild", DEFAULTCHILD},
39   {"denies", DENIES},
40   {"description", DESCRIPTION},
41   {"drummode", DRUMMODE},
42   {"drums", DRUMS},
43   {"figuremode", FIGUREMODE},
44   {"figures", FIGURES},
45   {"grobdescriptions", GROBDESCRIPTIONS},
46   {"header", HEADER},
47   {"key", KEY},
48   {"layout", LAYOUT},
49   {"lyricmode", LYRICMODE},
50   {"lyrics", LYRICS},
51   {"lyricsto", LYRICSTO},
52   {"mark", MARK},
53   {"markup", MARKUP},
54   {"midi", MIDI},
55   {"name", NAME},
56   {"new", NEWCONTEXT},
57   {"notemode", NOTEMODE},
58   {"objectid", OBJECTID},
59   {"octave", OCTAVE},
60   {"once", ONCE},
61   {"override", OVERRIDE},
62   {"paper", PAPER},
63   {"partial", PARTIAL},
64   {"relative", RELATIVE},
65   {"remove", REMOVE},
66   {"repeat", REPEAT},
67   {"rest", REST},
68   {"revert", REVERT},
69   {"score", SCORE},
70   {"sequential", SEQUENTIAL},
71   {"set", SET},
72   {"simultaneous", SIMULTANEOUS},
73   {"skip", SKIP},
74   {"tempo", TEMPO},
75   {"time", TIME_T},
76   {"times", TIMES},
77   {"transpose", TRANSPOSE},
78   {"transposition", TRANSPOSITION},
79   {"type", TYPE},
80   {"unset", UNSET},
81   {"with", WITH},
82   {0, 0}
83 };
84
85 Lily_lexer::Lily_lexer (Sources *sources)
86 {
87   keytable_ = new Keyword_table (the_key_tab);
88   chordmodifier_tab_ = SCM_EOL;
89   pitchname_tab_stack_ = SCM_EOL;
90   sources_ = sources;
91   scopes_ = SCM_EOL;
92   error_level_ = 0;
93   is_main_input_ = false;
94   start_module_ = SCM_EOL;
95   smobify_self ();
96
97   add_scope (ly_make_anonymous_module (false));
98   push_note_state (scm_c_make_hash_table (0));
99   chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
100 }
101
102 Lily_lexer::Lily_lexer (Lily_lexer const &src)
103   : Includable_lexer ()
104 {
105   keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
106   chordmodifier_tab_ = src.chordmodifier_tab_;
107   pitchname_tab_stack_ = src.pitchname_tab_stack_;
108   sources_ = src.sources_;
109   start_module_ = SCM_EOL;
110
111   error_level_ = src.error_level_;
112   is_main_input_ = src.is_main_input_;
113
114   scopes_ = SCM_EOL;
115
116   smobify_self ();
117
118   SCM scopes = SCM_EOL;
119   SCM *tail = &scopes;
120   for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
121     {
122       SCM newmod = ly_make_anonymous_module (false);
123       ly_module_copy (newmod, scm_car (s));
124       *tail = scm_cons (newmod, SCM_EOL);
125       tail = SCM_CDRLOC (*tail);
126     }
127
128   scopes_ = scopes;
129   push_note_state (scm_c_make_hash_table (0));
130 }
131
132 Lily_lexer::~Lily_lexer ()
133 {
134   delete keytable_;
135 }
136
137 void
138 Lily_lexer::add_scope (SCM module)
139 {
140   ly_reexport_module (scm_current_module ());
141   if (!scm_is_pair (scopes_))
142     start_module_ = scm_current_module ();
143
144   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
145     ly_use_module (module, scm_car (s));
146   scopes_ = scm_cons (module, scopes_);
147
148   set_current_scope ();
149 }
150
151 SCM
152 Lily_lexer::remove_scope ()
153 {
154   SCM sc = scm_car (scopes_);
155   scopes_ = scm_cdr (scopes_);
156   set_current_scope ();
157   return sc;
158 }
159
160 SCM
161 Lily_lexer::set_current_scope ()
162 {
163   SCM old = scm_current_module ();
164
165   if (scm_is_pair (scopes_))
166     scm_set_current_module (scm_car (scopes_));
167   else
168     scm_set_current_module (start_module_);
169
170   return old;
171 }
172
173 int
174 Lily_lexer::lookup_keyword (String s)
175 {
176   return keytable_->lookup (s.to_str0 ());
177 }
178
179 SCM
180 Lily_lexer::lookup_identifier_symbol (SCM sym)
181 {
182   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
183     {
184       SCM var = ly_module_lookup (scm_car (s), sym);
185       if (var != SCM_BOOL_F)
186         return scm_variable_ref (var);
187     }
188
189   return SCM_UNDEFINED;
190 }
191
192 SCM
193 Lily_lexer::lookup_identifier (String name)
194 {
195   return lookup_identifier_symbol (ly_symbol2scm (name.to_str0 ()));
196 }
197
198 void
199 Lily_lexer::start_main_input ()
200 {
201   // yy_flex_debug = 1;
202   new_input (main_input_name_, sources_);
203
204   /* Do not allow \include in --safe-mode */
205   allow_includes_b_ = allow_includes_b_ && !be_safe_global;
206
207   scm_module_define (scm_car (scopes_),
208                      ly_symbol2scm ("input-file-name"),
209                      scm_makfrom0str (main_input_name_.to_str0 ()));
210 }
211
212 void
213 Lily_lexer::set_identifier (SCM name, SCM s)
214 {
215   SCM sym = name;
216   if (scm_is_string (name))
217     sym = scm_string_to_symbol (name);
218
219   if (scm_is_symbol (sym))
220     {
221       if (lookup_keyword (ly_symbol2string (sym)) >= 0)
222         {
223           String symstr = ly_symbol2string (sym);
224           warning (_f ("identifier name is a keyword: `%s'", symstr.to_str0 ()));
225         }
226
227       SCM mod = scm_car (scopes_);
228
229       scm_module_define (mod, sym, s);
230     }
231   else
232     programming_error ("identifier is not a symbol");
233 }
234
235 void
236 Lily_lexer::LexerError (char const *s)
237 {
238   if (include_stack_.is_empty ())
239     message (_f ("error at EOF: %s", s) + "\n");
240   else
241     {
242       error_level_ |= 1;
243       Input spot (*lexloc);
244       spot.error (s);
245     }
246 }
247
248 char
249 Lily_lexer::escaped_char (char c) const
250 {
251   switch (c)
252     {
253     case 'n':
254       return '\n';
255     case 't':
256       return '\t';
257     case '\'':
258     case '\"':
259     case '\\':
260       return c;
261     }
262   return 0;
263 }
264
265 Input
266 Lily_lexer::here_input () const
267 {
268   return Input (*lexloc);
269 }
270
271 void
272 Lily_lexer::prepare_for_next_token ()
273 {
274   last_input_ = here_input ();
275 }
276
277 /**
278    Since we don't create the buffer state from the bytes directly, we
279    don't know about the location of the lexer. Add this as a
280    YY_USER_ACTION */
281 void
282 Lily_lexer::add_lexed_char (int count)
283 {
284   char const *start = here_str0 ();
285   lexloc->set (get_source_file (),
286                start, start + count);
287   char_count_stack_.top () += count;
288 }
289
290 #include "ly-smobs.icc"
291
292 IMPLEMENT_SMOBS (Lily_lexer);
293 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
294 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
295
296 SCM
297 Lily_lexer::mark_smob (SCM s)
298 {
299   Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
300
301   scm_gc_mark (lexer->chordmodifier_tab_);
302   scm_gc_mark (lexer->pitchname_tab_stack_);
303   scm_gc_mark (lexer->start_module_);
304   return lexer->scopes_;
305 }
306
307 int
308 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
309 {
310   Lily_lexer *lexer = Lily_lexer::unsmob (s);
311
312   scm_puts ("#<Lily_lexer ", port);
313   scm_display (lexer->scopes_, port);
314   scm_puts (" >", port);
315   return 1;
316 }