]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
48f08e8e98652677eede2f50e10bd6915c659954
[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--2003 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-modules.hh"
26
27
28 static Keyword_ent the_key_tab[]={
29   {"accacciatura", ACCACCIATURA},
30   {"accepts", ACCEPTS},
31   {"addlyrics", ADDLYRICS},
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   {"breathe", BREATHE},
41   {"chordmodifiers", CHORDMODIFIERS},
42   {"chords", CHORDS},
43   {"clef", CLEF},
44   {"consists", CONSISTS},
45   {"consistsend", CONSISTSEND},
46   {"context", CONTEXT},
47   {"default", DEFAULT},
48   {"denies", DENIES},
49   {"description", DESCRIPTION},
50   {"duration", DURATION},
51   {"figures",FIGURES},
52   {"grace", GRACE},
53   {"grobdescriptions", GROBDESCRIPTIONS},
54   {"header", HEADER},
55   {"key", KEY},
56   {"lyrics", LYRICS},
57   {"mark", MARK},
58   {"markup", MARKUP},
59   {"midi", MIDI},
60   {"name", NAME},
61   {"new", NEWCONTEXT},
62   {"notes", NOTES},
63   {"octave", OCTAVE},
64   {"once", ONCE},
65   {"outputproperty", OUTPUTPROPERTY},
66   {"override", OVERRIDE},
67   {"paper", PAPER},
68   {"partcombine", PARTCOMBINE},
69   {"partial", PARTIAL},
70   {"pitch", PITCH},
71   {"pitchnames", PITCHNAMES},
72   {"property", PROPERTY},
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   {"tempo", TEMPO},
84   {"time", TIME_T},
85   {"times", TIMES},
86   {"translator", TRANSLATOR},
87   {"transpose", TRANSPOSE},
88   {"type", TYPE},
89   {"unset", UNSET},
90   {0,0}
91 };
92
93
94 My_lily_lexer::My_lily_lexer ()
95 {
96   keytable_ = new Keyword_table (the_key_tab);
97   scopes_ = SCM_EOL;
98   
99   add_scope(ly_make_anonymous_module());
100   errorlevel_ =0; 
101
102   main_input_b_ = false;
103 }
104
105 void
106 My_lily_lexer::add_scope (SCM module)
107 {
108   ly_reexport_module (scm_current_module());
109   scm_set_current_module (module);
110   for (SCM s = scopes_; gh_pair_p (s); s = gh_cdr (s))
111     {
112       /*
113         UGH. how to do this more neatly? 
114       */      
115       SCM expr = scm_list_n (ly_symbol2scm ("module-use!"),
116                              module, scm_list_n (ly_symbol2scm ("module-public-interface"),
117                                                  gh_car (s), SCM_UNDEFINED),
118                              SCM_UNDEFINED);
119       
120       scm_primitive_eval(expr);
121     }
122   
123   scopes_ = scm_cons (module, scopes_);
124 }
125
126 SCM
127 My_lily_lexer::remove_scope ()
128 {
129   SCM sc = gh_car (scopes_);
130   scopes_ = gh_cdr (scopes_);
131   scm_set_current_module (gh_car (scopes_));
132
133   return sc;
134 }
135
136
137 int
138 My_lily_lexer::lookup_keyword (String s)
139 {
140   return keytable_->lookup (s.to_str0 ());
141 }
142
143 SCM
144 My_lily_lexer::lookup_identifier (String s)
145 {
146   SCM sym = ly_symbol2scm (s.to_str0());
147   for (SCM s = scopes_; gh_pair_p (s); s = gh_cdr (s))
148     {
149       SCM var = ly_module_lookup (gh_car (s), sym);
150       if (var != SCM_BOOL_F)
151         return scm_variable_ref(var);
152     }
153
154   return SCM_UNDEFINED;
155 }
156
157 void
158 My_lily_lexer::start_main_input ()
159 {  
160   new_input (main_input_string_, &global_input_file->sources_);
161   allow_includes_b_ = allow_includes_b_ &&  ! (safe_global_b);
162
163   scm_module_define (gh_car (scopes_),
164                      ly_symbol2scm ("input-file-name"),
165                      scm_makfrom0str (main_input_string_.to_str0()));
166 }
167
168 void
169 My_lily_lexer::set_identifier (SCM name, SCM s)
170 {
171   assert (gh_string_p (name));
172   
173   if (lookup_keyword (ly_scm2string (name)) >= 0)
174     {
175       size_t sz;
176       char * str = gh_scm2newstr (name, &sz) ;
177       warning (_f ("Identifier name is a keyword: `%s'", str));
178       free  (str);
179     }
180
181   SCM sym = scm_string_to_symbol (name);
182   SCM mod = gh_car (scopes_);
183
184   scm_module_define (mod, sym, s);
185 }
186
187 My_lily_lexer::~My_lily_lexer ()
188 {
189   delete keytable_;
190 }
191
192
193
194 void
195 My_lily_lexer::LexerError (char const *s)
196 {
197   if (include_stack_.empty ())
198     {
199       progress_indication (_f ("error at EOF: %s", s)+ String ("\n"));
200     }
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 #if 0
241 SCM
242 My_lily_lexer::scan_markup_word (String s)
243 {
244   /*
245     TODO: better implementation:
246
247     - make a table of markup functions, for quicker lookup
248
249     - error handling.
250     
251    */
252   SCM s = scm_c_eval_str ((s + "-markup").to_str0());
253   yylval.scm = s;
254   return MARKUP_HEAD;
255 }
256 #endif