]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-lexer.cc
release commit
[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@xs4all.nl>
7 */
8
9 #include "lily-lexer.hh"
10
11 #include <cctype>
12 #include <sstream>
13 using namespace std;
14
15 #include "scm-hash.hh"
16 #include "interval.hh"
17 #include "parser.hh"
18 #include "keyword.hh"
19 #include "warn.hh"
20 #include "source-file.hh"
21 #include "main.hh"
22 #include "moment.hh"
23
24 static Keyword_ent the_key_tab[]
25 = {
26   {"accepts", ACCEPTS},
27   {"addlyrics", ADDLYRICS},
28   {"addquote", ADDQUOTE},
29   {"alias", ALIAS},
30   {"alternative", ALTERNATIVE},
31   {"bar", BAR},
32   {"book", BOOK},
33   {"change", CHANGE},
34   {"chordmode", CHORDMODE},
35   {"chords", CHORDS},
36   {"clef", CLEF},
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   {"octave", OCTAVE},
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   {"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 Lily_lexer::Lily_lexer (Sources *sources)
89 {
90   keytable_ = new Keyword_table (the_key_tab);
91   chordmodifier_tab_ = SCM_EOL;
92   pitchname_tab_stack_ = SCM_EOL;
93   sources_ = sources;
94   scopes_ = SCM_EOL;
95   error_level_ = 0;
96   is_main_input_ = false;
97   start_module_ = SCM_EOL;
98   smobify_self ();
99
100   add_scope (ly_make_anonymous_module (false));
101   push_note_state (scm_c_make_hash_table (0));
102   chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
103 }
104
105 Lily_lexer::Lily_lexer (Lily_lexer const &src)
106   : Includable_lexer ()
107 {
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
154 SCM
155 Lily_lexer::remove_scope ()
156 {
157   SCM sc = scm_car (scopes_);
158   scopes_ = scm_cdr (scopes_);
159   set_current_scope ();
160   return sc;
161 }
162
163 SCM
164 Lily_lexer::set_current_scope ()
165 {
166   SCM old = scm_current_module ();
167
168   if (scm_is_pair (scopes_))
169     scm_set_current_module (scm_car (scopes_));
170   else
171     scm_set_current_module (start_module_);
172
173   return old;
174 }
175
176 int
177 Lily_lexer::lookup_keyword (String s)
178 {
179   return keytable_->lookup (s.to_str0 ());
180 }
181
182 SCM
183 Lily_lexer::lookup_identifier_symbol (SCM sym)
184 {
185   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
186     {
187       SCM var = ly_module_lookup (scm_car (s), sym);
188       if (var != SCM_BOOL_F)
189         return scm_variable_ref (var);
190     }
191
192   return SCM_UNDEFINED;
193 }
194
195 SCM
196 Lily_lexer::lookup_identifier (String name)
197 {
198   return lookup_identifier_symbol (ly_symbol2scm (name.to_str0 ()));
199 }
200
201 void
202 Lily_lexer::start_main_input ()
203 {
204   // yy_flex_debug = 1;
205   new_input (main_input_name_, sources_);
206
207   /* Do not allow \include in --safe-mode */
208   allow_includes_b_ = allow_includes_b_ && !be_safe_global;
209
210   scm_module_define (scm_car (scopes_),
211                      ly_symbol2scm ("input-file-name"),
212                      scm_makfrom0str (main_input_name_.to_str0 ()));
213 }
214
215 void
216 Lily_lexer::set_identifier (SCM name, SCM s)
217 {
218   SCM sym = name;
219   if (scm_is_string (name))
220     sym = scm_string_to_symbol (name);
221
222   if (scm_is_symbol (sym))
223     {
224       if (lookup_keyword (ly_symbol2string (sym)) >= 0)
225         {
226           String symstr = ly_symbol2string (sym);
227           warning (_f ("identifier name is a keyword: `%s'", symstr.to_str0 ()));
228         }
229
230       SCM mod = scm_car (scopes_);
231
232       scm_module_define (mod, sym, s);
233     }
234   else
235     programming_error ("identifier is not a symbol");
236 }
237
238 void
239 Lily_lexer::LexerError (char const *s)
240 {
241   if (include_stack_.is_empty ())
242     message (_f ("error at EOF: %s", s) + "\n");
243   else
244     {
245       error_level_ |= 1;
246       Input spot (*lexloc);
247       spot.error (s);
248     }
249 }
250
251 char
252 Lily_lexer::escaped_char (char c) const
253 {
254   switch (c)
255     {
256     case 'n':
257       return '\n';
258     case 't':
259       return '\t';
260     case '\'':
261     case '\"':
262     case '\\':
263       return c;
264     }
265   return 0;
266 }
267
268 Input
269 Lily_lexer::here_input () const
270 {
271   return Input (*lexloc);
272 }
273
274 void
275 Lily_lexer::prepare_for_next_token ()
276 {
277   last_input_ = here_input ();
278 }
279
280 /**
281    Since we don't create the buffer state from the bytes directly, we
282    don't know about the location of the lexer. Add this as a
283    YY_USER_ACTION */
284 void
285 Lily_lexer::add_lexed_char (int count)
286 {
287   char const *start = here_str0 ();
288   lexloc->set (get_source_file (),
289                start, start + count);
290   char_count_stack_.top () += count;
291 }
292
293 #include "ly-smobs.icc"
294
295 IMPLEMENT_SMOBS (Lily_lexer);
296 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
297 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
298
299 SCM
300 Lily_lexer::mark_smob (SCM s)
301 {
302   Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
303
304   scm_gc_mark (lexer->chordmodifier_tab_);
305   scm_gc_mark (lexer->pitchname_tab_stack_);
306   scm_gc_mark (lexer->start_module_);
307   return lexer->scopes_;
308 }
309
310 int
311 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
312 {
313   Lily_lexer *lexer = Lily_lexer::unsmob (s);
314
315   scm_puts ("#<Lily_lexer ", port);
316   scm_display (lexer->scopes_, port);
317   scm_puts (" >", port);
318   return 1;
319 }