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