]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-lexer.cc
Breakable markups with \markuplines.
[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--2007 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 #include "program-option.hh"
25 #include "lily-parser.hh"
26
27 static Keyword_ent the_key_tab[]
28 = {
29   {"accepts", ACCEPTS},
30   {"addlyrics", ADDLYRICS},
31   {"alias", ALIAS},
32   {"alternative", ALTERNATIVE},
33   {"book", BOOK},
34   {"change", CHANGE},
35   {"chordmode", CHORDMODE},
36   {"chords", CHORDS},
37   {"consists", CONSISTS},
38   {"context", CONTEXT},
39   {"default", DEFAULT},
40   {"defaultchild", DEFAULTCHILD},
41   {"denies", DENIES},
42   {"description", DESCRIPTION},
43   {"drummode", DRUMMODE},
44   {"drums", DRUMS},
45   {"figuremode", FIGUREMODE},
46   {"figures", FIGURES},
47   {"grobdescriptions", GROBDESCRIPTIONS},
48   {"header", HEADER},
49   {"key", KEY},
50   {"layout", LAYOUT},
51   {"lyricmode", LYRICMODE},
52   {"lyrics", LYRICS},
53   {"lyricsto", LYRICSTO},
54   {"mark", MARK},
55   {"markup", MARKUP},
56   {"markuplines", MARKUPLINES},
57   {"midi", MIDI},
58   {"name", NAME},
59   {"new", NEWCONTEXT},
60   {"notemode", NOTEMODE},
61   {"objectid", OBJECTID},
62   {"once", ONCE},
63   {"override", OVERRIDE},
64   {"paper", PAPER},
65   {"partial", PARTIAL},
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   {"tempo", TEMPO},
77   {"time", TIME_T},
78   {"times", TIMES},
79   {"transpose", TRANSPOSE},
80   {"type", TYPE},
81   {"unset", UNSET},
82   {"with", WITH},
83   {0, 0}
84 };
85
86 Lily_lexer::Lily_lexer (Sources *sources, Lily_parser *parser)
87 {
88   parser_ = parser;
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, Lily_parser *parser)
105   : Includable_lexer ()
106 {
107   parser_ = parser; 
108   keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
109   chordmodifier_tab_ = src.chordmodifier_tab_;
110   pitchname_tab_stack_ = src.pitchname_tab_stack_;
111   sources_ = src.sources_;
112   start_module_ = SCM_EOL;
113
114   error_level_ = src.error_level_;
115   is_main_input_ = src.is_main_input_;
116
117   scopes_ = SCM_EOL;
118
119   smobify_self ();
120
121   SCM scopes = SCM_EOL;
122   SCM *tail = &scopes;
123   for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
124     {
125       SCM newmod = ly_make_anonymous_module (false);
126       ly_module_copy (newmod, scm_car (s));
127       *tail = scm_cons (newmod, SCM_EOL);
128       tail = SCM_CDRLOC (*tail);
129     }
130
131   scopes_ = scopes;
132   push_note_state (scm_c_make_hash_table (0));
133 }
134
135 Lily_lexer::~Lily_lexer ()
136 {
137   delete keytable_;
138 }
139
140 void
141 Lily_lexer::add_scope (SCM module)
142 {
143   ly_reexport_module (scm_current_module ());
144   if (!scm_is_pair (scopes_))
145     start_module_ = scm_current_module ();
146
147   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
148     ly_use_module (module, scm_car (s));
149   scopes_ = scm_cons (module, scopes_);
150
151   set_current_scope ();
152 }
153 bool
154 Lily_lexer::has_scope () const
155 {
156   return scm_is_pair (scopes_);
157 }
158
159 SCM
160 Lily_lexer::remove_scope ()
161 {
162   SCM sc = scm_car (scopes_);
163   scopes_ = scm_cdr (scopes_);
164   set_current_scope ();
165   return sc;
166 }
167
168 SCM
169 Lily_lexer::set_current_scope ()
170 {
171   SCM old = scm_current_module ();
172
173   if (scm_is_pair (scopes_))
174     scm_set_current_module (scm_car (scopes_));
175   else
176     scm_set_current_module (start_module_);
177
178   return old;
179 }
180
181 int
182 Lily_lexer::lookup_keyword (string s)
183 {
184   return keytable_->lookup (s.c_str ());
185 }
186
187 SCM
188 Lily_lexer::keyword_list () const
189 {
190   if (!keytable_)
191     return SCM_EOL;
192   
193   SCM l = SCM_EOL;
194   SCM *tail = &l;
195   for (vsize i = 0; i < keytable_->table_.size (); i++)
196     {
197       *tail = scm_acons (scm_from_locale_string (keytable_->table_[i].name_),
198                          scm_from_int (keytable_->table_[i].tokcode_),
199                          SCM_EOL);
200
201       tail = SCM_CDRLOC (*tail);
202     }
203
204   return l;
205 }
206
207 SCM
208 Lily_lexer::lookup_identifier_symbol (SCM sym)
209 {
210   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
211     {
212       SCM var = ly_module_lookup (scm_car (s), sym);
213       if (var != SCM_BOOL_F)
214         return scm_variable_ref (var);
215     }
216
217   return SCM_UNDEFINED;
218 }
219
220 SCM
221 Lily_lexer::lookup_identifier (string name)
222 {
223   return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
224 }
225
226 void
227 Lily_lexer::start_main_input ()
228 {
229   yy_flex_debug = get_program_option ("debug-lexer");
230   parser_->set_yydebug (get_program_option ("debug-parser"));
231
232   
233   new_input (main_input_name_, sources_);
234
235   scm_module_define (scm_car (scopes_),
236                      ly_symbol2scm ("input-file-name"),
237                      ly_string2scm (main_input_name_));
238 }
239
240 void
241 Lily_lexer::new_input (string str, string d, Sources *ss)
242 {
243   Includable_lexer::new_input (str, d, ss);
244 }
245
246 void
247 Lily_lexer::new_input (string str, Sources *ss)
248 {
249   if (is_main_input_ && be_safe_global)
250     {
251       LexerError (_ ("include files are not allowed in safe mode").c_str ());
252       return;
253     }
254
255   Includable_lexer::new_input (str, ss);
256 }
257
258 void
259 Lily_lexer::set_identifier (SCM name, SCM s)
260 {
261   SCM sym = name;
262   if (scm_is_string (name))
263     sym = scm_string_to_symbol (name);
264
265   if (scm_is_symbol (sym))
266     {
267       if (lookup_keyword (ly_symbol2string (sym)) >= 0)
268         {
269           string symstr = ly_symbol2string (sym);
270           warning (_f ("identifier name is a keyword: `%s'", symstr.c_str ()));
271         }
272
273       SCM mod = scm_car (scopes_);
274
275       scm_module_define (mod, sym, s);
276     }
277   else
278     programming_error ("identifier is not a symbol");
279 }
280
281 void
282 Lily_lexer::LexerError (char const *s)
283 {
284   if (include_stack_.empty ())
285     message (_f ("error at EOF: %s", s) + "\n");
286   else
287     {
288       error_level_ |= 1;
289       Input spot (*lexloc);
290       spot.error (s);
291     }
292 }
293
294 char
295 Lily_lexer::escaped_char (char c) const
296 {
297   switch (c)
298     {
299     case 'n':
300       return '\n';
301     case 't':
302       return '\t';
303     case '\'':
304     case '\"':
305     case '\\':
306       return c;
307     }
308   return 0;
309 }
310
311 Input
312 Lily_lexer::here_input () const
313 {
314   return Input (*lexloc);
315 }
316
317 void
318 Lily_lexer::prepare_for_next_token ()
319 {
320   last_input_ = here_input ();
321 }
322
323 /**
324    Since we don't create the buffer state from the bytes directly, we
325    don't know about the location of the lexer. Add this as a
326    YY_USER_ACTION */
327 void
328 Lily_lexer::add_lexed_char (int count)
329 {
330   char const *start = here_str0 ();
331   lexloc->set (get_source_file (),
332                start, start + count);
333   char_count_stack_.back () += count;
334 }
335
336 #include "ly-smobs.icc"
337
338 IMPLEMENT_SMOBS (Lily_lexer);
339 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
340 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
341
342 SCM
343 Lily_lexer::mark_smob (SCM s)
344 {
345   ASSERT_LIVE_IS_ALLOWED ();
346   
347   Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
348
349   scm_gc_mark (lexer->chordmodifier_tab_);
350   if (lexer->parser_)
351     scm_gc_mark (lexer->parser_->self_scm ());
352   scm_gc_mark (lexer->pitchname_tab_stack_);
353   scm_gc_mark (lexer->start_module_);
354   return lexer->scopes_;
355 }
356
357 int
358 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
359 {
360   Lily_lexer *lexer = Lily_lexer::unsmob (s);
361
362   scm_puts ("#<Lily_lexer ", port);
363   scm_display (lexer->scopes_, port);
364   scm_puts (" >", port);
365   return 1;
366 }