]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
971fe0ae7caed5b0714a0bff92058e989f1e7a76
[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   {"acciaccatura", ACCIACCATURA},
29   {"accepts", ACCEPTS},
30   {"addlyrics", ADDLYRICS},
31   {"addquote", ADDQUOTE},
32   {"alias", ALIAS},
33   {"alternative", ALTERNATIVE},
34   {"applycontext", APPLYCONTEXT},
35   {"applyoutput", APPLYOUTPUT},
36   {"appoggiatura", APPOGGIATURA},
37   {"autochange", AUTOCHANGE},
38   {"bar", BAR},
39   {"book", BOOK},
40   {"breathe", BREATHE},
41   {"change", CHANGE},
42   {"chords", CHORDS},
43   {"clef", CLEF},
44   {"consists", CONSISTS},
45   {"consistsend", CONSISTSEND},
46   {"context", CONTEXT},
47   {"default", DEFAULT},
48   {"denies", DENIES},
49   {"drums", DRUMS},
50   {"description", DESCRIPTION},
51   {"figures",FIGURES},
52   {"grace", GRACE},
53   {"grobdescriptions", GROBDESCRIPTIONS},
54   {"header", HEADER},
55   {"key", KEY},
56   {"lyrics", LYRICS},
57   {"lyricsto", LYRICSTO},
58   {"mark", MARK},
59   {"markup", MARKUP},
60   {"midi", MIDI},
61   {"name", NAME},
62   {"new", NEWCONTEXT},
63   {"newlyrics", NEWLYRICS},
64   {"notes", NOTES},
65   {"octave", OCTAVE},
66   {"once", ONCE},
67   {"override", OVERRIDE},
68   {"paper", PAPER},
69   {"partcombine", PARTCOMBINE},
70   {"partial", PARTIAL},
71   {"quote", QUOTE},
72   {"relative", RELATIVE},
73   {"remove", REMOVE},
74   {"repeat", REPEAT},
75   {"rest", REST},
76   {"revert", REVERT},
77   {"score", SCORE},
78   {"sequential", SEQUENTIAL},
79   {"set", SET},
80   {"simultaneous", SIMULTANEOUS},
81   {"skip", SKIP},
82   {"tag", TAG},
83   {"tempo", TEMPO},
84   {"time", TIME_T},
85   {"times", TIMES},
86   {"transpose", TRANSPOSE},
87   {"transposition", TRANSPOSITION},
88   {"type", TYPE},
89   {"unset", UNSET},
90   {"with", WITH},
91   {0, 0}
92 };
93
94
95 My_lily_lexer::My_lily_lexer (Sources *sources)
96   
97 {
98   keytable_ = new Keyword_table (the_key_tab);
99   encoding_ = SCM_EOL;
100   chordmodifier_tab_ = scm_make_vector (scm_int2num (1), SCM_EOL);
101   pitchname_tab_stack_ = SCM_EOL; 
102   sources_ = sources;
103   scopes_ = SCM_EOL;
104   error_level_ = 0; 
105   main_input_b_ = false;
106   
107   add_scope (ly_make_anonymous_module ());
108 }
109
110 My_lily_lexer::My_lily_lexer (My_lily_lexer const &src)
111   : Includable_lexer ()
112 {
113   keytable_ = src.keytable_;
114   encoding_ = src.encoding_;
115   chordmodifier_tab_ = src.chordmodifier_tab_;
116   pitchname_tab_stack_ = src.pitchname_tab_stack_;
117   sources_ = src.sources_;
118   scopes_ = src.scopes_;
119   error_level_ = src.error_level_; 
120   main_input_b_ = src.main_input_b_;
121 }
122
123 SCM
124 My_lily_lexer::encoding () const
125 {
126   return encoding_ ;
127 }
128
129 void
130 My_lily_lexer::add_scope (SCM module)
131 {
132   ly_reexport_module (scm_current_module ());
133   scm_set_current_module (module);
134   for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
135     {
136       /* UGH. how to do this more neatly? */      
137       SCM expr
138         = scm_list_3 (ly_symbol2scm ("module-use!"),
139                       module,
140                       scm_list_2 (ly_symbol2scm ("module-public-interface"),
141                                   ly_car (s)));
142       scm_primitive_eval (expr);
143     }
144   scopes_ = scm_cons (module, scopes_);
145 }
146
147 SCM
148 My_lily_lexer::remove_scope ()
149 {
150   SCM sc = ly_car (scopes_);
151   scopes_ = ly_cdr (scopes_);
152   scm_set_current_module (ly_car (scopes_));
153
154   return sc;
155 }
156
157
158 int
159 My_lily_lexer::lookup_keyword (String s)
160 {
161   return keytable_->lookup (s.to_str0 ());
162 }
163
164 SCM
165 My_lily_lexer::lookup_identifier (String name)
166 {
167   SCM sym = ly_symbol2scm (name.to_str0 ());
168   for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
169     {
170       SCM var = ly_module_lookup (ly_car (s), sym);
171       if (var != SCM_BOOL_F)
172         return scm_variable_ref (var);
173     }
174
175   return SCM_UNDEFINED;
176 }
177
178 void
179 My_lily_lexer::start_main_input ()
180 {
181   // yy_flex_debug = 1;
182   new_input (main_input_name_, sources_);
183   /* Do not allow \include in --safe-mode */
184   allow_includes_b_ = allow_includes_b_ && ! safe_global_b;
185
186   scm_module_define (ly_car (scopes_),
187                      ly_symbol2scm ("input-file-name"),
188                      scm_makfrom0str (main_input_name_.to_str0 ()));
189 }
190
191 void
192 My_lily_lexer::set_identifier (SCM name, SCM s)
193 {
194   assert (ly_c_string_p (name));
195   
196   if (lookup_keyword (ly_scm2string (name)) >= 0)
197     {
198       warning (_f ("Identifier name is a keyword: `%s'", SCM_STRING_CHARS (name)));
199     }
200
201   SCM sym = scm_string_to_symbol (name);
202   SCM mod = ly_car (scopes_);
203
204   scm_module_define (mod, sym, s);
205 }
206
207 My_lily_lexer::~My_lily_lexer ()
208 {
209   delete keytable_;
210 }
211
212
213
214 void
215 My_lily_lexer::LexerError (char const *s)
216 {
217   if (include_stack_.is_empty ())
218     progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
219   else
220     {
221       error_level_ |= 1;
222       Input spot (get_source_file (), here_str0 ());
223       spot.error (s);
224     }
225 }
226
227 char
228 My_lily_lexer::escaped_char (char c) const
229 {
230   switch (c)
231     {
232     case 'n':
233       return '\n';
234     case 't':
235       return '\t';
236
237     case '\'':
238     case '\"':
239     case '\\':
240       return c;
241     }
242   return 0;
243 }
244
245 Input
246 My_lily_lexer::here_input () const
247 {
248   Source_file * f= get_source_file ();
249   return Input (f, (char*)here_str0 ());
250 }
251
252 void
253 My_lily_lexer::prepare_for_next_token ()
254 {
255   last_input_ = here_input ();
256 }
257
258 void
259 My_lily_lexer::set_encoding (String s)
260 {
261   if (s.length ())
262     encoding_ = ly_symbol2scm (s.to_str0 ());
263   else
264     encoding_ = SCM_EOL;
265 }