]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
* lily/my-lily-lexer.cc (My_lily_lexer): copy keytable.
[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   {"consistsend", CONSISTSEND},
40   {"context", CONTEXT},
41   {"default", DEFAULT},
42   {"denies", DENIES},
43   {"drums", DRUMS},
44   {"description", DESCRIPTION},
45   {"figures",FIGURES},
46   {"grobdescriptions", GROBDESCRIPTIONS},
47   {"header", HEADER},
48   {"key", KEY},
49   {"lyrics", LYRICS},
50   {"lyricsto", LYRICSTO},
51   {"mark", MARK},
52   {"markup", MARKUP},
53   {"midi", MIDI},
54   {"name", NAME},
55   {"new", NEWCONTEXT},
56   {"newlyrics", NEWLYRICS},
57   {"notes", NOTES},
58   {"octave", OCTAVE},
59   {"once", ONCE},
60   {"override", OVERRIDE},
61   {"paper", PAPER},
62   {"partial", PARTIAL},
63   {"quote", QUOTE},
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 My_lily_lexer::My_lily_lexer (Sources *sources)
88   
89 {
90   keytable_ = new Keyword_table (the_key_tab);
91   encoding_ = SCM_EOL;
92   chordmodifier_tab_ = scm_make_vector (scm_int2num (1), SCM_EOL);
93   pitchname_tab_stack_ = SCM_EOL; 
94   sources_ = sources;
95   scopes_ = SCM_EOL;
96   error_level_ = 0; 
97   main_input_b_ = false;
98   
99   add_scope (ly_make_anonymous_module (false));
100 }
101
102 My_lily_lexer::My_lily_lexer (My_lily_lexer const &src)
103   : Includable_lexer ()
104 {
105   keytable_ = new Keyword_table (*src.keytable_);
106   encoding_ = src.encoding_;
107   chordmodifier_tab_ = src.chordmodifier_tab_;
108   pitchname_tab_stack_ = src.pitchname_tab_stack_;
109   sources_ = src.sources_;
110   scopes_ = src.scopes_;
111   error_level_ = src.error_level_; 
112   main_input_b_ = src.main_input_b_;
113 }
114
115 My_lily_lexer::~My_lily_lexer ()
116 {
117   delete keytable_;
118 }
119
120
121
122 SCM
123 My_lily_lexer::encoding () const
124 {
125   return encoding_ ;
126 }
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       ly_use_module (module, ly_car (s));
137     }
138   scopes_ = scm_cons (module, scopes_);
139 }
140
141 SCM
142 My_lily_lexer::remove_scope ()
143 {
144   SCM sc = ly_car (scopes_);
145   scopes_ = ly_cdr (scopes_);
146   scm_set_current_module (ly_car (scopes_));
147
148   return sc;
149 }
150
151
152 int
153 My_lily_lexer::lookup_keyword (String s)
154 {
155   return keytable_->lookup (s.to_str0 ());
156 }
157
158 SCM
159 My_lily_lexer::lookup_identifier_symbol (SCM sym)
160 {
161   for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
162     {
163       SCM var = ly_module_lookup (ly_car (s), sym);
164       if (var != SCM_BOOL_F)
165         return scm_variable_ref (var);
166     }
167
168   return SCM_UNDEFINED;
169 }
170
171 SCM
172 My_lily_lexer::lookup_identifier (String name)
173 {
174   return lookup_identifier_symbol ( ly_symbol2scm (name.to_str0 ()));
175 }
176
177 void
178 My_lily_lexer::start_main_input ()
179 {
180   // yy_flex_debug = 1;
181   new_input (main_input_name_, sources_);
182   /* Do not allow \include in --safe-mode */
183   allow_includes_b_ = allow_includes_b_ && ! safe_global_b;
184
185   scm_module_define (ly_car (scopes_),
186                      ly_symbol2scm ("input-file-name"),
187                      scm_makfrom0str (main_input_name_.to_str0 ()));
188 }
189
190 void
191 My_lily_lexer::set_identifier (SCM name, SCM s)
192 {
193   SCM sym = name;
194   if (ly_c_string_p (name))
195     sym =  scm_string_to_symbol (name);
196   
197   if (ly_c_symbol_p (sym))
198     {
199       if (lookup_keyword (ly_symbol2string (sym)) >= 0)
200         {
201           warning (_f ("Identifier name is a keyword: `%s'", SCM_SYMBOL_CHARS (sym)));
202         }
203
204       SCM mod = ly_car (scopes_);
205
206       scm_module_define (mod, sym, s);
207     }
208   
209   else
210     {
211       programming_error ("Identifier is not a symbol.");
212     }
213 }
214
215
216
217 void
218 My_lily_lexer::LexerError (char const *s)
219 {
220   if (include_stack_.is_empty ())
221     progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
222   else
223     {
224       error_level_ |= 1;
225       Input spot (get_source_file (), here_str0 ());
226       spot.error (s);
227     }
228 }
229
230 char
231 My_lily_lexer::escaped_char (char c) const
232 {
233   switch (c)
234     {
235     case 'n':
236       return '\n';
237     case 't':
238       return '\t';
239
240     case '\'':
241     case '\"':
242     case '\\':
243       return c;
244     }
245   return 0;
246 }
247
248 Input
249 My_lily_lexer::here_input () const
250 {
251   Source_file * f= get_source_file ();
252   return Input (f, (char*)here_str0 ());
253 }
254
255 void
256 My_lily_lexer::prepare_for_next_token ()
257 {
258   last_input_ = here_input ();
259 }
260
261 void
262 My_lily_lexer::set_encoding (String s)
263 {
264   if (s.length ())
265     encoding_ = ly_symbol2scm (s.to_str0 ());
266   else
267     encoding_ = SCM_EOL;
268 }