]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
* lily/my-lily-lexer.cc (start_main_input): define input-file-name
[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   {"accepts", ACCEPTS},
30   {"addlyrics", ADDLYRICS},
31   {"alias", ALIAS},
32   {"alternative", ALTERNATIVE},
33   {"apply", APPLY},
34   {"applycontext", APPLYCONTEXT},
35   {"applyoutput", APPLYOUTPUT},
36   {"autochange", AUTOCHANGE},
37   {"bar", BAR},
38   {"breathe", BREATHE},
39   {"chordmodifiers", CHORDMODIFIERS},
40   {"chords", CHORDS},
41   {"clef", CLEF},
42   {"consists", CONSISTS},
43   {"consistsend", CONSISTSEND},
44   {"context", CONTEXT},
45   {"default", DEFAULT},
46   {"denies", DENIES},
47   {"description", DESCRIPTION},
48   {"duration", DURATION},
49   {"figures",FIGURES},
50   {"grace", GRACE},
51   {"grobdescriptions", GROBDESCRIPTIONS},
52   {"header", HEADER},
53   {"key", KEY},
54   {"lyrics", LYRICS},
55   {"mark", MARK},
56   {"markup", MARKUP},
57   {"midi", MIDI},
58   {"name", NAME},
59   {"notes", NOTES},
60   {"octave", OCTAVE},
61   {"once", ONCE},
62   {"outputproperty", OUTPUTPROPERTY},
63   {"override", OVERRIDE},
64   {"paper", PAPER},
65   {"partcombine", PARTCOMBINE},
66   {"partial", PARTIAL},
67   {"pitch", PITCH},
68   {"pitchnames", PITCHNAMES},
69   {"property", PROPERTY},
70   {"relative", RELATIVE},
71   {"remove", REMOVE},
72   {"repeat", REPEAT},
73   {"rest", REST},
74   {"revert", REVERT},
75   {"score", SCORE},
76   {"sequential", SEQUENTIAL},
77   {"set", SET},
78   {"simultaneous", SIMULTANEOUS},
79   {"skip", SKIP},
80   {"tempo", TEMPO},
81   {"time", TIME_T},
82   {"times", TIMES},
83   {"translator", TRANSLATOR},
84   {"transpose", TRANSPOSE},
85   {"type", TYPE},
86   {"unset", UNSET},
87   {0,0}
88 };
89
90
91 My_lily_lexer::My_lily_lexer ()
92 {
93   keytable_ = new Keyword_table (the_key_tab);
94   scopes_ = SCM_EOL;
95   
96   add_scope(ly_make_anonymous_module());
97   errorlevel_ =0; 
98
99   main_input_b_ = false;
100 }
101
102 void
103 My_lily_lexer::add_scope (SCM module)
104 {
105   ly_reexport_module (scm_current_module());
106   scm_set_current_module (module);
107   for (SCM s = scopes_; gh_pair_p (s); s = gh_cdr (s))
108     {
109       /*
110         UGH. how to do this more neatly? 
111       */      
112       SCM expr = scm_list_n (ly_symbol2scm ("module-use!"),
113                              module, scm_list_n (ly_symbol2scm ("module-public-interface"),
114                                                  gh_car (s), SCM_UNDEFINED),
115                              SCM_UNDEFINED);
116       
117       scm_primitive_eval(expr);
118     }
119   
120   scopes_ = scm_cons (module, scopes_);
121 }
122
123 SCM
124 My_lily_lexer::remove_scope ()
125 {
126   SCM sc = gh_car (scopes_);
127   scopes_ = gh_cdr (scopes_);
128   scm_set_current_module (gh_car (scopes_));
129
130   return sc;
131 }
132
133
134 int
135 My_lily_lexer::lookup_keyword (String s)
136 {
137   return keytable_->lookup (s.to_str0 ());
138 }
139
140 SCM
141 My_lily_lexer::lookup_identifier (String s)
142 {
143   SCM sym = ly_symbol2scm (s.to_str0());
144   for (SCM s = scopes_; gh_pair_p (s); s = gh_cdr (s))
145     {
146       SCM var = ly_module_lookup (gh_car (s), sym);
147       if (var != SCM_BOOL_F)
148         return scm_variable_ref(var);
149     }
150
151   return SCM_UNDEFINED;
152 }
153
154 void
155 My_lily_lexer::start_main_input ()
156 {  
157   new_input (main_input_string_, &global_input_file->sources_);
158   allow_includes_b_ = allow_includes_b_ &&  ! (safe_global_b);
159
160   scm_module_define (gh_car (scopes_),
161                      ly_symbol2scm ("input-file-name"),
162                      scm_makfrom0str (main_input_string_.to_str0()));
163 }
164
165 void
166 My_lily_lexer::set_identifier (SCM name, SCM s)
167 {
168   assert (gh_string_p (name));
169   
170   if (lookup_keyword (ly_scm2string (name)) >= 0)
171     {
172       size_t sz;
173       char * str = gh_scm2newstr (name, &sz) ;
174       warning (_f ("Identifier name is a keyword: `%s'", str));
175       free  (str);
176     }
177
178   SCM sym = scm_string_to_symbol (name);
179   SCM mod = gh_car (scopes_);
180
181   scm_module_define (mod, sym, s);
182 }
183
184 My_lily_lexer::~My_lily_lexer ()
185 {
186   delete keytable_;
187 }
188
189
190
191 void
192 My_lily_lexer::LexerError (char const *s)
193 {
194   if (include_stack_.empty ())
195     {
196       progress_indication (_f ("error at EOF: %s", s)+ String ("\n"));
197     }
198   else
199     {
200       errorlevel_ |= 1;
201       Input spot (get_source_file (), here_str0 ());
202       spot.error (s);
203     }
204 }
205
206 char
207 My_lily_lexer::escaped_char (char c) const
208 {
209   switch (c)
210     {
211     case 'n':
212       return '\n';
213     case 't':
214       return '\t';
215
216     case '\'':
217     case '\"':
218     case '\\':
219       return c;
220     }
221   return 0;
222 }
223
224 Input
225 My_lily_lexer::here_input () const
226 {
227   Source_file * f= get_source_file ();
228   return Input (f, (char*)here_str0 ());
229 }
230
231 void
232 My_lily_lexer::prepare_for_next_token ()
233 {
234   last_input_ = here_input();
235 }
236
237 #if 0
238 SCM
239 My_lily_lexer::scan_markup_word (String s)
240 {
241   /*
242     TODO: better implementation:
243
244     - make a table of markup functions, for quicker lookup
245
246     - error handling.
247     
248    */
249   SCM s = scm_c_eval_str ((s + "-markup").to_str0());
250   yylval.scm = s;
251   return MARKUP_HEAD;
252 }
253 #endif