]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-lexer.cc
* lily/parser.yy (lilypond): Add token aliases.
[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 = {
25   {"accepts", ACCEPTS},
26   {"addlyrics", ADDLYRICS},
27   {"addquote", ADDQUOTE},
28   {"alias", ALIAS},
29   {"alternative", ALTERNATIVE},
30   {"bar", BAR},
31   {"book", BOOK},
32   {"change", CHANGE},
33   {"chordmode", CHORDMODE},
34   {"chords", CHORDS},
35   {"clef", CLEF},
36   {"consists", CONSISTS},
37   {"context", CONTEXT},
38   {"default", DEFAULT},
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   {"octave", OCTAVE},
60   {"once", ONCE},
61   {"override", OVERRIDE},
62   {"paper", PAPER},
63   {"partial", PARTIAL},
64   {"relative", RELATIVE},
65   {"remove", REMOVE},
66   {"repeat", REPEAT},
67   {"rest", REST},
68   {"revert", REVERT},
69   {"score", SCORE},
70   {"sequential", SEQUENTIAL},
71   {"set", SET},
72   {"simultaneous", SIMULTANEOUS},
73   {"skip", SKIP},
74   {"tag", TAG},
75   {"tempo", TEMPO},
76   {"time", TIME_T},
77   {"times", TIMES},
78   {"transpose", TRANSPOSE},
79   {"transposition", TRANSPOSITION},
80   {"type", TYPE},
81   {"unset", UNSET},
82   {"with", WITH},
83   {0, 0}
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 int
160 Lily_lexer::lookup_keyword (String s)
161 {
162   return keytable_->lookup (s.to_str0 ());
163 }
164
165 SCM
166 Lily_lexer::lookup_identifier_symbol (SCM sym)
167 {
168   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
169     {
170       SCM var = ly_module_lookup (scm_car (s), sym);
171       if (var != SCM_BOOL_F)
172         return scm_variable_ref (var);
173     }
174
175   return SCM_UNDEFINED;
176 }
177
178 SCM
179 Lily_lexer::lookup_identifier (String name)
180 {
181   return lookup_identifier_symbol (ly_symbol2scm (name.to_str0 ()));
182 }
183
184 void
185 Lily_lexer::start_main_input ()
186 {
187   // yy_flex_debug = 1;
188   new_input (main_input_name_, sources_);
189
190   /* Do not allow \include in --safe-mode */
191   allow_includes_b_ = allow_includes_b_ && !be_safe_global;
192
193   scm_module_define (scm_car (scopes_),
194                      ly_symbol2scm ("input-file-name"),
195                      scm_makfrom0str (main_input_name_.to_str0 ()));
196 }
197
198 void
199 Lily_lexer::set_identifier (SCM name, SCM s)
200 {
201   SCM sym = name;
202   if (scm_is_string (name))
203     sym = scm_string_to_symbol (name);
204
205   if (scm_is_symbol (sym))
206     {
207       if (lookup_keyword (ly_symbol2string (sym)) >= 0)
208         {
209           String symstr = ly_symbol2string (sym);
210           warning (_f ("identifier name is a keyword: `%s'", symstr.to_str0 ()));
211         }
212
213       SCM mod = scm_car (scopes_);
214
215       scm_module_define (mod, sym, s);
216     }
217   else
218     programming_error ("identifier is not a symbol");
219 }
220
221 void
222 Lily_lexer::LexerError (char const *s)
223 {
224   if (include_stack_.is_empty ())
225     message (_f ("error at EOF: %s", s) + "\n");
226   else
227     {
228       error_level_ |= 1;
229       Input spot (*lexloc);
230       spot.error (s);
231     }
232 }
233
234 char
235 Lily_lexer::escaped_char (char c) const
236 {
237   switch (c)
238     {
239     case 'n':
240       return '\n';
241     case 't':
242       return '\t';
243     case '\'':
244     case '\"':
245     case '\\':
246       return c;
247     }
248   return 0;
249 }
250
251 Input
252 Lily_lexer::here_input () const
253 {
254   return Input(*lexloc);
255 }
256
257 void
258 Lily_lexer::prepare_for_next_token ()
259 {
260   last_input_ = here_input ();
261 }
262
263 /**
264    Since we don't create the buffer state from the bytes directly, we
265    don't know about the location of the lexer. Add this as a
266    YY_USER_ACTION */
267 void
268 Lily_lexer::add_lexed_char (int count)
269 {
270   lexloc->source_file_ = get_source_file ();
271   lexloc->start_ = here_str0 ();
272   lexloc->end_ = lexloc->start_ + count;
273   char_count_stack_.top () += count;
274 }
275
276 #include "ly-smobs.icc"
277
278 IMPLEMENT_SMOBS (Lily_lexer);
279 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
280 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
281
282 SCM
283 Lily_lexer::mark_smob (SCM s)
284 {
285   Lily_lexer *lexer = (Lily_lexer *) SCM_CELL_WORD_1 (s);
286
287   scm_gc_mark (lexer->chordmodifier_tab_);
288   scm_gc_mark (lexer->pitchname_tab_stack_);
289   return lexer->scopes_;
290 }
291
292 int
293 Lily_lexer::print_smob (SCM, SCM port, scm_print_state*)
294 {
295   scm_puts ("#<Lily_lexer ", port);
296   scm_puts (" >", port);
297   return 1;
298 }