]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
release: 1.2.12
[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--1999 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 "parser.hh"
15 #include "keyword.hh"
16 #include "my-lily-lexer.hh"
17 #include "debug.hh"
18 #include "source-file.hh"
19 #include "main.hh"
20 #include "scope.hh"
21 #include "input.hh"
22
23 static Keyword_ent the_key_tab[]={
24   {"autochange", AUTOCHANGE},
25   {"spanrequest", SPANREQUEST},
26   {"simultaneous", SIMULTANEOUS},
27   {"sequential", SEQUENTIAL},
28   {"accepts", ACCEPTS},
29   {"alternative", ALTERNATIVE},
30   {"bar", BAR},
31   {"breathe", BREATHE},
32   {"cadenza", CADENZA},
33   {"chordmodifiers", CHORDMODIFIERS},
34   {"chords", CHORDS},
35   {"clef", CLEF},
36   {"cm", CM_T},
37   {"consists", CONSISTS},
38   {"consistsend", CONSISTSEND},
39   {"context", CONTEXT},
40   {"duration", DURATION},
41   {"font", FONT},
42   {"grace", GRACE},
43   {"header", HEADER},
44   {"in", IN_T},
45   {"lyrics", LYRICS},
46   {"key", KEY},
47   {"keysignature", KEYSIGNATURE},
48   {"mark", MARK},
49   {"musicalpitch", MUSICAL_PITCH},
50   {"time", TIME_T},
51   {"times", TIMES},
52   {"midi", MIDI},
53   {"mm", MM_T},
54   {"name", NAME},
55   {"notenames", NOTENAMES},
56   {"notes", NOTES},
57   {"partial", PARTIAL},
58   {"paper", PAPER},
59   {"penalty", PENALTY},
60   {"property", PROPERTY},
61   {"pt", PT_T},
62   {"relative", RELATIVE},
63   {"remove", REMOVE},
64   {"repeat", REPEAT},
65   {"repetitions", REPETITIONS},
66   {"addlyrics", ADDLYRICS},
67   {"scm", SCM_T},
68   {"scmfile", SCMFILE},
69   {"score", SCORE},
70   {"script", SCRIPT},
71   {"shape", SHAPE},
72   {"skip", SKIP},
73   {"textscript", TEXTSCRIPT},
74   {"tempo", TEMPO},
75   {"translator", TRANSLATOR},
76   {"transpose", TRANSPOSE},
77   {"type", TYPE},
78   {"version", VERSION},
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_symbol (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   if (safe_global_b)
121     allow_includes_b_ = false;
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       *mlog << _f ("error at EOF: %s", s) << endl;
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_)[s];
187 }
188
189 Musical_pitch
190 My_lily_lexer::lookup_chordmodifier (String s)
191 {
192   return (*chordmodifier_tab_p_)[s];
193 }
194
195 bool
196 My_lily_lexer::notename_b (String s) const
197 {
198   return note_tab_p_->elem_b (s);
199 }
200
201 void
202 My_lily_lexer::set_notename_table (Notename_table *p)
203 {
204   delete note_tab_p_;
205   note_tab_p_ = p;
206 }
207
208 bool
209 My_lily_lexer::chordmodifier_b (String s) const
210 {
211   return chordmodifier_tab_p_->elem_b (s);
212 }
213
214 void
215 My_lily_lexer::set_chordmodifier_table (Notename_table *p)
216 {
217   delete chordmodifier_tab_p_;
218   chordmodifier_tab_p_ = p;
219 }
220
221 char
222 My_lily_lexer::escaped_char(char c) const
223 {
224   switch(c)
225     {
226     case 'n':
227       return '\n';
228     case 't':
229       return '\t';
230
231     case '\'':
232     case '\"':
233     case '\\':
234       return c;
235     }
236   return 0;
237 }
238
239 Input
240 My_lily_lexer::here_input () const
241 {
242   Source_file * f_l= source_file_l ();
243   return Input (f_l, (char*)here_ch_C ());
244 }