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