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