]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-lexer.cc
* scm/music-functions.scm (has-request-chord): don't use
[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   {"accepts", ACCEPTS},
25   {"addquote", ADDQUOTE},
26   {"addlyrics", ADDLYRICS},
27   {"alias", ALIAS},
28   {"alternative", ALTERNATIVE},
29   {"bar", BAR},
30   {"book", BOOK},
31   {"change", CHANGE},
32   {"chords", CHORDS},
33   {"chordmode", CHORDMODE},
34   {"clef", CLEF},
35   {"consists", CONSISTS},
36   {"context", CONTEXT},
37   {"default", DEFAULT},
38   {"denies", DENIES},
39   {"drummode", DRUMMODE},
40   {"drums", DRUMS},
41   {"description", DESCRIPTION},
42   {"figures", FIGURES},
43   {"figuremode", FIGUREMODE},
44   {"grobdescriptions", GROBDESCRIPTIONS},
45   {"header", HEADER},
46   {"key", KEY},
47   {"layout", LAYOUT},
48   {"lyricmode", LYRICMODE},
49   {"lyricsto", LYRICSTO},
50   {"lyrics", LYRICS},
51   {"mark", MARK},
52   {"markup", MARKUP},
53   {"midi", MIDI},
54   {"name", NAME},
55   {"new", NEWCONTEXT},
56   {"objectid", OBJECTID},
57   {"notemode", NOTEMODE},
58   {"octave", OCTAVE},
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   {"tag", TAG},
74   {"tempo", TEMPO},
75   {"time", TIME_T},
76   {"times", TIMES},
77   {"transpose", TRANSPOSE},
78   {"transposition", TRANSPOSITION},
79   {"type", TYPE},
80   {"unset", UNSET},
81   {"with", WITH},
82   {0, 0}
83 };
84
85
86 Lily_lexer::Lily_lexer (Sources *sources)
87 {
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
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_int2num (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   
111   error_level_ = src.error_level_; 
112   is_main_input_ = src.is_main_input_;
113
114   scopes_ = SCM_EOL;
115   
116   smobify_self ();
117   
118   SCM scopes = SCM_EOL;
119   SCM *tail = &scopes;
120   for (SCM s = src.scopes_; scm_is_pair (s); s = scm_cdr (s))
121     {
122       SCM newmod = ly_make_anonymous_module (false);
123       ly_module_copy (newmod, scm_car (s));
124       *tail = scm_cons (newmod, SCM_EOL);
125       tail = SCM_CDRLOC (*tail);
126     }
127   
128   scopes_ =  scopes;
129   push_note_state (scm_c_make_hash_table (0));
130 }
131
132 Lily_lexer::~Lily_lexer ()
133 {
134   delete keytable_;
135 }
136
137 void
138 Lily_lexer::add_scope (SCM module)
139 {
140   ly_reexport_module (scm_current_module ());
141   scm_set_current_module (module);
142   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
143     {
144       ly_use_module (module, scm_car (s));
145     }
146   scopes_ = scm_cons (module, scopes_);
147 }
148
149 SCM
150 Lily_lexer::remove_scope ()
151 {
152   SCM sc = scm_car (scopes_);
153   scopes_ = scm_cdr (scopes_);
154   scm_set_current_module (scm_car (scopes_));
155
156   return sc;
157 }
158
159
160 int
161 Lily_lexer::lookup_keyword (String s)
162 {
163   return keytable_->lookup (s.to_str0 ());
164 }
165
166 SCM
167 Lily_lexer::lookup_identifier_symbol (SCM sym)
168 {
169   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
170     {
171       SCM var = ly_module_lookup (scm_car (s), sym);
172       if (var != SCM_BOOL_F)
173         return scm_variable_ref (var);
174     }
175
176   return SCM_UNDEFINED;
177 }
178
179 SCM
180 Lily_lexer::lookup_identifier (String name)
181 {
182   return lookup_identifier_symbol (ly_symbol2scm (name.to_str0 ()));
183 }
184
185 void
186 Lily_lexer::start_main_input ()
187 {
188   // yy_flex_debug = 1;
189   new_input (main_input_name_, sources_);
190   
191   /* Do not allow \include in --safe-mode */
192   allow_includes_b_ = allow_includes_b_ && !be_safe_global;
193
194   scm_module_define (scm_car (scopes_),
195                      ly_symbol2scm ("input-file-name"),
196                      scm_makfrom0str (main_input_name_.to_str0 ()));
197 }
198
199 void
200 Lily_lexer::set_identifier (SCM name, SCM s)
201 {
202   SCM sym = name;
203   if (scm_is_string (name))
204     sym =  scm_string_to_symbol (name);
205   
206   if (scm_is_symbol (sym))
207     {
208       if (lookup_keyword (ly_symbol2string (sym)) >= 0)
209         {
210           String symstr = ly_symbol2string (sym); 
211           warning (_f ("Identifier name is a keyword: `%s'", symstr.to_str0()));
212         }
213
214       SCM mod = scm_car (scopes_);
215
216       scm_module_define (mod, sym, s);
217     }
218   else
219     {
220       programming_error ("Identifier is not a symbol.");
221     }
222 }
223
224 void
225 Lily_lexer::LexerError (char const *s)
226 {
227   if (include_stack_.is_empty ())
228     progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
229   else
230     {
231       error_level_ |= 1;
232       Input spot (*lexloc);
233       spot.error (s);
234     }
235 }
236
237 char
238 Lily_lexer::escaped_char (char c) const
239 {
240   switch (c)
241     {
242     case 'n':
243       return '\n';
244     case 't':
245       return '\t';
246     case '\'':
247     case '\"':
248     case '\\':
249       return c;
250     }
251   return 0;
252 }
253
254 Input
255 Lily_lexer::here_input () const
256 {
257   return Input(*lexloc);
258 }
259
260 void
261 Lily_lexer::prepare_for_next_token ()
262 {
263   last_input_ = here_input ();
264 }
265
266 /**
267   Since we don't create the buffer state from the bytes directly, we
268   don't know about the location of the lexer. Add this as a
269   YY_USER_ACTION */
270 void
271 Lily_lexer::add_lexed_char (int count)
272 {
273   lexloc->source_file_ = get_source_file ();
274   lexloc->start_ = here_str0 ();
275   lexloc->end_ = lexloc->start_ + count;
276   char_count_stack_.top () += count;
277 }
278
279
280 #include "ly-smobs.icc"
281
282 IMPLEMENT_SMOBS (Lily_lexer);
283 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
284 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
285
286 SCM
287 Lily_lexer::mark_smob (SCM s)
288 {
289   Lily_lexer *lexer = (Lily_lexer*) SCM_CELL_WORD_1 (s);
290
291   scm_gc_mark (lexer->chordmodifier_tab_);
292   scm_gc_mark (lexer->pitchname_tab_stack_);
293   return lexer->scopes_;
294 }
295
296 int
297 Lily_lexer::print_smob (SCM, SCM port, scm_print_state*)
298 {
299   scm_puts ("#<Lily_lexer ", port);
300   scm_puts (" >", port);
301   return 1;
302 }