]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
ae20b4d8efcc851ba269fa1c10ac85e89ea74e34
[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", 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 (Sources *srcs)
96 {
97   keytable_ = new Keyword_table (the_key_tab);
98   encoding_ = SCM_EOL;
99   chordmodifier_tab_ = scm_make_vector (scm_int2num (1), SCM_EOL);
100   pitchname_tab_stack_ = SCM_EOL; 
101   sources_ = srcs;
102   scopes_ = SCM_EOL;
103   
104   add_scope (ly_make_anonymous_module ());
105   errorlevel_ = 0; 
106
107   main_input_b_ = false;
108 }
109
110 SCM
111 My_lily_lexer::encoding () const
112 {
113   return encoding_ ;
114 }
115
116 void
117 My_lily_lexer::add_scope (SCM module)
118 {
119   ly_reexport_module (scm_current_module ());
120   scm_set_current_module (module);
121   for (SCM s = scopes_; is_pair (s); s = ly_cdr (s))
122     {
123       /* UGH. how to do this more neatly? */      
124       SCM expr
125         = scm_list_3 (ly_symbol2scm ("module-use!"),
126                       module,
127                       scm_list_2 (ly_symbol2scm ("module-public-interface"),
128                                   ly_car (s)));
129       scm_primitive_eval (expr);
130     }
131   scopes_ = scm_cons (module, scopes_);
132 }
133
134 SCM
135 My_lily_lexer::remove_scope ()
136 {
137   SCM sc = ly_car (scopes_);
138   scopes_ = ly_cdr (scopes_);
139   scm_set_current_module (ly_car (scopes_));
140
141   return sc;
142 }
143
144
145 int
146 My_lily_lexer::lookup_keyword (String s)
147 {
148   return keytable_->lookup (s.to_str0 ());
149 }
150
151 SCM
152 My_lily_lexer::lookup_identifier (String s)
153 {
154   SCM sym = ly_symbol2scm (s.to_str0());
155   for (SCM s = scopes_; is_pair (s); s = ly_cdr (s))
156     {
157       SCM var = ly_module_lookup (ly_car (s), sym);
158       if (var != SCM_BOOL_F)
159         return scm_variable_ref (var);
160     }
161
162   return SCM_UNDEFINED;
163 }
164
165 void
166 My_lily_lexer::start_main_input ()
167 {
168   // yy_flex_debug = 1;
169   new_input (main_input_name_, sources_);
170   /* Do not allow \include in --safe-mode */
171   allow_includes_b_ = allow_includes_b_ && ! safe_global_b;
172
173   scm_module_define (ly_car (scopes_),
174                      ly_symbol2scm ("input-file-name"),
175                      scm_makfrom0str (main_input_name_.to_str0 ()));
176 }
177
178 void
179 My_lily_lexer::set_identifier (SCM name, SCM s)
180 {
181   assert (is_string (name));
182   
183   if (lookup_keyword (ly_scm2string (name)) >= 0)
184     {
185       warning (_f ("Identifier name is a keyword: `%s'", SCM_STRING_CHARS (name)));
186     }
187
188   SCM sym = scm_string_to_symbol (name);
189   SCM mod = ly_car (scopes_);
190
191   scm_module_define (mod, sym, s);
192 }
193
194 My_lily_lexer::~My_lily_lexer ()
195 {
196   delete keytable_;
197 }
198
199
200
201 void
202 My_lily_lexer::LexerError (char const *s)
203 {
204   if (include_stack_.is_empty ())
205     progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
206   else
207     {
208       errorlevel_ |= 1;
209       Input spot (get_source_file (), here_str0 ());
210       spot.error (s);
211     }
212 }
213
214 char
215 My_lily_lexer::escaped_char (char c) const
216 {
217   switch (c)
218     {
219     case 'n':
220       return '\n';
221     case 't':
222       return '\t';
223
224     case '\'':
225     case '\"':
226     case '\\':
227       return c;
228     }
229   return 0;
230 }
231
232 Input
233 My_lily_lexer::here_input () const
234 {
235   Source_file * f= get_source_file ();
236   return Input (f, (char*)here_str0 ());
237 }
238
239 void
240 My_lily_lexer::prepare_for_next_token ()
241 {
242   last_input_ = here_input ();
243 }
244
245 void
246 My_lily_lexer::set_encoding (String s)
247 {
248   if (s.length ())
249     encoding_ = ly_symbol2scm (s.to_str0 ());
250   else
251     encoding_ = SCM_EOL;
252 }