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