]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
2abbd0946e210eb4181c3664244dc0daad2566ad
[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   {"arpeggio", ARPEGGIO },
32   {"autochange", AUTOCHANGE},
33   {"spanrequest", SPANREQUEST},
34   {"commandspanrequest", COMMANDSPANREQUEST},  
35   {"simultaneous", SIMULTANEOUS},
36   {"sequential", SEQUENTIAL},
37   {"accepts", ACCEPTS},
38   {"alternative", ALTERNATIVE},
39   {"bar", BAR},
40   {"breathe", BREATHE},
41   {"char", CHAR_T},
42   {"chordmodifiers", CHORDMODIFIERS},
43   {"chords", CHORDS},
44   {"clef", CLEF},
45   {"cm", CM_T},
46   {"consists", CONSISTS},
47   {"consistsend", CONSISTSEND},
48   {"context", CONTEXT},
49   {"default", DEFAULT},
50   {"denies", DENIES},
51   {"duration", DURATION},
52   {"grobdescriptions", GROBDESCRIPTIONS},
53   {"figures",FIGURES},
54   {"grace", GRACE},
55   {"glissando", GLISSANDO},
56   {"header", HEADER},
57   {"in", IN_T},
58   {"lyrics", LYRICS},
59   {"key", KEY},
60   {"mark", MARK},
61   {"once", ONCE},
62   {"pitch", PITCH},
63   {"time", TIME_T},
64   {"times", TIMES},
65   {"midi", MIDI},
66   {"mm", MM_T},
67   {"name", NAME},
68   {"pitchnames", PITCHNAMES},
69   {"notes", NOTES},
70   {"outputproperty", OUTPUTPROPERTY},
71   {"override", OVERRIDE},
72   {"set", SET},
73   {"rest", REST},
74   {"revert", REVERT},
75   {"partial", PARTIAL},
76   {"paper", PAPER},
77   {"penalty", PENALTY},
78   {"property", PROPERTY},
79   {"pt", PT_T},
80   {"relative", RELATIVE},
81   {"remove", REMOVE},
82   {"repeat", REPEAT},
83   {"addlyrics", ADDLYRICS},
84   {"partcombine", PARTCOMBINE},
85   {"score", SCORE},
86   {"script", SCRIPT},
87   {"stylesheet", STYLESHEET},
88   {"skip", SKIP},
89   {"tempo", TEMPO},
90   {"translator", TRANSLATOR},
91   {"transpose", TRANSPOSE},
92   {"type", TYPE},
93   {"unset", UNSET},
94   {0,0}
95 };
96
97
98 My_lily_lexer::My_lily_lexer ()
99 {
100   keytable_ = new Keyword_table (the_key_tab);
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_UNSPECIFIED;
159 }
160
161 void
162 My_lily_lexer::start_main_input ()
163 {  
164   new_input (main_input_string_, &global_input_file->sources_);
165   allow_includes_b_ = allow_includes_b_ &&  ! (safe_global_b);
166 }
167
168 void
169 My_lily_lexer::set_identifier (SCM name, SCM s)
170 {
171   assert (gh_string_p (name));
172   
173   if (lookup_keyword (ly_scm2string (name)) >= 0)
174     {
175       size_t sz;
176       char * str = gh_scm2newstr (name, &sz) ;
177       warning (_f ("Identifier name is a keyword: `%s'", str));
178       free  (str);
179     }
180
181   SCM sym = scm_string_to_symbol (name);
182   SCM mod = gh_car (scopes_);
183
184   scm_module_define (mod, sym, s);
185 }
186
187 My_lily_lexer::~My_lily_lexer ()
188 {
189   delete keytable_;
190 }
191
192
193
194 void
195 My_lily_lexer::LexerError (char const *s)
196 {
197   if (include_stack_.empty ())
198     {
199       progress_indication (_f ("error at EOF: %s", s)+ String ("\n"));
200     }
201   else
202     {
203       errorlevel_ |= 1;
204       Input spot (get_source_file (), here_str0 ());
205       spot.error (s);
206     }
207 }
208
209 char
210 My_lily_lexer::escaped_char (char c) const
211 {
212   switch (c)
213     {
214     case 'n':
215       return '\n';
216     case 't':
217       return '\t';
218
219     case '\'':
220     case '\"':
221     case '\\':
222       return c;
223     }
224   return 0;
225 }
226
227 Input
228 My_lily_lexer::here_input () const
229 {
230   Source_file * f= get_source_file ();
231   return Input (f, (char*)here_str0 ());
232 }
233
234 void
235 My_lily_lexer::prepare_for_next_token ()
236 {
237   last_input_ = here_input();
238 }