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