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