]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
7fa4569c05cdd444ebb30e7cc4cb6f45124ac1f8
[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 (safe_global_b));
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
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       ly_use_module (module, ly_car (s));
138     }
139   scopes_ = scm_cons (module, scopes_);
140 }
141
142 SCM
143 My_lily_lexer::remove_scope ()
144 {
145   SCM sc = ly_car (scopes_);
146   scopes_ = ly_cdr (scopes_);
147   scm_set_current_module (ly_car (scopes_));
148
149   return sc;
150 }
151
152
153 int
154 My_lily_lexer::lookup_keyword (String s)
155 {
156   return keytable_->lookup (s.to_str0 ());
157 }
158
159 SCM
160 My_lily_lexer::lookup_identifier (String name)
161 {
162   SCM sym = ly_symbol2scm (name.to_str0 ());
163   for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
164     {
165       SCM var = ly_module_lookup (ly_car (s), sym);
166       if (var != SCM_BOOL_F)
167         return scm_variable_ref (var);
168     }
169
170   return SCM_UNDEFINED;
171 }
172
173 void
174 My_lily_lexer::start_main_input ()
175 {
176   // yy_flex_debug = 1;
177   new_input (main_input_name_, sources_);
178   /* Do not allow \include in --safe-mode */
179   allow_includes_b_ = allow_includes_b_ && ! safe_global_b;
180
181   scm_module_define (ly_car (scopes_),
182                      ly_symbol2scm ("input-file-name"),
183                      scm_makfrom0str (main_input_name_.to_str0 ()));
184 }
185
186 void
187 My_lily_lexer::set_identifier (SCM name, SCM s)
188 {
189   assert (ly_c_string_p (name));
190   
191   if (lookup_keyword (ly_scm2string (name)) >= 0)
192     {
193       warning (_f ("Identifier name is a keyword: `%s'", SCM_STRING_CHARS (name)));
194     }
195
196   SCM sym = scm_string_to_symbol (name);
197   SCM mod = ly_car (scopes_);
198
199   scm_module_define (mod, sym, s);
200 }
201
202 My_lily_lexer::~My_lily_lexer ()
203 {
204   delete keytable_;
205 }
206
207
208
209 void
210 My_lily_lexer::LexerError (char const *s)
211 {
212   if (include_stack_.is_empty ())
213     progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
214   else
215     {
216       error_level_ |= 1;
217       Input spot (get_source_file (), here_str0 ());
218       spot.error (s);
219     }
220 }
221
222 char
223 My_lily_lexer::escaped_char (char c) const
224 {
225   switch (c)
226     {
227     case 'n':
228       return '\n';
229     case 't':
230       return '\t';
231
232     case '\'':
233     case '\"':
234     case '\\':
235       return c;
236     }
237   return 0;
238 }
239
240 Input
241 My_lily_lexer::here_input () const
242 {
243   Source_file * f= get_source_file ();
244   return Input (f, (char*)here_str0 ());
245 }
246
247 void
248 My_lily_lexer::prepare_for_next_token ()
249 {
250   last_input_ = here_input ();
251 }
252
253 void
254 My_lily_lexer::set_encoding (String s)
255 {
256   if (s.length ())
257     encoding_ = ly_symbol2scm (s.to_str0 ());
258   else
259     encoding_ = SCM_EOL;
260 }