]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-lexer.cc
2392f47b363d63917061b20583dd43ec9a18b01b
[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   {"type", TYPE},
78   {"unset", UNSET},
79   {"with", WITH},
80   {0, 0}
81 };
82
83 Lily_lexer::Lily_lexer (Sources *sources)
84 {
85   keytable_ = new Keyword_table (the_key_tab);
86   chordmodifier_tab_ = SCM_EOL;
87   pitchname_tab_stack_ = SCM_EOL;
88   sources_ = sources;
89   scopes_ = SCM_EOL;
90   error_level_ = 0;
91   is_main_input_ = false;
92   start_module_ = SCM_EOL;
93   smobify_self ();
94
95   add_scope (ly_make_anonymous_module (false));
96   push_note_state (scm_c_make_hash_table (0));
97   chordmodifier_tab_ = scm_make_vector (scm_from_int (1), SCM_EOL);
98 }
99
100 Lily_lexer::Lily_lexer (Lily_lexer const &src)
101   : Includable_lexer ()
102 {
103   keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
104   chordmodifier_tab_ = src.chordmodifier_tab_;
105   pitchname_tab_stack_ = src.pitchname_tab_stack_;
106   sources_ = src.sources_;
107   start_module_ = SCM_EOL;
108
109   error_level_ = src.error_level_;
110   is_main_input_ = src.is_main_input_;
111
112   scopes_ = SCM_EOL;
113
114   smobify_self ();
115
116   SCM scopes = SCM_EOL;
117   SCM *tail = &scopes;
118   for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
119     {
120       SCM newmod = ly_make_anonymous_module (false);
121       ly_module_copy (newmod, scm_car (s));
122       *tail = scm_cons (newmod, SCM_EOL);
123       tail = SCM_CDRLOC (*tail);
124     }
125
126   scopes_ = scopes;
127   push_note_state (scm_c_make_hash_table (0));
128 }
129
130 Lily_lexer::~Lily_lexer ()
131 {
132   delete keytable_;
133 }
134
135 void
136 Lily_lexer::add_scope (SCM module)
137 {
138   ly_reexport_module (scm_current_module ());
139   if (!scm_is_pair (scopes_))
140     start_module_ = scm_current_module ();
141
142   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
143     ly_use_module (module, scm_car (s));
144   scopes_ = scm_cons (module, scopes_);
145
146   set_current_scope ();
147 }
148
149 SCM
150 Lily_lexer::remove_scope ()
151 {
152   SCM sc = scm_car (scopes_);
153   scopes_ = scm_cdr (scopes_);
154   set_current_scope ();
155   return sc;
156 }
157
158 SCM
159 Lily_lexer::set_current_scope ()
160 {
161   SCM old = scm_current_module ();
162
163   if (scm_is_pair (scopes_))
164     scm_set_current_module (scm_car (scopes_));
165   else
166     scm_set_current_module (start_module_);
167
168   return old;
169 }
170
171 int
172 Lily_lexer::lookup_keyword (string s)
173 {
174   return keytable_->lookup (s.c_str ());
175 }
176
177 SCM
178 Lily_lexer::lookup_identifier_symbol (SCM sym)
179 {
180   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
181     {
182       SCM var = ly_module_lookup (scm_car (s), sym);
183       if (var != SCM_BOOL_F)
184         return scm_variable_ref (var);
185     }
186
187   return SCM_UNDEFINED;
188 }
189
190 SCM
191 Lily_lexer::lookup_identifier (string name)
192 {
193   return lookup_identifier_symbol (ly_symbol2scm (name.c_str ()));
194 }
195
196 void
197 Lily_lexer::start_main_input ()
198 {
199   // yy_flex_debug = 1;
200   new_input (main_input_name_, sources_);
201
202   /* Do not allow \include in --safe-mode */
203   allow_includes_b_ = allow_includes_b_ && !be_safe_global;
204
205   scm_module_define (scm_car (scopes_),
206                      ly_symbol2scm ("input-file-name"),
207                      scm_makfrom0str (main_input_name_.c_str ()));
208 }
209
210 void
211 Lily_lexer::set_identifier (SCM name, SCM s)
212 {
213   SCM sym = name;
214   if (scm_is_string (name))
215     sym = scm_string_to_symbol (name);
216
217   if (scm_is_symbol (sym))
218     {
219       if (lookup_keyword (ly_symbol2string (sym)) >= 0)
220         {
221           string symstr = ly_symbol2string (sym);
222           warning (_f ("identifier name is a keyword: `%s'", symstr.c_str ()));
223         }
224
225       SCM mod = scm_car (scopes_);
226
227       scm_module_define (mod, sym, s);
228     }
229   else
230     programming_error ("identifier is not a symbol");
231 }
232
233 void
234 Lily_lexer::LexerError (char const *s)
235 {
236   if (include_stack_.empty ())
237     message (_f ("error at EOF: %s", s) + "\n");
238   else
239     {
240       error_level_ |= 1;
241       Input spot (*lexloc);
242       spot.error (s);
243     }
244 }
245
246 char
247 Lily_lexer::escaped_char (char c) const
248 {
249   switch (c)
250     {
251     case 'n':
252       return '\n';
253     case 't':
254       return '\t';
255     case '\'':
256     case '\"':
257     case '\\':
258       return c;
259     }
260   return 0;
261 }
262
263 Input
264 Lily_lexer::here_input () const
265 {
266   return Input (*lexloc);
267 }
268
269 void
270 Lily_lexer::prepare_for_next_token ()
271 {
272   last_input_ = here_input ();
273 }
274
275 /**
276    Since we don't create the buffer state from the bytes directly, we
277    don't know about the location of the lexer. Add this as a
278    YY_USER_ACTION */
279 void
280 Lily_lexer::add_lexed_char (int count)
281 {
282   char const *start = here_str0 ();
283   lexloc->set (get_source_file (),
284                start, start + count);
285   char_count_stack_.back () += count;
286 }
287
288 #include "ly-smobs.icc"
289
290 IMPLEMENT_SMOBS (Lily_lexer);
291 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
292 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
293
294 SCM
295 Lily_lexer::mark_smob (SCM s)
296 {
297   Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
298
299   scm_gc_mark (lexer->chordmodifier_tab_);
300   scm_gc_mark (lexer->pitchname_tab_stack_);
301   scm_gc_mark (lexer->start_module_);
302   return lexer->scopes_;
303 }
304
305 int
306 Lily_lexer::print_smob (SCM s, SCM port, scm_print_state*)
307 {
308   Lily_lexer *lexer = Lily_lexer::unsmob (s);
309
310   scm_puts ("#<Lily_lexer ", port);
311   scm_display (lexer->scopes_, port);
312   scm_puts (" >", port);
313   return 1;
314 }