]> git.donarmstrong.com Git - lilypond.git/blob - lily/my-lily-lexer.cc
release: 1.1.18
[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--1998 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   {"absdynamic", ABSDYNAMIC},
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   {"duration", DURATION},
35   {"font", FONT},
36   {"grouping", GROUPING},
37   {"header", HEADER},
38   {"in", IN_T},
39   {"lyrics", LYRICS},
40   {"key", KEY},
41   {"keysignature", KEYSIGNATURE},
42   {"mark", MARK},
43   {"musicalpitch", MUSICAL_PITCH},
44   {"time", TIME_T},
45   {"times", TIMES},
46   {"midi", MIDI},
47   {"mm", MM_T},
48   {"name", NAME},
49   {"notenames", NOTENAMES},
50   {"notes" , NOTES},
51   {"partial", PARTIAL},
52   {"paper", PAPER},
53   {"penalty", PENALTY},
54   {"property", PROPERTY},
55   {"pt", PT_T},
56   {"relative", RELATIVE},
57   {"remove", REMOVE},
58   {"repeat", REPEAT},
59   {"scm", SCM_T},
60   {"scmfile", SCMFILE},
61   {"score", SCORE},
62   {"script", SCRIPT},
63   {"shape", SHAPE},
64   {"skip", SKIP},
65   {"table", TABLE},
66   {"spandynamic", SPANDYNAMIC},
67   {"symboltables", SYMBOLTABLES},
68   {"tempo", TEMPO},
69   {"translator", TRANSLATOR},
70   {"type", TYPE},
71   {"transpose", TRANSPOSE},
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   for (int i = scope_l_arr_.size (); i--; )
97     if (scope_l_arr_[i]->elem_b (s))
98       return (*scope_l_arr_[i])[s];
99   return 0;
100 }
101
102 void
103 My_lily_lexer::start_main_input ()
104 {  
105   if (!monitor->silent_b ("InitDeclarations") && check_debug)
106     print_declarations (true);
107   if (!monitor->silent_b ("InitLexer") && check_debug)
108     set_debug (1);
109
110
111   new_input (main_input_str_, source_global_l);
112   if (safe_global_b)
113     allow_includes_b_ = false;
114   
115   print_declarations(true);
116 }
117
118 void
119 My_lily_lexer::set_identifier (String name_str, Identifier* i, bool )
120 {
121   Identifier *old =0;
122   if (scope_l_arr_.top ()->elem_b (name_str))
123     old = scope_l_arr_.top ()->elem(name_str);
124  
125    
126   if  (old)
127     {
128 #if 0
129       if (unique_b)
130         old->warning(_f ("redeclaration of `\\%s\'", name_str));
131 #endif
132       delete old;
133     }
134   if (lookup_keyword (name_str) >= 0)
135     {
136       warning (  _f ("Identifier name is a keyword (`%s')", name_str));
137     }
138   
139   (*scope_l_arr_.top ())[name_str] = i;
140 }
141
142 My_lily_lexer::~My_lily_lexer()
143 {
144   delete keytable_p_;
145   delete toplevel_scope_p_ ;
146   delete note_tab_p_;
147 }
148
149 void
150 My_lily_lexer::print_declarations (bool ) const
151 {
152   for (int i=scope_l_arr_.size (); i--; )
153     {
154       DOUT << "Scope no. " << i << '\n';
155       scope_l_arr_[i]->print ();
156     }
157 }
158
159 void
160 My_lily_lexer::LexerError (char const *s)
161 {
162   if (include_stack_.empty())
163     {
164       *mlog << _f ("error at EOF: %s", s) << endl;
165     }
166   else
167     {
168       errorlevel_i_ |= 1;
169       Input spot (source_file_l(),here_ch_C());
170       spot.error (s);
171     }
172 }
173
174 Musical_pitch
175 My_lily_lexer::lookup_notename (String s)
176 {
177   return (*note_tab_p_)[s];
178 }
179
180 Musical_pitch
181 My_lily_lexer::lookup_chordmodifier (String s)
182 {
183   return (*chordmodifier_tab_p_)[s];
184 }
185
186 bool
187 My_lily_lexer::notename_b (String s) const
188 {
189   return note_tab_p_->elem_b (s);
190 }
191
192 void
193 My_lily_lexer::set_notename_table (Notename_table *p)
194 {
195   delete note_tab_p_;
196   note_tab_p_ = p;
197 }
198
199 bool
200 My_lily_lexer::chordmodifier_b (String s) const
201 {
202   return chordmodifier_tab_p_->elem_b (s);
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 }