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