]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-lexer.cc
* input/regression/new-markup-scheme.ly: oops. font-family=music
[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--2004 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 #include "ly-module.hh"
23
24 static Keyword_ent the_key_tab[] = {
25   {"accepts", ACCEPTS},
26   {"addquote", ADDQUOTE},
27   {"addlyrics", ADDLYRICS},
28   {"alias", ALIAS},
29   {"alternative", ALTERNATIVE},
30   {"bar", BAR},
31   {"book", BOOK},
32   {"change", CHANGE},
33   {"chords", CHORDS},
34   {"chordmode", CHORDMODE},
35   {"clef", CLEF},
36   {"consists", CONSISTS},
37   {"context", CONTEXT},
38   {"default", DEFAULT},
39   {"denies", DENIES},
40   {"drummode", DRUMMODE},
41   {"drums", DRUMS},
42   {"description", DESCRIPTION},
43   {"figures",FIGURES},
44   {"figuremode",FIGUREMODE},
45   {"grobdescriptions", GROBDESCRIPTIONS},
46   {"header", HEADER},
47   {"key", KEY},
48   {"layout", LAYOUT},
49   {"lyricmode", LYRICMODE},
50   {"lyricsto", LYRICSTO},
51   {"lyrics", LYRICS},
52   {"mark", MARK},
53   {"markup", MARKUP},
54   {"midi", MIDI},
55   {"name", NAME},
56   {"new", NEWCONTEXT},
57   {"objectid", OBJECTID},
58   {"notemode", NOTEMODE},
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
87 Lily_lexer::Lily_lexer (Sources *sources)
88 {
89   keytable_ = new Keyword_table (the_key_tab);
90   chordmodifier_tab_ = SCM_EOL;
91   pitchname_tab_stack_ = SCM_EOL; 
92   sources_ = sources;
93   scopes_ = SCM_EOL;
94   error_level_ = 0; 
95   main_input_b_ = false;
96
97   smobify_self ();
98   
99   add_scope (ly_make_anonymous_module (false));
100   push_note_state (scm_c_make_hash_table (0));
101   chordmodifier_tab_ = scm_make_vector (scm_int2num (1), SCM_EOL);
102 }
103
104 Lily_lexer::Lily_lexer (Lily_lexer const &src)
105   : Includable_lexer ()
106 {
107   keytable_ = (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
108   chordmodifier_tab_ = src.chordmodifier_tab_;
109   pitchname_tab_stack_ = src.pitchname_tab_stack_;
110   sources_ = src.sources_;
111   
112   error_level_ = src.error_level_; 
113   main_input_b_ = src.main_input_b_;
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
139
140 void
141 Lily_lexer::add_scope (SCM module)
142 {
143   ly_reexport_module (scm_current_module ());
144   scm_set_current_module (module);
145   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
146     {
147       ly_use_module (module, scm_car (s));
148     }
149   scopes_ = scm_cons (module, scopes_);
150 }
151
152 SCM
153 Lily_lexer::remove_scope ()
154 {
155   SCM sc = scm_car (scopes_);
156   scopes_ = scm_cdr (scopes_);
157   scm_set_current_module (scm_car (scopes_));
158
159   return sc;
160 }
161
162
163 int
164 Lily_lexer::lookup_keyword (String s)
165 {
166   return keytable_->lookup (s.to_str0 ());
167 }
168
169 SCM
170 Lily_lexer::lookup_identifier_symbol (SCM sym)
171 {
172   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
173     {
174       SCM var = ly_module_lookup (scm_car (s), sym);
175       if (var != SCM_BOOL_F)
176         return scm_variable_ref (var);
177     }
178
179   return SCM_UNDEFINED;
180 }
181
182 SCM
183 Lily_lexer::lookup_identifier (String name)
184 {
185   return lookup_identifier_symbol (ly_symbol2scm (name.to_str0 ()));
186 }
187
188 void
189 Lily_lexer::start_main_input ()
190 {
191   // yy_flex_debug = 1;
192   new_input (main_input_name_, sources_);
193   
194   /* Do not allow \include in --safe-mode */
195   allow_includes_b_ = allow_includes_b_ && !safe_global_b;
196
197   scm_module_define (scm_car (scopes_),
198                      ly_symbol2scm ("input-file-name"),
199                      scm_makfrom0str (main_input_name_.to_str0 ()));
200 }
201
202 void
203 Lily_lexer::set_identifier (SCM name, SCM s)
204 {
205   SCM sym = name;
206   if (scm_is_string (name))
207     sym =  scm_string_to_symbol (name);
208   
209   if (scm_is_symbol (sym))
210     {
211       if (lookup_keyword (ly_symbol2string (sym)) >= 0)
212         {
213           String symstr = ly_symbol2string (sym); 
214           warning (_f ("Identifier name is a keyword: `%s'", symstr.to_str0()));
215         }
216
217       SCM mod = scm_car (scopes_);
218
219       scm_module_define (mod, sym, s);
220     }
221   else
222     {
223       programming_error ("Identifier is not a symbol.");
224     }
225 }
226
227 void
228 Lily_lexer::LexerError (char const *s)
229 {
230   if (include_stack_.is_empty ())
231     progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
232   else
233     {
234       error_level_ |= 1;
235       Input spot (get_source_file (), here_str0 ());
236       spot.error (s);
237     }
238 }
239
240 char
241 Lily_lexer::escaped_char (char c) const
242 {
243   switch (c)
244     {
245     case 'n':
246       return '\n';
247     case 't':
248       return '\t';
249     case '\'':
250     case '\"':
251     case '\\':
252       return c;
253     }
254   return 0;
255 }
256
257 Input
258 Lily_lexer::here_input () const
259 {
260   Source_file * f = get_source_file ();
261   return Input (f, (char*)here_str0 ());
262 }
263
264 void
265 Lily_lexer::prepare_for_next_token ()
266 {
267   last_input_ = here_input ();
268 }
269
270 #include "ly-smobs.icc"
271
272 IMPLEMENT_SMOBS (Lily_lexer);
273 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
274 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
275
276 SCM
277 Lily_lexer::mark_smob (SCM s)
278 {
279   Lily_lexer *lexer = (Lily_lexer*) SCM_CELL_WORD_1 (s);
280
281   scm_gc_mark (lexer->chordmodifier_tab_);
282   scm_gc_mark (lexer->pitchname_tab_stack_);
283   return lexer->scopes_;
284 }
285
286 int
287 Lily_lexer::print_smob (SCM, SCM port, scm_print_state*)
288 {
289   scm_puts ("#<Lily_lexer ", port);
290   scm_puts (" >", port);
291   return 1;
292 }