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