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