]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-lexer.cc
ef579dc7f05fdefb2bb7a258c0e5fdf60594ba67
[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 #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   {"midi", MIDI},
57   {"name", NAME},
58   {"new", NEWCONTEXT},
59   {"notemode", NOTEMODE},
60   {"objectid", OBJECTID},
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   {"tempo", TEMPO},
76   {"time", TIME_T},
77   {"times", TIMES},
78   {"transpose", TRANSPOSE},
79   {"type", TYPE},
80   {"unset", UNSET},
81   {"with", WITH},
82   {0, 0}
83 };
84
85 Lily_lexer::Lily_lexer (Sources *sources, Lily_parser *parser)
86 {
87   parser_ = parser;
88   keytable_ = new Keyword_table (the_key_tab);
89   chordmodifier_tab_ = SCM_EOL;
90   pitchname_tab_stack_ = SCM_EOL;
91   sources_ = sources;
92   scopes_ = SCM_EOL;
93   error_level_ = 0;
94   is_main_input_ = false;
95   start_module_ = SCM_EOL;
96   smobify_self ();
97
98   add_scope (ly_make_anonymous_module (false));
99   push_note_state (scm_c_make_hash_table (0));
100   chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
101 }
102
103 Lily_lexer::Lily_lexer (Lily_lexer const &src)
104   : Includable_lexer ()
105 {
106   keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
107   chordmodifier_tab_ = src.chordmodifier_tab_;
108   pitchname_tab_stack_ = src.pitchname_tab_stack_;
109   sources_ = src.sources_;
110   start_module_ = SCM_EOL;
111
112   error_level_ = src.error_level_;
113   is_main_input_ = src.is_main_input_;
114
115   scopes_ = SCM_EOL;
116
117   smobify_self ();
118
119   SCM scopes = SCM_EOL;
120   SCM *tail = &scopes;
121   for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
122     {
123       SCM newmod = ly_make_anonymous_module (false);
124       ly_module_copy (newmod, scm_car (s));
125       *tail = scm_cons (newmod, SCM_EOL);
126       tail = SCM_CDRLOC (*tail);
127     }
128
129   scopes_ = scopes;
130   push_note_state (scm_c_make_hash_table (0));
131 }
132
133 Lily_lexer::~Lily_lexer ()
134 {
135   delete keytable_;
136 }
137
138 void
139 Lily_lexer::add_scope (SCM module)
140 {
141   ly_reexport_module (scm_current_module ());
142   if (!scm_is_pair (scopes_))
143     start_module_ = scm_current_module ();
144
145   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
146     ly_use_module (module, scm_car (s));
147   scopes_ = scm_cons (module, scopes_);
148
149   set_current_scope ();
150 }
151
152 SCM
153 Lily_lexer::remove_scope ()
154 {
155   SCM sc = scm_car (scopes_);
156   scopes_ = scm_cdr (scopes_);
157   set_current_scope ();
158   return sc;
159 }
160
161 SCM
162 Lily_lexer::set_current_scope ()
163 {
164   SCM old = scm_current_module ();
165
166   if (scm_is_pair (scopes_))
167     scm_set_current_module (scm_car (scopes_));
168   else
169     scm_set_current_module (start_module_);
170
171   return old;
172 }
173
174 int
175 Lily_lexer::lookup_keyword (string s)
176 {
177   return keytable_->lookup (s.c_str ());
178 }
179
180 SCM
181 Lily_lexer::lookup_identifier_symbol (SCM sym)
182 {
183   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
184     {
185       SCM var = ly_module_lookup (scm_car (s), sym);
186       if (var != SCM_BOOL_F)
187         return scm_variable_ref (var);
188     }
189
190   return SCM_UNDEFINED;
191 }
192
193 SCM
194 Lily_lexer::lookup_identifier (string name)
195 {
196   return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
197 }
198
199 void
200 Lily_lexer::start_main_input ()
201 {
202   yy_flex_debug = get_program_option ("debug-lexer");
203   parser_->set_yydebug (get_program_option ("debug-parser"));
204
205   
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_.c_str ()));
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.c_str ()));
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_.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_.back () += 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   if (lexer->parser_)
307     scm_gc_mark (lexer->parser_->self_scm ());
308   scm_gc_mark (lexer->pitchname_tab_stack_);
309   scm_gc_mark (lexer->start_module_);
310   return lexer->scopes_;
311 }
312
313 int
314 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
315 {
316   Lily_lexer *lexer = Lily_lexer::unsmob (s);
317
318   scm_puts ("#<Lily_lexer ", port);
319   scm_display (lexer->scopes_, port);
320   scm_puts (" >", port);
321   return 1;
322 }