]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
* scm/define-grobs.scm: uniform naming for definitions and output
[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   {"description", DESCRIPTION},
50   {"duration", DURATION},
51   {"grobdescriptions", GROBDESCRIPTIONS},
52   {"figures",FIGURES},
53   {"grace", GRACE},
54   {"header", HEADER},
55   {"lyrics", LYRICS},
56   {"key", KEY},
57   {"mark", MARK},
58   {"markup", MARKUP},
59   {"once", ONCE},
60   {"pitch", PITCH},
61   {"time", TIME_T},
62   {"times", TIMES},
63   {"midi", MIDI},
64   {"name", NAME},
65   {"pitchnames", PITCHNAMES},
66   {"notes", NOTES},
67   {"outputproperty", OUTPUTPROPERTY},
68   {"override", OVERRIDE},
69   {"set", SET},
70   {"rest", REST},
71   {"revert", REVERT},
72   {"partial", PARTIAL},
73   {"paper", PAPER},
74   {"property", PROPERTY},
75   {"relative", RELATIVE},
76   {"remove", REMOVE},
77   {"repeat", REPEAT},
78   {"addlyrics", ADDLYRICS},
79   {"partcombine", PARTCOMBINE},
80   {"score", SCORE},
81   {"skip", SKIP},
82   {"tempo", TEMPO},
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_UNSPECIFIED;
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
161 void
162 My_lily_lexer::set_identifier (SCM name, SCM s)
163 {
164   assert (gh_string_p (name));
165   
166   if (lookup_keyword (ly_scm2string (name)) >= 0)
167     {
168       size_t sz;
169       char * str = gh_scm2newstr (name, &sz) ;
170       warning (_f ("Identifier name is a keyword: `%s'", str));
171       free  (str);
172     }
173
174   SCM sym = scm_string_to_symbol (name);
175   SCM mod = gh_car (scopes_);
176
177   scm_module_define (mod, sym, s);
178 }
179
180 My_lily_lexer::~My_lily_lexer ()
181 {
182   delete keytable_;
183 }
184
185
186
187 void
188 My_lily_lexer::LexerError (char const *s)
189 {
190   if (include_stack_.empty ())
191     {
192       progress_indication (_f ("error at EOF: %s", s)+ String ("\n"));
193     }
194   else
195     {
196       errorlevel_ |= 1;
197       Input spot (get_source_file (), here_str0 ());
198       spot.error (s);
199     }
200 }
201
202 char
203 My_lily_lexer::escaped_char (char c) const
204 {
205   switch (c)
206     {
207     case 'n':
208       return '\n';
209     case 't':
210       return '\t';
211
212     case '\'':
213     case '\"':
214     case '\\':
215       return c;
216     }
217   return 0;
218 }
219
220 Input
221 My_lily_lexer::here_input () const
222 {
223   Source_file * f= get_source_file ();
224   return Input (f, (char*)here_str0 ());
225 }
226
227 void
228 My_lily_lexer::prepare_for_next_token ()
229 {
230   last_input_ = here_input();
231 }
232
233 #if 0
234 SCM
235 My_lily_lexer::scan_markup_word (String s)
236 {
237   /*
238     TODO: better implementation:
239
240     - make a table of markup functions, for quicker lookup
241
242     - error handling.
243     
244    */
245   SCM s = scm_c_eval_str ((s + "-markup").to_str0());
246   yylval.scm = s;
247   return MARKUP_HEAD;
248 }
249 #endif