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