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