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