]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
dc91e7d96fd4662aabcfedaebb744e81fb1379ec
[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-module.hh"
26
27
28 static Keyword_ent the_key_tab[] = {
29   {"acciaccatura", ACCIACCATURA},
30   {"accepts", ACCEPTS},
31   {"addlyrics", ADDLYRICS},
32   {"addquote", ADDQUOTE},
33   {"alias", ALIAS},
34   {"alternative", ALTERNATIVE},
35   {"apply", APPLY},
36   {"applycontext", APPLYCONTEXT},
37   {"applyoutput", APPLYOUTPUT},
38   {"appoggiatura", APPOGGIATURA},
39   {"autochange", AUTOCHANGE},
40   {"bar", BAR},
41   {"book", BOOK},
42   {"breathe", BREATHE},
43   {"change", CHANGE},
44   {"chords", CHORDS},
45   {"clef", CLEF},
46   {"consists", CONSISTS},
47   {"consistsend", CONSISTSEND},
48   {"context", CONTEXT},
49   {"default", DEFAULT},
50   {"denies", DENIES},
51   {"drums", DRUMS},
52   {"description", DESCRIPTION},
53   {"figures",FIGURES},
54   {"grace", GRACE},
55   {"grobdescriptions", GROBDESCRIPTIONS},
56   {"header", HEADER},
57   {"key", KEY},
58   {"lyrics", LYRICS},
59   {"lyricsto", NEWADDLYRICS},
60   {"mark", MARK},
61   {"markup", MARKUP},
62   {"midi", MIDI},
63   {"name", NAME},
64   {"new", NEWCONTEXT},
65   {"notes", NOTES},
66   {"octave", OCTAVE},
67   {"once", ONCE},
68   {"override", OVERRIDE},
69   {"paper", PAPER},
70   {"partcombine", PARTCOMBINE},
71   {"partial", PARTIAL},
72   {"quote", QUOTE},
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   {"transpose", TRANSPOSE},
88   {"transposition", TRANSPOSITION},
89   {"type", TYPE},
90   {"unset", UNSET},
91   {"with", WITH},
92   {0, 0}
93 };
94
95
96 My_lily_lexer::My_lily_lexer (Sources *srcs)
97 {
98   keytable_ = new Keyword_table (the_key_tab);
99   encoding_ = SCM_EOL;
100   chordmodifier_tab_ = scm_make_vector (scm_int2num (1), SCM_EOL);
101   pitchname_tab_stack_ = SCM_EOL; 
102   sources_ = srcs;
103   scopes_ = SCM_EOL;
104   
105   add_scope (ly_make_anonymous_module ());
106   errorlevel_ = 0; 
107
108   main_input_b_ = false;
109 }
110
111 SCM
112 My_lily_lexer::encoding () const
113 {
114   return encoding_ ;
115 }
116
117 void
118 My_lily_lexer::add_scope (SCM module)
119 {
120   ly_reexport_module (scm_current_module ());
121   scm_set_current_module (module);
122   for (SCM s = scopes_; is_pair (s); s = ly_cdr (s))
123     {
124       /* UGH. how to do this more neatly? */      
125       SCM expr
126         = scm_list_3 (ly_symbol2scm ("module-use!"),
127                       module,
128                       scm_list_2 (ly_symbol2scm ("module-public-interface"),
129                                   ly_car (s)));
130       scm_primitive_eval (expr);
131     }
132   scopes_ = scm_cons (module, scopes_);
133 }
134
135 SCM
136 My_lily_lexer::remove_scope ()
137 {
138   SCM sc = ly_car (scopes_);
139   scopes_ = ly_cdr (scopes_);
140   scm_set_current_module (ly_car (scopes_));
141
142   return sc;
143 }
144
145
146 int
147 My_lily_lexer::lookup_keyword (String s)
148 {
149   return keytable_->lookup (s.to_str0 ());
150 }
151
152 SCM
153 My_lily_lexer::lookup_identifier (String s)
154 {
155   SCM sym = ly_symbol2scm (s.to_str0());
156   for (SCM s = scopes_; is_pair (s); s = ly_cdr (s))
157     {
158       SCM var = ly_module_lookup (ly_car (s), sym);
159       if (var != SCM_BOOL_F)
160         return scm_variable_ref (var);
161     }
162
163   return SCM_UNDEFINED;
164 }
165
166 void
167 My_lily_lexer::start_main_input ()
168 {
169   // yy_flex_debug = 1;
170   new_input (main_input_name_, sources_);
171   /* Do not allow \include in --safe-mode */
172   allow_includes_b_ = allow_includes_b_ && ! safe_global_b;
173
174   scm_module_define (ly_car (scopes_),
175                      ly_symbol2scm ("input-file-name"),
176                      scm_makfrom0str (main_input_name_.to_str0 ()));
177 }
178
179 void
180 My_lily_lexer::set_identifier (SCM name, SCM s)
181 {
182   assert (is_string (name));
183   
184   if (lookup_keyword (ly_scm2string (name)) >= 0)
185     {
186       warning (_f ("Identifier name is a keyword: `%s'", SCM_STRING_CHARS (name)));
187     }
188
189   SCM sym = scm_string_to_symbol (name);
190   SCM mod = ly_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_.is_empty ())
206     progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
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 }
245
246 void
247 My_lily_lexer::set_encoding (String s)
248 {
249   if (s.length ())
250     encoding_ = ly_symbol2scm (s.to_str0 ());
251   else
252     encoding_ = SCM_EOL;
253 }