]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
* scm/output-tex.scm (text): remove debugging code.
[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 "input-file-results.hh"
16 #include "lily-guile.hh"
17 #include "parser.hh"
18 #include "keyword.hh"
19 #include "my-lily-lexer.hh"
20 #include "warn.hh"
21 #include "source-file.hh"
22 #include "main.hh"
23 #include "input.hh"
24 #include "moment.hh"
25 #include "ly-module.hh"
26
27
28 static Keyword_ent the_key_tab[] = {
29   {"acciaccatura", ACCIACCATURA},
30   {"accepts", ACCEPTS},
31   {"addlyrics", ADDLYRICS},
32   {"addquote", ADDQUOTE},
33   {"alias", ALIAS},
34   {"alternative", ALTERNATIVE},
35   {"apply", APPLY},
36   {"applycontext", APPLYCONTEXT},
37   {"applyoutput", APPLYOUTPUT},
38   {"appoggiatura", APPOGGIATURA},
39   {"autochange", AUTOCHANGE},
40   {"bar", BAR},
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", NEWADDLYRICS},
59   {"mark", MARK},
60   {"markup", MARKUP},
61   {"midi", MIDI},
62   {"name", NAME},
63   {"new", NEWCONTEXT},
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 ()
96 {
97   keytable_ = new Keyword_table (the_key_tab);
98
99   chordmodifier_tab_ = scm_make_vector (gh_int2scm (1), SCM_EOL);
100   pitchname_tab_stack_ = SCM_EOL; 
101   
102   scopes_ = SCM_EOL;
103   
104   add_scope (ly_make_anonymous_module ());
105   errorlevel_ = 0; 
106
107   main_input_b_ = false;
108 }
109
110 void
111 My_lily_lexer::add_scope (SCM module)
112 {
113   ly_reexport_module (scm_current_module ());
114   scm_set_current_module (module);
115   for (SCM s = scopes_; gh_pair_p (s); s = gh_cdr (s))
116     {
117       /* UGH. how to do this more neatly? */      
118       SCM expr
119         = scm_list_3 (ly_symbol2scm ("module-use!"),
120                       module,
121                       scm_list_2 (ly_symbol2scm ("module-public-interface"),
122                                   gh_car (s)));
123       scm_primitive_eval (expr);
124     }
125   scopes_ = scm_cons (module, scopes_);
126 }
127
128 SCM
129 My_lily_lexer::remove_scope ()
130 {
131   SCM sc = gh_car (scopes_);
132   scopes_ = gh_cdr (scopes_);
133   scm_set_current_module (gh_car (scopes_));
134
135   return sc;
136 }
137
138
139 int
140 My_lily_lexer::lookup_keyword (String s)
141 {
142   return keytable_->lookup (s.to_str0 ());
143 }
144
145 SCM
146 My_lily_lexer::lookup_identifier (String s)
147 {
148   SCM sym = ly_symbol2scm (s.to_str0());
149   for (SCM s = scopes_; gh_pair_p (s); s = gh_cdr (s))
150     {
151       SCM var = ly_module_lookup (gh_car (s), sym);
152       if (var != SCM_BOOL_F)
153         return scm_variable_ref (var);
154     }
155
156   return SCM_UNDEFINED;
157 }
158
159 void
160 My_lily_lexer::start_main_input ()
161 {
162   // yy_flex_debug = 1;
163
164   new_input (main_input_name_, &global_input_file->sources_);
165   /* Do not allow \include in --safe-mode */
166   allow_includes_b_ = allow_includes_b_ && ! safe_global_b;
167
168   scm_module_define (gh_car (scopes_),
169                      ly_symbol2scm ("input-file-name"),
170                      scm_makfrom0str (main_input_name_.to_str0 ()));
171 }
172
173 void
174 My_lily_lexer::set_identifier (SCM name, SCM s)
175 {
176   assert (gh_string_p (name));
177   
178   if (lookup_keyword (ly_scm2string (name)) >= 0)
179     {
180       warning (_f ("Identifier name is a keyword: `%s'", SCM_STRING_CHARS (name)));
181     }
182
183   SCM sym = scm_string_to_symbol (name);
184   SCM mod = gh_car (scopes_);
185
186   scm_module_define (mod, sym, s);
187 }
188
189 My_lily_lexer::~My_lily_lexer ()
190 {
191   delete keytable_;
192 }
193
194
195
196 void
197 My_lily_lexer::LexerError (char const *s)
198 {
199   if (include_stack_.is_empty ())
200     progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
201   else
202     {
203       errorlevel_ |= 1;
204       Input spot (get_source_file (), here_str0 ());
205       spot.error (s);
206     }
207 }
208
209 char
210 My_lily_lexer::escaped_char (char c) const
211 {
212   switch (c)
213     {
214     case 'n':
215       return '\n';
216     case 't':
217       return '\t';
218
219     case '\'':
220     case '\"':
221     case '\\':
222       return c;
223     }
224   return 0;
225 }
226
227 Input
228 My_lily_lexer::here_input () const
229 {
230   Source_file * f= get_source_file ();
231   return Input (f, (char*)here_str0 ());
232 }
233
234 void
235 My_lily_lexer::prepare_for_next_token ()
236 {
237   last_input_ = here_input ();
238 }
239
240 void
241 My_lily_lexer::set_encoding (String s)
242 {
243   encoding_ = s;
244 }