]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
e11bf1d682fa6143087a2e8d312da106734a2481
[lilypond.git] / lily / my-lily-lexer.cc
1 /*
2   my-lily-lexer.cc -- implement My_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 <ctype.h>
10 #include <sstream>
11
12 #include "lily-proto.hh"
13 #include "scm-hash.hh"
14 #include "interval.hh"
15 #include "lily-guile.hh"
16 #include "parser.hh"
17 #include "keyword.hh"
18 #include "my-lily-lexer.hh"
19 #include "warn.hh"
20 #include "source-file.hh"
21 #include "main.hh"
22 #include "input.hh"
23 #include "moment.hh"
24 #include "ly-module.hh"
25
26
27 static Keyword_ent the_key_tab[] = {
28   {"accepts", ACCEPTS},
29   {"addquote", ADDQUOTE},
30   {"alias", ALIAS},
31   {"alternative", ALTERNATIVE},
32   {"bar", BAR},
33   {"book", BOOK},
34   {"bookpaper", BOOKPAPER},
35   {"change", CHANGE},
36   {"chords", CHORDS},
37   {"clef", CLEF},
38   {"consists", CONSISTS},
39   {"context", CONTEXT},
40   {"default", DEFAULT},
41   {"denies", DENIES},
42   {"drums", DRUMS},
43   {"description", DESCRIPTION},
44   {"figures",FIGURES},
45   {"grobdescriptions", GROBDESCRIPTIONS},
46   {"header", HEADER},
47   {"key", KEY},
48   {"lyrics", LYRICS},
49   {"lyricsto", LYRICSTO},
50   {"mark", MARK},
51   {"markup", MARKUP},
52   {"midi", MIDI},
53   {"name", NAME},
54   {"new", NEWCONTEXT},
55   {"newlyrics", NEWLYRICS},
56   {"notes", NOTES},
57   {"octave", OCTAVE},
58   {"once", ONCE},
59   {"override", OVERRIDE},
60   {"paper", PAPER},
61   {"partial", PARTIAL},
62   {"quote", QUOTE},
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 My_lily_lexer::My_lily_lexer (Sources *sources)
87 {
88   keytable_ = new Keyword_table (the_key_tab);
89   encoding_ = SCM_EOL;
90   chordmodifier_tab_ = scm_make_vector (scm_int2num (1), 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   add_scope (ly_make_anonymous_module (false));
98
99   push_note_state (scm_c_make_hash_table (0));
100
101 }
102
103 My_lily_lexer::My_lily_lexer (My_lily_lexer const &src)
104   : Includable_lexer ()
105 {
106   keytable_ =  (src.keytable_) ? new Keyword_table (*src.keytable_) : 0;
107   encoding_ = src.encoding_;
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   SCM scopes = SCM_EOL;
116   SCM* tail = &scopes;
117   for (SCM s = src.scopes_; ly_c_pair_p (s); s = ly_cdr (s))
118     {
119       SCM newmod = ly_make_anonymous_module (false);
120       ly_import_module (newmod, ly_car (s));
121       *tail = scm_cons (newmod, SCM_EOL);
122       tail = SCM_CDRLOC (*tail);
123     }
124   
125   scopes_ =  scopes;
126   push_note_state (scm_c_make_hash_table (0));
127 }
128
129 My_lily_lexer::~My_lily_lexer ()
130 {
131   delete keytable_;
132 }
133
134 SCM
135 My_lily_lexer::encoding () const
136 {
137   return encoding_ ;
138 }
139
140
141 void
142 My_lily_lexer::add_scope (SCM module)
143 {
144   ly_reexport_module (scm_current_module ());
145   scm_set_current_module (module);
146   for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
147     {
148       ly_use_module (module, ly_car (s));
149     }
150   scopes_ = scm_cons (module, scopes_);
151 }
152
153 SCM
154 My_lily_lexer::remove_scope ()
155 {
156   SCM sc = ly_car (scopes_);
157   scopes_ = ly_cdr (scopes_);
158   scm_set_current_module (ly_car (scopes_));
159
160   return sc;
161 }
162
163
164 int
165 My_lily_lexer::lookup_keyword (String s)
166 {
167   return keytable_->lookup (s.to_str0 ());
168 }
169
170 SCM
171 My_lily_lexer::lookup_identifier_symbol (SCM sym)
172 {
173   for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
174     {
175       SCM var = ly_module_lookup (ly_car (s), sym);
176       if (var != SCM_BOOL_F)
177         return scm_variable_ref (var);
178     }
179
180   return SCM_UNDEFINED;
181 }
182
183 SCM
184 My_lily_lexer::lookup_identifier (String name)
185 {
186   return lookup_identifier_symbol (ly_symbol2scm (name.to_str0 ()));
187 }
188
189 void
190 My_lily_lexer::start_main_input ()
191 {
192   // yy_flex_debug = 1;
193   new_input (main_input_name_, sources_);
194   
195   /* Do not allow \include in --safe-mode */
196   allow_includes_b_ = allow_includes_b_ && !safe_global_b;
197
198   scm_module_define (ly_car (scopes_),
199                      ly_symbol2scm ("input-file-name"),
200                      scm_makfrom0str (main_input_name_.to_str0 ()));
201 }
202
203 void
204 My_lily_lexer::set_identifier (SCM name, SCM s)
205 {
206   SCM sym = name;
207   if (ly_c_string_p (name))
208     sym =  scm_string_to_symbol (name);
209   
210   if (ly_c_symbol_p (sym))
211     {
212       if (lookup_keyword (ly_symbol2string (sym)) >= 0)
213         {
214           warning (_f ("Identifier name is a keyword: `%s'", SCM_SYMBOL_CHARS (sym)));
215         }
216
217       SCM mod = ly_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 My_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 My_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 My_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 My_lily_lexer::prepare_for_next_token ()
266 {
267   last_input_ = here_input ();
268 }
269
270 void
271 My_lily_lexer::set_encoding (String s)
272 {
273   if (s.length ())
274     encoding_ = ly_symbol2scm (s.to_str0 ());
275   else
276     encoding_ = SCM_EOL;
277 }