]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-lexer.cc
* lily/include/main.hh: lose _b hungarian suffixes for global
[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
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   main_input_b_ = 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   main_input_b_ = src.main_input_b_;
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
138
139 void
140 Lily_lexer::add_scope (SCM module)
141 {
142   ly_reexport_module (scm_current_module ());
143   scm_set_current_module (module);
144   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
145     {
146       ly_use_module (module, scm_car (s));
147     }
148   scopes_ = scm_cons (module, scopes_);
149 }
150
151 SCM
152 Lily_lexer::remove_scope ()
153 {
154   SCM sc = scm_car (scopes_);
155   scopes_ = scm_cdr (scopes_);
156   scm_set_current_module (scm_car (scopes_));
157
158   return sc;
159 }
160
161
162 int
163 Lily_lexer::lookup_keyword (String s)
164 {
165   return keytable_->lookup (s.to_str0 ());
166 }
167
168 SCM
169 Lily_lexer::lookup_identifier_symbol (SCM sym)
170 {
171   for (SCM s = scopes_; scm_is_pair (s); s = scm_cdr (s))
172     {
173       SCM var = ly_module_lookup (scm_car (s), sym);
174       if (var != SCM_BOOL_F)
175         return scm_variable_ref (var);
176     }
177
178   return SCM_UNDEFINED;
179 }
180
181 SCM
182 Lily_lexer::lookup_identifier (String name)
183 {
184   return lookup_identifier_symbol (ly_symbol2scm (name.to_str0 ()));
185 }
186
187 void
188 Lily_lexer::start_main_input ()
189 {
190   // yy_flex_debug = 1;
191   new_input (main_input_name_, sources_);
192   
193   /* Do not allow \include in --safe-mode */
194   allow_includes_b_ = allow_includes_b_ && !be_safe_global;
195
196   scm_module_define (scm_car (scopes_),
197                      ly_symbol2scm ("input-file-name"),
198                      scm_makfrom0str (main_input_name_.to_str0 ()));
199 }
200
201 void
202 Lily_lexer::set_identifier (SCM name, SCM s)
203 {
204   SCM sym = name;
205   if (scm_is_string (name))
206     sym =  scm_string_to_symbol (name);
207   
208   if (scm_is_symbol (sym))
209     {
210       if (lookup_keyword (ly_symbol2string (sym)) >= 0)
211         {
212           String symstr = ly_symbol2string (sym); 
213           warning (_f ("Identifier name is a keyword: `%s'", symstr.to_str0()));
214         }
215
216       SCM mod = scm_car (scopes_);
217
218       scm_module_define (mod, sym, s);
219     }
220   else
221     {
222       programming_error ("Identifier is not a symbol.");
223     }
224 }
225
226 void
227 Lily_lexer::LexerError (char const *s)
228 {
229   if (include_stack_.is_empty ())
230     progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
231   else
232     {
233       error_level_ |= 1;
234       Input spot (get_source_file (), here_str0 ());
235       spot.error (s);
236     }
237 }
238
239 char
240 Lily_lexer::escaped_char (char c) const
241 {
242   switch (c)
243     {
244     case 'n':
245       return '\n';
246     case 't':
247       return '\t';
248     case '\'':
249     case '\"':
250     case '\\':
251       return c;
252     }
253   return 0;
254 }
255
256 Input
257 Lily_lexer::here_input () const
258 {
259   Source_file * f = get_source_file ();
260   return Input (f, (char*)here_str0 ());
261 }
262
263 void
264 Lily_lexer::prepare_for_next_token ()
265 {
266   last_input_ = here_input ();
267 }
268
269 #include "ly-smobs.icc"
270
271 IMPLEMENT_SMOBS (Lily_lexer);
272 IMPLEMENT_TYPE_P (Lily_lexer, "ly:lily-lexer?");
273 IMPLEMENT_DEFAULT_EQUAL_P (Lily_lexer);
274
275 SCM
276 Lily_lexer::mark_smob (SCM s)
277 {
278   Lily_lexer *lexer = (Lily_lexer*) SCM_CELL_WORD_1 (s);
279
280   scm_gc_mark (lexer->chordmodifier_tab_);
281   scm_gc_mark (lexer->pitchname_tab_stack_);
282   return lexer->scopes_;
283 }
284
285 int
286 Lily_lexer::print_smob (SCM, SCM port, scm_print_state*)
287 {
288   scm_puts ("#<Lily_lexer ", port);
289   scm_puts (" >", port);
290   return 1;
291 }