]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-lexer.cc
Fix some bugs in the dynamic engraver and PostScript backend
[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 "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
25 static Keyword_ent the_key_tab[]
26 = {
27   {"accepts", ACCEPTS},
28   {"addlyrics", ADDLYRICS},
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   {"once", ONCE},
60   {"override", OVERRIDE},
61   {"paper", PAPER},
62   {"partial", PARTIAL},
63   {"relative", RELATIVE},
64   {"remove", REMOVE},
65   {"repeat", REPEAT},
66   {"rest", REST},
67   {"revert", REVERT},
68   {"score", SCORE},
69   {"sequential", SEQUENTIAL},
70   {"set", SET},
71   {"simultaneous", SIMULTANEOUS},
72   {"skip", SKIP},
73   {"tempo", TEMPO},
74   {"time", TIME_T},
75   {"times", TIMES},
76   {"transpose", TRANSPOSE},
77   {"transposition", TRANSPOSITION},
78   {"type", TYPE},
79   {"unset", UNSET},
80   {"with", WITH},
81   {0, 0}
82 };
83
84 Lily_lexer::Lily_lexer (Sources *sources)
85 {
86   keytable_ = new Keyword_table (the_key_tab);
87   chordmodifier_tab_ = SCM_EOL;
88   pitchname_tab_stack_ = SCM_EOL;
89   sources_ = sources;
90   scopes_ = SCM_EOL;
91   error_level_ = 0;
92   is_main_input_ = false;
93   start_module_ = SCM_EOL;
94   smobify_self ();
95
96   add_scope (ly_make_anonymous_module (false));
97   push_note_state (scm_c_make_hash_table (0));
98   chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
99 }
100
101 Lily_lexer::Lily_lexer (Lily_lexer const &src)
102   : Includable_lexer ()
103 {
104   keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
105   chordmodifier_tab_ = src.chordmodifier_tab_;
106   pitchname_tab_stack_ = src.pitchname_tab_stack_;
107   sources_ = src.sources_;
108   start_module_ = SCM_EOL;
109
110   error_level_ = src.error_level_;
111   is_main_input_ = src.is_main_input_;
112
113   scopes_ = SCM_EOL;
114
115   smobify_self ();
116
117   SCM scopes = SCM_EOL;
118   SCM *tail = &scopes;
119   for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
120     {
121       SCM newmod = ly_make_anonymous_module (false);
122       ly_module_copy (newmod, scm_car (s));
123       *tail = scm_cons (newmod, SCM_EOL);
124       tail = SCM_CDRLOC (*tail);
125     }
126
127   scopes_ = scopes;
128   push_note_state (scm_c_make_hash_table (0));
129 }
130
131 Lily_lexer::~Lily_lexer ()
132 {
133   delete keytable_;
134 }
135
136 void
137 Lily_lexer::add_scope (SCM module)
138 {
139   ly_reexport_module (scm_current_module ());
140   if (!scm_is_pair (scopes_))
141     start_module_ = scm_current_module ();
142
143   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
144     ly_use_module (module, scm_car (s));
145   scopes_ = scm_cons (module, scopes_);
146
147   set_current_scope ();
148 }
149
150 SCM
151 Lily_lexer::remove_scope ()
152 {
153   SCM sc = scm_car (scopes_);
154   scopes_ = scm_cdr (scopes_);
155   set_current_scope ();
156   return sc;
157 }
158
159 SCM
160 Lily_lexer::set_current_scope ()
161 {
162   SCM old = scm_current_module ();
163
164   if (scm_is_pair (scopes_))
165     scm_set_current_module (scm_car (scopes_));
166   else
167     scm_set_current_module (start_module_);
168
169   return old;
170 }
171
172 int
173 Lily_lexer::lookup_keyword (string s)
174 {
175   return keytable_->lookup (s.c_str ());
176 }
177
178 SCM
179 Lily_lexer::lookup_identifier_symbol (SCM sym)
180 {
181   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
182     {
183       SCM var = ly_module_lookup (scm_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.c_str ()));
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_ && !be_safe_global;
205
206   scm_module_define (scm_car (scopes_),
207                      ly_symbol2scm ("input-file-name"),
208                      scm_makfrom0str (main_input_name_.c_str ()));
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.c_str ()));
224         }
225
226       SCM mod = scm_car (scopes_);
227
228       scm_module_define (mod, sym, s);
229     }
230   else
231     programming_error ("identifier is not a symbol");
232 }
233
234 void
235 Lily_lexer::LexerError (char const *s)
236 {
237   if (include_stack_.empty ())
238     message (_f ("error at EOF: %s", s) + "\n");
239   else
240     {
241       error_level_ |= 1;
242       Input spot (*lexloc);
243       spot.error (s);
244     }
245 }
246
247 char
248 Lily_lexer::escaped_char (char c) const
249 {
250   switch (c)
251     {
252     case 'n':
253       return '\n';
254     case 't':
255       return '\t';
256     case '\'':
257     case '\"':
258     case '\\':
259       return c;
260     }
261   return 0;
262 }
263
264 Input
265 Lily_lexer::here_input () const
266 {
267   return Input (*lexloc);
268 }
269
270 void
271 Lily_lexer::prepare_for_next_token ()
272 {
273   last_input_ = here_input ();
274 }
275
276 /**
277    Since we don't create the buffer state from the bytes directly, we
278    don't know about the location of the lexer. Add this as a
279    YY_USER_ACTION */
280 void
281 Lily_lexer::add_lexed_char (int count)
282 {
283   char const *start = here_str0 ();
284   lexloc->set (get_source_file (),
285                start, start + count);
286   char_count_stack_.back () += count;
287 }
288
289 #include "ly-smobs.icc"
290
291 IMPLEMENT_SMOBS (Lily_lexer);
292 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
293 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
294
295 SCM
296 Lily_lexer::mark_smob (SCM s)
297 {
298   Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
299
300   scm_gc_mark (lexer->chordmodifier_tab_);
301   scm_gc_mark (lexer->pitchname_tab_stack_);
302   scm_gc_mark (lexer->start_module_);
303   return lexer->scopes_;
304 }
305
306 int
307 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
308 {
309   Lily_lexer *lexer = Lily_lexer::unsmob (s);
310
311   scm_puts ("#<Lily_lexer ", port);
312   scm_display (lexer->scopes_, port);
313   scm_puts (" >", port);
314   return 1;
315 }