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