]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
release: 1.3.23
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <strstream.h>
10 #include <ctype.h>
11 #include "notename-table.hh"
12 #include "interval.hh"
13 #include "identifier.hh"
14 #include "lily-guile.hh"
15 #include "parser.hh"
16 #include "keyword.hh"
17 #include "my-lily-lexer.hh"
18 #include "debug.hh"
19 #include "source-file.hh"
20 #include "main.hh"
21 #include "scope.hh"
22 #include "input.hh"
23 #include "moment.hh"
24
25 static Keyword_ent the_key_tab[]={
26   {"autochange", AUTOCHANGE},
27   {"spanrequest", SPANREQUEST},
28   {"commandspanrequest", COMMANDSPANREQUEST},  
29   {"simultaneous", SIMULTANEOUS},
30   {"sequential", SEQUENTIAL},
31   {"accepts", ACCEPTS},
32   {"alternative", ALTERNATIVE},
33   {"bar", BAR},
34   {"breathe", BREATHE},
35   {"chordmodifiers", CHORDMODIFIERS},
36   {"chords", CHORDS},
37   {"clef", CLEF},
38   {"cm", CM_T},
39   {"consists", CONSISTS},
40   {"consistsend", CONSISTSEND},
41   {"context", CONTEXT},
42   {"duration", DURATION},
43   {"font", FONT},
44   {"grace", GRACE},
45   {"header", HEADER},
46   {"in", IN_T},
47   {"lyrics", LYRICS},
48   {"key", KEY},
49   {"keysignature", KEYSIGNATURE},
50   {"mark", MARK},
51   {"musicalpitch", MUSICAL_PITCH},
52   {"time", TIME_T},
53   {"times", TIMES},
54   {"midi", MIDI},
55   {"mm", MM_T},
56   {"name", NAME},
57   {"notenames", NOTENAMES},
58   {"notes", NOTES},
59   {"partial", PARTIAL},
60   {"paper", PAPER},
61   {"penalty", PENALTY},
62   {"property", PROPERTY},
63   {"pt", PT_T},
64   {"relative", RELATIVE},
65   {"remove", REMOVE},
66   {"repeat", REPEAT},
67   {"repetitions", REPETITIONS},
68   {"addlyrics", ADDLYRICS},
69   {"score", SCORE},
70   {"script", SCRIPT},
71   {"skip", SKIP},
72   {"textscript", TEXTSCRIPT},
73   {"tempo", TEMPO},
74   {"translator", TRANSLATOR},
75   {"transpose", TRANSPOSE},
76   {"type", TYPE},
77   {0,0}
78 };
79
80 My_lily_lexer::My_lily_lexer()
81 {
82   keytable_p_ = new Keyword_table (the_key_tab);
83   toplevel_scope_p_ = new Scope;
84   scope_l_arr_.push (toplevel_scope_p_);
85   errorlevel_i_ = 0;
86   note_tab_p_ = new Notename_table;
87   chordmodifier_tab_p_ = new Notename_table;
88   main_input_b_ = false;
89 }
90
91 int
92 My_lily_lexer::lookup_keyword (String s)
93 {
94   return keytable_p_->lookup (s.ch_C ());
95 }
96
97 Identifier*
98 My_lily_lexer::lookup_identifier (String s)
99 {
100   SCM sym = ly_symbol2scm (s.ch_C());
101   
102   for (int i = scope_l_arr_.size (); i--; )
103     if (scope_l_arr_[i]->elem_b (sym))
104       return scope_l_arr_[i]->elem(sym);
105   return 0;
106 }
107
108 void
109 My_lily_lexer::start_main_input ()
110 {  
111   if (flower_dstream && !flower_dstream->silent_b ("InitDeclarations") && flower_dstream)
112     print_declarations (true);
113   if (flower_dstream && !flower_dstream->silent_b ("InitLexer") && flower_dstream)
114     set_debug (1);
115
116
117   new_input (main_input_str_, source_global_l);
118   allow_includes_b_ = allow_includes_b_ &&  !(safe_global_b);
119   
120   
121   print_declarations(true);
122 }
123
124 void
125 My_lily_lexer::set_identifier (String name_str, Identifier* i, bool )
126 {
127   Identifier *old =0;
128   if (scope_l_arr_.top ()->elem_b (name_str))
129     old = scope_l_arr_.top ()->elem(name_str);
130  
131    
132   if  (old)
133     {
134 #if 0
135       if (unique_b)
136         old->warning(_f ("redeclaration of `\\%s'", name_str));
137 #endif
138       delete old;
139     }
140   if (lookup_keyword (name_str) >= 0)
141     {
142       warning (  _f ("Identifier name is a keyword: `%s'", name_str));
143     }
144   
145   scope_l_arr_.top ()->elem (name_str) = i;
146 }
147
148 My_lily_lexer::~My_lily_lexer()
149 {
150   delete chordmodifier_tab_p_;
151   delete keytable_p_;
152   delete toplevel_scope_p_ ;
153   delete note_tab_p_;
154 }
155
156 void
157 My_lily_lexer::print_declarations (bool ) const
158 {
159   for (int i=scope_l_arr_.size (); i--; )
160     {
161       DEBUG_OUT << "Scope no. " << i << '\n';
162       scope_l_arr_[i]->print ();
163     }
164 }
165
166 void
167 My_lily_lexer::LexerError (char const *s)
168 {
169   if (include_stack_.empty())
170     {
171       progress_indication (_f ("error at EOF: %s", s)+ String ("\n"));
172     }
173   else
174     {
175       errorlevel_i_ |= 1;
176       Input spot (source_file_l(),here_ch_C());
177       spot.error (s);
178     }
179 }
180
181 Musical_pitch
182 My_lily_lexer::lookup_notename (String s)
183 {
184   return note_tab_p_->get_pitch (s);
185 }
186
187 Musical_pitch
188 My_lily_lexer::lookup_chordmodifier (String s)
189 {
190   return chordmodifier_tab_p_->get_pitch (s);
191 }
192
193
194 void
195 My_lily_lexer::set_notename_table (Notename_table *p)
196 {
197   delete note_tab_p_;
198   note_tab_p_ = p;
199 }
200
201
202
203 void
204 My_lily_lexer::set_chordmodifier_table (Notename_table *p)
205 {
206   delete chordmodifier_tab_p_;
207   chordmodifier_tab_p_ = p;
208 }
209
210 char
211 My_lily_lexer::escaped_char(char c) const
212 {
213   switch(c)
214     {
215     case 'n':
216       return '\n';
217     case 't':
218       return '\t';
219
220     case '\'':
221     case '\"':
222     case '\\':
223       return c;
224     }
225   return 0;
226 }
227
228 Input
229 My_lily_lexer::here_input () const
230 {
231   Source_file * f_l= source_file_l ();
232   return Input (f_l, (char*)here_ch_C ());
233 }