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