]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
($(outdir)/%.pdf): add DVIPS_FLAGS. This will
[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--2004 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   {"change", CHANGE},
42   {"chords", CHORDS},
43   {"clef", CLEF},
44   {"consists", CONSISTS},
45   {"consistsend", CONSISTSEND},
46   {"context", CONTEXT},
47   {"default", DEFAULT},
48   {"denies", DENIES},
49   {"drums", DRUMS},
50   {"description", DESCRIPTION},
51   {"figures",FIGURES},
52   {"grace", GRACE},
53   {"grobdescriptions", GROBDESCRIPTIONS},
54   {"header", HEADER},
55   {"key", KEY},
56   {"lyrics", LYRICS},
57   {"lyricsto", NEWADDLYRICS},
58   {"mark", MARK},
59   {"markup", MARKUP},
60   {"midi", MIDI},
61   {"name", NAME},
62   {"new", NEWCONTEXT},
63   {"notes", NOTES},
64   {"octave", OCTAVE},
65   {"once", ONCE},
66   {"override", OVERRIDE},
67   {"paper", PAPER},
68   {"partcombine", PARTCOMBINE},
69   {"partial", PARTIAL},
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   {"with", WITH},
89   {0,0}
90 };
91
92
93 My_lily_lexer::My_lily_lexer ()
94 {
95   //  yy_flex_debug = 1;
96   
97   keytable_ = new Keyword_table (the_key_tab);
98
99   chordmodifier_tab_ = scm_make_vector (gh_int2scm (1), SCM_EOL);
100   pitchname_tab_stack_ = SCM_EOL; 
101   
102   scopes_ = SCM_EOL;
103   
104   add_scope(ly_make_anonymous_module());
105   errorlevel_ =0; 
106
107   main_input_b_ = false;
108 }
109
110 void
111 My_lily_lexer::add_scope (SCM module)
112 {
113   ly_reexport_module (scm_current_module ());
114   scm_set_current_module (module);
115   for (SCM s = scopes_; gh_pair_p (s); s = gh_cdr (s))
116     {
117       /*
118         UGH. how to do this more neatly? 
119       */      
120       SCM expr = scm_list_n (ly_symbol2scm ("module-use!"),
121                              module, scm_list_n (ly_symbol2scm ("module-public-interface"),
122                                                  gh_car (s), SCM_UNDEFINED),
123                              SCM_UNDEFINED);
124       
125       scm_primitive_eval(expr);
126     }
127   
128   scopes_ = scm_cons (module, scopes_);
129 }
130
131 SCM
132 My_lily_lexer::remove_scope ()
133 {
134   SCM sc = gh_car (scopes_);
135   scopes_ = gh_cdr (scopes_);
136   scm_set_current_module (gh_car (scopes_));
137
138   return sc;
139 }
140
141
142 int
143 My_lily_lexer::lookup_keyword (String s)
144 {
145   return keytable_->lookup (s.to_str0 ());
146 }
147
148 SCM
149 My_lily_lexer::lookup_identifier (String s)
150 {
151   SCM sym = ly_symbol2scm (s.to_str0());
152   for (SCM s = scopes_; gh_pair_p (s); s = gh_cdr (s))
153     {
154       SCM var = ly_module_lookup (gh_car (s), sym);
155       if (var != SCM_BOOL_F)
156         return scm_variable_ref(var);
157     }
158
159   return SCM_UNDEFINED;
160 }
161
162 void
163 My_lily_lexer::start_main_input ()
164 {  
165   new_input (main_input_name_, &global_input_file->sources_);
166   allow_includes_b_ = allow_includes_b_ && ! safe_global_b;
167
168   scm_module_define (gh_car (scopes_),
169                      ly_symbol2scm ("input-file-name"),
170                      scm_makfrom0str (main_input_name_.to_str0 ()));
171 }
172
173 void
174 My_lily_lexer::set_identifier (SCM name, SCM s)
175 {
176   assert (gh_string_p (name));
177   
178   if (lookup_keyword (ly_scm2string (name)) >= 0)
179     {
180       size_t sz;
181       char * str = gh_scm2newstr (name, &sz) ;
182       warning (_f ("Identifier name is a keyword: `%s'", str));
183       free  (str);
184     }
185
186   SCM sym = scm_string_to_symbol (name);
187   SCM mod = gh_car (scopes_);
188
189   scm_module_define (mod, sym, s);
190 }
191
192 My_lily_lexer::~My_lily_lexer ()
193 {
194   delete keytable_;
195 }
196
197
198
199 void
200 My_lily_lexer::LexerError (char const *s)
201 {
202   if (include_stack_.is_empty ())
203     {
204       progress_indication (_f ("error at EOF: %s", s)+ String ("\n"));
205     }
206   else
207     {
208       errorlevel_ |= 1;
209       Input spot (get_source_file (), here_str0 ());
210       spot.error (s);
211     }
212 }
213
214 char
215 My_lily_lexer::escaped_char (char c) const
216 {
217   switch (c)
218     {
219     case 'n':
220       return '\n';
221     case 't':
222       return '\t';
223
224     case '\'':
225     case '\"':
226     case '\\':
227       return c;
228     }
229   return 0;
230 }
231
232 Input
233 My_lily_lexer::here_input () const
234 {
235   Source_file * f= get_source_file ();
236   return Input (f, (char*)here_str0 ());
237 }
238
239 void
240 My_lily_lexer::prepare_for_next_token ()
241 {
242   last_input_ = here_input();
243 }