]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
new file, move from
[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 "lily-guile.hh"
16 #include "parser.hh"
17 #include "keyword.hh"
18 #include "my-lily-lexer.hh"
19 #include "warn.hh"
20 #include "source-file.hh"
21 #include "main.hh"
22 #include "input.hh"
23 #include "moment.hh"
24 #include "ly-module.hh"
25
26
27 static Keyword_ent the_key_tab[] = {
28   {"accepts", ACCEPTS},
29   {"addquote", ADDQUOTE},
30   {"alias", ALIAS},
31   {"alternative", ALTERNATIVE},
32   {"bar", BAR},
33   {"book", BOOK},
34   {"bookpaper", BOOKPAPER},
35   {"change", CHANGE},
36   {"chords", CHORDS},
37   {"clef", CLEF},
38   {"consists", CONSISTS},
39   {"consistsend", CONSISTSEND},
40   {"context", CONTEXT},
41   {"default", DEFAULT},
42   {"denies", DENIES},
43   {"drums", DRUMS},
44   {"description", DESCRIPTION},
45   {"figures",FIGURES},
46   {"grobdescriptions", GROBDESCRIPTIONS},
47   {"header", HEADER},
48   {"key", KEY},
49   {"lyrics", LYRICS},
50   {"lyricsto", LYRICSTO},
51   {"mark", MARK},
52   {"markup", MARKUP},
53   {"midi", MIDI},
54   {"name", NAME},
55   {"new", NEWCONTEXT},
56   {"newlyrics", NEWLYRICS},
57   {"notes", NOTES},
58   {"octave", OCTAVE},
59   {"once", ONCE},
60   {"override", OVERRIDE},
61   {"paper", PAPER},
62   {"partial", PARTIAL},
63   {"quote", QUOTE},
64   {"relative", RELATIVE},
65   {"remove", REMOVE},
66   {"repeat", REPEAT},
67   {"rest", REST},
68   {"revert", REVERT},
69   {"score", SCORE},
70   {"sequential", SEQUENTIAL},
71   {"set", SET},
72   {"simultaneous", SIMULTANEOUS},
73   {"skip", SKIP},
74   {"tag", TAG},
75   {"tempo", TEMPO},
76   {"time", TIME_T},
77   {"times", TIMES},
78   {"transpose", TRANSPOSE},
79   {"transposition", TRANSPOSITION},
80   {"type", TYPE},
81   {"unset", UNSET},
82   {"with", WITH},
83   {0, 0}
84 };
85
86
87 My_lily_lexer::My_lily_lexer (Sources *sources)
88   
89 {
90   keytable_ = new Keyword_table (the_key_tab);
91   encoding_ = SCM_EOL;
92   chordmodifier_tab_ = scm_make_vector (scm_int2num (1), SCM_EOL);
93   pitchname_tab_stack_ = SCM_EOL; 
94   sources_ = sources;
95   scopes_ = SCM_EOL;
96   error_level_ = 0; 
97   main_input_b_ = false;
98   
99   add_scope (ly_make_anonymous_module (false));
100 }
101
102 My_lily_lexer::My_lily_lexer (My_lily_lexer const &src)
103   : Includable_lexer ()
104 {
105   keytable_ = src.keytable_;
106   encoding_ = src.encoding_;
107   chordmodifier_tab_ = src.chordmodifier_tab_;
108   pitchname_tab_stack_ = src.pitchname_tab_stack_;
109   sources_ = src.sources_;
110   scopes_ = src.scopes_;
111   error_level_ = src.error_level_; 
112   main_input_b_ = src.main_input_b_;
113 }
114
115 SCM
116 My_lily_lexer::encoding () const
117 {
118   return encoding_ ;
119 }
120
121
122 void
123 My_lily_lexer::add_scope (SCM module)
124 {
125   ly_reexport_module (scm_current_module ());
126   scm_set_current_module (module);
127   for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
128     {
129       ly_use_module (module, ly_car (s));
130     }
131   scopes_ = scm_cons (module, scopes_);
132 }
133
134 SCM
135 My_lily_lexer::remove_scope ()
136 {
137   SCM sc = ly_car (scopes_);
138   scopes_ = ly_cdr (scopes_);
139   scm_set_current_module (ly_car (scopes_));
140
141   return sc;
142 }
143
144
145 int
146 My_lily_lexer::lookup_keyword (String s)
147 {
148   return keytable_->lookup (s.to_str0 ());
149 }
150
151 SCM
152 My_lily_lexer::lookup_identifier (String name)
153 {
154   SCM sym = ly_symbol2scm (name.to_str0 ());
155   for (SCM s = scopes_; ly_c_pair_p (s); s = ly_cdr (s))
156     {
157       SCM var = ly_module_lookup (ly_car (s), sym);
158       if (var != SCM_BOOL_F)
159         return scm_variable_ref (var);
160     }
161
162   return SCM_UNDEFINED;
163 }
164
165 void
166 My_lily_lexer::start_main_input ()
167 {
168   // yy_flex_debug = 1;
169   new_input (main_input_name_, sources_);
170   /* Do not allow \include in --safe-mode */
171   allow_includes_b_ = allow_includes_b_ && ! safe_global_b;
172
173   scm_module_define (ly_car (scopes_),
174                      ly_symbol2scm ("input-file-name"),
175                      scm_makfrom0str (main_input_name_.to_str0 ()));
176 }
177
178 void
179 My_lily_lexer::set_identifier (SCM name, SCM s)
180 {
181   SCM sym = name;
182   if (ly_c_string_p (name))
183     sym =  scm_string_to_symbol (name);
184   
185   if (ly_c_symbol_p (sym))
186     {
187       if (lookup_keyword (ly_symbol2string (sym)) >= 0)
188         {
189           warning (_f ("Identifier name is a keyword: `%s'", SCM_SYMBOL_CHARS (sym)));
190         }
191
192       SCM mod = ly_car (scopes_);
193
194       scm_module_define (mod, sym, s);
195     }
196   
197   else
198     {
199       programming_error ("Identifier is not a symbol.");
200     }
201 }
202
203 My_lily_lexer::~My_lily_lexer ()
204 {
205   delete keytable_;
206 }
207
208
209
210 void
211 My_lily_lexer::LexerError (char const *s)
212 {
213   if (include_stack_.is_empty ())
214     progress_indication (_f ("error at EOF: %s", s) + String ("\n"));
215   else
216     {
217       error_level_ |= 1;
218       Input spot (get_source_file (), here_str0 ());
219       spot.error (s);
220     }
221 }
222
223 char
224 My_lily_lexer::escaped_char (char c) const
225 {
226   switch (c)
227     {
228     case 'n':
229       return '\n';
230     case 't':
231       return '\t';
232
233     case '\'':
234     case '\"':
235     case '\\':
236       return c;
237     }
238   return 0;
239 }
240
241 Input
242 My_lily_lexer::here_input () const
243 {
244   Source_file * f= get_source_file ();
245   return Input (f, (char*)here_str0 ());
246 }
247
248 void
249 My_lily_lexer::prepare_for_next_token ()
250 {
251   last_input_ = here_input ();
252 }
253
254 void
255 My_lily_lexer::set_encoding (String s)
256 {
257   if (s.length ())
258     encoding_ = ly_symbol2scm (s.to_str0 ());
259   else
260     encoding_ = SCM_EOL;
261 }