]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-lexer.cc
* scripts/convert-ly.py (conv): add conversion.
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <ctype.h>
10 #include <sstream>
11
12 #include "lily-proto.hh"
13 #include "scm-hash.hh"
14 #include "interval.hh"
15 #include "lily-guile.hh"
16 #include "parser.hh"
17 #include "keyword.hh"
18 #include "lily-lexer.hh"
19 #include "warn.hh"
20 #include "source-file.hh"
21 #include "main.hh"
22 #include "input.hh"
23 #include "moment.hh"
24 #include "ly-module.hh"
25
26
27 static Keyword_ent the_key_tab[] = {
28   {"accepts", ACCEPTS},
29   {"addquote", ADDQUOTE},
30   {"addlyrics", ADDLYRICS},
31   {"alias", ALIAS},
32   {"alternative", ALTERNATIVE},
33   {"bar", BAR},
34   {"book", BOOK},
35   {"bookpaper", BOOKPAPER},
36   {"change", CHANGE},
37   {"chords", CHORDS},
38   {"chordmode", CHORDMODE},
39   {"clef", CLEF},
40   {"consists", CONSISTS},
41   {"context", CONTEXT},
42   {"default", DEFAULT},
43   {"denies", DENIES},
44   {"drummode", DRUMMODE},
45   {"drums", DRUMS},
46   {"description", DESCRIPTION},
47   {"figures",FIGURES},
48   {"figuremode",FIGUREMODE},
49   {"grobdescriptions", GROBDESCRIPTIONS},
50   {"header", HEADER},
51   {"key", KEY},
52   {"lyricmode", LYRICMODE},
53   {"lyricsto", LYRICSTO},
54   {"mark", MARK},
55   {"markup", MARKUP},
56   {"midi", MIDI},
57   {"name", NAME},
58   {"new", NEWCONTEXT},
59   {"notemode", NOTEMODE},
60   {"octave", OCTAVE},
61   {"once", ONCE},
62   {"override", OVERRIDE},
63   {"paper", PAPER},
64   {"partial", PARTIAL},
65   {"quote", QUOTE},
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   {"tag", TAG},
77   {"tempo", TEMPO},
78   {"time", TIME_T},
79   {"times", TIMES},
80   {"transpose", TRANSPOSE},
81   {"transposition", TRANSPOSITION},
82   {"type", TYPE},
83   {"unset", UNSET},
84   {"with", WITH},
85   {0, 0}
86 };
87
88
89 Lily_lexer::Lily_lexer (Sources *sources)
90 {
91   keytable_ = new Keyword_table (the_key_tab);
92   encoding_ = SCM_EOL;
93   chordmodifier_tab_ = SCM_EOL;
94   pitchname_tab_stack_ = SCM_EOL; 
95   sources_ = sources;
96   scopes_ = SCM_EOL;
97   error_level_ = 0; 
98   main_input_b_ = false;
99
100   smobify_self ();
101   
102   add_scope (ly_make_anonymous_module (false));
103   push_note_state (scm_c_make_hash_table (0));
104   chordmodifier_tab_ = scm_make_vector (scm_int2num (1), SCM_EOL);
105 }
106
107 Lily_lexer::Lily_lexer (Lily_lexer const &src)
108   : Includable_lexer ()
109 {
110   keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
111   encoding_ = src.encoding_;
112   chordmodifier_tab_ = src.chordmodifier_tab_;
113   pitchname_tab_stack_ = src.pitchname_tab_stack_;
114   sources_ = src.sources_;
115   
116   error_level_ = src.error_level_; 
117   main_input_b_ = src.main_input_b_;
118
119   scopes_ = SCM_EOL;
120   
121   smobify_self ();
122   
123   SCM scopes = SCM_EOL;
124   SCM *tail = &scopes;
125   for (SCM s = src.scopes_; ly_c_pair_p (s); s = ly_cdr (s))
126     {
127       SCM newmod = ly_make_anonymous_module (false);
128       ly_import_module (newmod, ly_car (s));
129       *tail = scm_cons (newmod, SCM_EOL);
130       tail = SCM_CDRLOC (*tail);
131     }
132   
133   scopes_ =  scopes;
134   push_note_state (scm_c_make_hash_table (0));
135 }
136
137 Lily_lexer::~Lily_lexer ()
138 {
139   delete keytable_;
140 }
141
142 SCM
143 Lily_lexer::encoding () const
144 {
145   return encoding_ ;
146 }
147
148
149 void
150 Lily_lexer::add_scope (SCM module)
151 {
152   ly_reexport_module (scm_current_module ());
153   scm_set_current_module (module);
154   for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
155     {
156       ly_use_module (module, ly_car (s));
157     }
158   scopes_ = scm_cons (module, scopes_);
159 }
160
161 SCM
162 Lily_lexer::remove_scope ()
163 {
164   SCM sc = ly_car (scopes_);
165   scopes_ = ly_cdr (scopes_);
166   scm_set_current_module (ly_car (scopes_));
167
168   return sc;
169 }
170
171
172 int
173 Lily_lexer::lookup_keyword (String s)
174 {
175   return keytable_->lookup (s.to_str0 ());
176 }
177
178 SCM
179 Lily_lexer::lookup_identifier_symbol (SCM sym)
180 {
181   for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
182     {
183       SCM var = ly_module_lookup (ly_car (s), sym);
184       if (var != SCM_BOOL_F)
185         return scm_variable_ref (var);
186     }
187
188   return SCM_UNDEFINED;
189 }
190
191 SCM
192 Lily_lexer::lookup_identifier (String name)
193 {
194   return lookup_identifier_symbol (ly_symbol2scm (name.to_str0 ()));
195 }
196
197 void
198 Lily_lexer::start_main_input ()
199 {
200   // yy_flex_debug = 1;
201   new_input (main_input_name_, sources_);
202   
203   /* Do not allow \include in --safe-mode */
204   allow_includes_b_ = allow_includes_b_ && !safe_global_b;
205
206   scm_module_define (ly_car (scopes_),
207                      ly_symbol2scm ("input-file-name"),
208                      scm_makfrom0str (main_input_name_.to_str0 ()));
209 }
210
211 void
212 Lily_lexer::set_identifier (SCM name, SCM s)
213 {
214   SCM sym = name;
215   if (scm_is_string (name))
216     sym =  scm_string_to_symbol (name);
217   
218   if (scm_is_symbol (sym))
219     {
220       if (lookup_keyword (ly_symbol2string (sym)) >= 0)
221         {
222           String symstr = ly_symbol2string (sym); 
223           warning (_f ("Identifier name is a keyword: `%s'", symstr.to_str0()));
224         }
225
226       SCM mod = ly_car (scopes_);
227
228       scm_module_define (mod, sym, s);
229     }
230   else
231     {
232       programming_error ("Identifier is not a symbol.");
233     }
234 }
235
236 void
237 Lily_lexer::LexerError (char const *s)
238 {
239   if (include_stack_.is_empty ())
240     progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
241   else
242     {
243       error_level_ |= 1;
244       Input spot (get_source_file (), here_str0 ());
245       spot.error (s);
246     }
247 }
248
249 char
250 Lily_lexer::escaped_char (char c) const
251 {
252   switch (c)
253     {
254     case 'n':
255       return '\n';
256     case 't':
257       return '\t';
258     case '\'':
259     case '\"':
260     case '\\':
261       return c;
262     }
263   return 0;
264 }
265
266 Input
267 Lily_lexer::here_input () const
268 {
269   Source_file * f= get_source_file ();
270   return Input (f, (char*)here_str0 ());
271 }
272
273 void
274 Lily_lexer::prepare_for_next_token ()
275 {
276   last_input_ = here_input ();
277 }
278
279 void
280 Lily_lexer::set_encoding (String s)
281 {
282   if (s.length ())
283     encoding_ = ly_symbol2scm (s.to_str0 ());
284   else
285     encoding_ = SCM_EOL;
286 }
287
288 #include "ly-smobs.icc"
289
290 IMPLEMENT_SMOBS (Lily_lexer);
291 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
292 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
293
294 SCM
295 Lily_lexer::mark_smob (SCM s)
296 {
297   Lily_lexer *lexer = (Lily_lexer*) ly_cdr (s);
298
299   scm_gc_mark (lexer->chordmodifier_tab_);
300   scm_gc_mark (lexer->pitchname_tab_stack_);
301   scm_gc_mark (lexer->scopes_);
302   return lexer->encoding_;
303 }
304
305 int
306 Lily_lexer::print_smob (SCM, SCM port, scm_print_state*)
307 {
308   scm_puts ("#<Lily_lexer ", port);
309   scm_puts (" >", port);
310   return 1;
311 }